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.
111 lines
4.0 KiB
111 lines
4.0 KiB
# -*- coding: utf-8 -*-
|
|
import json
|
|
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.core.context_processors import csrf
|
|
from django.conf import settings
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.utils.translation import get_language
|
|
|
|
from exposition.admin import get_by_lang
|
|
from forms import CityForm, CityDeleteForm, CityFilterForm
|
|
from models import City
|
|
from file.models import FileModel
|
|
from file.forms import FileModelForm
|
|
from haystack.query import SearchQuerySet
|
|
from functions.custom_views import objects_list, add_object_with_file, delete_object
|
|
from functions.admin_views import AdminListView
|
|
from functions.http import JsonResponse
|
|
|
|
|
|
def city_add(request):
|
|
"""
|
|
Return form of city and post it on the server.
|
|
If form is posted redirect on the page of all cities.
|
|
"""
|
|
return add_object_with_file(request, CityForm, 'city_add.html', '/admin/city/all')
|
|
|
|
|
|
def city_delete(request, url):
|
|
return delete_object(request, City, CityDeleteForm, url, '/admin/city/all/')
|
|
|
|
|
|
@login_required
|
|
def city_change(request, url):
|
|
"""
|
|
Return form and fill it with existing City object data.
|
|
|
|
If form is posted redirect on the page of all cities.
|
|
"""
|
|
try:
|
|
#check if city_id exists else redirect to the list of cities
|
|
c = City.objects.get(url=url)
|
|
city_id = getattr(c, 'id')
|
|
file_form = FileModelForm(initial={'model': 'city.City'})
|
|
except:
|
|
return HttpResponseRedirect('/admin/city/all')
|
|
|
|
if request.POST:
|
|
form = CityForm(request.POST, request.FILES)
|
|
if form.is_valid():
|
|
form.save(city_id)
|
|
return HttpResponseRedirect('/admin/city/all')
|
|
else:
|
|
#fill form with data from database
|
|
data = {'population' : c.population, 'phone_code' : c.phone_code,
|
|
'city_id' : city_id, 'inflect':c.inflect, 'logo': c.logo}
|
|
|
|
if c.country:
|
|
data['country'] = c.country.id
|
|
|
|
if c.code_IATA:
|
|
data['code_IATA'] = c.code_IATA.id
|
|
|
|
#data from translated fields
|
|
for code, name in settings.LANGUAGES:
|
|
obj = City._meta.translations_model.objects.get(language_code = code,master__id=city_id) #access to translated fields
|
|
data['name_%s' % code] = obj.name
|
|
data['region_%s' % code] = obj.region
|
|
data['description_%s' % code] = obj.description
|
|
data['famous_places_%s' % code] = obj.famous_places
|
|
data['shoping_%s' % code] = obj.shoping
|
|
data['transport_%s' % code] = obj.transport
|
|
data['title_%s' % code] = obj.title
|
|
data['keywords_%s' % code] = obj.keywords
|
|
data['descriptions_%s' % code] = obj.descriptions
|
|
#fill form
|
|
form = CityForm(initial=data)
|
|
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
args['languages'] = settings.LANGUAGES
|
|
args['form'] = form
|
|
args['file_form'] = file_form
|
|
|
|
#get list of files which connected with specific model object
|
|
args['files'] = FileModel.objects.filter(content_type=ContentType.objects.get_for_model(c),
|
|
object_id=getattr(c, 'id'))
|
|
args['obj_id'] = city_id
|
|
|
|
return render_to_response('city_add.html', args)
|
|
|
|
|
|
def search_city(request):
|
|
term = request.GET.get('term')
|
|
country = request.GET.get('country')
|
|
if not term:
|
|
qs = City.objects.language().filter(country=country)[:200]
|
|
else:
|
|
qs = City.objects.language().filter(country=country, translations__name__contains=term)
|
|
result = list(set([{'id': city.id, 'label': city.name} for city in qs]))
|
|
return JsonResponse(json.dumps(result), content_type='application/json', safe=False)
|
|
|
|
|
|
class CityListView(AdminListView):
|
|
template_name = 'c_admin/city/city_list.html'
|
|
form_class = CityFilterForm
|
|
model = City
|
|
|