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.
100 lines
3.8 KiB
100 lines
3.8 KiB
# -*- coding: utf-8 -*-
|
|
from django.shortcuts import render_to_response
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.core.context_processors import csrf
|
|
from django.conf import settings
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.db.models.loading import get_model
|
|
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
|
|
#models and forms
|
|
from models import Country
|
|
from forms import CountryForm, CountryChangeForm
|
|
from file.models import FileModel, TmpFile
|
|
from file.forms import FileModelForm
|
|
#custom views
|
|
from functions.custom_views import objects_list, add_object_with_file
|
|
|
|
def country_all(request):
|
|
"""
|
|
Return list of all countries with pagination
|
|
"""
|
|
return objects_list(request, Country, 'country_all.html')
|
|
|
|
|
|
def country_add(request):
|
|
"""
|
|
Return form of country and file and post it on the server.
|
|
"""
|
|
return add_object_with_file(request, CountryForm, 'country_add.html', '/country/all/')
|
|
|
|
|
|
@login_required
|
|
def country_change(request, url):
|
|
"""
|
|
Return form of county and file and fill it with existing Country object data.
|
|
|
|
If form of country is posted redirect on the page of all countries.
|
|
|
|
FileForm posts with ajax with calling ajax_post function
|
|
"""
|
|
|
|
#check if country_id exists else redirect to the list of countries
|
|
try:
|
|
c = Country.objects.get(url=url)
|
|
country_id = getattr(c, 'id')
|
|
#initial hidden input for checking model of object
|
|
file_form = FileModelForm(initial={'model': 'country.Country'})
|
|
except:
|
|
return HttpResponseRedirect('/country/all')
|
|
|
|
if request.POST:
|
|
#country_id sending for saving capital field in __init__
|
|
form = CountryChangeForm(request.POST, country_id=country_id)
|
|
|
|
if form.is_valid():
|
|
form.save(country_id)
|
|
return HttpResponseRedirect('/country/all/')
|
|
else:
|
|
#fill form with data from database
|
|
data = {'population' : c.population, 'teritory' : c.teritory, #data from NOT translated fields
|
|
'timezone' : c.timezone, 'region' : c.region,
|
|
'phone_code' : c.phone_code, 'time_delivery' : c.time_delivery}
|
|
|
|
if c.capital:
|
|
data['capital'] = c.capital.id
|
|
|
|
#data from translated fields
|
|
for code, name in settings.LANGUAGES:
|
|
obj = Country._meta.translations_model.objects.get(language_code = code,master__id=country_id) #access to translated fields
|
|
data['name_%s' % code] = obj.name
|
|
data['description_%s' % code] = obj.description
|
|
data['rules_%s' % code] = obj.rules
|
|
data['documents_%s' % code] = obj.documents
|
|
data['consulate_%s' % code] = obj.consulate
|
|
data['title_%s' % code] = obj.title
|
|
data['keywords_%s' % code] = obj.keywords
|
|
data['descriptions_%s' % code] = obj.descriptions
|
|
#data from manytomany fields
|
|
data['big_cities'] = [item.id for item in c.big_cities.all()]
|
|
data['language'] = [item.id for item in c.language.all()]
|
|
data['currency'] = [item.id for item in c.currency.all()]
|
|
#initial forms
|
|
#country_id sending for initialing capital field in __init__
|
|
form = CountryChangeForm(initial=data, country_id = c.id)
|
|
|
|
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'))
|
|
|
|
#uses for creating hidden input which will be used for generating ajax url
|
|
args['obj_id'] = country_id
|
|
|
|
return render_to_response('country_add.html', args)
|
|
|
|
|