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 |
| 5 | from core.models import * |
| 6 | from 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 Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 17 | props: list of properties minus refs |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 18 | |
| 19 | TODO: Deal with subnets |
| 20 | """ |
| 21 | |
| 22 | # Based on api_root.py |
| 23 | |
| 24 | @api_view(['GET']) |
| 25 | def api_root(request, format=None): |
| 26 | return Response({ |
Sapan Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 27 | {% 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] | 28 | {% endfor %} |
| 29 | }) |
| 30 | |
| 31 | # Based on serializers.py |
| 32 | |
| 33 | {% for object in generator.all %} |
| 34 | |
| 35 | class {{ object.camel }}Serializer(serializers.HyperlinkedModelSerializer): |
| 36 | id = serializers.Field() |
| 37 | {% for ref in object.refs %} |
Sapan Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 38 | {{ ref.plural }} = serializers.HyperlinkedRelatedField(view_name='{{ ref }}-detail') |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 39 | {% endfor %} |
| 40 | class Meta: |
| 41 | model = {{ object }} |
Sapan Bhatia | 86db4df | 2014-01-14 21:18:07 -0500 | [diff] [blame] | 42 | fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 43 | {% endfor %} |
| 44 | |
| 45 | serializerLookUp = { |
| 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 | |
| 55 | class {{ object.camel }}List(generics.ListCreateAPIView): |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 56 | #queryset = {{ object.camel }}.objects.all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 57 | serializer_class = {{ object.camel }}Serializer |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 58 | |
| 59 | def get_queryset(self): |
Tony Mack | 8f04ee3 | 2014-02-05 10:27:39 -0500 | [diff] [blame] | 60 | return {{ object.camel }}.select_by_user(self.request.user) |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 61 | |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 62 | def create(self, request, *args, **kwargs): |
| 63 | #obj = {{ object.camel }}().update(request.DATA) |
| 64 | obj = self.get_object() |
Tony Mack | 3d04279 | 2014-03-17 19:18:37 -0400 | [diff] [blame] | 65 | obj.caller = request.user |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 66 | 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 Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 71 | class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView): |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 72 | #queryset = {{ object.camel }}.objects.all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 73 | serializer_class = {{ object.camel }}Serializer |
Tony Mack | eb8eb31 | 2014-02-04 20:50:39 -0500 | [diff] [blame] | 74 | |
| 75 | def get_queryset(self): |
Tony Mack | 9525eba | 2014-02-05 10:57:21 -0500 | [diff] [blame] | 76 | 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 Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 92 | |
| 93 | {% endfor %} |