# -*- coding: utf-8 -*- from city.models import City from country.models import Country from django.conf import settings from django.core.context_processors import csrf from django.http import Http404, HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render_to_response from django.template import RequestContext from django.utils import translation from django.utils.translation import ugettext as _ from django.views.generic import DetailView, FormView from django.utils import timezone from functions.cache_mixin import CacheMixin, JitterCacheMixin from functions.custom_views import ListView from meta.views import MetadataMixin from place_conference.models import PlaceConference from exposition.models import Exposition from .models import PlaceExposition def catalog(request): expo = list(PlaceExposition.objects.all()) conf = list(PlaceConference.objects.all()) places = expo+conf args = {'objects': places} return render_to_response('place_catalog.html', args, context_instance=RequestContext(request)) def place(request, url, photo=None): try: place = PlaceConference.objects.get(url=url) except PlaceConference.DoesNotExist: try: place = PlaceExposition.objects.get(url=url) except: raise Http404 args = {'place': place} if photo: args['object'] = place return render_to_response('photoreport.html', args, context_instance=RequestContext(request)) return render_to_response('place.html', args, context_instance=RequestContext(request)) class PlaceDetail(JitterCacheMixin, MetadataMixin, DetailView): cache_range = settings.CACHE_RANGE model = PlaceExposition slug_field = 'url' template_name = 'client/place/place_detail.html' def get_object(self, queryset=None): """ Returns the object the view is displaying. By default this requires `self.queryset` and a `pk` or `slug` argument in the URLconf, but subclasses can override this to return any object. """ # Use a custom queryset if provided; this is required for subclasses # like DateDetailView if queryset is None: queryset = self.get_queryset() # Next, try looking up by primary key. pk = self.kwargs.get(self.pk_url_kwarg, None) slug = self.kwargs.get(self.slug_url_kwarg, None) if pk is not None: queryset = queryset.filter(pk=pk) # Next, try looking up by slug. elif slug is not None: slug_field = self.get_slug_field() queryset = queryset.filter(**{slug_field: slug}) # If none of those are defined, it's an error. else: raise AttributeError("Generic detail view %s must be called with " "either an object pk or a slug." % self.__class__.__name__) try: # Get the single item from the filtered queryset obj = queryset.get() except queryset.model.DoesNotExist: try: obj = PlaceConference.objects.get(url=slug) except PlaceConference.DoesNotExist: raise Http404(_("No %(verbose_name)s found matching the query") % {'verbose_name': queryset.model._meta.verbose_name}) return obj def get_context_data(self, **kwargs): context = super(PlaceDetail, self).get_context_data(**kwargs) obj = self.object context['city'] = str(obj.city_id) context['country'] = str(obj.country_id) return context class PlacePhoto(MetadataMixin, ListView): template_name = 'client/place/photo.html' obj = None paginate_by = settings.CLIENT_PAGINATION def get_queryset(self): slug = self.kwargs.get('slug') try: place = PlaceExposition.objects.get(url=slug) except PlaceExposition.DoesNotExist: try: place = PlaceConference.objects.get(url=slug) except PlaceConference.DoesNotExist: raise Http404(_("No %(verbose_name)s found matching the query") % {'verbose_name': PlaceExposition._meta.verbose_name}) self.obj = place self.object = place # self.kwargs['object'] = place if place.photogallery: return place.photogallery.photos.all() else: raise Http404() def get_context_data(self, **kwargs): context = super(PlacePhoto, self).get_context_data(**kwargs) obj = self.obj context['object'] = obj context['city'] = str(obj.city_id) context['country'] = str(obj.country_id) return context class PlaceList(JitterCacheMixin, MetadataMixin, ListView): model = PlaceExposition paginate_by = settings.CLIENT_PAGINATION template_name = 'client/place/place_list.html' def get_queryset(self): qs = PlaceExposition.objects.language().select_related('country', 'city').order_by('-rating') conf_qs = PlaceConference.objects.language().select_related('country', 'city') return list(qs)+list(conf_qs) class PlaceCatalogBy(JitterCacheMixin, MetadataMixin, ListView): cache_range = [60*30, 60*60] template_name = 'client/place/catalog_by.html' title1 = '' # bread_scrumbs title2 = '' # page_title """ abstact class """ def get_context_data(self, **kwargs): context = super(PlaceCatalogBy, self).get_context_data(**kwargs) context.update({ 'title1': self.title1, 'title2': self.title2, 'catalog': self.catalog, 'place_catalog': '/places/' }) return context class PlaceByCountry(PlaceCatalogBy): model = Country title1 = _(u'По странам') title2 = _(u'Места по странам') catalog = 'country/' def get_queryset(self): return self.model.objects.place_countries_with_count() class PlaceByCity(PlaceCatalogBy): model = City title1 = _(u'По городам') title2 = _(u'Места по городам') catalog = 'city/' def get_queryset(self): return self.model.used.place_cities_with_count() class PlaceCatalog(JitterCacheMixin, MetadataMixin, ListView): cache_range = settings.CACHE_RANGE model = PlaceExposition paginate_by = settings.CLIENT_PAGINATION template_name = 'client/place/catalog.html' filter_object = None def get_context_data(self, **kwargs): context = super(PlaceCatalog, self).get_context_data(**kwargs) context['filter_object'] = self.filter_object context['catalog_url'] = self.catalog_url return context class PlaceCountryCatalog(PlaceCatalog): catalog_url = '/places/' def get_queryset(self): slug = self.kwargs.get('slug') country = get_object_or_404(Country, url=slug) self.kwargs['country'] = country self.filter_object = country qs = self.model.objects.language().select_related('country', 'city').filter(country=country).order_by('-rating') conf_qs = PlaceConference.objects.language().select_related('country', 'city').filter(country=country) return list(qs) + list(conf_qs) def get_context_data(self, **kwargs): context = super(PlaceCountryCatalog, self).get_context_data(**kwargs) context['country'] = str(self.kwargs['country'].id) return context class PlaceCityCatalog(PlaceCatalog): catalog_url = '/places/' def get_queryset(self): slug = self.kwargs.get('slug') city = get_object_or_404(City, url=slug) self.kwargs['city'] = city self.filter_object = city qs = self.model.objects.language().select_related('country', 'city').filter(city=city).order_by('-rating') conf_qs = PlaceConference.objects.language().select_related('country', 'city').filter(city=city) return list(qs) + list(conf_qs) def get_context_data(self, **kwargs): context = super(PlaceCityCatalog, self).get_context_data(**kwargs) context['city'] = str(self.kwargs['city'].id) return context class PlaceExpositionListView(MetadataMixin, ListView): """ Представление перечня событий относительно места. Переход на эту страницу происходит со страницы подробного просмотра места, по ссылке "Все события" """ template_name = 'client/place/place_exposition_list.html' # cache_range = settings.CACHE_RANGE def get_object(self): slug = self.kwargs.get('slug') self.object = get_object_or_404(PlaceExposition, url=slug) return self.object def get_queryset(self): return Exposition.objects.filter( place=self.get_object(), data_begin__gte=timezone.now() ).select_related( 'country', 'city' ).prefetch_related( 'tag' ) def get_context_data(self, **kwargs): ctx = super(PlaceExpositionListView, self).get_context_data(**kwargs) ctx['object'] = self.get_object() return ctx