[damned-lies] Convert person detail views to class-based views



commit 58ceebfa0605b5fbb915c9683abd4e9842e2e1f6
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat May 14 23:18:26 2011 +0200

    Convert person detail views to class-based views

 people/urls.py  |   14 +++++++-------
 people/views.py |   38 ++++++++++++++++++--------------------
 2 files changed, 25 insertions(+), 27 deletions(-)
---
diff --git a/people/urls.py b/people/urls.py
index 0cd6aba..046b31f 100644
--- a/people/urls.py
+++ b/people/urls.py
@@ -1,6 +1,6 @@
 from django.conf.urls.defaults import *
 
-from people.views import PeopleListView
+from people.views import PeopleListView, PersonDetailView
 
 
 # Regex order is really important here
@@ -22,14 +22,14 @@ urlpatterns = patterns('people.views',
         view = 'person_team_leave',
         name='person_team_leave'),
     url(
-        regex = r'^(?P<person_id>\d+)/$',
-        view = 'person_detail',
-        name='person_detail_id'),
+        r'^(?P<pk>\d+)/$',
+        PersonDetailView.as_view(),
+        name = 'person_detail_id'),
     # Equivalent to the previous, but using username instead of user pk
     url(
-        regex = r'^(?P<person_username>[\w \ \-]+)/$',
-        view = 'person_detail',
-        name='person_detail_username'),
+        r'^(?P<slug>[\w \ \-]+)/$',
+        PersonDetailView.as_view(),
+        name = 'person_detail_username'),
 )
 
 urlpatterns += patterns('',
diff --git a/people/views.py b/people/views.py
index 9c64b33..e83328e 100644
--- a/people/views.py
+++ b/people/views.py
@@ -33,7 +33,7 @@ from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext
 from django.utils import formats
 from django.utils.translation import ugettext_lazy, ugettext as _
-from django.views.generic import ListView
+from django.views.generic import ListView, DetailView
 
 from people.models import Person
 from teams.models import Team, Role
@@ -49,26 +49,24 @@ class PeopleListView(ListView):
         context['pageSection'] = "teams"
         return context
 
+class PersonDetailView(DetailView):
+    model = Person
+    slug_field = 'username'
+    context_object_name = 'person'
 
-def person_detail(request, person_id=None, person_username=None):
-    if person_id:
-        person = get_object_or_404(Person, pk=person_id)
-    else:
-        person = get_object_or_404(Person, username=person_username)
-
-    states = StateDb.objects.filter(actiondb__person=person).distinct()
-    all_languages = [(lg[0], LANG_INFO.get(lg[0], {'name_local': lg[1]})['name_local']) for lg in settings.LANGUAGES]
-    all_languages.sort(key=itemgetter(1))
-    context = {
-        'pageSection': "teams",
-        'all_languages': all_languages,
-        'person': person,
-        'on_own_page': request.user.is_authenticated() and person.username == request.user.username,
-        'states': states,
-        'dateformat': formats.get_format('DATE_FORMAT'),
-    }
-    return render_to_response('people/person_detail.html', context,
-            context_instance=RequestContext(request))
+    def get_context_data(self, **kwargs):
+        context = super(PersonDetailView, self).get_context_data(**kwargs)
+        states = StateDb.objects.filter(actiondb__person=self.object).distinct()
+        all_languages = [(lg[0], LANG_INFO.get(lg[0], {'name_local': lg[1]})['name_local']) for lg in settings.LANGUAGES]
+        all_languages.sort(key=itemgetter(1))
+        context.update({
+            'pageSection': "teams",
+            'all_languages': all_languages,
+            'on_own_page': self.request.user.is_authenticated() and self.object.username == self.request.user.username,
+            'states': states,
+            'dateformat': formats.get_format('DATE_FORMAT'),
+        })
+        return context
 
 @login_required
 def person_detail_change(request):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]