Beispiel #1
0
def _build_field_history_model(cls: typing.Type[Clocked], field: str,
                               schema: str) -> FieldHistory:
    """
    Build a Django model for the temporal history of a given field

    Args:
        cls (typing.Type[Clocked]): class to refer back to
        field (str): field for which to to build a history class
        schema (str): schema to use for history table

    Returns:
        FieldHistory: History model for the given field
    """
    class_name = "%s%s_%s" % (cls.__name__, 'History', field)
    table_name = _truncate_identifier('%s_%s_%s' %
                                      (cls._meta.db_table, 'history', field))

    gist_exclusion_key = 'entity_id'
    if isinstance(cls._meta.pk, models.UUIDField):
        # Due to a limitation of postgres, UUIDs cannot be used in a GiST index
        gist_exclusion_key = '(entity_id::text)'

    attrs = dict(
        id=models.UUIDField(primary_key=True, default=uuid.uuid4),
        entity=models.ForeignKey(
            cls,
            related_name='%s_history' % field,
        ),
        effective=DateTimeRangeField(),
        vclock=IntegerRangeField(),
        Meta=type(
            'Meta', (), {
                'app_label':
                cls._meta.app_label,
                'db_table':
                table_name,
                'indexes': [
                    GistExclusionConstraint(
                        fields=[
                            '(%s) WITH =, effective WITH &&' %
                            gist_exclusion_key
                        ],
                        name=_truncate_identifier(table_name +
                                                  '_excl_effective'),
                    ),
                    GistExclusionConstraint(
                        fields=[
                            '(%s) WITH =, vclock WITH &&' % gist_exclusion_key
                        ],
                        name=_truncate_identifier(table_name + '_excl_vclock'),
                    )
                ]
            }),
        __module__=cls.__module__,
    )

    attrs[field] = next(f for f in cls._meta.fields if f.name == field)

    model = type(class_name, (FieldHistory, ), attrs)
    return model
Beispiel #2
0
class BlockchainPaymentRoute(PaymentRoute):
    NAME = "blockchain"

    account = models.ForeignKey(settings.ETHEREUM_ACCOUNT_MODEL,
                                on_delete=models.CASCADE,
                                related_name="payment_routes")
    payment_window = IntegerRangeField(
        default=calculate_blockchain_payment_window)

    objects = BlockchainRouteManager()

    @property
    def start_block_number(self):
        return self.payment_window.lower

    @property
    def expiration_block_number(self):
        return self.payment_window.upper

    @property
    def is_expired(self):
        return self.order.chain.highest_block > self.expiration_block_number
Beispiel #3
0
class Person(models.Model):
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    #  Jards Macalé is an amazing brazilian musician! =]
    enjoy_jards_macale = models.BooleanField(default=True)
    like_metal_music = models.BooleanField(default=False)
    name = models.CharField(max_length=30)
    nickname = models.SlugField(max_length=36)
    age = models.IntegerField()
    bio = models.TextField()
    birthday = models.DateField()
    birth_time = models.TimeField()
    appointment = models.DateTimeField()
    blog = models.URLField()
    occupation = models.CharField(max_length=10, choices=OCCUPATION_CHOICES)
    uuid = models.UUIDField(primary_key=False)
    name_hash = models.BinaryField(max_length=16)
    days_since_last_login = models.BigIntegerField()
    duration_of_sleep = models.DurationField()
    email = models.EmailField()
    id_document = models.CharField(unique=True, max_length=10)

    try:
        from django.db.models import JSONField

        data = JSONField()
    except ImportError:
        # Skip JSONField-related fields
        pass

    try:
        from django.contrib.postgres.fields import ArrayField, HStoreField
        from django.contrib.postgres.fields import JSONField as PostgresJSONField
        from django.contrib.postgres.fields.citext import (
            CICharField,
            CIEmailField,
            CITextField,
        )
        from django.contrib.postgres.fields.ranges import (
            BigIntegerRangeField,
            DateRangeField,
            DateTimeRangeField,
            IntegerRangeField,
        )

        if settings.USING_POSTGRES:
            acquaintances = ArrayField(models.IntegerField())
            postgres_data = PostgresJSONField()
            hstore_data = HStoreField()
            ci_char = CICharField(max_length=30)
            ci_email = CIEmailField()
            ci_text = CITextField()
            int_range = IntegerRangeField()
            bigint_range = BigIntegerRangeField()
            date_range = DateRangeField()
            datetime_range = DateTimeRangeField()
    except ImportError:
        # Skip PostgreSQL-related fields
        pass

    try:
        from django.contrib.postgres.fields.ranges import FloatRangeField

        if settings.USING_POSTGRES:
            float_range = FloatRangeField()
    except ImportError:
        # Django version greater or equal than 3.1
        pass

    try:
        from django.contrib.postgres.fields.ranges import DecimalRangeField

        if settings.USING_POSTGRES:
            decimal_range = DecimalRangeField()
    except ImportError:
        # Django version lower than 2.2
        pass

    if BAKER_GIS:
        geom = models.GeometryField()
        point = models.PointField()
        line_string = models.LineStringField()
        polygon = models.PolygonField()
        multi_point = models.MultiPointField()
        multi_line_string = models.MultiLineStringField()
        multi_polygon = models.MultiPolygonField()
        geom_collection = models.GeometryCollectionField()