blob: e9dbeb8af59f362b5e072a30e5bd380f45969927 [file] [log] [blame]
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -05001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -05005from rest_framework import generics
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -05006from core.models import *
7from django.forms import widgets
Scott Baker46b58542014-08-11 17:26:12 -07008from rest_framework import filters
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -05009
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 Bhatia86db4df2014-01-14 21:18:07 -050019 props: list of properties minus refs
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050020
21 TODO: Deal with subnets
22"""
23
24# Based on api_root.py
25
26@api_view(['GET'])
27def api_root(request, format=None):
28 return Response({
Sapan Bhatia86db4df2014-01-14 21:18:07 -050029 {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format),
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050030 {% endfor %}
31 })
32
33# Based on serializers.py
34
35{% for object in generator.all %}
36
37class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
38 id = serializers.Field()
39 {% for ref in object.refs %}
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050040 {% if ref.multi %}
41 {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
42 {% else %}
Sapan Bhatiacdd90b72014-01-28 20:03:13 -050043 {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail')
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050044 {% endif %}
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050045 {% endfor %}
46 class Meta:
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050047 model = {{ object.camel }}
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050048 fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}{%if ref.multi %}'{{ ref.plural }}'{% else %}'{{ ref }}'{% endif %},{% endfor %})
Scott Baker9a270922014-07-03 18:01:30 -070049
50class {{ 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 Bhatia3bbf5ed2014-01-13 13:29:12 -050064{% endfor %}
65
66serializerLookUp = {
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
76class {{ object.camel }}List(generics.ListCreateAPIView):
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050077 queryset = {{ object.camel }}.objects.select_related().all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050078 serializer_class = {{ object.camel }}Serializer
Scott Baker9a270922014-07-03 18:01:30 -070079 id_serializer_class = {{ object.camel }}IdSerializer
Scott Baker46b58542014-08-11 17:26:12 -070080 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 Baker9a270922014-07-03 18:01:30 -070082
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 Mackeb8eb312014-02-04 20:50:39 -050090 def get_queryset(self):
Tony Mack8f04ee32014-02-05 10:27:39 -050091 return {{ object.camel }}.select_by_user(self.request.user)
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050092
Tony Mack9525eba2014-02-05 10:57:21 -050093 def create(self, request, *args, **kwargs):
94 #obj = {{ object.camel }}().update(request.DATA)
95 obj = self.get_object()
Tony Mack3d042792014-03-17 19:18:37 -040096 obj.caller = request.user
Tony Mack9525eba2014-02-05 10:57:21 -050097 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 Bhatia3bbf5ed2014-01-13 13:29:12 -0500101
102class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView):
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -0500103 queryset = {{ object.camel }}.objects.select_related().all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -0500104 serializer_class = {{ object.camel }}Serializer
Scott Baker46b58542014-08-11 17:26:12 -0700105 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 Mackeb8eb312014-02-04 20:50:39 -0500113
114 def get_queryset(self):
Tony Mack9525eba2014-02-05 10:57:21 -0500115 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 Bhatia3bbf5ed2014-01-13 13:29:12 -0500131
132{% endfor %}