[extensions-web: 47/75] Add registration.



commit 836c0410094f6e1f3ebde7912d4511ecf66bada4
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Sep 20 15:33:34 2011 -0400

    Add registration.

 sweettooth/auth/forms.py                           |   10 +++++++-
 sweettooth/auth/templates/auth/login.html          |    2 +-
 .../auth/templates/auth/login_popup_form.html      |    1 +
 sweettooth/auth/templates/auth/register.html       |   13 +++++++++++
 sweettooth/auth/views.py                           |   23 +++++++++++++++++++-
 sweettooth/static/css/sweettooth.css               |   15 ++++++++----
 6 files changed, 56 insertions(+), 8 deletions(-)
---
diff --git a/sweettooth/auth/forms.py b/sweettooth/auth/forms.py
index d26d38e..99dec80 100644
--- a/sweettooth/auth/forms.py
+++ b/sweettooth/auth/forms.py
@@ -1,5 +1,5 @@
 
-from django.contrib.auth import forms
+from django.contrib.auth import forms, models
 
 class PlainOutputForm(object):
     def as_plain(self):
@@ -27,3 +27,11 @@ class InlineAuthenticationForm(PlainOutputForm, AutoFocusForm,
 
 class AuthenticationForm(AutoFocusForm, forms.AuthenticationForm):
     pass
+
+class UserCreationEmailForm(forms.UserCreationForm):
+    class Meta:
+        model = models.User
+        fields = 'username', 'email'
+
+class UserCreationForm(AutoFocusForm, UserCreationEmailForm):
+    pass
diff --git a/sweettooth/auth/templates/auth/login.html b/sweettooth/auth/templates/auth/login.html
index aa79567..92edb50 100644
--- a/sweettooth/auth/templates/auth/login.html
+++ b/sweettooth/auth/templates/auth/login.html
@@ -1,6 +1,6 @@
 {% extends "base.html" %}
 {% block body %}
-<form action="{% url auth-login %}" method="POST" id="login_form">
+<form action="{% url auth-login %}" method="POST" id="auth_form">
   <input type="hidden" name="next" value="{{ next }}">
   {% csrf_token %}
 
diff --git a/sweettooth/auth/templates/auth/login_popup_form.html b/sweettooth/auth/templates/auth/login_popup_form.html
index cc01adb..5851198 100644
--- a/sweettooth/auth/templates/auth/login_popup_form.html
+++ b/sweettooth/auth/templates/auth/login_popup_form.html
@@ -2,4 +2,5 @@
   {% csrf_token %}
   {{ login_popup_form.as_plain }}
   <input type="submit" value="Login">
+  <a href="{% url auth-register %}">Register</a>
 </form>
diff --git a/sweettooth/auth/templates/auth/register.html b/sweettooth/auth/templates/auth/register.html
new file mode 100644
index 0000000..a074183
--- /dev/null
+++ b/sweettooth/auth/templates/auth/register.html
@@ -0,0 +1,13 @@
+{% extends "base.html" %}
+{% block body %}
+<form action="" method="POST" id="auth_form">
+  {% csrf_token %}
+  <ol>
+    {{ form.as_ul }}
+
+    <li class="submit">
+      <input type="submit" value="Register">
+    </li>
+  </ol>
+</form>
+{% endblock %}
diff --git a/sweettooth/auth/views.py b/sweettooth/auth/views.py
index 451dda4..aff2012 100644
--- a/sweettooth/auth/views.py
+++ b/sweettooth/auth/views.py
@@ -1,9 +1,12 @@
 
 from django.contrib.auth import models
+from django.contrib.auth import login as auth_login, authenticate
 from django.contrib.auth.decorators import login_required
 from django.contrib.auth.views import login, logout
 from django.shortcuts import get_object_or_404, render, redirect
 
+from auth import forms
+
 from review.models import CodeReview
 from extensions.models import Extension
 
@@ -28,4 +31,22 @@ def profile_redirect(request):
     return redirect('auth-profile', user=request.user.username)
 
 def register(request):
-    return None
+    if request.method == 'POST':
+        form = forms.UserCreationForm(request.POST, request.FILES)
+        if form.is_valid():
+            user = form.save()
+
+            # We want to log the user in after this.
+            # The user object that was just returned isn't "authenticated",
+            # so a login will fail. The user model doesn't have the real
+            # password, but a hash of it, so grab the data from the form.
+            authed_user = authenticate(username=form.cleaned_data['username'],
+                                       password=form.cleaned_data['password1'])
+            auth_login(request, authed_user)
+
+            # Then bounce him to his profile afterwards.
+            return redirect('auth-profile', user=user.username)
+    else:
+        form = forms.UserCreationForm()
+
+    return render(request, 'auth/register.html', dict(form=form))
diff --git a/sweettooth/static/css/sweettooth.css b/sweettooth/static/css/sweettooth.css
index 0aee092..41f189b 100644
--- a/sweettooth/static/css/sweettooth.css
+++ b/sweettooth/static/css/sweettooth.css
@@ -70,17 +70,22 @@
     background: rgba(100%, 100%, 100%, 0.2);
 }
 
-#login_form ol {
+#auth_form ol {
     list-style-type: none;
-    font-size: 200%;
+    font-size: 2em;
+}
+
+#auth_form .helptext {
+    font-size: 0.5em;
+    display: block;
 }
 
-#login_form li.submit input {
+#auth_form li.submit input {
     width: 86.5%;
     margin: 0 5%;
 }
 
-#login_form label {
+#auth_form label {
     display: inline-block;
     display: -moz-inline-block;
     width: 45%;
@@ -89,7 +94,7 @@
     vertical-align: top;
 }
 
-#login_form input {
+#auth_form input {
     width: 40%;
 }
 



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