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