use class-based generic views
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 2 May 2012 09:35:00 +0000 (11:35 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Wed, 2 May 2012 09:35:00 +0000 (11:35 +0200)
apps/api/urls.py
apps/dictionary/urls.py
apps/dictionary/views.py
wolnelektury/urls.py

index f9f9c22..e24c3c5 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from django.conf.urls.defaults import *
 from django.views.decorators.csrf import csrf_exempt
+from django.views.generic import TemplateView
 from piston.authentication import OAuthAuthentication, oauth_access_token 
 from piston.resource import Resource
 
@@ -32,8 +33,7 @@ urlpatterns = patterns(
     url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
 
 ) + patterns('',
-    url(r'^$', 'django.views.generic.simple.direct_to_template',
-            {'template': 'api/main.html'}, name='api'),
+    url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
 
 
     # changes handlers
index 435e0bd..1a33af9 100755 (executable)
@@ -3,10 +3,10 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.conf.urls.defaults import *
-
+from dictionary.views import NotesView
 
 urlpatterns = patterns('dictionary.views',
-    url(r'^$', 'letter_notes', name='dictionary_notes'),
-    url(r'(?P<letter>[a-z]|0-9)/$', 'letter_notes', name='dictionary_notes'),
+    url(r'^$', NotesView.as_view(), name='dictionary_notes'),
+    url(r'(?P<letter>[a-z]|0-9)/$', NotesView.as_view(), name='dictionary_notes'),
 )
 
index 47204fd..a2a814b 100755 (executable)
@@ -2,15 +2,24 @@
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-from django.views.generic.list_detail import object_list
 from dictionary.models import Note
+from django.views.generic.list import ListView
 
-def letter_notes(request, letter=None):
-    letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
-    objects = Note.objects.all()
-    if letter == "0-9":
-        objects = objects.filter(sort_key__regex=r"^[0-9]")
-    elif letter:
-        objects = objects.filter(sort_key__startswith=letter)
 
-    return object_list(request, queryset=objects, extra_context=locals())
+class NotesView(ListView):
+    def get_queryset(self):
+        self.letters = ["0-9"] + [chr(a) for a in range(ord('a'), ord('z')+1)]
+        self.letter = self.kwargs.get('letter')
+
+        objects = Note.objects.all()
+        if self.letter == "0-9":
+            objects = objects.filter(sort_key__regex=r"^[0-9]")
+        elif self.letter:
+            objects = objects.filter(sort_key__startswith=self.letter)
+        return ListView.get_queryset(self)
+
+    def get_context_data(self, **kwargs):
+        context = super(NotesView, self).get_context_data(**kwargs)
+        context['letter'] = self.letter
+        context['letters'] = self.letters
+        return context
index 5323ada..e501ae1 100644 (file)
@@ -4,6 +4,7 @@ import os
 from django.conf.urls.defaults import *
 from django.conf import settings
 from django.contrib import admin
+from django.views.generic import RedirectView
 import wolnelektury_core.views
 
 
@@ -55,20 +56,20 @@ urlpatterns += patterns('',
     url(r'^i18n/', include('django.conf.urls.i18n')),
 )
 
-urlpatterns += patterns('django.views.generic.simple',
+urlpatterns += patterns('',
     # old static pages - redirected
-    url(r'^1procent/$', 'redirect_to',
-        {'url': 'http://nowoczesnapolska.org.pl/wesprzyj_nas/'}),
-    url(r'^epub/$', 'redirect_to',
-        {'url': '/katalog/lektury/'}),
-    url(r'^mozesz-nam-pomoc/$', 'redirect_to',
-        {'url': '/info/mozesz-nam-pomoc'}),
-    url(r'^o-projekcie/$', 'redirect_to',
-        {'url': '/info/o-projekcie'}),
-    url(r'^widget/$', 'redirect_to',
-        {'url': '/info/widget'}),
-    url(r'^wolontariat/$', 'redirect_to',
-        {'url': '/info/mozesz-nam-pomoc/'}),
+    url(r'^1procent/$', RedirectView.as_view(
+        url='http://nowoczesnapolska.org.pl/wesprzyj_nas/')),
+    url(r'^epub/$', RedirectView.as_view(
+        url='/katalog/lektury/')),
+    url(r'^mozesz-nam-pomoc/$', RedirectView.as_view(
+        url='/info/mozesz-nam-pomoc')),
+    url(r'^o-projekcie/$', RedirectView.as_view(
+        url='/info/o-projekcie')),
+    url(r'^widget/$', RedirectView.as_view(
+        url='/info/widget')),
+    url(r'^wolontariat/$', RedirectView.as_view(
+        url='/info/mozesz-nam-pomoc/')),
 )