# -*- 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 ListView from functions.views_help import split_params from django.utils.translation import ugettext as _ from django.http import HttpResponse class PlaceListView(ListView): paginate_by = 2 params = None single_page = False template_name = 'place_catalog_test.html' model = 'places' 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 return context class PlacePhotoView(PlaceListView): paginate_by = 20 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].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