class SpecialtyPizza(models.Model): toppings = postgres_fields.ArrayField(models.CharField(max_length=20), size=4) metadata = postgres_fields.HStoreField() price_range = postgres_fields.IntegerRangeField() sales = postgres_fields.BigIntegerRangeField() available_on = postgres_fields.DateTimeRangeField() season = postgres_fields.DateRangeField()
class DesignProjectTbl(models.Model): '''Table of Design Projects If a design project requires construction it will be linked to this table''' name = models.CharField(max_length=200) super_design_project = models.ForeignKey(SuperDesignProjectTbl, models.PROTECT, blank=True, null=True) phase_number = models.IntegerField(blank=True, null=True) op_bus_unit = models.ForeignKey(BusinessUnit, models.PROTECT) contract_number = models.CharField(max_length=50, blank=True, null=True, unique=True) planned_date_range = fields.DateRangeField(blank=True, null=True) budget = models.DecimalField(max_digits=14, decimal_places=2, blank=True, null=True) # limited to 999B scope_description = models.TextField(blank=True, null=True) designer_organization_name = models.CharField(max_length=50) class Meta: db_table = 'djangoAPI_DesignProjectTbl'
class DiscountBase(models.Model): code = models.CharField( _('code'), max_length=30, unique=True, default=generate_random_code, ) is_active = models.BooleanField(_('is active'), default=True) valid = pg_fields.DateRangeField(_('validity range'), default=(date.today, None)) allowed_uses = models.IntegerField( _('number of allowed uses'), blank=True, null=True, help_text=_('Leave empty if there is no limit on the number of uses' ' of this discount.')) used = models.IntegerField(_('number of times already used'), default=0) amount = MoneyField( max_digits=18, decimal_places=2, default=0, ) objects = InheritanceManager() class Meta: verbose_name = _('discount') verbose_name_plural = _('discounts') def apply_to_order(self, order): raise NotImplementedError( 'Subclasses need to provide an "apply_to_order" method.')
class Project(models.Model): geom = models.MultiPointField() bureau = models.CharField(max_length=1000) contact = models.CharField(max_length=1000) name = models.CharField(max_length=1000) description = models.CharField(max_length=1000) period = pg_fields.DateRangeField() original = models.ForeignKey(ApiElement)
class DesignStageTbl(models.Model): '''list all design phases''' design_project = models.ForeignKey(DesignProjectTbl, models.CASCADE) design_stage_type = models.ForeignKey(DesignStageTypeTbl, models.PROTECT) planned_date_range = fields.DateRangeField() class Meta: db_table = 'djangoAPI_DesignStageTbl'
class ConstructionStageTbl(models.Model): '''list all contruction stages, stage is a subject of phase, daterange for stage must be within phase''' construction_phase = models.ForeignKey(ConstructionPhaseTbl, models.CASCADE) construction_stage_type = models.ForeignKey(ConstructionStageTypeTbl, models.PROTECT) planned_date_range = fields.DateRangeField() class Meta: db_table = 'djangoAPI_ConstructionStageTbl'
class PostgresFieldsModel(models.Model): arrayfield = fields.ArrayField(models.CharField()) hstorefield = fields.HStoreField() jsonfield = fields.JSONField() rangefield = fields.RangeField() integerrangefield = fields.IntegerRangeField() bigintegerrangefield = fields.BigIntegerRangeField() floatrangefield = fields.FloatRangeField() datetimerangefield = fields.DateTimeRangeField() daterangefield = fields.DateRangeField() def arrayfield_tests(self): sorted_array = self.arrayfield.sort() print(sorted_array) def dictfield_tests(self): print(self.hstorefield.keys()) print(self.hstorefield.values()) print(self.hstorefield.update({'foo': 'bar'})) print(self.jsonfield.keys()) print(self.jsonfield.values()) print(self.jsonfield.update({'foo': 'bar'})) def rangefield_tests(self): print(self.rangefield.lower) print(self.rangefield.upper) print(self.integerrangefield.lower) print(self.integerrangefield.upper) print(self.bigintegerrangefield.lower) print(self.bigintegerrangefield.upper) print(self.floatrangefield.lower) print(self.floatrangefield.upper) print(self.datetimerangefield.lower) print(self.datetimerangefield.upper) print(self.daterangefield.lower) print(self.daterangefield.upper)
class ConstructionPhaseTbl(models.Model): '''list all construction stages''' name = models.CharField(max_length=200) design_project = models.ForeignKey(DesignProjectTbl, models.SET_NULL, blank=True, null=True) phase_number = models.BigIntegerField(blank=True, null=True) op_bus_unit = models.ForeignKey(BusinessUnit, models.PROTECT) contract_number = models.CharField(max_length=50, blank=True, null=True, unique=True) budget = models.DecimalField(max_digits=14, decimal_places=2, blank=True, null=True) planned_date_range = fields.DateRangeField(blank=True, null=True) scope_description = models.TextField() constructor_organization_name = models.CharField(max_length=50) class Meta: db_table = 'djangoAPI_ConstructionPhaseTbl'
def test_model_field_formfield_date(self): model_field = pg_fields.DateRangeField() form_field = model_field.formfield() self.assertIsInstance(form_field, pg_forms.DateRangeField)
class Period(models.Model): """ A period of time to express state of open or closed Days that specifies the actual activity hours link here """ resource = models.ForeignKey('Resource', verbose_name=_('Resource'), db_index=True, null=True, blank=True, related_name='periods') unit = models.ForeignKey('Unit', verbose_name=_('Unit'), db_index=True, null=True, blank=True, related_name='periods') start = models.DateField(verbose_name=_('Start date')) end = models.DateField(verbose_name=_('End date')) duration = pgfields.DateRangeField(verbose_name=_('Length of period'), null=True, blank=True, db_index=True) name = models.CharField(max_length=200, verbose_name=_('Name'), blank=True, default='') description = models.CharField(verbose_name=_('Description'), null=True, blank=True, max_length=500) closed = models.BooleanField(verbose_name=_('Closed'), default=False, editable=False) class Meta: verbose_name = _("period") verbose_name_plural = _("periods") def __str__(self): # FIXME: output date in locale-specific format return "{0}, {3}: {1:%d.%m.%Y} - {2:%d.%m.%Y}".format( self.name, self.start, self.end, STATE_BOOLS[self.closed]) def _validate_belonging(self): if not (self.resource_id or self.unit_id): raise ValidationError(_("You must set 'resource' or 'unit'"), code="no_belonging") if self.resource_id and self.unit_id: raise ValidationError( _("You must set either 'resource' or 'unit', but not both"), code="invalid_belonging") def _check_closed(self): # TODO: why is this automagically closing itself upon creation when there's no days added yet if self.pk: # The period is not `closed` if it has any `open` days self.closed = not self.days.filter(closed=False).exists() else: # Unsaved period, thus has no days, thus is closed. self.closed = True def clean(self, ignore_overlap=False): super(Period, self).clean() if self.start > self.end: raise ValidationError("Period must start before its end", code="invalid_date_range") self._validate_belonging() self._check_closed() def save(self, *args, **kwargs): ignore_overlap = kwargs.pop('ignore_overlap', False) self.clean(ignore_overlap=ignore_overlap) self.duration = DateRange(self.start, self.end, '[]') return super(Period, self).save(*args, **kwargs) def save_closedness(self): """ Recalculate and save the `closed`ness state for the day. """ self._check_closed() self.save(force_update=True, update_fields=("closed", ))