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 | """ |
| 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 |
Sapan Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 20 | props: list of properties minus refs |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 21 | |
| 22 | TODO: Deal with subnets |
| 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 | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 30 | {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format), |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 31 | {% endfor %} |
| 32 | }) |
| 33 | |
| 34 | # Based on serializers.py |
| 35 | |
| 36 | {% for object in generator.all %} |
| 37 | |
| 38 | class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer): |
| 39 | id = serializers.Field() |
| 40 | {% for ref in object.refs %} |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 41 | {% if ref.multi %} |
| 42 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 43 | {% else %} |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 44 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 45 | {% endif %} |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 46 | {% endfor %} |
| 47 | class Meta: |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 48 | model = {{ object.camel }} |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 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): |
| 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 %}) |
| 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): |
| 114 | return super({{ object.camel }}Detail, self).destroy(request, *args, **kwargs) |
| 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): |
| 140 | #obj = {{ object.camel }}().update(request.DATA) |
| 141 | obj = self.get_object() |
Tony Mack | 3d04279 | 2014-03-17 19:18:37 -0400 | [diff] [blame] | 142 | obj.caller = request.user |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 143 | if obj.can_update(request.user): |
| 144 | return super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 145 | else: |
| 146 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 147 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 148 | class {{ object.camel }}Detail(PlanetStackRetrieveUpdateDestroyAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 149 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 150 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 151 | id_serializer_class = {{ object.camel }}IdSerializer |
| 152 | |
| 153 | def get_serializer_class(self): |
| 154 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 155 | if (no_hyperlinks): |
| 156 | return self.id_serializer_class |
| 157 | else: |
| 158 | return self.serializer_class |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 159 | |
| 160 | def get_queryset(self): |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 161 | return {{ object.camel }}.select_by_user(self.request.user) |
| 162 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 163 | # update() is handled by PlanetStackRetrieveUpdateDestroyAPIView |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 164 | |
Scott Baker | 89a7b7c | 2014-10-07 00:10:17 -0700 | [diff] [blame] | 165 | # destroy() is handled by PlanetStackRetrieveUpdateDestroyAPIView |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 166 | |
| 167 | {% endfor %} |