Пример #1
0
class Foo(Model):
    """
        Models a sample model with a json field and user foreign key
    """

    # Unique human readable identifier for URLs, etc
    key = CharField(max_length=20, unique=True, null=False)
    name = CharField(max_length=50, unique=False, null=False)
    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)
    # Example of a json field
    data = JSONField(null=False, default=dict(example=1.1))

    # Example of a foreign key
    user = ForeignKey(get_user_model(), on_delete=models.DO_NOTHING)
    bars = ManyToManyField(Bar)
    # Example of geojson container
    geo_collection = GeometryCollectionField(null=False)
    # This stores the full geojson, whereas geo_collection only stores geometry for PostGIS operations
    # The two must be kept in sync. It might be better to get rid of geo_collection and just use this
    geojson = JSONField()

    class Meta:
        app_label = "sample_webapp"

    def __str__(self):
        return self.name
Пример #2
0
class SpatialResource(RandomIDModel):

    class Meta:
        ordering = ('name',)

    class TutelaryMeta:
        perm_type = 'resource'
        path_fields = ('resource', 'pk')
        actions = (
            ('resource.list',
             {'description': _("List resources"),
              'permissions_object': 'resource',
              'error_message': messages.RESOURCE_LIST}),
            ('resource.view',
             {'description': _("View resource"),
              'error_message': messages.RESOURCE_VIEW}),
            ('resource.archive',
             {'description': _("Archive resource"),
              'error_message': messages.RESOURCE_ARCHIVE}),
            ('resource.unarchive',
             {'description': _("Unarchive resource"),
              'error_message': messages.RESOURCE_UNARCHIVE}),
        )

    name = models.CharField(max_length=256, null=True, blank=True)

    time = models.DateTimeField(auto_now_add=True, null=True, blank=True)

    resource = models.ForeignKey(
        Resource,
        on_delete=models.CASCADE, related_name='spatial_resources'
    )
    geom = GeometryCollectionField(
        srid=4326, blank=True, null=True
    )
    attributes = JSONAttributeField(default={})

    # Audit history
    created_date = models.DateTimeField(auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True)

    def __repr__(self):
        repr_string = ('<SpatialResource id={obj.id}'
                       ' resource={obj.resource.id}>')
        return repr_string.format(obj=self)

    @property
    def archived(self):
        return self.resource.archived

    @property
    def project(self):
        return self.resource.project