[extensions-web] Add inline editing for display name



commit 773b22d2c001af7a79814b8e21cb7c6660d8b9d8
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Nov 7 17:28:52 2011 -0500

    Add inline editing for display name

 .../auth/templates/registration/profile.html       |    2 +-
 .../auth/templates/registration/profile_edit.html  |    4 ++++
 sweettooth/auth/urls.py                            |    2 ++
 sweettooth/auth/views.py                           |   17 +++++++++++++++++
 4 files changed, 24 insertions(+), 1 deletions(-)
---
diff --git a/sweettooth/auth/templates/registration/profile.html b/sweettooth/auth/templates/registration/profile.html
index e996756..dcb263e 100644
--- a/sweettooth/auth/templates/registration/profile.html
+++ b/sweettooth/auth/templates/registration/profile.html
@@ -5,7 +5,7 @@
 {% load thumbnail %}
 {% load gravatar %}
 <div class="profile">
-  <h2>{{ display_name }}</h2>
+  <h2 id="new_display_name">{{ display_name }}</h2>
   <img class="gravatar" src="{% gravatar_url user.email 128 %}">
 
   <h3> {{ display_name }} has authored </h3>
diff --git a/sweettooth/auth/templates/registration/profile_edit.html b/sweettooth/auth/templates/registration/profile_edit.html
index e1fce22..714ca9f 100644
--- a/sweettooth/auth/templates/registration/profile_edit.html
+++ b/sweettooth/auth/templates/registration/profile_edit.html
@@ -1 +1,5 @@
 {% extends "registration/profile.html" %}
+
+{% block document-ready %}{{ block.super }}
+    $("#new_display_name").csrfEditable("{% url auth-ajax-edit-display-name %}");
+{% endblock %}
diff --git a/sweettooth/auth/urls.py b/sweettooth/auth/urls.py
index 7e08523..d2e5f5e 100644
--- a/sweettooth/auth/urls.py
+++ b/sweettooth/auth/urls.py
@@ -10,6 +10,8 @@ urlpatterns = patterns('',
         dict(template_name='registration/login.html',
              authentication_form=forms.AuthenticationForm), name='auth-login'),
 
+    url(r'^ajax/edit/display_name/', views.ajax_change_display_name, name='auth-ajax-edit-display-name'),
+
     url(r'^logout/', logout,
         dict(next_page='/'), name='auth-logout'),
 
diff --git a/sweettooth/auth/views.py b/sweettooth/auth/views.py
index 1e26fb8..2191b1c 100644
--- a/sweettooth/auth/views.py
+++ b/sweettooth/auth/views.py
@@ -1,11 +1,13 @@
 
 from django.contrib.auth import models
 from django.contrib.auth.decorators import login_required
+from django.http import HttpResponseForbidden
 from django.shortcuts import get_object_or_404, redirect
 
 from review.models import CodeReview
 from extensions.models import Extension
 
+from decorators import ajax_view, post_only_view
 from utils import render
 
 def profile(request, user):
@@ -24,6 +26,21 @@ def profile(request, user):
                                           extensions=extensions,
                                           reviews=reviews,))
 
+ ajax_view
+ post_only_view
+ login_required
+def ajax_change_display_name(request):
+    if request.POST['id'] != 'new_display_name':
+        return HttpResponseForbidden()
+
+    if not request.user.is_authenticated():
+        return HttpResponseForbidden()
+
+    # display name is "%s %s" % (first_name, last_name). Change the first name.
+    request.user.first_name = request.POST['value']
+    request.user.save()
+    return request.POST['value']
+
 @login_required
 def profile_redirect(request):
     return redirect('auth-profile', user=request.user.username)



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