[#2534] Améliorations à l'interface utilisateur du formulaire d'établissements.
[auf_django_references.git] / auf / django / references / views.py
CommitLineData
652c5d02
EMS
1from django.http import HttpResponse
2from django.utils import simplejson
3
4from auf.django.references import models as ref
5
6def autocomplete_etablissements(request):
7 term = request.GET.get('term')
8 if term:
9 etablissements = ref.Etablissement.objects.filter(nom__icontains=term)
10 pays = request.GET.get('pays')
11 if pays:
12 etablissements = etablissements.filter(pays=pays)
13 result = [{'id': e.id, 'label': e.nom} for e in etablissements]
14 else:
15 result = [{'id': e.id, 'label': '%s (%s)' % (e.nom, e.pays.nom), 'value': e.nom}
16 for e in etablissements]
17 else:
18 result = None
19 return HttpResponse(simplejson.dumps(result), mimetype='application/json')
20
21def etablissement_json(request, id):
22 etablissement = ref.Etablissement.objects.get(id=int(id))
23 result = {}
24 for f in etablissement._meta.fields:
25 result[f.name] = getattr(etablissement, f.attname)
e5fdef4e
EMS
26 json = simplejson.dumps(result,
27 default=lambda obj: obj.isoformat if hasattr(obj, 'isoformat') else '')
28 return HttpResponse(json, mimetype='application/json')