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.
164 lines
6.6 KiB
164 lines
6.6 KiB
# -*- coding: utf-8 -*-
|
|
# custom views
|
|
from city.models import City
|
|
from django.conf import settings
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.core.context_processors import csrf
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
from django.shortcuts import render_to_response
|
|
from file.forms import FileModelForm
|
|
from file.models import FileModel, TmpFile
|
|
from forms import CompanyDeleteForm, CompanyFilterForm, CompanyForm
|
|
from functions.admin_views import AdminListView, AdminView
|
|
from functions.custom_views import (add_object_with_file, delete_object,
|
|
objects_list)
|
|
# models and forms
|
|
from models import Company
|
|
from theme.models import Tag
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
def company_all(request):
|
|
"""
|
|
Return list of all companies with pagination
|
|
"""
|
|
return objects_list(request, Company, 'company_all.html')
|
|
|
|
|
|
def company_add(request):
|
|
"""
|
|
Return form of company and post it on the server.
|
|
If form is posted redirect on the page of all companies.
|
|
"""
|
|
return add_object_with_file(request, CompanyForm,
|
|
'company_add.html', '/admin/company/all/',
|
|
{'city': City, 'tag': Tag})
|
|
|
|
|
|
def company_delete(request, url):
|
|
return delete_object(request, Company,
|
|
CompanyDeleteForm, url, '/admin/company/all')
|
|
|
|
|
|
@login_required
|
|
def company_change(request, url):
|
|
"""
|
|
Return form and fill it with existing Company object data.
|
|
|
|
If form is posted redirect on the page of all companies.
|
|
"""
|
|
company = Company.objects.safe_get(url=url)
|
|
# try get company by id if doesnt work by url
|
|
if company is None:
|
|
company = Company.objects.safe_get(id=url)
|
|
# redirect to list of all companies if cannot find user
|
|
if company is None:
|
|
return HttpResponseRedirect('/admin/company/all/')
|
|
|
|
company_id = getattr(company, 'id')
|
|
file_form = FileModelForm(initial={'model': 'company.Company'})
|
|
|
|
if request.POST:
|
|
form = CompanyForm(request.POST)
|
|
# set choices filled by ajax
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()]
|
|
form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=request.POST['country'])]
|
|
|
|
if form.is_valid():
|
|
form.save(company_id)
|
|
return HttpResponseRedirect('/admin/company/all/')
|
|
else:
|
|
# fill form with data from database
|
|
data = {'url':company.url, 'staff_number':company.staff_number, 'address': company.address,
|
|
'phone':company.phone, 'fax':company.fax, 'web_page':company.web_page,
|
|
'email':company.email, 'foundation': company.foundation,
|
|
'company_id':company.id}
|
|
|
|
if company.country:
|
|
data['country'] = company.country.id
|
|
|
|
if company.city:
|
|
data['city'] = company.city.id
|
|
|
|
data['theme'] = [item.id for item in company.theme.all()]
|
|
data['tag'] = [item.id for item in company.tag.all()]
|
|
# data from translated fields
|
|
for code, name in settings.LANGUAGES:
|
|
obj = Company._meta.translations_model.objects.get(language_code = code,master__id=company_id) #access to translated fields
|
|
data['name_%s' % code] = obj.name
|
|
data['description_%s' % code] = obj.description
|
|
data['specialization_%s' % code] = obj.specialization
|
|
data['address_inf_%s' % code] = obj.address_inf
|
|
data['title_%s' % code] = obj.title
|
|
data['keywords_%s' % code] = obj.keywords
|
|
data['descriptions_%s' % code] = obj.descriptions
|
|
# fill form
|
|
form = CompanyForm(initial=data)
|
|
form.fields['city'].widget.attrs['data-init-text'] = company.city.name
|
|
# set choices filled by ajax
|
|
# form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=data['country'])]
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])]
|
|
|
|
args = {}
|
|
args.update(csrf(request))
|
|
|
|
args['form'] = form
|
|
args['languages'] = settings.LANGUAGES
|
|
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(company), object_id=getattr(company, 'id'))
|
|
args['obj_id'] = company_id
|
|
|
|
return render_to_response('company_add.html', args)
|
|
|
|
|
|
class CompanyListView(AdminListView):
|
|
template_name = 'c_admin/company/company_list.html'
|
|
form_class = CompanyFilterForm
|
|
model = Company
|
|
|
|
|
|
class CompanyView(AdminView):
|
|
form_class = CompanyForm
|
|
model = Company
|
|
template_name = 'c_admin/company/company.html'
|
|
success_url = '/admin/company/all/'
|
|
|
|
def get_form(self, form_class):
|
|
if self.request.POST:
|
|
return super(CompanyView, self).get_form(form_class)
|
|
obj = self.set_obj()
|
|
if obj:
|
|
data = {'url':obj.url, 'staff_number':obj.staff_number, 'address': obj.address,
|
|
'phone':obj.phone, 'fax':obj.fax, 'web_page':obj.web_page,
|
|
'email':obj.email, 'foundation': obj.foundation,
|
|
'company_id':obj.id}
|
|
|
|
if obj.country:
|
|
data['country'] = obj.country_id
|
|
|
|
if obj.city:
|
|
data['city'] = obj.city_id
|
|
|
|
data['theme'] = [item.id for item in obj.theme.all()]
|
|
data['tag'] = ','.join(['%s:%s'%(item.id, item.name) for item in obj.tag.all()])
|
|
|
|
for code, name in settings.LANGUAGES:
|
|
trans_obj = self.model._meta.translations_model.objects.get(language_code = code,master__id=obj.id) #access to translated fields
|
|
data['name_%s' % code] = trans_obj.name
|
|
data['description_%s' % code] = trans_obj.description
|
|
data['specialization_%s' % code] = trans_obj.specialization
|
|
data['address_inf_%s' % code] = trans_obj.address_inf
|
|
data['representation_%s' % code] = trans_obj.representation
|
|
data['title_%s' % code] = trans_obj.title
|
|
data['keywords_%s' % code] = trans_obj.keywords
|
|
data['descriptions_%s' % code] = trans_obj.descriptions
|
|
|
|
form =form_class(initial=data)
|
|
form.fields['city'].widget.attrs['data-init-text'] = obj.city.name
|
|
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])]
|
|
return form
|
|
else:
|
|
return form_class()
|
|
|