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