Commits

Chris Grubbs committed 277a68a

adding/testing account creation view

Comments (0)

Files changed (7)

phlombay/recipes/tests.py

         self.assertTemplateUsed(response, 'recipes/detail.html')
         self.assertContains(response, 'Prosciutto & Melon Bites')
 
-    def test_login(self):
+    def test_login_get(self):
         response = self.client.get('/recipes/login/')
         self.assertTemplateUsed(response, 'recipes/login.html')
         
         self.assertTemplateUsed(response, 'recipes/login.html')
         self.assertContains(response, 'Please enter a correct username and password. Note that both fields are case-sensitive.')
 
+    def test_create_account_get(self):
+        response = self.client.get('/recipes/create-account/')
+        self.assertTemplateUsed(response, 'recipes/create_account.html')
+
+    def test_create_account_successful(self):
+        response = self.client.post('/recipes/create-account/', {'username': 'juliachild',
+                                                                 'password1': 'souffle',
+                                                                 'password2': 'souffle'}, follow=True)
+        self.assertRedirects(response, '/recipes/my-page/')
+        self.assertContains(response, "juliachild's Page")
+
+    def test_create_account_unsuccessful(self):
+        response = self.client.post('/recipes/create-account/', {})
+        self.assertFormError(response, 'user_creation_form', 'username', u'This field is required.')
+        self.assertFormError(response, 'user_creation_form', 'password1', u'This field is required.')
+        self.assertFormError(response, 'user_creation_form', 'password2', u'This field is required.')
+
+        response = self.client.post('/recipes/create-account/', {'username': 'user1',
+                                                                 'password1': 'souffle',
+                                                                 'password2': 'souffle'})
+        self.assertFormError(response, 'user_creation_form', 'username', u'A user with that username already exists.')
+
+        response = self.client.post('/recipes/create-account/', {'username': 'juliachild',
+                                                                 'password1': 'souffle',
+                                                                 'password2': 'not_souffle'})
+        self.assertFormError(response, 'user_creation_form', 'password2', u"The two password fields didn't match.")
+
     def test_my_page(self):
         self.client.login(username='user2', password='secret')
         response = self.client.get('/recipes/my-page/')

phlombay/recipes/urls.py

     # Authentication views
     (r'^login/$', 'phlombay_login'),
     (r'^logout/$', 'phlombay_logout'),
+    (r'^create-account/$', 'create_account'),
 
     # Logged-in views
     url(r'^my-page/$', 'my_page', name='my_page'),

phlombay/recipes/views.py

 from django.core.urlresolvers import reverse
-from django.contrib.auth import login, logout
+from django.contrib.auth import login, logout, authenticate
 from django.contrib.auth.decorators import login_required
-from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
 from django import forms
-from django.forms.models import inlineformset_factory
 from django.http import HttpResponse, HttpResponseRedirect, Http404
-from django.template import RequestContext
-from django.shortcuts import get_object_or_404, render_to_response, render
+from django.shortcuts import render
 
 from phlombay.recipes.models import Recipe
 from phlombay.recipes.widgets import IngredientsWidget, StepsWidget
     return HttpResponseRedirect(reverse('homepage'))
 
 
+def create_account(request):
+    if request.method == 'GET':
+        user_creation_form = UserCreationForm()
+    elif request.method == 'POST':
+        user_creation_form = UserCreationForm(data=request.POST)
+        if user_creation_form.is_valid():
+            user_creation_form.save()
+            user = authenticate(username=request.POST['username'], password=request.POST['password1'])
+            login(request, user)
+            return HttpResponseRedirect(reverse('my_page'))
+
+    return render(request, 'recipes/create_account.html', {'nav_section': 'create_account', 'user_creation_form': user_creation_form})
+
+
 @login_required
 def my_page(request):
     my_recipes = Recipe.objects.all().filter(creator=request.user)

phlombay/templates/base.html

 					    	<li{% if nav_section == 'my_page' %} class="active"{% endif %}><a href="{% url recipes.views.my_page %}">My Page</a></li>
 					    	<li><a href="{% url recipes.views.phlombay_logout %}">Logout</a></li>
 					    {% else %}
+					    	<li{% if nav_section == 'create_account' %} class="active"{% endif %}><a href="{% url recipes.views.create_account %}">Create Account</a></li>
 					    	<li{% if nav_section == 'login' %} class="active"{% endif %}><a href="{% url recipes.views.phlombay_login %}">Login</a></li>
 					    {% endif %}
 				    </ul>

phlombay/templates/recipes/create_account.html

+{% extends "base.html" %}
+
+{% block title %}Create New Account | phlombay.com{% endblock %}
+
+{% block content %}
+	<form class="form-horizontal" method="post">
+		<legend>Create New Account</legend>
+		<div class="control-group{% if user_creation_form.username.errors %} error{% endif %}">
+			<label class="control-label" for="id_username">Username:</label>
+			<div class="controls">
+        		{{ user_creation_form.username }}
+        		{% if user_creation_form.username.errors %}
+	        		{% for error in user_creation_form.username.errors %}
+	        		<span class="help-inline">{{ error }}</span>
+	        		{% endfor %}
+        		{% else %}
+        			<span class="help-inline"><small>Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.</small></span>
+        		{% endif %}
+        	</div>
+		</div>
+		<div class="control-group{% if user_creation_form.password1.errors %} error{% endif %}">
+			<label class="control-label" for="id_password1">Password:</label>
+			<div class="controls">
+        		{{ user_creation_form.password1 }}
+        		{% for error in user_creation_form.password1.errors %}
+        		<span class="help-inline">{{ error }}</span>
+        		{% endfor %}
+        	</div>
+		</div>
+		<div class="control-group{% if user_creation_form.password2.errors %} error{% endif %}">
+			<label class="control-label" for="id_password2">Password confirmation:</label>
+			<div class="controls">
+        		{{ user_creation_form.password2 }}
+        		{% if user_creation_form.password2.errors %}
+	        		{% for error in user_creation_form.password2.errors %}
+	        		<span class="help-inline">{{ error }}</span>
+	        		{% endfor %}
+        		{% else %}
+        			<span class="help-inline"><small>Enter the same password as above, for verification.</small></span>
+        		{% endif %}
+        	</div>
+		</div>
+		<div class="control-group">
+			<div class="controls">
+				<button type="submit" class="btn">Submit</button>
+			</div>
+		</div>
+		{% csrf_token %}
+	</form>
+{% endblock %}

phlombay/templates/register.html

-{% extends "base.html" %}
-
-{% block title %}register with phlombay{% endblock %}
-
-{% block content %}
-<h2>Register with phlombay</h2>
-<form method="post" action=".">
-  <table>{{form}}</table>
-  <input type="submit" value="Register"/>
-</form>
-{% endblock %}

phlombay/templates/register_created.html

-{% extends "base.html" %}
-
-{% block content %}
-      <h1>Congratulations!</h1>
-      You registered with phombay.com!
-{% endblock %}