Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response |
| 3 | from rest_framework.reverse import reverse |
| 4 | from rest_framework import serializers |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 5 | from rest_framework import generics |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 6 | from rest_framework import status |
Scott Baker | 6ad1790 | 2014-11-12 01:12:28 -0800 | [diff] [blame] | 7 | from rest_framework.generics import GenericAPIView |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 8 | from core.models import * |
| 9 | from django.forms import widgets |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 10 | from rest_framework import filters |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 11 | from django.conf.urls import patterns, url |
Scott Baker | 4af864e | 2015-02-02 13:53:46 -0800 | [diff] [blame] | 12 | from rest_framework.exceptions import PermissionDenied as RestFrameworkPermissionDenied |
Scott Baker | 15af565 | 2015-02-02 14:33:08 -0800 | [diff] [blame] | 13 | from django.core.exceptions import PermissionDenied as DjangoPermissionDenied |
Scott Baker | 96e2b2c | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 14 | from xosapibase import XOSRetrieveUpdateDestroyAPIView, XOSListCreateAPIView |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 15 | |
Scott Baker | a009d56 | 2014-12-23 11:13:52 -0800 | [diff] [blame] | 16 | if hasattr(serializers, "ReadOnlyField"): |
| 17 | # rest_framework 3.x |
| 18 | IdField = serializers.ReadOnlyField |
| 19 | else: |
| 20 | # rest_framework 2.x |
| 21 | IdField = serializers.Field |
| 22 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 23 | """ |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 24 | Schema of the generator object: |
| 25 | all: Set of all Model objects |
| 26 | all_if(regex): Set of Model objects that match regex |
Scott Baker | 6ad1790 | 2014-11-12 01:12:28 -0800 | [diff] [blame] | 27 | |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 28 | Model object: |
| 29 | plural: English plural of object name |
| 30 | camel: CamelCase version of object name |
| 31 | refs: list of references to other Model objects |
| 32 | props: list of properties minus refs |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 33 | |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 34 | TODO: Deal with subnets |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 35 | """ |
| 36 | |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 37 | def get_REST_patterns(): |
| 38 | return patterns('', |
Scott Baker | 1faefd3 | 2015-02-02 15:53:37 -0800 | [diff] [blame] | 39 | url(r'^xos/$', api_root), |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 40 | {% for object in generator.all %} |
Scott Baker | 1faefd3 | 2015-02-02 15:53:37 -0800 | [diff] [blame] | 41 | url(r'xos/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list'), |
| 42 | url(r'xos/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail'), |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 43 | {% endfor %} |
| 44 | ) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 45 | |
| 46 | @api_view(['GET']) |
| 47 | def api_root(request, format=None): |
| 48 | return Response({ |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 49 | {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format), |
| 50 | {% endfor %} |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 51 | }) |
| 52 | |
| 53 | # Based on serializers.py |
| 54 | |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 55 | class XOSModelSerializer(serializers.ModelSerializer): |
| 56 | def save_object(self, obj, **kwargs): |
| 57 | |
| 58 | """ rest_framework can't deal with ManyToMany relations that have a |
Scott Baker | 1faefd3 | 2015-02-02 15:53:37 -0800 | [diff] [blame] | 59 | through table. In xos, most of the through tables we have |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 60 | use defaults or blank fields, so there's no reason why we shouldn't |
| 61 | be able to save these objects. |
| 62 | |
| 63 | So, let's strip out these m2m relations, and deal with them ourself. |
| 64 | """ |
| 65 | obj._complex_m2m_data={};
|
| 66 | if getattr(obj, '_m2m_data', None):
|
| 67 | for relatedObject in obj._meta.get_all_related_many_to_many_objects():
|
| 68 | if (relatedObject.field.rel.through._meta.auto_created):
|
| 69 | # These are non-trough ManyToMany relations and
|
| 70 | # can be updated just fine
|
| 71 | continue
|
| 72 | fieldName = relatedObject.get_accessor_name()
|
| 73 | if fieldName in obj._m2m_data.keys():
|
| 74 | obj._complex_m2m_data[fieldName] = (relatedObject, obj._m2m_data[fieldName])
|
| 75 | del obj._m2m_data[fieldName]
|
| 76 |
|
| 77 | serializers.ModelSerializer.save_object(self, obj, **kwargs); |
| 78 | |
| 79 | for (accessor, stuff) in obj._complex_m2m_data.items(): |
| 80 | (relatedObject, data) = stuff |
| 81 | through = relatedObject.field.rel.through |
| 82 | local_fieldName = relatedObject.field.m2m_reverse_field_name() |
Scott Baker | d923b81 | 2014-12-18 12:59:04 -0800 | [diff] [blame] | 83 | remote_fieldName = relatedObject.field.m2m_field_name() |
| 84 | |
| 85 | # get the current set of existing relations |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 86 | existing = through.objects.filter(**{local_fieldName: obj}); |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 87 | |
Scott Baker | d923b81 | 2014-12-18 12:59:04 -0800 | [diff] [blame] | 88 | data_ids = [item.id for item in data] |
| 89 | existing_ids = [getattr(item,remote_fieldName).id for item in existing] |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 90 | |
Scott Baker | d923b81 | 2014-12-18 12:59:04 -0800 | [diff] [blame] | 91 | #print "data_ids", data_ids |
| 92 | #print "existing_ids", existing_ids |
| 93 | |
| 94 | # remove relations that are in 'existing' but not in 'data' |
| 95 | for item in list(existing): |
| 96 | if (getattr(item,remote_fieldName).id not in data_ids): |
| 97 | print "delete", getattr(item,remote_fieldName) |
| 98 | item.delete() #(purge=True) |
| 99 | |
| 100 | # add relations that are in 'data' but not in 'existing' |
| 101 | for item in data: |
| 102 | if (item.id not in existing_ids): |
| 103 | #print "add", item |
| 104 | newModel = through(**{local_fieldName: obj, remote_fieldName: item}) |
| 105 | newModel.save() |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 106 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 107 | {% for object in generator.all %} |
| 108 | |
| 109 | class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer): |
Scott Baker | a009d56 | 2014-12-23 11:13:52 -0800 | [diff] [blame] | 110 | id = IdField() |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 111 | {% for ref in object.refs %} |
| 112 | {% if ref.multi %} |
| 113 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 114 | {% else %} |
| 115 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
| 116 | {% endif %} |
| 117 | {% endfor %} |
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 118 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
Scott Baker | 96eb3fd | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 119 | validators = serializers.SerializerMethodField("getValidators") |
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 120 | def getHumanReadableName(self, obj):
|
| 121 | return str(obj) |
Scott Baker | 96eb3fd | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 122 | def getValidators(self, obj): |
| 123 | try: |
| 124 | return obj.getValidators() |
| 125 | except: |
| 126 | return None |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 127 | class Meta: |
| 128 | model = {{ object.camel }} |
Scott Baker | 96eb3fd | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 129 | fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %}) |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 130 | |
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 131 | class {{ object.camel }}IdSerializer(XOSModelSerializer): |
Scott Baker | a009d56 | 2014-12-23 11:13:52 -0800 | [diff] [blame] | 132 | id = IdField() |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 133 | {% for ref in object.refs %} |
| 134 | {% if ref.multi %} |
Scott Baker | 0461aee | 2014-12-23 10:43:03 -0800 | [diff] [blame] | 135 | {{ ref.plural }} = serializers.PrimaryKeyRelatedField(many=True, queryset = {{ ref.camel }}.objects.all()) |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 136 | {% else %} |
Scott Baker | 0461aee | 2014-12-23 10:43:03 -0800 | [diff] [blame] | 137 | {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel }}.objects.all()) |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 138 | {% endif %} |
| 139 | {% endfor %} |
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 140 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
Scott Baker | 96eb3fd | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 141 | validators = serializers.SerializerMethodField("getValidators") |
Scott Baker | b744f1c | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 142 | def getHumanReadableName(self, obj):
|
| 143 | return str(obj)
|
Scott Baker | 96eb3fd | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 144 | def getValidators(self, obj):
|
| 145 | try: |
| 146 | return obj.getValidators() |
| 147 | except: |
| 148 | return None
|
Scott Baker | 71b21f6 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 149 | class Meta:
|
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 150 | model = {{ object.camel }} |
Scott Baker | 96eb3fd | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 151 | fields = ('humanReadableName', 'validators', {% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %}) |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 152 | |
| 153 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 154 | {% endfor %} |
| 155 | |
Scott Baker | 96e2b2c | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 156 | serializerLookUp = { |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 157 | {% for object in generator.all %} |
| 158 | {{ object.camel }}: {{ object.camel }}Serializer, |
| 159 | {% endfor %} |
| 160 | None: None, |
| 161 | } |
| 162 | |
| 163 | # Based on core/views/*.py |
| 164 | {% for object in generator.all %} |
| 165 | |
Scott Baker | 96e2b2c | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 166 | class {{ object.camel }}List(XOSListCreateAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 167 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 168 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 169 | id_serializer_class = {{ object.camel }}IdSerializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 170 | filter_backends = (filters.DjangoFilterBackend,) |
| 171 | filter_fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %}) |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 172 | |
| 173 | def get_serializer_class(self): |
Scott Baker | cc18479 | 2015-01-30 15:42:02 -0800 | [diff] [blame] | 174 | no_hyperlinks=False |
| 175 | if hasattr(self.request,"QUERY_PARAMS"): |
| 176 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 177 | if (no_hyperlinks): |
| 178 | return self.id_serializer_class |
| 179 | else: |
| 180 | return self.serializer_class |
| 181 | |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 182 | def get_queryset(self): |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 183 | if (not self.request.user.is_authenticated()): |
Scott Baker | 4af864e | 2015-02-02 13:53:46 -0800 | [diff] [blame] | 184 | raise RestFrameworkPermissionDenied("You must be authenticated in order to use this API") |
Tony Mack | 8f04ee3 | 2014-02-05 10:27:39 -0500 | [diff] [blame] | 185 | return {{ object.camel }}.select_by_user(self.request.user) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 186 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 187 | |
Scott Baker | 96e2b2c | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 188 | class {{ object.camel }}Detail(XOSRetrieveUpdateDestroyAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 189 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 190 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 191 | id_serializer_class = {{ object.camel }}IdSerializer |
| 192 | |
| 193 | def get_serializer_class(self): |
Scott Baker | cc18479 | 2015-01-30 15:42:02 -0800 | [diff] [blame] | 194 | no_hyperlinks=False |
| 195 | if hasattr(self.request,"QUERY_PARAMS"): |
| 196 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 197 | if (no_hyperlinks): |
| 198 | return self.id_serializer_class |
| 199 | else: |
| 200 | return self.serializer_class |
Scott Baker | cc18479 | 2015-01-30 15:42:02 -0800 | [diff] [blame] | 201 | |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 202 | def get_queryset(self): |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 203 | if (not self.request.user.is_authenticated()): |
Scott Baker | 4af864e | 2015-02-02 13:53:46 -0800 | [diff] [blame] | 204 | raise RestFrameworkPermissionDenied("You must be authenticated in order to use this API") |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 205 | return {{ object.camel }}.select_by_user(self.request.user) |
| 206 | |
Scott Baker | 96e2b2c | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 207 | # update() is handled by XOSRetrieveUpdateDestroyAPIView |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 208 | |
Scott Baker | 96e2b2c | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 209 | # destroy() is handled by XOSRetrieveUpdateDestroyAPIView |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 210 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 211 | {% endfor %} |