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.
304 lines
9.7 KiB
304 lines
9.7 KiB
# -*- coding: utf-8 -*-
|
|
from country.models import Country
|
|
from city.models import City
|
|
from place_exposition.models import PlaceExposition
|
|
from place_conference.models import PlaceConference
|
|
from django.views.generic import CreateView, DeleteView, UpdateView, DetailView
|
|
from functions.custom_views import ListView
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from functions.views_help import split_params
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
|
#from forms import PlaceSearchForm
|
|
from functions.search_forms import PlaceSearchForm
|
|
from functions.search_forms import EventSearchForm
|
|
from haystack.query import EmptySearchQuerySet
|
|
|
|
class EventSearchView(ListView):
|
|
paginate_by = 10
|
|
template_name = 'exposition/search.html'
|
|
search_form = EventSearchForm
|
|
|
|
|
|
def get_queryset(self):
|
|
if self.request.GET:
|
|
form = self.search_form(self.request.GET)
|
|
if form.is_valid():
|
|
return form.search()
|
|
else:
|
|
return EmptySearchQuerySet()
|
|
else:
|
|
return EmptySearchQuerySet()
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(EventSearchView, self).get_context_data(**kwargs)
|
|
context['search_form'] = EventSearchForm(self.request.GET)
|
|
queries = self.request.GET.copy()
|
|
if queries.has_key('page'):
|
|
del queries['page']
|
|
context['queries'] = queries
|
|
context['search_action'] = '/events/search/'
|
|
|
|
|
|
return context
|
|
|
|
class PlaceSearchView(ListView):
|
|
paginate_by = 10
|
|
template_name = 'place/search.html'
|
|
model = PlaceExposition
|
|
search_form = PlaceSearchForm
|
|
|
|
def get_queryset(self):
|
|
if self.request.GET:
|
|
form = self.search_form(self.request.GET)
|
|
if form.is_valid():
|
|
return form.search()
|
|
else:
|
|
return []
|
|
else:
|
|
return []
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(PlaceSearchView, self).get_context_data(**kwargs)
|
|
context['search_form'] = PlaceSearchForm(self.request.GET)
|
|
queries = self.request.GET.copy()
|
|
if queries.has_key('page'):
|
|
del queries['page']
|
|
context['queries'] = queries
|
|
context['search_action'] = '/places/search/'
|
|
context['type'] = 'places search'
|
|
|
|
return context
|
|
|
|
|
|
class PlaceListView(ListView):
|
|
paginate_by = 10
|
|
params = None
|
|
single_page = False
|
|
template_name = 'place_catalog_test.html'
|
|
model = 'places'
|
|
search_form = PlaceSearchForm
|
|
order = 'data_begin'
|
|
|
|
def get_params(self):
|
|
model_names = {'places': _(u'Места')}
|
|
model_alternative_name = {'places': 'place'}
|
|
params = [{'type':'model', 'url':self.model, 'name': model_names.get(self.model),
|
|
'alternative_name': model_alternative_name.get(self.model)}]
|
|
|
|
st = self.kwargs.get('params')
|
|
if st:
|
|
params = params + split_params(st)
|
|
|
|
return params
|
|
|
|
def get_queryset(self):
|
|
pl_ex = PlaceExposition.objects.all()
|
|
pl_conf = PlaceConference.objects.all()
|
|
|
|
params = self.get_params()
|
|
for param in params:
|
|
|
|
if param.get('type') == 'country':
|
|
country = Country.objects.safe_get(url=param.get('url'))
|
|
if country:
|
|
param['name'] = country.name
|
|
pl_ex = pl_ex.filter(country=country)
|
|
pl_conf = pl_conf.filter(country=country)
|
|
if param.get('type') == 'city':
|
|
city = City.objects.safe_get(url=param.get('url'))
|
|
if city:
|
|
param['name'] = city.name
|
|
pl_ex = pl_ex.filter(city=city)
|
|
pl_conf = pl_conf.filter(city=city)
|
|
|
|
if param.get('type') == 'place':
|
|
pl_ex = pl_ex.filter(url=param.get('url'))
|
|
if pl_ex:
|
|
query = pl_ex
|
|
else:
|
|
query = pl_conf.filter(url=param.get('url'))
|
|
|
|
self.single_page = True
|
|
|
|
if query:
|
|
param['name'] = query[0].name
|
|
|
|
#if self.request:
|
|
# views = query[0].views
|
|
# query.update(views=views+1)
|
|
self.params = params
|
|
return query
|
|
|
|
self.params = params
|
|
return list(pl_ex) + list(pl_conf)
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(PlaceListView, self).get_context_data(**kwargs)
|
|
context['filter'] = self.params
|
|
context['single_page'] = self.single_page
|
|
context['search_form'] = self.search_form
|
|
context['search_action'] = '/places/search/'
|
|
context['type'] = 'places search'
|
|
return context
|
|
|
|
class PlacePhotoView(PlaceListView):
|
|
paginate_by = 12
|
|
template_name = 'place/place_photo.html'
|
|
obj = None
|
|
|
|
def get_queryset(self):
|
|
pl_ex = PlaceExposition.objects.all()
|
|
pl_conf = PlaceConference.objects.all()
|
|
|
|
params = self.get_params()
|
|
for param in params:
|
|
|
|
if param.get('type') == 'country':
|
|
country = Country.objects.safe_get(url=param.get('url'))
|
|
if country:
|
|
param['name'] = country.name
|
|
pl_ex = pl_ex.filter(country=country)
|
|
pl_conf = pl_conf.filter(country=country)
|
|
if param.get('type') == 'city':
|
|
city = City.objects.safe_get(url=param.get('url'))
|
|
if city:
|
|
param['name'] = city.name
|
|
pl_ex = pl_ex.filter(city=city)
|
|
pl_conf = pl_conf.filter(city=city)
|
|
|
|
if param.get('type') == 'place':
|
|
pl_ex = pl_ex.filter(url=param.get('url'))
|
|
if pl_ex:
|
|
query = pl_ex
|
|
else:
|
|
query = pl_conf.filter(url=param.get('url'))
|
|
|
|
self.single_page = True
|
|
|
|
if query:
|
|
param['name'] = query[0].name
|
|
|
|
#if self.request:
|
|
# views = query[0].views
|
|
# query.update(views=views+1)
|
|
|
|
params.append({'type':'photo', 'name':_(u'Фото')})
|
|
self.params = params
|
|
self.obj = query[0]
|
|
|
|
return query[0].photogallery.photos.all()
|
|
|
|
self.params = params
|
|
return list(pl_ex) + list(pl_conf)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(PlacePhotoView, self).get_context_data(**kwargs)
|
|
context['object'] = self.obj
|
|
return context
|
|
|
|
|
|
# --------------------- Page views ------------------------
|
|
|
|
from forms import PageForm
|
|
from models import Page
|
|
|
|
class NewPage(CreateView):
|
|
form_class= PageForm
|
|
template_name = 'new_page.html'
|
|
success_url = '/admin/page/all/'
|
|
|
|
|
|
class PageList(ListView):
|
|
paginate_by = 10
|
|
template_name = 'page_admin_list.html'
|
|
model = Page
|
|
order = 'created'
|
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
import datetime
|
|
|
|
class EditPage(UpdateView):
|
|
model = Page
|
|
template_name = 'admin/page/new_page.html'
|
|
form_class = PageForm
|
|
slug_url_kwarg = 'url'
|
|
slug_field = 'url'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(EditPage,self).get_context_data(**kwargs)
|
|
context['request'] = self.request
|
|
return context
|
|
|
|
def get_success_url(self):
|
|
return reverse_lazy('page_list')
|
|
|
|
|
|
class DeletePage(DeleteView):
|
|
template_name = 'admin/page/page_confirm_delete.html'
|
|
model = Page
|
|
success_url = reverse_lazy('page_list')
|
|
slug_field = 'url'
|
|
slug_url_kwarg = 'url'
|
|
|
|
class PageDetailed(DetailView):
|
|
model = Page
|
|
template_name = 'admin/page/page_template_view.html'
|
|
slug_field = 'url'
|
|
slug_url_kwarg = 'url'
|
|
|
|
|
|
# ------------ XLS Export ----------------
|
|
|
|
from django.http import HttpResponse
|
|
from django.utils.translation import get_language
|
|
from .utils import queryset_to_workbook
|
|
from exposition.models import Exposition
|
|
from conference.models import Conference
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
def download_workbook(request):
|
|
lang = get_language()
|
|
data = request.GET
|
|
if data:
|
|
qs = []
|
|
for i,obj in enumerate(data):
|
|
if data.get('data[%i][name]'%i) == 'expo':
|
|
qs.append(Exposition.objects.language(lang).get(id=data['data[%i][value]'%i]))
|
|
elif data.get('data[%i][name]'%i) == 'conf':
|
|
qs.append(Conference.objects.language(lang).get(id=data['data[%i][value]'%i]))
|
|
|
|
earliest_event = qs[0].data_begin
|
|
for i, obj in enumerate(qs, start=1):
|
|
if obj.data_begin < earliest_event:
|
|
earliest_event = obj.data_begin
|
|
setattr(obj, 'number', i)
|
|
setattr(obj, 'dates', u'%s - %s'%(obj.data_begin.strftime('%d %B %Y'),obj.data_end.strftime('%d %B %Y')))
|
|
setattr(obj, 'full_place', u'%s, %s, %s' % (obj.country, obj.city, getattr(obj.place, 'name', '')))
|
|
try:
|
|
setattr(obj, 'link', u'http://www.expomap.ru%s'%obj.get_absolute_url())
|
|
except:
|
|
setattr(obj, 'link', u'http://www.expomap.ru%s'%obj.get_permanent_url())
|
|
|
|
columns = (
|
|
'number',
|
|
'name',
|
|
'dates',
|
|
'main_title',
|
|
'full_place',
|
|
'participation_note',
|
|
'link')
|
|
|
|
workbook = queryset_to_workbook(qs, columns, earliest_event)
|
|
response = HttpResponse(content_type='application/vnd.ms-excel')
|
|
response['Content-Disposition'] = 'attachment; filename="My calendar for %s.xls"' % earliest_event.strftime("%B %Y")
|
|
workbook.save(response)
|
|
return response
|
|
else:
|
|
return HttpResponseRedirect(request.META.get('HTTP_REFERER'), "/profile/calendar/")
|
|
|