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 | 60af03d | 2014-01-28 20:12:08 -0500 | [diff] [blame^] | 47 | fields = ({% for prop in object.props %}'{{ prop }}',{% endfor %}{% for ref in object.refs %}'{{ ref }}',{% 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 |
| 63 | |
| 64 | class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView): |
Sapan Bhatia | df2b49e | 2014-01-28 19:41:07 -0500 | [diff] [blame] | 65 | queryset = {{ object.camel }}.objects.select_related().all() |
Sapan Bhatia | 3bbf5ed | 2014-01-13 13:29:12 -0500 | [diff] [blame] | 66 | serializer_class = {{ object.camel }}Serializer |
| 67 | |
| 68 | {% endfor %} |