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.
 
 
 
 
 
 

144 lines
5.0 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
#models and forms
from models import Country
from forms import CountryForm, CountryChangeForm
from file.models import FileModel, TmpFile
from file.forms import FileModelForm
#python
import random
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
@login_required
def country_all(request):
"""
Return list of all countries with pagination
"""
country_list = Country.objects.all()
paginator = Paginator(country_list, 2)#show 2 item per page
page = request.GET.get('page')
try:
countries = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
countries = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
countries = paginator.page(paginator._num_pages)
return render_to_response('country_all.html', {'countries' : countries})
@login_required
def country_add(request):
"""
Return form of country and file and post it on the server.
Create key which will be check tmp files
If form is posted redirect on the page of all countries.
FileForm posts with ajax
"""
#cheks if key already exist(when form wasn't validated)
if request.POST.get('key'):
key = request.POST['key']
else:
key = random.getrandbits(128)
file_form = FileModelForm(initial={'key': key})
if request.POST:
form = CountryForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect('/country/all/')
else:
form = CountryForm(initial={'key': key})
args = {}
args.update(csrf(request))
args['languages'] = settings.LANGUAGES
args['form'] = form
args['file_form'] = file_form
args['files'] = TmpFile.objects.filter(key=key)
return render_to_response('country_add.html', args)
@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)