blob: c3e76e1c471ef3551c7d3c00daf688c08d43a97a [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 %})
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050048{% endfor %}
49
50serializerLookUp = {
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
60class {{ object.camel }}List(generics.ListCreateAPIView):
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050061 queryset = {{ object.camel }}.objects.select_related().all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050062 serializer_class = {{ object.camel }}Serializer
63
64class {{ object.camel }}Detail(generics.RetrieveUpdateDestroyAPIView):
Sapan Bhatiadf2b49e2014-01-28 19:41:07 -050065 queryset = {{ object.camel }}.objects.select_related().all()
Sapan Bhatia3bbf5ed2014-01-13 13:29:12 -050066 serializer_class = {{ object.camel }}Serializer
67
68{% endfor %}