From 7fac56f29a26ebc8befaafd2dd66a4cdc7ef7a58 Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Mon, 8 Jun 2015 22:38:13 +0200 Subject: [PATCH] Popisnejsi url pro cislo a rocnik --- seminar/models.py | 4 ++-- seminar/urls.py | 7 ++++--- seminar/views.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/seminar/models.py b/seminar/models.py index 7fc35b44..58828e9c 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -223,7 +223,7 @@ class Rocnik(SeminarModelBase): return self.prvni_rok + 1 def verejne_url(self): - return reverse('seminar_rocnik', kwargs={'pk': self.id}) + return reverse('seminar_rocnik', kwargs={'rocnik': self.rocnik}) @classmethod def cached_rocnik(cls, r_id): @@ -279,7 +279,7 @@ class Cislo(SeminarModelBase): verejne.boolean = True def verejne_url(self): - return reverse('seminar_cislo', kwargs={'pk': self.id}) + return reverse('seminar_cislo', kwargs={'rocnik': self.rocnik.rocnik, 'cislo': self.cislo}) def nasledujici(self): u"VracĂ­ None, pokud je toto poslednĂ­" diff --git a/seminar/urls.py b/seminar/urls.py index 31ae2886..3ed44bd0 100644 --- a/seminar/urls.py +++ b/seminar/urls.py @@ -6,9 +6,10 @@ urlpatterns = patterns('', url(r'^co-je-MaM/organizatori/$', views.CojemamOrganizatoriView.as_view()), url(r'^archiv/cisla/$', views.CislaView.as_view()), - url(r'^archiv/cisla/rocnik/(?P\d+)/$', views.RocnikView.as_view(), name='seminar_rocnik'), - url(r'^archiv/cisla/cislo/(?P\d+)/$', views.CisloView.as_view(), name='seminar_cislo'), - url(r'^archiv/cisla/problem/(?P\d+)/$', views.ProblemView.as_view(), name='seminar_problem'), + + url(r'^rocnik/(?P\d+)/$', views.RocnikView.as_view(), name='seminar_rocnik'), + url(r'^cislo/(?P\d+).(?P\d+)/$', views.CisloView.as_view(), name='seminar_cislo'), + url(r'^problem/(?P\d+)/$', views.ProblemView.as_view(), name='seminar_problem'), url(r'^soustredeni/(?P\d+)/$', views.SoustredeniView.as_view(), name='seminar_soustredeni'), diff --git a/seminar/views.py b/seminar/views.py index 96a89d5e..4c5063be 100644 --- a/seminar/views.py +++ b/seminar/views.py @@ -2,6 +2,9 @@ from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.views import generic +from django.utils.translation import ugettext as _ +from django.http import Http404 + from .models import Problem, Cislo, Reseni, VysledkyKCislu, Nastaveni, Rocnik, Soustredeni @@ -25,6 +28,20 @@ class RocnikView(generic.DetailView): model = Rocnik template_name = 'seminar/archiv/rocnik.html' + # Vlastni ziskavani objektu z databaze podle (Rocnik.rocnik) + def get_object(self, queryset=None): + if queryset is None: + queryset = self.get_queryset() + rocnik_arg = self.kwargs.get('rocnik') + queryset = queryset.filter(rocnik=rocnik_arg) + + try: + obj = queryset.get() + except queryset.model.DoesNotExist: + raise Http404(_("No %(verbose_name)s found matching the query") % + {'verbose_name': queryset.model._meta.verbose_name}) + return obj + class SoustredeniView(generic.DetailView): model = Soustredeni template_name = 'seminar/archiv/soustredeni.html' @@ -39,6 +56,22 @@ class RadekVysledkovky(object): class CisloView(generic.DetailView): model = Cislo template_name = 'seminar/archiv/cislo.html' + + # Vlastni ziskavani objektu z databaze podle (Rocnik.rocnik) + def get_object(self, queryset=None): + if queryset is None: + queryset = self.get_queryset() + rocnik_arg = self.kwargs.get('rocnik') + cislo_arg = self.kwargs.get('cislo') + queryset = queryset.filter(rocnik__rocnik=rocnik_arg, cislo=cislo_arg) + + try: + obj = queryset.get() + except queryset.model.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(CisloView, self).get_context_data(**kwargs)