From bf56347534bb77282111c06915d0b9a2932e5f95 Mon Sep 17 00:00:00 2001 From: Nilovna Bascunan-Vasquez Date: Tue, 24 Jan 2012 01:49:36 -0500 Subject: [PATCH 1/1] =?utf8?q?2563:=20API=20lister=20les=20chercheurs=20d'un?= =?utf8?q?e=20r=C3=A9gion,=20partie=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- auf_savoirs_en_partage/chercheurs/api.py | 58 ++++++++++++++++++++++++++++++ auf_savoirs_en_partage/conf.py.edit | 18 ---------- auf_savoirs_en_partage/urls.py | 5 +++ buildout.cfg | 2 +- 4 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 auf_savoirs_en_partage/chercheurs/api.py delete mode 100644 auf_savoirs_en_partage/conf.py.edit diff --git a/auf_savoirs_en_partage/chercheurs/api.py b/auf_savoirs_en_partage/chercheurs/api.py new file mode 100644 index 0000000..5e80de2 --- /dev/null +++ b/auf_savoirs_en_partage/chercheurs/api.py @@ -0,0 +1,58 @@ +# -*- encoding: utf-8 -* +from django.http import HttpResponse +from django.core import serializers +from chercheurs.models import Chercheur + +STATUS_OK = 200 +STATUS_ERROR = 400 +STATUS_ERROR_PERMISSIONS = 403 +STATUS_ERROR_NOT_FOUND = 404 +STATUS_ERROR_BADMETHOD = 405 + +def api(request, pays=None, region_id=None, *args, **kwargs): + api = API(request) + # if not hasattr(api, 'api_%s' % method): + # return api_return(STATUS_ERROR) + # if pays is not None: + return api.api_chercheurs_liste(pays=pays, region_id=region_id) + #elif region_id is not None: + # return api.api_chercheurs_liste(region_id=region_id) + +def api_return(status, text='', json=False): + content_type = 'text/html' + if status == STATUS_OK and json: + content_type = 'text/json' + if text is None: + if status == STATUS_ERROR: + text = 'Error' + elif status == STATUS_ERROR_NOT_FOUND: + text = 'Resource Not Found' + elif status == STATUS_ERROR_PERMISSIONS: + text = 'Invalid username or password' + elif status == STATUS_ERROR_BADMETHOD: + text = 'Invalid request method' + elif status == STATUS_OK: + text = 'OK' + + r = HttpResponse(status=status, content=text, content_type=content_type) + + if status == STATUS_ERROR_BADMETHOD: + r.Allow = 'POST' + + return r + +class API: + def __init__(self, request): + self.request = request + + def api_chercheurs_liste(self, pays=None, region_id=None): + import pdb;pdb.set_trace() + if pays is not None: + chercheurs = Chercheur.objects.filter_pays(pays) + elif region_id is not None: + chercheurs = Chercheur.objects.filter_region(region_id) + else: + return api_return(STATUS_ERROR, "Erreur dans la requete de recherche de chercheurs") + + data = serializers.serialize('json', chercheurs) + return api_return(STATUS_OK, data) diff --git a/auf_savoirs_en_partage/conf.py.edit b/auf_savoirs_en_partage/conf.py.edit deleted file mode 100644 index bbf402b..0000000 --- a/auf_savoirs_en_partage/conf.py.edit +++ /dev/null @@ -1,18 +0,0 @@ -DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. -DATABASE_NAME = 'auf_savoirs_en_partage_django.db' -DATABASE_USER = '' # Not used with sqlite3. -DATABASE_PASSWORD = '' # Not used with sqlite3. -DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. -DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. -ROA_API_KEY = '' - -# Don't share this with anybody. -SECRET_KEY = '' - -CALENDRIER_URL = 'http://davin:davin@cal.ro.auf.org/caldav.php/davin/test/' -SITE_ROOT_URL = 'http://www.savoirsenpartage.auf.org' - -AUTH_PASSWORD_REQUIRED = True -EMAIL_SUBJECT_PREFIX = '[savoirsenpartage - TEST] ' - -SITE_DOMAIN = 'test.savoirsenpartage.auf.org' diff --git a/auf_savoirs_en_partage/urls.py b/auf_savoirs_en_partage/urls.py index 2f8428c..52e3a99 100644 --- a/auf_savoirs_en_partage/urls.py +++ b/auf_savoirs_en_partage/urls.py @@ -103,6 +103,11 @@ urlpatterns = sep_patterns + patterns( (r'^etablissements/autocomplete/$', 'chercheurs.views.etablissements_autocomplete'), (r'^etablissements/autocomplete/(?P.*)/$', 'chercheurs.views.etablissements_autocomplete'), + # API chercheurs + (r'^api/chercheurs/(?P.*)/$', 'chercheurs.api.api'), # (?P.*)/$', 'chercheurs.api.api'), + (r'^api/chercheurs/(?P\d+)/$', 'chercheurs.api.api'), + + # groupes (r'^groupes/$', 'chercheurs.views.groupe_index'), url(r'^groupes/(?P\d+)/$', 'chercheurs.views.groupe_retrieve', name='groupe_retrieve'), diff --git a/buildout.cfg b/buildout.cfg index 2049f20..c52b161 100644 --- a/buildout.cfg +++ b/buildout.cfg @@ -25,7 +25,7 @@ eggs = auf_references_client==0.4.9 chardet==1.0.1 pytz==2010l feedparser==4.1 - BeautifulSoup==3.1.0.1 + BeautifulSoup==3.2.0 django_compressor==0.5.3 django_pagination==1.0.7 django_sorting==0.1 -- 1.7.10.4