Add login/logout functionality

This commit is contained in:
Gergely Polonkai 2014-12-17 13:30:58 +01:00 committed by Gergely Polonkai
parent 5e23c73594
commit 4d9f4242e3
7 changed files with 61 additions and 3 deletions

0
accounts/__init__.py Normal file
View File

View File

@ -0,0 +1,17 @@
{% extends 'front_template.html' %}
{% block body %}
{% if form.errors %}
<div class="error">Username/password mismatch. Please try again!</div>
{% endif %}
<form method="post" action="{% url 'accounts:login' %}">
{% csrf_token %}
{{ form.username.label_tag }} {{ form.username }}<br>
{{ form.password.label_tag }} {{ form.password }}<br>
<button type="submit">Login</button>
<input type="hidden" name="next" value="{% url 'booking:list' %}">
</form>
{% endblock %}

View File

@ -7,6 +7,29 @@ class FrontTest(TestCase):
def setUp(self): def setUp(self):
self.client = Client() self.client = Client()
self.admin = User.objects.create_user(
username = 'admin',
password = 'password')
self.admin.save()
def test_login_page(self):
response = self.client.get('/accounts/login')
self.assertEqual(response.status_code, 200)
def test_login(self):
response = self.client.post('/accounts/login', {
'next': '/',
'username': 'admin',
'password': 'password'})
self.assertRedirects(response, '/')
def test_logout(self):
self.client.login(username = 'admin', password = 'aeou')
response = self.client.get('/accounts/logout')
self.assertRedirects(response, '/')
def test_registration_page(self): def test_registration_page(self):
response = self.client.get('/accounts/register') response = self.client.get('/accounts/register')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)

View File

@ -9,4 +9,16 @@ urlpatterns = patterns(
RegistrationFormView.as_view(), RegistrationFormView.as_view(),
name = 'register' name = 'register'
), ),
url(
r'^login$',
'django.contrib.auth.views.login',
{'template_name': 'accounts/login.html'},
name = 'login'
),
url(
r'^logout$',
'django.contrib.auth.views.logout',
{'next_page': 'booking:list'},
name = 'logout'
),
) )

View File

@ -18,6 +18,6 @@ class RegistrationFormView(generic.View):
if form.is_valid(): if form.is_valid():
form.save() form.save()
return HttpResponseRedirect(reverse('booking:index')) return HttpResponseRedirect(reverse('booking:list'))
return render(request, self.template_name, {'form': form}) return render(request, self.template_name, {'form': form})

View File

@ -5,9 +5,16 @@
</head> </head>
<body> <body>
<h1>Rubber Duck Booking Tool</h1> <h1>Rubber Duck Booking Tool</h1>
{% if user.is_authenticated %}
<div> <div>
<a href="{% url 'accounts:register' %}">Register</a> Logged in as {{ user }}
<a href="{% url 'accounts:logout' %}">Logout</a>
</div> </div>
{% else %}
<div>
<a href="{% url 'accounts:login' %}">Login</a> or <a href="{% url 'accounts:register' %}">Register</a>
</div>
{% endif %}
<a href="{% url 'booking:list' %}">Home</a> <a href="{% url 'booking:list' %}">Home</a>
<a href="{% url 'booking:terms' %}">Terms and Conditions</a> <a href="{% url 'booking:terms' %}">Terms and Conditions</a>
<a href="{% url 'booking:vocabulary' %}">Vocabulary</a> <a href="{% url 'booking:vocabulary' %}">Vocabulary</a>

View File

@ -1,5 +1,4 @@
from django.conf.urls import patterns, include, url from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns( urlpatterns = patterns(
'', '',