Commit | Line | Data |
---|---|---|
bf563475 NBV |
1 | # -*- encoding: utf-8 -* |
2 | from django.http import HttpResponse | |
3 | from django.core import serializers | |
4 | from chercheurs.models import Chercheur | |
5 | ||
6 | STATUS_OK = 200 | |
7 | STATUS_ERROR = 400 | |
8 | STATUS_ERROR_PERMISSIONS = 403 | |
9 | STATUS_ERROR_NOT_FOUND = 404 | |
10 | STATUS_ERROR_BADMETHOD = 405 | |
11 | ||
87567c72 | 12 | def api(request, pays=None, region=None, *args, **kwargs): |
bf563475 NBV |
13 | api = API(request) |
14 | # if not hasattr(api, 'api_%s' % method): | |
15 | # return api_return(STATUS_ERROR) | |
16 | # if pays is not None: | |
87567c72 | 17 | return api.api_chercheurs_liste(pays=pays, region=region) |
bf563475 NBV |
18 | #elif region_id is not None: |
19 | # return api.api_chercheurs_liste(region_id=region_id) | |
20 | ||
21 | def api_return(status, text='', json=False): | |
22 | content_type = 'text/html' | |
23 | if status == STATUS_OK and json: | |
24 | content_type = 'text/json' | |
25 | if text is None: | |
26 | if status == STATUS_ERROR: | |
27 | text = 'Error' | |
28 | elif status == STATUS_ERROR_NOT_FOUND: | |
29 | text = 'Resource Not Found' | |
30 | elif status == STATUS_ERROR_PERMISSIONS: | |
31 | text = 'Invalid username or password' | |
32 | elif status == STATUS_ERROR_BADMETHOD: | |
33 | text = 'Invalid request method' | |
34 | elif status == STATUS_OK: | |
35 | text = 'OK' | |
36 | ||
37 | r = HttpResponse(status=status, content=text, content_type=content_type) | |
38 | ||
39 | if status == STATUS_ERROR_BADMETHOD: | |
40 | r.Allow = 'POST' | |
41 | ||
42 | return r | |
43 | ||
44 | class API: | |
45 | def __init__(self, request): | |
46 | self.request = request | |
47 | ||
87567c72 | 48 | def api_chercheurs_liste(self, pays=None, region=None): |
bf563475 NBV |
49 | if pays is not None: |
50 | chercheurs = Chercheur.objects.filter_pays(pays) | |
87567c72 | 51 | elif region is not None: |
ebde48e0 | 52 | chercheurs = Chercheur.objects.filter_region(region) |
bf563475 NBV |
53 | else: |
54 | return api_return(STATUS_ERROR, "Erreur dans la requete de recherche de chercheurs") | |
55 | ||
56 | data = serializers.serialize('json', chercheurs) | |
87567c72 | 57 | import pdb;pdb.set_trace() |
bf563475 | 58 | return api_return(STATUS_OK, data) |