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 |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 6 | from core.models import * |
| 7 | from django.forms import widgets |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 8 | from rest_framework import filters |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 9 | |
| 10 | """ |
| 11 | Schema of the generator object: |
| 12 | all: Set of all Model objects |
| 13 | all_if(regex): Set of Model objects that match regex |
| 14 | |
| 15 | Model object: |
| 16 | plural: English plural of object name |
| 17 | camel: CamelCase version of object name |
| 18 | refs: list of references to other Model objects |
Sapan Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 19 | props: list of properties minus refs |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 20 | |
| 21 | TODO: Deal with subnets |
| 22 | """ |
| 23 | |
| 24 | # Based on api_root.py |
| 25 | |
| 26 | @api_view(['GET']) |
| 27 | def api_root(request, format=None): |
| 28 | return Response({ |
Sapan Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 29 | {% 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] | 30 | {% endfor %} |
| 31 | }) |
| 32 | |
| 33 | # Based on serializers.py |
| 34 | |
| 35 | {% for object in generator.all %} |
| 36 | |
| 37 | class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer): |
| 38 | id = serializers.Field() |
| 39 | {% for ref in object.refs %} |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 40 | {% if ref.multi %} |
| 41 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 42 | {% else %} |
Sapan Bhatia | cdd90b7 | 2014-01-28 20:03:13 -0500 | [diff] [blame] | 43 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
Sapan Bhatia | eb62ad6 | 2014-01-28 14:29:08 -0500 | [diff] [blame] | 44 | {% endif %} |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 45 | {% endfor %} |
| 46 | class Meta: |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 47 | model = {{ object.camel }} |
Sapan Bhatia | be42fba | 2014-01-28 23:53:49 -0500 | [diff] [blame] | 48 | 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] | 49 | |
| 50 | class {{ object.camel }}IdSerializer(serializers.ModelSerializer): |
| 51 | id = serializers.Field() |
| 52 | {% for ref in object.refs %} |
| 53 | {% if ref.multi %} |
| 54 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail') |
| 55 | {% else %} |
| 56 | {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail') |
| 57 | {% endif %} |
| 58 | {% endfor %} |
| 59 | class Meta: |
| 60 | model = {{ object.camel }} |
| 61 | fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %}) |
| 62 | |
| 63 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 64 | {% endfor %} |
| 65 | |
| 66 | serializerLookUp = { |
| 67 | {% for object in generator.all %} |
| 68 | {{ object.camel }}: {{ object.camel }}Serializer, |
| 69 | {% endfor %} |
| 70 | None: None, |
| 71 | } |
| 72 | |
| 73 | # Based on core/views/*.py |
| 74 | {% for object in generator.all %} |
| 75 | |
| 76 | class {{ object.camel }}List(generics.ListCreateAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 77 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 78 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 9a27092 | 2014-07-03 18:01:30 -0700 | [diff] [blame] | 79 | id_serializer_class = {{ object.camel }}IdSerializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 80 | filter_backends = (filters.DjangoFilterBackend,) |
| 81 | 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] | 82 | |
| 83 | def get_serializer_class(self): |
| 84 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 85 | if (no_hyperlinks): |
| 86 | return self.id_serializer_class |
| 87 | else: |
| 88 | return self.serializer_class |
| 89 | |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 90 | def get_queryset(self): |
Tony Mack | 8f04ee3 | 2014-02-05 10:27:39 -0500 | [diff] [blame] | 91 | return {{ object.camel }}.select_by_user(self.request.user) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 92 | |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 93 | def create(self, request, *args, **kwargs): |
| 94 | #obj = {{ object.camel }}().update(request.DATA) |
| 95 | obj = self.get_object() |
Tony Mack | 3d04279 | 2014-03-17 19:18:37 -0400 | [diff] [blame] | 96 | obj.caller = request.user |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 97 | if obj.can_update(request.user): |
| 98 | return super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 99 | else: |
| 100 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 101 | |
| 102 | class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 103 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 104 | serializer_class = {{ object.camel }}Serializer |
Scott Baker | 46b5854 | 2014-08-11 17:26:12 -0700 | [diff] [blame] | 105 | id_serializer_class = {{ object.camel }}IdSerializer |
| 106 | |
| 107 | def get_serializer_class(self): |
| 108 | no_hyperlinks = self.request.QUERY_PARAMS.get('no_hyperlinks', False) |
| 109 | if (no_hyperlinks): |
| 110 | return self.id_serializer_class |
| 111 | else: |
| 112 | return self.serializer_class |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 113 | |
| 114 | def get_queryset(self): |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 115 | return {{ object.camel }}.select_by_user(self.request.user) |
| 116 | |
| 117 | def update(self, request, *args, **kwargs): |
| 118 | obj = self.get_object() |
| 119 | if obj.can_update(request.user): |
| 120 | return super({{ object.camel }}Detail, self).update(request, *args, **kwargs) |
| 121 | else: |
| 122 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 123 | |
| 124 | def destroy(self, request, *args, **kwargs): |
| 125 | obj = self.get_object() |
| 126 | if obj.can_update(request.user): |
| 127 | return super({{ object.camel }}Detail, self).destroy(request, *args, **kwargs) |
| 128 | else: |
| 129 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 130 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 131 | |
| 132 | {% endfor %} |