diff --git a/events/__init__.py b/events/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/events/forms.py b/events/forms.py new file mode 100644 index 00000000..afa9cdba --- /dev/null +++ b/events/forms.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +from django import forms +from django.utils.translation import ugettext as _ + +from haystack.query import SearchQuerySet + +from functions.model_utils import EnumChoices +from exposition.models import Exposition +from conference.models import Conference + + +class FilterForm(forms.Form): + # class Meta: + # widgets = { + # 'model': forms.CheckboxSelectMultiple(), + # } + + TYPES = EnumChoices( + EXPO=(1, _(u'Выставки')), + CONF=(2, _(_(u'Конференции'))), + ) + + model = forms.TypedMultipleChoiceField(label=_(u'Тип события'), coerce=int, choices=TYPES, required=False, widget=forms.CheckboxSelectMultiple()) + + def get_models(self): + val = self.cleaned_data.get('model') + models = [] + if val: + if self.TYPES.EXPO in val: + models.append(Exposition) + if self.TYPES.CONF in val: + models.append(Conference) + else: + models = [Conference, Exposition] + return models + + def filter(self): + qs = SearchQuerySet().models(*self.get_models()).all() + d = self.cleaned_data + return qs diff --git a/events/models.py b/events/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/events/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/events/tests.py b/events/tests.py new file mode 100644 index 00000000..501deb77 --- /dev/null +++ b/events/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/events/urls.py b/events/urls.py new file mode 100644 index 00000000..fd323d55 --- /dev/null +++ b/events/urls.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +from django.conf.urls import include, patterns, url + +from .views import FilterListView + + +urlpatterns = patterns('', + url(r'^$', FilterListView.as_view(), name='main'), +) diff --git a/events/views.py b/events/views.py new file mode 100644 index 00000000..462b54c6 --- /dev/null +++ b/events/views.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +from django.views.generic.edit import FormMixin +from django.conf import settings + +from haystack.query import SearchQuerySet + +from functions.custom_views import ContextMixin +from functions.custom_views import ListView +from exposition.models import Exposition +from conference.models import Conference + +from .forms import FilterForm + + +class FilterListView(ContextMixin, FormMixin, ListView): + initial_ctx = {'filtering': True} + form_class = FilterForm + paginate_by = settings.CLIENT_PAGINATION + template_name = 'events/filter_listview.html' + + def get_form_kwargs(self): + kwargs = super(FilterListView, self).get_form_kwargs() + kwargs.update({'data': self.request.GET}) + return kwargs + + def get_queryset(self): + if self.form.is_valid(): + qs = self.form.filter() + else: + qs = SearchQuerySet().models(Exposition, Conference).all() + return qs + + def get(self, request, *args, **kwargs): + self.form = self.get_form(self.get_form_class()) + self.extra_ctx['form'] = self.form + return super(FilterListView, self).get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + return self.get(request, *args, **kwargs) diff --git a/functions/custom_views.py b/functions/custom_views.py index ef1e59f7..786259c4 100644 --- a/functions/custom_views.py +++ b/functions/custom_views.py @@ -516,3 +516,15 @@ class CreateUpdateView(SingleObjectTemplateResponseMixin, ModelFormMixin, Proces def post(self, request, *args, **kwargs): self.object = self.get_object() return super(CreateUpdateView, self).post(request, *args, **kwargs) + + +class ContextMixin(object): + initial_ctx = {} + def dispatch(self, request, *args, **kwargs): + self.extra_ctx = self.initial_ctx.copy() + return super(ContextMixin, self).dispatch(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + context = super(ContextMixin, self).get_context_data(**kwargs) + context.update(self.extra_ctx) + return context diff --git a/functions/model_utils.py b/functions/model_utils.py index d548d446..3686b0d1 100644 --- a/functions/model_utils.py +++ b/functions/model_utils.py @@ -1,3 +1,7 @@ +# -*- coding: utf-8 -*- +from operator import itemgetter + + def base_concrete_model(abstract, instance): """ Used in methods of abstract models to find the super-most concrete @@ -35,3 +39,24 @@ def base_concrete_model(abstract, instance): if issubclass(cls, abstract) and not cls._meta.abstract: return cls return instance.__class__ + + +class EnumChoices(list): + """ + A helper to create constants from choices tuples. + + Usage: + CONNECTION_CHOICES = EnumChoices( + WIRED = (1, 'Wired connection'), + WIRELESS = (2, 'Wi-fi or other generic wireless type'), + MOBILE = (3, 'Mobile broadband'), + ) + + It will then return a list with the passed tuples so you can use in the + Django's fields choices option and, additionally, the returned object will + have the constant names as attributes (eg. CONNECTION_CHOICES.MOBILE == 3). + """ + def __init__(self, **data): + for item in sorted(data.items(), key=itemgetter(1)): + self.append((item[1][0], item[1][1])) + setattr(self, item[0], item[1][0]) diff --git a/proj/urls.py b/proj/urls.py index 90549e94..c765d870 100644 --- a/proj/urls.py +++ b/proj/urls.py @@ -78,6 +78,7 @@ urlpatterns += solid_i18n_patterns('', #url(r'^translators/', include('translator.urls')), url(r'^translators/', include('specialist_catalog.urls')), url(r'^newsletters/', include('emencia.django.newsletter.urls')), + url(r'^filter/', include('events.urls', namespace='events')), url(r'^', include('accounts.urls')), url(r'^', include('exposition.urls')), url(r'^', include('settings.conference_old_urls')), # conference redirects from old version diff --git a/templates/client/base_catalog.html b/templates/client/base_catalog.html index 2c4a857d..bb32d757 100644 --- a/templates/client/base_catalog.html +++ b/templates/client/base_catalog.html @@ -19,6 +19,10 @@ {# {% include 'client/includes/online_consult.html' %} #} + {% if filtering == True %} + {% include 'client/includes/events/filter_form.html' %} + {% endif %} + {% include 'client/includes/banners/aside_1.html' %} {% include 'client/includes/services.html' %} diff --git a/templates/client/events/filter_listview.html b/templates/client/events/filter_listview.html new file mode 100644 index 00000000..13d58ea2 --- /dev/null +++ b/templates/client/events/filter_listview.html @@ -0,0 +1,41 @@ +{% extends 'base_catalog.html' %} +{% load i18n %} +{% load template_filters %} + +{% block bread_scrumbs %} +
+{% endblock %} + +{% block page_title %} +
+