[snowy: 7/26] Add initial preferences form for new OpenID users



commit 909fa53ac704b84539fd08bb51f4de5620d6ee39
Author: Leon Handreke <leon handreke gmail com>
Date:   Sat Apr 10 19:26:11 2010 +0200

    Add initial preferences form for new OpenID users

 accounts/forms.py                                  |   16 ++++++++++-
 .../templates/accounts/initial_preferences.html    |   23 ++++++++++++++++
 accounts/urls.py                                   |    3 ++
 accounts/views.py                                  |   28 +++++++++++++++++++-
 4 files changed, 68 insertions(+), 2 deletions(-)
---
diff --git a/accounts/forms.py b/accounts/forms.py
index a088a27..4e30635 100644
--- a/accounts/forms.py
+++ b/accounts/forms.py
@@ -73,7 +73,7 @@ from snowy.accounts.models import UserProfile
 class InternationalizationForm(forms.ModelForm):
     class Meta:
         model = UserProfile
-        fields = ('language',)
+        fields = ('language', )
 
 class DisplayNameChangeForm(forms.ModelForm):
     class Meta:
@@ -102,3 +102,17 @@ class EmailChangeForm(forms.ModelForm):
         u.email = self.cleaned_data['email']
         u.save()
         return u.email
+
+class InitialPreferencesForm(forms.ModelForm):
+
+    def __init__(self, *args, **kwargs):
+        super(InitialPreferencesForm, self).__init__(*args, **kwargs)
+        try:
+            self.fields['email'].initial = self.instance.user.email
+            self.fields['display_name'].initial = self.instance.display_name
+        except User.DoesNotExist:
+            pass
+
+    email = forms.EmailField(label="Email address")
+    display_name = forms.CharField(max_length=80, label="Display Name",
+                                   help_text="This name will be shown to other users when sharing notes")
diff --git a/accounts/templates/accounts/initial_preferences.html b/accounts/templates/accounts/initial_preferences.html
new file mode 100644
index 0000000..bbb5f3b
--- /dev/null
+++ b/accounts/templates/accounts/initial_preferences.html
@@ -0,0 +1,23 @@
+{% extends "site_base.html" %}
+
+{% load i18n %}
+
+{% block content %}
+<h1>Tell {{ site.name }} about yourself!</h1>
+
+<form method="POST">
+    <table class="input-form">
+    {{ initial_preferences_form.as_table }}
+        <tfoot>
+            <tr>
+                <th></th>
+                <td>
+                    <input type="submit" value="{% trans "Save" %}"/>
+                </td>
+            </tr>
+        </tfoot>
+    </table>
+    <input type="hidden" name="initial_preferences_form" value="1" />
+</form>
+
+{% endblock %}
diff --git a/accounts/urls.py b/accounts/urls.py
index 3c0a06b..f2f1c87 100644
--- a/accounts/urls.py
+++ b/accounts/urls.py
@@ -30,6 +30,9 @@ urlpatterns = patterns('',
     url(r'^preferences/$', 'snowy.accounts.views.accounts_preferences',
         name='preferences'),
 
+    url(r'^initial_preferences/$', 'snowy.accounts.views.initial_preferences',
+        name='initial_preferences'),
+
     url(r'^logout/$', auth_views.logout, {'template_name': 'registration/logout.html'},
         name='auth_logout'),
 
diff --git a/accounts/views.py b/accounts/views.py
index 592d840..dadfaa2 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -23,7 +23,33 @@ from django.template import RequestContext
 from django.conf import settings
 
 from snowy.accounts.forms import InternationalizationForm, EmailChangeForm, \
-    DisplayNameChangeForm
+    DisplayNameChangeForm, InitialPreferencesForm
+
+ login_required
+def initial_preferences(request, template_name='accounts/initial_preferences.html'):
+    user = request.user
+    profile = user.get_profile()
+
+    if 'initial_preferences_form' in request.POST:
+        email_form = EmailChangeForm(request.POST, instance=profile)
+        if email_form.is_valid():
+            print 'Email form is valid!'
+            email_form.save()
+
+        display_name_form = DisplayNameChangeForm(request.POST, instance=profile)
+        if display_name_form.is_valid():
+            print 'Display Name form is valid!'
+            display_name_form.save()
+
+        if email_form.is_valid() and display_name_form.is_valid():
+            return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
+
+    initial_preferences_form = InitialPreferencesForm(instance=profile)
+
+    return render_to_response(template_name,
+                              {'user': user,
+                               'initial_preferences_form' : initial_preferences_form},
+                              context_instance=RequestContext(request))
 
 @login_required
 def accounts_preferences(request, template_name='accounts/preferences.html'):



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