[snowy: 6/26] Add display name property to the user profile and preferences



commit 33b7d572701adaa3ce92c4bbbf6c5de768fbee8f
Author: Leon Handreke <leon handreke gmail com>
Date:   Thu Apr 8 20:54:46 2010 +0200

    Add display name property to the user profile and preferences

 accounts/forms.py                            |    5 +++++
 accounts/models.py                           |    1 +
 accounts/templates/accounts/preferences.html |   17 +++++++++++++++++
 accounts/views.py                            |   15 +++++++++++++--
 templates/base.html                          |    8 +++++++-
 5 files changed, 43 insertions(+), 3 deletions(-)
---
diff --git a/accounts/forms.py b/accounts/forms.py
index bf2cfbd..a088a27 100644
--- a/accounts/forms.py
+++ b/accounts/forms.py
@@ -75,6 +75,11 @@ class InternationalizationForm(forms.ModelForm):
         model = UserProfile
         fields = ('language',)
 
+class DisplayNameChangeForm(forms.ModelForm):
+    class Meta:
+        model = UserProfile
+        fields = ('display_name',)
+
 class EmailChangeForm(forms.ModelForm):
     """
     This code is adapted from
diff --git a/accounts/models.py b/accounts/models.py
index 259b105..6eeb141 100644
--- a/accounts/models.py
+++ b/accounts/models.py
@@ -33,6 +33,7 @@ class UserProfile(models.Model):
     language = models.CharField(max_length=5, choices=settings.LANGUAGES,
                                 verbose_name=_(u'Application Language'),
                                 null=True, blank=True)
+    display_name = models.CharField(_('display name'), max_length=80)
 
     def __unicode__(self):
         return str(self.user)
diff --git a/accounts/templates/accounts/preferences.html b/accounts/templates/accounts/preferences.html
index b88f2fe..0272c5a 100644
--- a/accounts/templates/accounts/preferences.html
+++ b/accounts/templates/accounts/preferences.html
@@ -36,6 +36,23 @@
     <input type="hidden" name="email_form" value="1" />
 </form>
 
+<h3>{% trans "Change your display name" %}</h3>
+<form method="POST">
+    <table class="input-form">
+    {{ display_name_form.as_table }}
+        <tfoot>
+            <tr>
+                <th></th>
+                <td>
+                    <input type="submit" value="{% trans "Save" %}"/>
+                </td>
+            </tr>
+        </tfoot>
+    </table>
+    <input type="hidden" name="display_name_form" value="1" />
+</form>
+
+
 <h3>{% trans "Internationalization" %}</h3>
 <form method="POST">
     <table class="input-form">
diff --git a/accounts/views.py b/accounts/views.py
index 7aed1b4..592d840 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -22,7 +22,8 @@ from django.http import HttpResponseRedirect
 from django.template import RequestContext
 from django.conf import settings
 
-from snowy.accounts.forms import InternationalizationForm, EmailChangeForm
+from snowy.accounts.forms import InternationalizationForm, EmailChangeForm, \
+    DisplayNameChangeForm
 
 @login_required
 def accounts_preferences(request, template_name='accounts/preferences.html'):
@@ -44,6 +45,15 @@ def accounts_preferences(request, template_name='accounts/preferences.html'):
     else:
         email_form = EmailChangeForm(instance=profile)
 
+    if 'display_name_form' in request.POST:
+        display_name_form = DisplayNameChangeForm(request.POST, instance=profile)
+        if display_name_form.is_valid():
+            print 'Display Name form is valid!'
+            display_name_form.save()
+    else:
+        display_name_form = DisplayNameChangeForm(instance=profile)
+
+
     if 'i18n_form' in request.POST:
         i18n_form = InternationalizationForm(request.POST, instance=profile)
         if i18n_form.is_valid():
@@ -55,5 +65,6 @@ def accounts_preferences(request, template_name='accounts/preferences.html'):
     return render_to_response(template_name,
                               {'user': user, 'i18n_form': i18n_form,
                                'password_form': password_form,
-                               'email_form' : email_form},
+                               'email_form' : email_form,
+                               'display_name_form' : display_name_form},
                               context_instance=RequestContext(request))
diff --git a/templates/base.html b/templates/base.html
index 0e48f95..455dd7d 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -30,7 +30,13 @@
                 </td>
                 <td id="header-auth">
 {% if user.is_authenticated %}
-                    <h3>{{ user }}</h3>
+                    <h3>
+                      {% if user.get_profile.display_name %}
+                      {{ user.get_profile.display_name }}
+                      {% else %}
+                      {{ user }}
+                      {% endif %}
+                    </h3>
                     <p><a href="{% url preferences %}">{% trans "preferences" %}</a> / <a href="{% url django.contrib.auth.views.logout %}">{% trans "log out" %}</a></p>
 {% else %}
                     <p>{% trans "hello stranger! care to " %}<a href="{% url openid_login %}">{% trans "log in" %}</a>{% trans "?" %}</p>



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