You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.3 KiB
33 lines
1.3 KiB
import json
|
|
from django.views.generic import DetailView
|
|
from django.http import HttpResponse
|
|
from django.utils import translation
|
|
from haystack.query import SearchQuerySet
|
|
from models import City
|
|
from settings.views import get_by_lang
|
|
|
|
|
|
def get_city(request):
|
|
"""
|
|
returns filtered cities in current language in json format
|
|
"""
|
|
if request.is_ajax():
|
|
country = request.GET.get('country')
|
|
term = request.GET.get('term', '').capitalize()
|
|
lang = translation.get_language()
|
|
if country:
|
|
if not term:
|
|
qs = City.objects.language().filter(country=country).order_by('translations__name').distinct()[:100]
|
|
else:
|
|
qs = City.objects.language().filter(country=country, translations__name__contains=term).distinct()
|
|
result = [{'id': city.id, 'label': city.name} for city in qs]
|
|
else:
|
|
if not term:
|
|
qs = SearchQuerySet().models(City).all().order_by('name_%s'%lang)[:30]
|
|
else:
|
|
qs = SearchQuerySet().models(City).autocomplete(content_auto=term).order_by('name_%s'%lang)[:30]
|
|
result = [{'id': city.pk, 'label': get_by_lang(city, 'name', lang)} for city in qs]
|
|
|
|
return HttpResponse(json.dumps(result, indent=4), content_type='application/json')
|
|
else:
|
|
return HttpResponse('not ajax')
|
|
|