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.
 
 
 
 
 
 

102 lines
3.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
from django.contrib.auth.decorators import login_required
#models and forms
from models import Company
from forms import CompanyForm
from theme.models import Tag
from city.models import City
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 company_all(request):
"""
Return list of all companies with pagination
"""
return objects_list(request, Company, 'company_all.html')
@login_required
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', '/company/all/',
{'city': City, 'tag': Tag})
@login_required
def company_change(request, company_id):
"""
Return form and fill it with existing Company object data.
If form is posted redirect on the page of all companies.
"""
try:
#check if company_id exists else redirect to the list of companies
company = Company.objects.get(id=company_id)
file_form = FileModelForm(initial={'model': 'company.Company'})
except:
return HttpResponseRedirect('/company/all/')
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('/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, 'social':company.social, 'foundation': company.foundation}
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(data)
#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)