[snowy: 5/26] Allow the user to change his Email address in the preferences



commit 2ddcd991bec73a820e0a40ada0c1b1b9cf6e6814
Author: Leon Handreke <leon handreke gmail com>
Date:   Thu Apr 8 18:02:59 2010 +0200

    Allow the user to change his Email address in the preferences

 accounts/forms.py                            |   23 +++++++++++++++++++++++
 accounts/templates/accounts/preferences.html |   16 ++++++++++++++++
 accounts/views.py                            |   13 +++++++++++--
 3 files changed, 50 insertions(+), 2 deletions(-)
---
diff --git a/accounts/forms.py b/accounts/forms.py
index e662620..bf2cfbd 100644
--- a/accounts/forms.py
+++ b/accounts/forms.py
@@ -74,3 +74,26 @@ class InternationalizationForm(forms.ModelForm):
     class Meta:
         model = UserProfile
         fields = ('language',)
+
+class EmailChangeForm(forms.ModelForm):
+    """
+    This code is adapted from
+    http://stackoverflow.com/questions/1075314/allow-changing-of-user-fields-like-email-with-django-profiles
+    """
+    def __init__(self, *args, **kwargs):
+        super(EmailChangeForm, self).__init__(*args, **kwargs)
+        try:
+            self.fields['email'].initial = self.instance.user.email
+        except User.DoesNotExist:
+            pass
+
+    email = forms.EmailField(label="Email address")
+
+    def save(self, *args, **kwargs):
+        """
+        Update the email address on the user object
+        """
+        u = self.instance.user
+        u.email = self.cleaned_data['email']
+        u.save()
+        return u.email
diff --git a/accounts/templates/accounts/preferences.html b/accounts/templates/accounts/preferences.html
index cc28b38..b88f2fe 100644
--- a/accounts/templates/accounts/preferences.html
+++ b/accounts/templates/accounts/preferences.html
@@ -20,6 +20,22 @@
     <input type="hidden" name="password_form" value="1" />
 </form>
 
+<h3>{% trans "Change your Email address" %}</h3>
+<form method="POST">
+    <table class="input-form">
+    {{ email_form.as_table }}
+        <tfoot>
+            <tr>
+                <th></th>
+                <td>
+                    <input type="submit" value="{% trans "Save" %}"/>
+                </td>
+            </tr>
+        </tfoot>
+    </table>
+    <input type="hidden" name="email_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 e4fedd4..7aed1b4 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -22,7 +22,7 @@ from django.http import HttpResponseRedirect
 from django.template import RequestContext
 from django.conf import settings
 
-from snowy.accounts.forms import InternationalizationForm
+from snowy.accounts.forms import InternationalizationForm, EmailChangeForm
 
 @login_required
 def accounts_preferences(request, template_name='accounts/preferences.html'):
@@ -36,6 +36,14 @@ def accounts_preferences(request, template_name='accounts/preferences.html'):
     else:
         password_form = PasswordChangeForm(user)
 
+    if 'email_form' in request.POST:
+        email_form = EmailChangeForm(request.POST, instance=profile)
+        if email_form.is_valid():
+            print 'Email form is valid!'
+            email_form.save()
+    else:
+        email_form = EmailChangeForm(instance=profile)
+
     if 'i18n_form' in request.POST:
         i18n_form = InternationalizationForm(request.POST, instance=profile)
         if i18n_form.is_valid():
@@ -46,5 +54,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},
+                               'password_form': password_form,
+                               'email_form' : email_form},
                               context_instance=RequestContext(request))



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