blob: 84f761352f774187b623c54514f21ac9f6bb5bf8 [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
5from core.models import *
6from django.forms import widgets
7
8"""
9 Schema of the generator object:
10 all: Set of all Model objects
11 all_if(regex): Set of Model objects that match regex
12
13 Model object:
14 plural: English plural of object name
15 camel: CamelCase version of object name
16 refs: list of references to other Model objects
Sapan Bhatia86db4df2014-01-14 21:18:07 -050017 props: list of properties minus refs
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050018
19 TODO: Deal with subnets
20"""
21
22# Based on api_root.py
23
24@api_view(['GET'])
25def api_root(request, format=None):
26 return Response({
Sapan Bhatia86db4df2014-01-14 21:18:07 -050027 {% for object in generator.all %}'{{ object.plural }}': reverse('{{ object }}-list', request=request, format=format),
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050028 {% endfor %}
29 })
30
31# Based on serializers.py
32
33{% for object in generator.all %}
34
35class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer):
36 id = serializers.Field()
37 {% for ref in object.refs %}
Sapan Bhatia86db4df2014-01-14 21:18:07 -050038 {{ ref.plural }} = serializers.HyperlinkedRelatedField(view_name='{{ ref }}-detail')
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050039 {% endfor %}
40 class Meta:
41 model = {{ object }}
Sapan Bhatia86db4df2014-01-14 21:18:07 -050042 fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %})
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050043{% endfor %}
44
45serializerLookUp = {
46{% for object in generator.all %}
47 {{ object.camel }}: {{ object.camel }}Serializer,
48{% endfor %}
49 None: None,
50 }
51
52# Based on core/views/*.py
53{% for object in generator.all %}
54
55class {{ object.camel }}List(generics.ListCreateAPIView):
Tony Mackeb8eb312014-02-04 20:50:39 -050056 #queryset = {{ object.camel }}.objects.all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050057 serializer_class = {{ object.camel }}Serializer
Tony Mackeb8eb312014-02-04 20:50:39 -050058
59 def get_queryset(self):
Tony Mack8f04ee32014-02-05 10:27:39 -050060 return {{ object.camel }}.select_by_user(self.request.user)
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050061
Tony Mack9525eba2014-02-05 10:57:21 -050062 def create(self, request, *args, **kwargs):
63 #obj = {{ object.camel }}().update(request.DATA)
64 obj = self.get_object()
Tony Mack3d042792014-03-17 19:18:37 -040065 obj.caller = request.user
Tony Mack9525eba2014-02-05 10:57:21 -050066 if obj.can_update(request.user):
67 return super({{ object.camel }}List, self).create(request, *args, **kwargs)
68 else:
69 return Response(status=status.HTTP_400_BAD_REQUEST)
70
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050071class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView):
Tony Mackeb8eb312014-02-04 20:50:39 -050072 #queryset = {{ object.camel }}.objects.all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050073 serializer_class = {{ object.camel }}Serializer
Tony Mackeb8eb312014-02-04 20:50:39 -050074
75 def get_queryset(self):
Tony Mack9525eba2014-02-05 10:57:21 -050076 return {{ object.camel }}.select_by_user(self.request.user)
77
78 def update(self, request, *args, **kwargs):
79 obj = self.get_object()
80 if obj.can_update(request.user):
81 return super({{ object.camel }}Detail, self).update(request, *args, **kwargs)
82 else:
83 return Response(status=status.HTTP_400_BAD_REQUEST)
84
85 def destroy(self, request, *args, **kwargs):
86 obj = self.get_object()
87 if obj.can_update(request.user):
88 return super({{ object.camel }}Detail, self).destroy(request, *args, **kwargs)
89 else:
90 return Response(status=status.HTTP_400_BAD_REQUEST)
91
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050092
93{% endfor %}