# -*- coding: utf-8 -*- from django.shortcuts import render_to_response from django.views.generic import DetailView, FormView from functions.custom_views import ListView from django.utils import translation from django.http import HttpResponseRedirect, HttpResponse from django.template import RequestContext from django.core.context_processors import csrf from django.http import Http404 from django.shortcuts import get_object_or_404 from django.utils.translation import ugettext as _ #models from place_conference.models import PlaceConference from country.models import Country from city.models import City from models import PlaceExposition from meta.views import MetadataMixin from django.conf import settings from functions.cache_mixin import JitterCacheMixin, CacheMixin 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: 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(ListView): template_name = 'client/place/photo.html' obj = None 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 return place.photogallery.photos.all() 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 = 10 template_name = 'client/place/place_list.html' def get_queryset(self): #qs = super(PlaceList, self).get_queryset().order_by('-rating') qs= PlaceExposition.objects.language().select_related('country', 'city').all().order_by('-rating') conf_qs = PlaceConference.objects.language().all() return list(qs)+list(conf_qs) class PlaceCatalog(JitterCacheMixin, MetadataMixin, ListView): cache_range = settings.CACHE_RANGE model = PlaceExposition paginate_by = 10 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