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.
 
 
 
 
 
 

127 lines
4.9 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
#models and forms
from models import Country
from forms import CountryForm, CountryDeleteForm, CountryFilterForm
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, filtered_list
from functions.admin_views import paginate_results
from functions.forms import AdminSearchForm
from functions.admin_views import AdminListView
from hvad.utils import get_translation_aware_manager
def country_all(request):
"""
Return list of all countries with pagination
"""
if request.GET:
form = AdminSearchForm(request.POST)
if form.is_valid():
s_name = request.GET.get('search_name')
query = get_translation_aware_manager(Country)
objects = query.filter(name__contains=s_name).distinct()
#return HttpResponse(objects)
#objects = Country.objects.all()
return filtered_list(request, objects, 'country_all.html', 1000)
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', '/admin/country/all/')
def country_delete(request, url):
return delete_object(request, Country, CountryDeleteForm, url, '/admin/country/all/')
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('/admin/country/all')
if request.POST:
#country_id sending for saving capital field in __init__
form = CountryForm(request.POST, request.FILES, country_id=country_id)
if form.is_valid():
form.save(country_id)
return HttpResponseRedirect('/admin/country/all/')
else:
#fill form with data from database
data = {'population' : c.population, 'teritory' : c.teritory, #data from NOT translated fields
'timezone' : c.timezone, 'country_id' : country_id,
'phone_code' : c.phone_code, 'time_delivery' : c.time_delivery,
'logo': c.logo}
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 = CountryForm(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)
class CountryListView(AdminListView):
template_name = 'admin/country/country_list.html'
form_class = CountryFilterForm
model = Country
def get_context_data(self, **kwargs):
context = super(AdminListView, self).get_context_data(**kwargs)
qs = self.model.objects.all()
result = paginate_results(qs, page=self.request.GET.get('page'))
context['object_list'] = result
return context