diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2014-12-30 16:59:44 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-05-06 15:38:32 +0200 |
commit | 25ec2a9794786ac83fa8ce743078305682d8298d (patch) | |
tree | 52e8f0c1333377628b60f0feefad64861980519f /ishtar_common/views.py | |
parent | 155b3890c7938e98f6c1596551260a92041bea68 (diff) | |
download | Ishtar-25ec2a9794786ac83fa8ce743078305682d8298d.tar.bz2 Ishtar-25ec2a9794786ac83fa8ce743078305682d8298d.zip |
Work on new town field (with state and department) - work on new UI for files
Diffstat (limited to 'ishtar_common/views.py')
-rw-r--r-- | ishtar_common/views.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/ishtar_common/views.py b/ishtar_common/views.py index a8ab91fb9..66b488254 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -202,6 +202,45 @@ def autocomplete_town(request): for town in towns]) return HttpResponse(data, mimetype='text/plain') +def autocomplete_advanced_town(request, department_id=None, state_id=None): + if not request.GET.get('term'): + return HttpResponse(mimetype='text/plain') + q = request.GET.get('term') + q = unicodedata.normalize("NFKD", q).encode('ascii','ignore') + query = Q() + for q in q.split(' '): + extra = Q(name__icontains=q) + if settings.COUNTRY == 'fr': + extra = extra | Q(numero_insee__istartswith=q) + if not department_id: + extra = extra | Q(departement__label__istartswith=q) + query = query & extra + if department_id: + query = query & Q(departement__number__iexact=department_id) + if state_id: + query = query & Q(departement__state__number__iexact=state_id) + limit = 20 + towns = models.Town.objects.filter(query)[:limit] + result = [] + for town in towns: + val = town.name + if hasattr(town, 'numero_insee'): + val += " (%s)" % town.numero_insee + result.append({'id':town.pk, 'value':val}) + data = json.dumps(result) + return HttpResponse(data, mimetype='text/plain') + +def department_by_state(request, state_id=''): + if not state_id: + data = [] + else: + departments = models.Department.objects.filter(state__number=state_id) + data = json.dumps([{'id':department.pk, 'number':department.number, + 'value':unicode(department)} + for department in departments]) + return HttpResponse(data, mimetype='text/plain') + + from types import NoneType def format_val(val): |