-
Notifications
You must be signed in to change notification settings - Fork 0
/
__api.py
48 lines (38 loc) · 1.7 KB
/
__api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from geonode.api.api import ProfileResource
from geonode.api.authorization import GeoNodeAuthorization
from geonode.layers.models import Attribute, Layer
from tastypie.constants import ALL_WITH_RELATIONS
from tastypie.resources import ModelResource, ALL
from tastypie import fields
class LayerResource(ModelResource):
owner = fields.ToOneField(ProfileResource, 'owner', full=True)
class Meta:
queryset = Layer.objects.all()
list_allowed_methods = ['get']
resource_name = 'layers'
fields = ['title', 'thumbnail_url', 'abstract', 'typename',
'detail_url',"owner"]
authorization = GeoNodeAuthorization()
filtering = {
'typename': ALL,
'id': ALL,
"owner": ALL_WITH_RELATIONS
}
# to serialize geometry_type | serialize a new field
# hint: make a new methode with: dehydrate_new_field(self, bundle)
geometry_type = fields.CharField(readonly=True)
def dehydrate_geometry_type(self, bundle):
return bundle.obj.attribute_set.get(attribute_type__contains="gml:").attribute_type
def apply_filters(self, request, applicable_filters):
layer_type = request.GET.get('type', None)
typename = request.GET.get('typename', None)
ownername = request.GET.get('owner', None)
filters = {}
if typename:
filters.update(dict(typename__exact=typename))
if layer_type:
filters.update(dict(attribute_set__in=Attribute.objects.filter(
attribute_type__contains=layer_type)))
if ownername:
filters.update(dict(owner__username=ownername))
return super(LayerResource, self).apply_filters(request, filters)