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.
93 lines
3.2 KiB
93 lines
3.2 KiB
# -*- coding: utf-8 -*-
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
from django.http import HttpResponseRedirect
|
|
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
|
|
#models and forms
|
|
from forms import CityForm, CityDeleteForm
|
|
from models import City
|
|
from file.models import FileModel
|
|
from file.forms import FileModelForm
|
|
#custom views
|
|
from functions.custom_views import objects_list, add_object_with_file, delete_object
|
|
|
|
|
|
def city_all(request):
|
|
"""
|
|
return list of all cities with pagination
|
|
"""
|
|
return objects_list(request, City, 'city_all.html')
|
|
|
|
|
|
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)
|
|
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}
|
|
|
|
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) |