[damned-lies] Change person edit view as class-based view



commit c79b80db5d31c3e40e45bb7caabc8d742e1f3fff
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat May 14 23:50:19 2011 +0200

    Change person edit view as class-based view

 people/urls.py  |   15 ++++++++-------
 people/views.py |   40 +++++++++++++++++++---------------------
 2 files changed, 27 insertions(+), 28 deletions(-)
---
diff --git a/people/urls.py b/people/urls.py
index 046b31f..72a4a6b 100644
--- a/people/urls.py
+++ b/people/urls.py
@@ -1,14 +1,15 @@
 from django.conf.urls.defaults import *
+from django.contrib.auth.decorators import login_required
 
-from people.views import PeopleListView, PersonDetailView
+from people import views
 
 
 # Regex order is really important here
 urlpatterns = patterns('people.views',
     url(
-        regex = r'^detail_change/$',
-        view = 'person_detail_change',
-        name='person_detail_change'),
+        r'^detail_change/$',
+        login_required(views.PersonEditView.as_view()),
+        name = 'person_detail_change'),
     url(
         regex = r'^password_change/$',
         view = 'person_password_change',
@@ -23,15 +24,15 @@ urlpatterns = patterns('people.views',
         name='person_team_leave'),
     url(
         r'^(?P<pk>\d+)/$',
-        PersonDetailView.as_view(),
+        views.PersonDetailView.as_view(),
         name = 'person_detail_id'),
     # Equivalent to the previous, but using username instead of user pk
     url(
         r'^(?P<slug>[\w \ \-]+)/$',
-        PersonDetailView.as_view(),
+        views.PersonDetailView.as_view(),
         name = 'person_detail_username'),
 )
 
 urlpatterns += patterns('',
-    url(r'^$', PeopleListView.as_view(), name='people'),
+    url(r'^$', views.PeopleListView.as_view(), name='people'),
 )
diff --git a/people/views.py b/people/views.py
index e83328e..0940ed1 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, DetailView
+from django.views.generic import ListView, DetailView, UpdateView
 
 from people.models import Person
 from teams.models import Team, Role
@@ -68,27 +68,25 @@ class PersonDetailView(DetailView):
         })
         return context
 
- login_required
-def person_detail_change(request):
-    """Handle the form to change the details"""
-    person = get_object_or_404(Person, username=request.user.username)
-    if request.method == 'POST':
-        form = DetailForm(request.POST, instance=person)
-        if form.is_valid():
-            form.save()
-        else:
-            messages.error(request, _("Sorry, the form is not valid."))
-    else:
-        form = DetailForm(instance=person)
+class PersonEditView(UpdateView):
+    model = Person
+    slug_field = 'username'
+    form_class = DetailForm
+    template_name = 'people/person_detail_change_form.html'
 
-    context = {
-        'pageSection': "teams",
-        'person': person,
-        'on_own_page': person.username == request.user.username,
-        'form': form,
-    }
-    return render_to_response('people/person_detail_change_form.html', context,
-            context_instance=RequestContext(request))
+    def get_object(self):
+        self.kwargs['slug'] = self.request.user.username
+        return super(PersonEditView, self).get_object()
+
+    def get_context_data(self, **kwargs):
+        context = super(PersonEditView, self).get_context_data(**kwargs)
+        context['pageSection'] = "teams"
+        context['on_own_page'] = self.object.username == self.request.user.username,
+        return context
+
+    def form_invalid(self, form):
+        messages.error(self.request, _("Sorry, the form is not valid."))
+        return super(PersonEditView, self).form_invalid(form)
 
 @login_required
 def person_team_join(request):



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