| author | durandn |
| Fri, 12 Aug 2016 16:59:53 +0200 | |
| changeset 123 | c8a1216fd28f |
| parent 6 | 37baf9d13f32 |
| permissions | -rw-r--r-- |
|
6
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
1 |
from django.shortcuts import render, HttpResponse |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
2 |
from pprint import pprint |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
3 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
4 |
from django.views.decorators.csrf import csrf_exempt |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
5 |
from rest_framework.renderers import JSONRenderer |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
6 |
from rest_framework.parsers import JSONParser |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
7 |
from iconolab.models import Annotation |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
8 |
from .serializers import AnnotationSerializer |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
9 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
10 |
class JSONResponse(HttpResponse): |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
11 |
def __init__(self, data, **kwargs): |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
12 |
content = JSONRenderer().render(data) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
13 |
kwargs['content_type'] = 'application/json' |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
14 |
super(JSONResponse, self).__init__(content) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
15 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
16 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
17 |
# Create your views here. |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
18 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
19 |
def index(r): |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
20 |
return HttpResponse('<p>You better know ... </p>') |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
21 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
22 |
@csrf_exempt |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
23 |
def annotation_list(request): |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
24 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
25 |
if request.method == 'GET': |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
26 |
annotations = Annotation.objects.all() |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
27 |
serializer = AnnotationSerializer(annotations, many=True) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
28 |
return JSONResponse(serializer.data) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
29 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
30 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
31 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
32 |
def get(request, pk): |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
33 |
if request.method == 'GET': |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
34 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
35 |
try: |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
36 |
annotation = Annotation.objects.get(pk=pk) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
37 |
except Annotation.DoesNotExist: |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
38 |
return HttpResponse(status=404) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
39 |
|
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
40 |
serializer = AnnotationSerializer(annotation) |
|
37baf9d13f32
first commit
Harris Baptiste <harris.baptiste@iri.centrepompidou.fr>
parents:
diff
changeset
|
41 |
return JSONResponse(serializer.data) |