| author | rougeronj |
| Tue, 17 Mar 2015 16:34:23 +0100 | |
| changeset 14 | 4d27fbc3f9df |
| parent 13 | 08f34bbc70ee |
| child 17 | cf07ed692810 |
| permissions | -rw-r--r-- |
| 13 | 1 |
|
2 |
from django.http import HttpResponse |
|
3 |
import requests |
|
4 |
import json |
|
5 |
from datetime import datetime |
|
6 |
from django.utils.dateparse import parse_datetime |
|
7 |
from django.contrib.auth.models import User |
|
|
14
4d27fbc3f9df
Succed to get the books from our api server and print them dynamically
rougeronj
parents:
13
diff
changeset
|
8 |
from .models import Book, AmmicoUser, Slide |
|
4d27fbc3f9df
Succed to get the books from our api server and print them dynamically
rougeronj
parents:
13
diff
changeset
|
9 |
from .serializers import BookSerializer, SlideSerializer |
| 13 | 10 |
|
11 |
from rest_framework.views import APIView |
|
12 |
from rest_framework.response import Response |
|
13 |
from rest_framework import authentication, permissions |
|
14 |
||
15 |
def populateUser(request): |
|
16 |
usermail = request.GET["usermail"] |
|
17 |
user = AmmicoUser.objects.get(user=User.objects.get(email=usermail)) |
|
18 |
data = {"user": usermail, "idUser": user.idUser} |
|
19 |
populateVisite(user) |
|
20 |
return HttpResponse(content=json.dumps(data), content_type='application/json') |
|
21 |
||
22 |
def populateVisite(user): |
|
23 |
# send request with usermail to get the visites of this user and add it to the database |
|
24 |
#r = requests.get('http://fui-ammico.jamespot.pro/api/api.php?&k=6c8bfcea247e8a5841288269887d88f0&d=2016-01-31&m=EXT-IRI&v=2.0&f=get&o=article&idArticle=169') |
|
25 |
||
26 |
#simulate the request |
|
27 |
r = requests.get('http://fui-ammico.jamespot.pro/api/api.php?&k=6c8bfcea247e8a5841288269887d88f0&d=2016-01-31&m=EXT-IRI&v=2.0&o=article&f=list&idUser='+user.idUser) |
|
28 |
visites = json.loads(r.content) |
|
29 |
||
30 |
for visite in visites['VAL']: |
|
31 |
r = requests.get('http://fui-ammico.jamespot.pro/api/api.php?&k=6c8bfcea247e8a5841288269887d88f0&d=2016-01-31&m=EXT-IRI&v=2.0&o=article&f=get&idArticle='+visite['idArticle']) |
|
32 |
visiteInfo = json.loads(r.content)['VAL'] |
|
33 |
book, _ = Book.objects.get_or_create( |
|
34 |
user=user, |
|
35 |
idArticle=visiteInfo['idArticle'], |
|
36 |
title=visiteInfo['title'], |
|
37 |
description=visiteInfo['description'], |
|
38 |
image=visiteInfo['image'], |
|
39 |
date=parse_datetime(visiteInfo['dateCreation']) |
|
40 |
) |
|
41 |
populateSlide(json.loads(visiteInfo['steps']), book) |
|
42 |
||
43 |
def populateSlide(steps, book): |
|
44 |
for step in steps: |
|
45 |
if (step['stop'] != None and step['stop'] != ''): |
|
46 |
||
47 |
if 'comment' in step: |
|
48 |
comment = step['comment'] |
|
49 |
else: |
|
50 |
comment = "" |
|
51 |
||
|
14
4d27fbc3f9df
Succed to get the books from our api server and print them dynamically
rougeronj
parents:
13
diff
changeset
|
52 |
print (datetime.strptime(step['DATE'] + " " + step['TIME'], "%d/%m/%Y %H:%M:%S")) |
| 13 | 53 |
|
54 |
Slide.objects.get_or_create( |
|
55 |
book=book, |
|
56 |
idStop=step['stop'], |
|
57 |
comment=comment, |
|
58 |
date=parse_datetime(str(datetime.strptime(step['DATE'] + " " + step['TIME'], "%d/%m/%Y %H:%M:%S"))) |
|
59 |
) |
|
60 |
||
61 |
class ListBooks(APIView): |
|
62 |
""" |
|
63 |
Views to list all books. |
|
64 |
""" |
|
65 |
#authentication_classes = (authentication.TokenAuthentication,) |
|
66 |
#permission_classes = (permissions.IsAdminUser,) |
|
67 |
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
|
68 |
||
69 |
def get(self, request, format=None): |
|
70 |
""" |
|
71 |
Return a list of all Books. |
|
72 |
""" |
|
73 |
books = Book.objects.all() |
|
74 |
serializer = BookSerializer(books, many=True) |
|
75 |
return Response(serializer.data) |
|
76 |
||
77 |
class InfoBook(APIView): |
|
78 |
""" |
|
79 |
View to get book informations. |
|
80 |
""" |
|
81 |
#authentication_classes = (authentication.TokenAuthentication,) |
|
82 |
#permission_classes = (permissions.IsAdminUser,) |
|
83 |
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
|
84 |
||
85 |
def get(self, request, id): |
|
86 |
try: |
|
87 |
book = Book.objects.get(id=id) |
|
88 |
except Book.DoesNotExist: |
|
89 |
return HttpResponse(status=404) |
|
90 |
||
91 |
serializer = BookSerializer(book) |
|
92 |
return Response(serializer.data) |
|
93 |
||
94 |
class ListSlides(APIView): |
|
95 |
""" |
|
96 |
Views to list all books. |
|
97 |
""" |
|
98 |
#authentication_classes = (authentication.TokenAuthentication,) |
|
99 |
#permission_classes = (permissions.IsAdminUser,) |
|
100 |
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
|
101 |
||
102 |
def get(self, request, format=None): |
|
103 |
""" |
|
104 |
Return a list of all Books. |
|
105 |
""" |
|
106 |
slides = Slide.objects.all() |
|
107 |
serializer = SlideSerializer(slides, many=True) |
|
108 |
return Response(serializer.data) |
|
109 |
||
110 |
class InfoSlide(APIView): |
|
111 |
""" |
|
112 |
View to get book informations. |
|
113 |
""" |
|
114 |
#authentication_classes = (authentication.TokenAuthentication,) |
|
115 |
#permission_classes = (permissions.IsAdminUser,) |
|
116 |
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) |
|
117 |
||
118 |
def get(self, request, id): |
|
119 |
try: |
|
120 |
slide = Slide.objects.get(id=id) |
|
121 |
except Slide.DoesNotExist: |
|
122 |
return HttpResponse(status=404) |
|
123 |
||
124 |
serializer = SlideSerializer(slide) |
|
125 |
return Response(serializer.data) |