2016-08-11 19:05:52 +00:00
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
2016-09-10 23:20:16 +00:00
|
|
|
from django.db import models
|
2015-04-04 16:10:26 +00:00
|
|
|
|
|
|
|
from .fields import (
|
2017-02-11 12:16:35 +00:00
|
|
|
ArrayField, BigIntegerRangeField, CICharField, CIEmailField, CITextField,
|
|
|
|
DateRangeField, DateTimeRangeField, FloatRangeField, HStoreField,
|
|
|
|
IntegerRangeField, JSONField, SearchVectorField,
|
2015-01-10 16:14:20 +00:00
|
|
|
)
|
2014-03-26 16:44:21 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Tag:
|
2015-07-22 17:51:05 +00:00
|
|
|
def __init__(self, tag_id):
|
|
|
|
self.tag_id = tag_id
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, Tag) and self.tag_id == other.tag_id
|
|
|
|
|
|
|
|
|
|
|
|
class TagField(models.SmallIntegerField):
|
|
|
|
|
2017-07-06 17:18:05 +00:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2015-07-22 17:51:05 +00:00
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
return Tag(int(value))
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if isinstance(value, Tag):
|
|
|
|
return value
|
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
return Tag(int(value))
|
|
|
|
|
|
|
|
def get_prep_value(self, value):
|
|
|
|
return value.tag_id
|
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class PostgreSQLModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
required_db_vendor = 'postgresql'
|
|
|
|
|
|
|
|
|
|
|
|
class IntegerArrayModel(PostgreSQLModel):
|
2017-08-17 23:21:35 +00:00
|
|
|
field = ArrayField(models.IntegerField(), default=list, blank=True)
|
2014-03-26 16:44:21 +00:00
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class NullableIntegerArrayModel(PostgreSQLModel):
|
2014-03-26 16:44:21 +00:00
|
|
|
field = ArrayField(models.IntegerField(), blank=True, null=True)
|
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class CharArrayModel(PostgreSQLModel):
|
2014-03-26 16:44:21 +00:00
|
|
|
field = ArrayField(models.CharField(max_length=10))
|
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class DateTimeArrayModel(PostgreSQLModel):
|
2015-01-10 18:13:28 +00:00
|
|
|
datetimes = ArrayField(models.DateTimeField())
|
|
|
|
dates = ArrayField(models.DateField())
|
|
|
|
times = ArrayField(models.TimeField())
|
2014-03-26 16:44:21 +00:00
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class NestedIntegerArrayModel(PostgreSQLModel):
|
2014-03-26 16:44:21 +00:00
|
|
|
field = ArrayField(ArrayField(models.IntegerField()))
|
2014-03-14 17:34:49 +00:00
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class OtherTypesArrayModel(PostgreSQLModel):
|
2015-01-10 18:13:28 +00:00
|
|
|
ips = ArrayField(models.GenericIPAddressField())
|
|
|
|
uuids = ArrayField(models.UUIDField())
|
|
|
|
decimals = ArrayField(models.DecimalField(max_digits=5, decimal_places=2))
|
2015-07-22 17:51:05 +00:00
|
|
|
tags = ArrayField(TagField(), blank=True, null=True)
|
2018-07-27 15:35:54 +00:00
|
|
|
json = ArrayField(JSONField(default=dict), default=list)
|
|
|
|
int_ranges = ArrayField(IntegerRangeField(), blank=True, null=True)
|
|
|
|
bigint_ranges = ArrayField(BigIntegerRangeField(), blank=True, null=True)
|
2015-01-10 18:13:28 +00:00
|
|
|
|
|
|
|
|
2015-04-04 16:10:26 +00:00
|
|
|
class HStoreModel(PostgreSQLModel):
|
2014-03-14 17:34:49 +00:00
|
|
|
field = HStoreField(blank=True, null=True)
|
2017-05-03 04:24:54 +00:00
|
|
|
array_field = ArrayField(HStoreField(), null=True)
|
2014-09-05 20:53:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CharFieldModel(models.Model):
|
|
|
|
field = models.CharField(max_length=16)
|
|
|
|
|
|
|
|
|
|
|
|
class TextFieldModel(models.Model):
|
|
|
|
field = models.TextField()
|
2014-12-21 10:21:37 +00:00
|
|
|
|
2015-05-31 21:45:03 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.field
|
|
|
|
|
|
|
|
|
|
|
|
# Scene/Character/Line models are used to test full text search. They're
|
|
|
|
# populated with content from Monty Python and the Holy Grail.
|
|
|
|
class Scene(models.Model):
|
|
|
|
scene = models.CharField(max_length=255)
|
|
|
|
setting = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.scene
|
|
|
|
|
|
|
|
|
|
|
|
class Character(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2017-02-11 12:16:35 +00:00
|
|
|
class CITestModel(PostgreSQLModel):
|
|
|
|
name = CICharField(primary_key=True, max_length=255)
|
|
|
|
email = CIEmailField()
|
|
|
|
description = CITextField()
|
2017-05-03 05:25:30 +00:00
|
|
|
array_field = ArrayField(CITextField(), null=True)
|
2016-06-01 21:43:59 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2015-05-31 21:45:03 +00:00
|
|
|
class Line(PostgreSQLModel):
|
|
|
|
scene = models.ForeignKey('Scene', models.CASCADE)
|
|
|
|
character = models.ForeignKey('Character', models.CASCADE)
|
|
|
|
dialogue = models.TextField(blank=True, null=True)
|
|
|
|
dialogue_search_vector = SearchVectorField(blank=True, null=True)
|
|
|
|
dialogue_config = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.dialogue or ''
|
|
|
|
|
2014-12-21 10:21:37 +00:00
|
|
|
|
2016-02-12 14:59:13 +00:00
|
|
|
class RangesModel(PostgreSQLModel):
|
|
|
|
ints = IntegerRangeField(blank=True, null=True)
|
|
|
|
bigints = BigIntegerRangeField(blank=True, null=True)
|
|
|
|
floats = FloatRangeField(blank=True, null=True)
|
|
|
|
timestamps = DateTimeRangeField(blank=True, null=True)
|
|
|
|
dates = DateRangeField(blank=True, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class RangeLookupsModel(PostgreSQLModel):
|
|
|
|
parent = models.ForeignKey(RangesModel, models.SET_NULL, blank=True, null=True)
|
|
|
|
integer = models.IntegerField(blank=True, null=True)
|
|
|
|
big_integer = models.BigIntegerField(blank=True, null=True)
|
|
|
|
float = models.FloatField(blank=True, null=True)
|
|
|
|
timestamp = models.DateTimeField(blank=True, null=True)
|
|
|
|
date = models.DateField(blank=True, null=True)
|
2015-01-10 16:14:20 +00:00
|
|
|
|
|
|
|
|
2017-09-06 14:26:45 +00:00
|
|
|
class JSONModel(PostgreSQLModel):
|
2016-09-10 23:20:16 +00:00
|
|
|
field = JSONField(blank=True, null=True)
|
|
|
|
field_custom = JSONField(blank=True, null=True, encoder=DjangoJSONEncoder)
|
|
|
|
|
2015-05-30 21:13:58 +00:00
|
|
|
|
2014-12-21 10:21:37 +00:00
|
|
|
class ArrayFieldSubclass(ArrayField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().__init__(models.IntegerField())
|
2015-02-08 15:21:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AggregateTestModel(models.Model):
|
|
|
|
"""
|
|
|
|
To test postgres-specific general aggregation functions
|
|
|
|
"""
|
|
|
|
char_field = models.CharField(max_length=30, blank=True)
|
|
|
|
integer_field = models.IntegerField(null=True)
|
2017-05-06 14:56:28 +00:00
|
|
|
boolean_field = models.BooleanField(null=True)
|
2015-02-08 15:21:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StatTestModel(models.Model):
|
|
|
|
"""
|
|
|
|
To test postgres-specific aggregation functions for statistics
|
|
|
|
"""
|
|
|
|
int1 = models.IntegerField()
|
|
|
|
int2 = models.IntegerField()
|
2015-07-22 14:43:21 +00:00
|
|
|
related_field = models.ForeignKey(AggregateTestModel, models.SET_NULL, null=True)
|
2015-06-01 17:00:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NowTestModel(models.Model):
|
|
|
|
when = models.DateTimeField(null=True, default=None)
|
2017-03-29 21:52:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UUIDTestModel(models.Model):
|
|
|
|
uuid = models.UUIDField(default=None, null=True)
|