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 %}) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 48 | {% endfor %} |
| 49 | |
| 50 | serializerLookUp = { |
| 51 | {% for object in generator.all %} |
| 52 | {{ object.camel }}: {{ object.camel }}Serializer, |
| 53 | {% endfor %} |
| 54 | None: None, |
| 55 | } |
| 56 | |
| 57 | # Based on core/views/*.py |
| 58 | {% for object in generator.all %} |
| 59 | |
| 60 | class {{ object.camel }}List(generics.ListCreateAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 61 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 62 | serializer_class = {{ object.camel }}Serializer |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 63 | |
| 64 | def get_queryset(self): |
Tony Mack | 8f04ee3 | 2014-02-05 10:27:39 -0500 | [diff] [blame] | 65 | return {{ object.camel }}.select_by_user(self.request.user) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 66 | |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 67 | def create(self, request, *args, **kwargs): |
| 68 | #obj = {{ object.camel }}().update(request.DATA) |
| 69 | obj = self.get_object() |
Tony Mack | 3d04279 | 2014-03-17 19:18:37 -0400 | [diff] [blame] | 70 | obj.caller = request.user |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 71 | if obj.can_update(request.user): |
| 72 | return super({{ object.camel }}List, self).create(request, *args, **kwargs) |
| 73 | else: |
| 74 | return Response(status=status.HTTP_400_BAD_REQUEST) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 75 | |
| 76 | class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView): |
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 |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 79 | |
| 80 | def get_queryset(self): |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 81 | return {{ object.camel }}.select_by_user(self.request.user) |
| 82 | |
| 83 | def update(self, request, *args, **kwargs): |
| 84 | obj = self.get_object() |
| 85 | if obj.can_update(request.user): |
| 86 | return super({{ object.camel }}Detail, self).update(request, *args, **kwargs) |
| 87 | else: |
| 88 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 89 | |
| 90 | def destroy(self, request, *args, **kwargs): |
| 91 | obj = self.get_object() |
| 92 | if obj.can_update(request.user): |
| 93 | return super({{ object.camel }}Detail, self).destroy(request, *args, **kwargs) |
| 94 | else: |
| 95 | return Response(status=status.HTTP_400_BAD_REQUEST) |
| 96 | |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 97 | |
| 98 | {% endfor %} |