blob: ef9884c7470a68958d0e742126dacc68f93241d5 [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
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 Bhatia86db4df2014-01-14 21:18:07 -050018 props: list of properties minus refs
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050019
20 TODO: Deal with subnets
21"""
22
23# Based on api_root.py
24
25@api_view(['GET'])
26def api_root(request, format=None):
27 return Response({
Sapan Bhatia86db4df2014-01-14 21:18:07 -050028 {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format),
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050029 {% endfor %}
30 })
31
32# Based on serializers.py
33
34{% for object in generator.all %}
35
36class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
37 id = serializers.Field()
38 {% for ref in object.refs %}
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050039 {% if ref.multi %}
40 {{ ref.plural }} = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='{{ ref }}-detail')
41 {% else %}
Sapan Bhatiacdd90b72014-01-28 20:03:13 -050042 {{ ref }} = serializers.HyperlinkedRelatedField(read_only=True, view_name='{{ ref }}-detail')
Sapan Bhatiaeb62ad62014-01-28 14:29:08 -050043 {% endif %}
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050044 {% endfor %}
45 class Meta:
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050046 model = {{ object.camel }}
Sapan Bhatiabe42fba2014-01-28 23:53:49 -050047 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 -070048
49class {{ 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 Bhatia3bbf5ed2014-01-13 13:29:12 -050063{% endfor %}
64
65serializerLookUp = {
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
75class {{ object.camel }}List(generics.ListCreateAPIView):
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050076 queryset = {{ object.camel }}.objects.select_related().all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050077 serializer_class = {{ object.camel }}Serializer
Scott Baker9a270922014-07-03 18:01:30 -070078 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 Mackeb8eb312014-02-04 20:50:39 -050087
88 def get_queryset(self):
Tony Mack8f04ee32014-02-05 10:27:39 -050089 return {{ object.camel }}.select_by_user(self.request.user)
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050090
Tony Mack9525eba2014-02-05 10:57:21 -050091 def create(self, request, *args, **kwargs):
92 #obj = {{ object.camel }}().update(request.DATA)
93 obj = self.get_object()
Tony Mack3d042792014-03-17 19:18:37 -040094 obj.caller = request.user
Tony Mack9525eba2014-02-05 10:57:21 -050095 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 Bhatia3bbf5ed2014-01-13 13:29:12 -050099
100class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView):
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -0500101 queryset = {{ object.camel }}.objects.select_related().all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -0500102 serializer_class = {{ object.camel }}Serializer
Tony Mackeb8eb312014-02-04 20:50:39 -0500103
104 def get_queryset(self):
Tony Mack9525eba2014-02-05 10:57:21 -0500105 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 Bhatia3bbf5ed2014-01-13 13:29:12 -0500121
122{% endfor %}