diff --git a/country/manager.py b/country/manager.py index 46e89c6e..b0d104de 100644 --- a/country/manager.py +++ b/country/manager.py @@ -50,6 +50,32 @@ class CountryManager(TranslationManager): cache.set(key, countries, self.cache_time) return countries + def place_countries_with_count(self, cached=True): + lang = translation.get_language() + key = 'used_place_countries_count_%s'%lang + cached_countries = cache.get(key) + if cached and cached_countries: + return cached_countries + else: + + from place_exposition.models import PlaceExposition + sql = {'place_count': + """SELECT COUNT(*) + FROM place_exposition_placeexposition + WHERE place_exposition_placeexposition.country_id = country_country.id + AND place_exposition_placeexposition.is_published = 1"""} + now = datetime.datetime.now().date() + # id of unique countries + countries_id = set( + PlaceExposition.objects\ + .filter(is_published=True)\ + .values_list('country_id', flat=True) + ) + countries = set(list(self.language().filter(id__in=countries_id).extra(select=sql))) + countries = sorted(countries, key=lambda x: x.name) + cache.set(key, countries, self.cache_time) + return countries + def conference_countries_with_count(self, cached=True): lang = translation.get_language() key = 'used_conference_countries_count_%s'%lang diff --git a/functions/models_methods.py b/functions/models_methods.py index 0877eb66..6ce53d68 100644 --- a/functions/models_methods.py +++ b/functions/models_methods.py @@ -106,6 +106,31 @@ class CityManager(TranslationManager): cache.set(key, cities, self.cache_time) return cities + def place_cities_with_count(self, cached=True): + lang = translation.get_language() + key = 'used_place_cities_count_%s' % lang + if cached and key in cache: + cached_cities = cache.get(key) + return cached_cities + else: + from place_exposition.models import PlaceExposition + sql = {'place_count': + """SELECT COUNT(*) + FROM place_exposition_placeexposition + WHERE place_exposition_placeexposition.city_id = city_city.id + AND place_exposition_placeexposition.is_published = 1"""} + now = datetime.datetime.now().date() + # id of unique countries + cities_id = set( + PlaceExposition.objects\ + .filter(is_published=True)\ + .values_list('city_id', flat=True) + ) + cities = set(list(self.language().filter(id__in=cities_id).extra(select=sql))) + cities = sorted(cities, key=lambda x: x.name) + cache.set(key, cities, self.cache_time) + return cities + def conference_cities_with_count(self, cached=True): lang = translation.get_language() key = 'used_conference_cities_count_%s' % lang diff --git a/place_exposition/models.py b/place_exposition/models.py index 7c197f5c..33a3100d 100644 --- a/place_exposition/models.py +++ b/place_exposition/models.py @@ -178,7 +178,6 @@ class PlaceExposition(TranslatableModel, ExpoMixin): scheme = self.files.filter(purpose='scheme teritory') return scheme - def events(self): events = Exposition.objects.filter(place=self)[:6] return events diff --git a/place_exposition/urls.py b/place_exposition/urls.py index b850e636..908e4ee6 100644 --- a/place_exposition/urls.py +++ b/place_exposition/urls.py @@ -1,18 +1,30 @@ # -*- coding: utf-8 -*- -from django.conf.urls import patterns, include, url -from views import PlaceDetail, PlaceList, PlaceCityCatalog, PlaceCountryCatalog, PlacePhoto - +from django.conf.urls import include, patterns, url +from .views import ( + PlaceCityCatalog, + PlaceCountryCatalog, + PlaceDetail, + PlaceList, + PlacePhoto, + PlaceByCountry, + PlaceByCity, +) urlpatterns = patterns('', - url(r'country/(?P.*)/page/(?P\d+)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}), - url(r'country/(?P.*)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}), - url(r'city/(?P.*)/page/(?P\d+)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}), - url(r'city/(?P.*)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}), - url(r'(?P.*)/photo/page/(?P\d+)/$', PlacePhoto.as_view(), {'meta_id': 91}), - url(r'(?P.*)/photo/$', PlacePhoto.as_view(), {'meta_id': 91}), - url(r'page/(?P\d+)/$', PlaceList.as_view(), {'meta_id': 46}), - url(r'(?P.*)/$', PlaceDetail.as_view(), {'meta_id': 47}), - url(r'$', PlaceList.as_view(), {'meta_id':46}), + url(r'^country/$', PlaceByCountry.as_view(), {'meta_id': 49}), + url(r'^country/(?P.*)/page/(?P\d+)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}), + url(r'^country/(?P.*)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}), + + url(r'^city/$', PlaceByCity.as_view(), {'meta_id': 48}), + url(r'^city/(?P.*)/page/(?P\d+)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}), + url(r'^city/(?P.*)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}), + + url(r'^(?P.*)/photo/page/(?P\d+)/$', PlacePhoto.as_view(), {'meta_id': 91}), + url(r'^(?P.*)/photo/$', PlacePhoto.as_view(), {'meta_id': 91}), + + url(r'^page/(?P\d+)/$', PlaceList.as_view(), {'meta_id': 46}), + url(r'^(?P.*)/$', PlaceDetail.as_view(), {'meta_id': 47}), + url(r'^$', PlaceList.as_view(), {'meta_id':46}), ) diff --git a/place_exposition/views.py b/place_exposition/views.py index a8a94806..50cdfe7f 100644 --- a/place_exposition/views.py +++ b/place_exposition/views.py @@ -1,22 +1,21 @@ # -*- 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 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 -from django.shortcuts import get_object_or_404 +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 _ -#models -from place_conference.models import PlaceConference -from country.models import Country -from city.models import City -from models import PlaceExposition +from django.views.generic import DetailView, FormView +from functions.cache_mixin import CacheMixin, JitterCacheMixin +from functions.custom_views import ListView from meta.views import MetadataMixin -from django.conf import settings -from functions.cache_mixin import JitterCacheMixin, CacheMixin +from place_conference.models import PlaceConference + +from .models import PlaceExposition def catalog(request): @@ -140,6 +139,46 @@ class PlaceList(JitterCacheMixin, MetadataMixin, ListView): 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 @@ -190,4 +229,4 @@ class PlaceCityCatalog(PlaceCatalog): def get_context_data(self, **kwargs): context = super(PlaceCityCatalog, self).get_context_data(**kwargs) context['city'] = str(self.kwargs['city'].id) - return context \ No newline at end of file + return context diff --git a/templates/client/base_catalog.html b/templates/client/base_catalog.html index c7efee4a..2c4a857d 100644 --- a/templates/client/base_catalog.html +++ b/templates/client/base_catalog.html @@ -27,6 +27,8 @@ {% include 'client/includes/announces.html' %} {% include 'client/includes/side_confs.html' %} + + {% include 'client/includes/side_places.html' %} {% include 'client/includes/banners/aside_2.html' %} diff --git a/templates/client/includes/side_places.html b/templates/client/includes/side_places.html new file mode 100644 index 00000000..c4aef0be --- /dev/null +++ b/templates/client/includes/side_places.html @@ -0,0 +1,13 @@ +{% load i18n %} + diff --git a/templates/client/place/catalog_by.html b/templates/client/place/catalog_by.html new file mode 100644 index 00000000..c0ee7f47 --- /dev/null +++ b/templates/client/place/catalog_by.html @@ -0,0 +1,44 @@ +{% extends 'base_catalog.html' %} +{% load static %} +{% load i18n %} +{% load template_filters %} + +{% block style %} + +{% endblock %} + + +{% block bread_scrumbs %} + +{% endblock %} + +{% block page_title %} +
+

{{ title2 }}:

+
+{% endblock %} + +{% block content_list %} + {% with objects=object_list %} + {% for obj in objects %} + {% set cur_word = obj.name %} + {% if cur_word|slice:":1"|lower != prev_word|slice:":1"|lower and forloop.counter != 1 %} + + {% endif %} + {% if cur_word|slice:":1"|lower != prev_word|slice:":1"|lower %} +