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 | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 6 | from rest_framework import status |
Scott Baker | c480fb2 | 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 | 8ffd7d7 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 11 | from django.conf.urls import patterns, url |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 12 | |
Scott Baker | b9973aa | 2014-12-23 11:13:52 -0800 | [diff] [blame] | 13 | if hasattr(serializers, "ReadOnlyField"): |
| 14 | # rest_framework 3.x |
| 15 | IdField = serializers.ReadOnlyField |
| 16 | else: |
| 17 | # rest_framework 2.x |
| 18 | IdField = serializers.Field |
| 19 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 20 | """ |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 21 | Schema of the generator object: |
| 22 | all: Set of all Model objects |
| 23 | all_if(regex): Set of Model objects that match regex |
Scott Baker | c480fb2 | 2014-11-12 01:12:28 -0800 | [diff] [blame] | 24 | |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 25 | Model object: |
| 26 | plural: English plural of object name |
| 27 | camel: CamelCase version of object name |
| 28 | refs: list of references to other Model objects |
| 29 | props: list of properties minus refs |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 30 | |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 31 | TODO: Deal with subnets |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 32 | """ |
| 33 | |
Scott Baker | 8ffd7d7 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 34 | def get_REST_patterns(): |
| 35 | return patterns('', |
| 36 | url(r'^plstackapi/$', api_root), |
| 37 | {% for object in generator.all %} |
| 38 | url(r'plstackapi/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list'), |
| 39 | url(r'plstackapi/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail'), |
| 40 | {% endfor %} |
| 41 | ) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 42 | |
| 43 | @api_view(['GET']) |
| 44 | def api_root(request, format=None): |
| 45 | return Response({ |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 46 | {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format), |
| 47 | {% endfor %} |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 48 | }) |
| 49 | |
| 50 | # Based on serializers.py |
| 51 | |
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 52 | class XOSModelSerializer(serializers.ModelSerializer): |
| 53 | def save_object(self, obj, **kwargs): |
| 54 | |
| 55 | """ rest_framework can't deal with ManyToMany relations that have a |
| 56 | through table. In plstackapi, most of the through tables we have |
| 57 | use defaults or blank fields, so there's no reason why we shouldn't |
| 58 | be able to save these objects. |
| 59 | |
| 60 | So, let's strip out these m2m relations, and deal with them ourself. |
| 61 | """ |
| 62 | obj._complex_m2m_data={};
|
| 63 | if getattr(obj, '_m2m_data', None):
|
| 64 | for relatedObject in obj._meta.get_all_related_many_to_many_objects():
|
| 65 | if (relatedObject.field.rel.through._meta.auto_created):
|
| 66 | # These are non-trough ManyToMany relations and
|
| 67 | # can be updated just fine
|
| 68 | continue
|
| 69 | fieldName = relatedObject.get_accessor_name()
|
| 70 | if fieldName in obj._m2m_data.keys():
|
| 71 | obj._complex_m2m_data[fieldName] = (relatedObject, obj._m2m_data[fieldName])
|
| 72 | del obj._m2m_data[fieldName]
|
| 73 |
|
| 74 | serializers.ModelSerializer.save_object(self, obj, **kwargs); |
| 75 | |
| 76 | for (accessor, stuff) in obj._complex_m2m_data.items(): |
| 77 | (relatedObject, data) = stuff |
| 78 | through = relatedObject.field.rel.through |
| 79 | local_fieldName = relatedObject.field.m2m_reverse_field_name() |
Scott Baker | 148f5e1 | 2014-12-18 12:59:04 -0800 | [diff] [blame] | 80 | remote_fieldName = relatedObject.field.m2m_field_name() |
| 81 | |
| 82 | # get the current set of existing relations |
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 83 | existing = through.objects.filter(**{local_fieldName: obj}); |
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 84 | |
Scott Baker | 148f5e1 | 2014-12-18 12:59:04 -0800 | [diff] [blame] | 85 | data_ids = [item.id for item in data] |
| 86 | existing_ids = [getattr(item,remote_fieldName).id for item in existing] |
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 87 | |
Scott Baker | 148f5e1 | 2014-12-18 12:59:04 -0800 | [diff] [blame] | 88 | #print "data_ids", data_ids |
| 89 | #print "existing_ids", existing_ids |
| 90 | |
| 91 | # remove relations that are in 'existing' but not in 'data' |
| 92 | for item in list(existing): |
| 93 | if (getattr(item,remote_fieldName).id not in data_ids): |
| 94 | print "delete", getattr(item,remote_fieldName) |
| 95 | item.delete() #(purge=True) |
| 96 | |
| 97 | # add relations that are in 'data' but not in 'existing' |
| 98 | for item in data: |
| 99 | if (item.id not in existing_ids): |
| 100 | #print "add", item |
| 101 | newModel = through(**{local_fieldName: obj, remote_fieldName: item}) |
| 102 | newModel.save() |
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 103 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 104 | {% for object in generator.all %} |
| 105 | |
| 106 | class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer): |
Scott Baker | b9973aa | 2014-12-23 11:13:52 -0800 | [diff] [blame] | 107 | id = IdField() |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 108 | {% for ref in object.refs %} |
| 109 | {% if ref.multi %} |
| 110 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 111 | {% else %} |
| 112 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
| 113 | {% endif %} |
| 114 | {% endfor %} |
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 115 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 116 | validators = serializers.SerializerMethodField("getValidators") |
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 117 | def getHumanReadableName(self, obj):
|
| 118 | return str(obj) |
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 119 | def getValidators(self, obj): |
| 120 | try: |
| 121 | return obj.getValidators() |
| 122 | except: |
| 123 | return None |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 124 | class Meta: |
| 125 | model = {{ object.camel }} |
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 126 | 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] | 127 | |
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 128 | class {{ object.camel }}IdSerializer(XOSModelSerializer): |
Scott Baker | b9973aa | 2014-12-23 11:13:52 -0800 | [diff] [blame] | 129 | id = IdField() |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 130 | {% for ref in object.refs %} |
| 131 | {% if ref.multi %} |
Scott Baker | 7934885 | 2014-12-23 10:43:03 -0800 | [diff] [blame] | 132 | {{ ref.plural }} = serializers.PrimaryKeyRelatedField(many=True, queryset = {{ ref.camel }}.objects.all()) |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 133 | {% else %} |
Scott Baker | 7934885 | 2014-12-23 10:43:03 -0800 | [diff] [blame] | 134 | {{ ref }} = serializers.PrimaryKeyRelatedField( queryset = {{ ref.camel }}.objects.all()) |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 135 | {% endif %} |
| 136 | {% endfor %} |
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 137 | humanReadableName = serializers.SerializerMethodField("getHumanReadableName") |
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 138 | validators = serializers.SerializerMethodField("getValidators") |
Scott Baker | 196ffef | 2014-11-21 11:32:24 -0800 | [diff] [blame] | 139 | def getHumanReadableName(self, obj):
|
| 140 | return str(obj)
|
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 141 | def getValidators(self, obj):
|
| 142 | try: |
| 143 | return obj.getValidators() |
| 144 | except: |
| 145 | return None
|
Scott Baker | cac0974 | 2014-12-17 18:22:33 -0800 | [diff] [blame] | 146 | class Meta:
|
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 147 | model = {{ object.camel }} |
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 148 | 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] | 149 | |
| 150 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 151 | {% endfor %} |
| 152 | |
| 153 | serializerLookUp = { |
| 154 | {% for object in generator.all %} |
| 155 | {{ object.camel }}: {{ object.camel }}Serializer, |
| 156 | {% endfor %} |
| 157 | None: None, |
| 158 | } |
| 159 | |
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 160 | class PlanetStackRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView): |
| 161 | |
| 162 | # To handle fine-grained field permissions, we have to check can_update |
| 163 | # the object has been updated but before it has been saved. |
| 164 | |
| 165 | def update(self, request, *args, **kwargs):
|
| 166 | partial = kwargs.pop('partial', False)
|
| 167 | self.object = self.get_object_or_none()
|
| 168 |
|
| 169 | serializer = self.get_serializer(self.object, data=request.DATA,
|
| 170 | files=request.FILES, partial=partial)
|
| 171 |
|
| 172 | if not serializer.is_valid():
|
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 173 | response = {"error": "validation",
|
| 174 | "specific_error": "not serializer.is_valid()",
|
| 175 | "reasons": serializer.errors}
|
| 176 | return Response(response, status=status.HTTP_400_BAD_REQUEST)
|
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 177 |
|
| 178 | try:
|
| 179 | self.pre_save(serializer.object)
|
| 180 | except ValidationError as err:
|
| 181 | # full_clean on model instance may be called in pre_save,
|
| 182 | # so we have to handle eventual errors.
|
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 183 | response = {"error": "validation",
|
| 184 | "specific_error": "ValidationError in pre_save",
|
| 185 | "reasons": err.message_dict}
|
| 186 | return Response(response, status=status.HTTP_400_BAD_REQUEST)
|
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 187 |
|
| 188 | if serializer.object is not None:
|
| 189 | if not serializer.object.can_update(request.user):
|
| 190 | return Response(status=status.HTTP_400_BAD_REQUEST)
|
| 191 |
|
| 192 | if self.object is None:
|
| 193 | self.object = serializer.save(force_insert=True)
|
| 194 | self.post_save(self.object, created=True)
|
| 195 | return Response(serializer.data, status=status.HTTP_201_CREATED)
|
| 196 |
|
| 197 | self.object = serializer.save(force_update=True)
|
| 198 | self.post_save(self.object, created=False)
|
| 199 | return Response(serializer.data, status=status.HTTP_200_OK) |
| 200 | |
| 201 | def destroy(self, request, *args, **kwargs): |
| 202 | obj = self.get_object() |
| 203 | if obj.can_update(request.user): |
Scott Baker | de12f09 | 2014-10-09 11:09:15 -0700 | [diff] [blame] | 204 | return super(generics.RetrieveUpdateDestroyAPIView, self).destroy(request, *args, **kwargs) |
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 205 | else: |
| 206 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 207 | |
| 208 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 209 | # Based on core/views/*.py |
| 210 | {% for object in generator.all %} |
| 211 | |
| 212 | class {{ object.camel }}List(generics.ListCreateAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 213 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 214 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 215 | id_serializer_class = {{ object.camel }}IdSerializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 216 | filter_backends = (filters.DjangoFilterBackend,) |
| 217 | 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] | 218 | |
| 219 | def get_serializer_class(self): |
| 220 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 221 | if (no_hyperlinks): |
| 222 | return self.id_serializer_class |
| 223 | else: |
| 224 | return self.serializer_class |
| 225 | |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 226 | def get_queryset(self): |
Tony Mack | 8f04ee3 | 2014-02-05 10:27:39 -0500 | [diff] [blame] | 227 | return {{ object.camel }}.select_by_user(self.request.user) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 228 | |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 229 | def create(self, request, *args, **kwargs): |
Scott Baker | c480fb2 | 2014-11-12 01:12:28 -0800 | [diff] [blame] | 230 | serializer = self.get_serializer(data=request.DATA, files=request.FILES) |
| 231 | if not (serializer.is_valid()): |
Scott Baker | 351148c | 2014-11-25 00:53:25 -0800 | [diff] [blame] | 232 | response = {"error": "validation", |
| 233 | "specific_error": "not serializer.is_valid()",
|
| 234 | "reasons": serializer.errors}
|
| 235 | return Response(response, status=status.HTTP_400_BAD_REQUEST) |
Scott Baker | c480fb2 | 2014-11-12 01:12:28 -0800 | [diff] [blame] | 236 | obj = serializer.object |
Scott Baker | de12f09 | 2014-10-09 11:09:15 -0700 | [diff] [blame] | 237 | obj.caller = request.user |
| 238 | if obj.can_update(request.user): |
| 239 | return super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 240 | else: |
| 241 | raise Exception("failed obj.can_update") |
| 242 | |
Sapan Bhatia | 970beb5 | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 243 | ret = super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 244 | if (ret.status_code%100 != 200): |
| 245 | raise Exception(ret.data) |
Scott Baker | de12f09 | 2014-10-09 11:09:15 -0700 | [diff] [blame] | 246 | |
| 247 | return ret |
| 248 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 249 | |
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 250 | class {{ object.camel }}Detail(PlanetStackRetrieveUpdateDestroyAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 251 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 252 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 253 | id_serializer_class = {{ object.camel }}IdSerializer |
| 254 | |
| 255 | def get_serializer_class(self): |
| 256 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 257 | if (no_hyperlinks): |
| 258 | return self.id_serializer_class |
| 259 | else: |
| 260 | return self.serializer_class |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 261 | |
| 262 | def get_queryset(self): |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 263 | return {{ object.camel }}.select_by_user(self.request.user) |
| 264 | |
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 265 | # update() is handled by PlanetStackRetrieveUpdateDestroyAPIView |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 266 | |
Scott Baker | dcb9b5d | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 267 | # destroy() is handled by PlanetStackRetrieveUpdateDestroyAPIView |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 268 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 269 | {% endfor %} |