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 |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 12 | |
| 13 | """ |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 14 | Schema of the generator object: |
| 15 | all: Set of all Model objects |
| 16 | all_if(regex): Set of Model objects that match regex |
Scott Baker | 6ad1790 | 2014-11-12 01:12:28 -0800 | [diff] [blame^] | 17 | |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 18 | Model object: |
| 19 | plural: English plural of object name |
| 20 | camel: CamelCase version of object name |
| 21 | refs: list of references to other Model objects |
| 22 | props: list of properties minus refs |
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 | TODO: Deal with subnets |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 25 | """ |
| 26 | |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 27 | def get_REST_patterns(): |
| 28 | return patterns('', |
| 29 | url(r'^plstackapi/$', api_root), |
| 30 | {% for object in generator.all %} |
| 31 | url(r'plstackapi/{{ object.rest_name }}/$', {{ object.camel }}List.as_view(), name='{{ object.singular }}-list'), |
| 32 | url(r'plstackapi/{{ object.rest_name }}/(?P<pk>[a-zA-Z0-9\-]+)/$', {{ object.camel }}Detail.as_view(), name ='{{ object.singular }}-detail'), |
Scott Baker | 6ad1790 | 2014-11-12 01:12:28 -0800 | [diff] [blame^] | 33 | # url(r'plstackapi/{{ object.rest_name }}/!new/$', {{ object.camel }}New.as_view(), name ='{{ object.singular }}-new'), |
Scott Baker | f556b92 | 2014-11-10 15:58:58 -0800 | [diff] [blame] | 34 | {% endfor %} |
| 35 | ) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 36 | |
| 37 | @api_view(['GET']) |
| 38 | def api_root(request, format=None): |
| 39 | return Response({ |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 40 | {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format), |
| 41 | {% endfor %} |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 42 | }) |
| 43 | |
| 44 | # Based on serializers.py |
| 45 | |
| 46 | {% for object in generator.all %} |
| 47 | |
| 48 | class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer): |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 49 | id = serializers.Field() |
| 50 | {% for ref in object.refs %} |
| 51 | {% if ref.multi %} |
| 52 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 53 | {% else %} |
| 54 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
| 55 | {% endif %} |
| 56 | {% endfor %} |
| 57 | class Meta: |
| 58 | model = {{ object.camel }} |
| 59 | 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] | 60 | |
| 61 | class {{ object.camel }}IdSerializer(serializers.ModelSerializer): |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 62 | id = serializers.Field() |
| 63 | {% for ref in object.refs %} |
| 64 | {% if ref.multi %} |
| 65 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 66 | {% else %} |
| 67 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
| 68 | {% endif %} |
| 69 | {% endfor %} |
| 70 | class Meta: |
| 71 | model = {{ object.camel }} |
| 72 | 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] | 73 | |
| 74 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 75 | {% endfor %} |
| 76 | |
| 77 | serializerLookUp = { |
| 78 | {% for object in generator.all %} |
| 79 | {{ object.camel }}: {{ object.camel }}Serializer, |
| 80 | {% endfor %} |
| 81 | None: None, |
| 82 | } |
| 83 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 84 | class PlanetStackRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView): |
| 85 | |
| 86 | # To handle fine-grained field permissions, we have to check can_update |
| 87 | # the object has been updated but before it has been saved. |
| 88 | |
| 89 | def update(self, request, *args, **kwargs):
|
| 90 | partial = kwargs.pop('partial', False)
|
| 91 | self.object = self.get_object_or_none()
|
| 92 |
|
| 93 | serializer = self.get_serializer(self.object, data=request.DATA,
|
| 94 | files=request.FILES, partial=partial)
|
| 95 |
|
| 96 | if not serializer.is_valid():
|
| 97 | print "UpdateModelMixin: not serializer.is_valid"
|
| 98 | print serializer.errors
|
| 99 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
| 100 |
|
| 101 | try:
|
| 102 | self.pre_save(serializer.object)
|
| 103 | except ValidationError as err:
|
| 104 | # full_clean on model instance may be called in pre_save,
|
| 105 | # so we have to handle eventual errors.
|
| 106 | return Response(err.message_dict, status=status.HTTP_400_BAD_REQUEST)
|
| 107 |
|
| 108 | if serializer.object is not None:
|
| 109 | if not serializer.object.can_update(request.user):
|
| 110 | return Response(status=status.HTTP_400_BAD_REQUEST)
|
| 111 |
|
| 112 | if self.object is None:
|
| 113 | self.object = serializer.save(force_insert=True)
|
| 114 | self.post_save(self.object, created=True)
|
| 115 | return Response(serializer.data, status=status.HTTP_201_CREATED)
|
| 116 |
|
| 117 | self.object = serializer.save(force_update=True)
|
| 118 | self.post_save(self.object, created=False)
|
| 119 | return Response(serializer.data, status=status.HTTP_200_OK) |
| 120 | |
| 121 | def destroy(self, request, *args, **kwargs): |
| 122 | obj = self.get_object() |
| 123 | if obj.can_update(request.user): |
Scott Baker | adcbf7a | 2014-10-09 11:09:15 -0700 | [diff] [blame] | 124 | return super(generics.RetrieveUpdateDestroyAPIView, self).destroy(request, *args, **kwargs) |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 125 | else: |
| 126 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 127 | |
| 128 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 129 | # Based on core/views/*.py |
| 130 | {% for object in generator.all %} |
| 131 | |
| 132 | class {{ object.camel }}List(generics.ListCreateAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 133 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 134 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 135 | id_serializer_class = {{ object.camel }}IdSerializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 136 | filter_backends = (filters.DjangoFilterBackend,) |
| 137 | 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] | 138 | |
| 139 | def get_serializer_class(self): |
| 140 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 141 | if (no_hyperlinks): |
| 142 | return self.id_serializer_class |
| 143 | else: |
| 144 | return self.serializer_class |
| 145 | |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 146 | def get_queryset(self): |
Tony Mack | 8f04ee3 | 2014-02-05 10:27:39 -0500 | [diff] [blame] | 147 | return {{ object.camel }}.select_by_user(self.request.user) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 148 | |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 149 | def create(self, request, *args, **kwargs): |
Scott Baker | 6ad1790 | 2014-11-12 01:12:28 -0800 | [diff] [blame^] | 150 | serializer = self.get_serializer(data=request.DATA, files=request.FILES) |
| 151 | if not (serializer.is_valid()): |
| 152 | raise Exception("failed serializer.is_valid: " + str(serializer.errors)) |
| 153 | obj = serializer.object |
Scott Baker | adcbf7a | 2014-10-09 11:09:15 -0700 | [diff] [blame] | 154 | obj.caller = request.user |
| 155 | if obj.can_update(request.user): |
| 156 | return super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 157 | else: |
| 158 | raise Exception("failed obj.can_update") |
| 159 | |
Sapan Bhatia | f69dd5c | 2014-10-08 11:34:23 -0400 | [diff] [blame] | 160 | ret = super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 161 | if (ret.status_code%100 != 200): |
| 162 | raise Exception(ret.data) |
Scott Baker | adcbf7a | 2014-10-09 11:09:15 -0700 | [diff] [blame] | 163 | |
| 164 | return ret |
| 165 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 166 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 167 | class {{ object.camel }}Detail(PlanetStackRetrieveUpdateDestroyAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 168 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 169 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 170 | id_serializer_class = {{ object.camel }}IdSerializer |
| 171 | |
| 172 | def get_serializer_class(self): |
| 173 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 174 | if (no_hyperlinks): |
| 175 | return self.id_serializer_class |
| 176 | else: |
| 177 | return self.serializer_class |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 178 | |
| 179 | def get_queryset(self): |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 180 | return {{ object.camel }}.select_by_user(self.request.user) |
| 181 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 182 | # update() is handled by PlanetStackRetrieveUpdateDestroyAPIView |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 183 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 184 | # destroy() is handled by PlanetStackRetrieveUpdateDestroyAPIView |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 185 | |
Scott Baker | 6ad1790 | 2014-11-12 01:12:28 -0800 | [diff] [blame^] | 186 | """ |
| 187 | XXX smbaker: my intent was to create a view that would return 'new' objects |
| 188 | filled with defaults. I solved it another way, so this code may soon be |
| 189 | abandoned. |
| 190 | |
| 191 | class {{ object.camel }}New(GenericAPIView): |
| 192 | serializer_class = {{ object.camel }}Serializer |
| 193 | id_serializer_class = {{ object.camel }}IdSerializer |
| 194 | |
| 195 | def get(self, request, *args, **kwargs): |
| 196 | return self.makenew(request, *args, **kwargs) |
| 197 | |
| 198 | def get_serializer_class(self): |
| 199 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 200 | if (no_hyperlinks): |
| 201 | return self.id_serializer_class |
| 202 | else: |
| 203 | return self.serializer_class |
| 204 | |
| 205 | def makenew(self, request, *args, **kwargs): |
| 206 | obj = {{ object.camel }}() |
| 207 | serializer = self.get_serializer(obj) |
| 208 | return Response(serializer.data) |
| 209 | """ |
| 210 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 211 | {% endfor %} |