summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohan Lundberg <lundberg@nordu.net>2011-01-26 09:27:29 +0100
committerJohan Lundberg <lundberg@nordu.net>2011-01-26 09:27:29 +0100
commit0d9f0270b46146b9701a21dfc344ff91d0dba603 (patch)
treed6b6fc790ba53fc73fb1131e9dc5bcbe40387b84
Initial commit.
-rw-r--r--.gitignore2
-rw-r--r--__init__.py0
-rw-r--r--models.py37
-rw-r--r--templates/changepw/change_password.html17
-rw-r--r--tests.py23
-rw-r--r--urls.py6
-rw-r--r--views.py24
7 files changed, 109 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2f836aa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*~
+*.pyc
diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/__init__.py
diff --git a/models.py b/models.py
new file mode 100644
index 0000000..46a24e8
--- /dev/null
+++ b/models.py
@@ -0,0 +1,37 @@
+from django.db import models
+from django import forms
+import re
+
+class ChangePasswordForm(forms.Form):
+ new_password = forms.CharField(widget=forms.PasswordInput)
+ new_password_again = forms.CharField(widget=forms.PasswordInput)
+
+ def clean(self):
+ '''
+ Validate the password submitted.
+ '''
+ cleaned_data = self.cleaned_data
+ # The two submitted strings need to match.
+ new_password = cleaned_data.get('new_password')
+ new_password_again = cleaned_data.get('new_password_again')
+ if new_password != new_password_again:
+ raise forms.ValidationError('The typed passwords do not \
+match.')
+ # Check that the length is at least 8 characters.
+ if not len(new_password) >= 8:
+ raise forms.ValidationError('Your password needs to be at \
+least 8 characters long. Currently %d characters.' % len(new_password))
+ # The password needs to contain at least one number, one upper
+ # and one lower case letter and one special character.
+ if not re.search('\d+', new_password):
+ raise forms.ValidationError('You need at least one number \
+in your password.')
+ if not re.search('[a-z]', new_password) or not re.search(
+ '[A-Z]', new_password):
+ raise forms.ValidationError('You need at least one upper \
+case letter and one lower case letter in your password.')
+ if not re.search('[,.\[\]!@#$%^&*?_\(\)-]', new_password):
+ raise forms.ValidationError('You need at least one special \
+character i.e. ,.[]!@#$%^&*?_()-')
+
+ return cleaned_data
diff --git a/templates/changepw/change_password.html b/templates/changepw/change_password.html
new file mode 100644
index 0000000..e8af7bd
--- /dev/null
+++ b/templates/changepw/change_password.html
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+
+{% block content %}
+{% if form %}
+ <form action="/changepw/" method="post">{% csrf_token %}
+ {{ form.as_p}}
+ <input type="submit" value="Submit" />
+ </form>
+{% else %}
+ {% if return_value == 0 %}
+ <p>Your password was changed successfully.</p>
+ {% else %}
+ <p>Something went wrong. Please contact an administrator.</p>
+ <p>Return code: {{ return_value }}</p>
+ {% endif %}
+{% endif %}
+{% endblock %}
diff --git a/tests.py b/tests.py
new file mode 100644
index 0000000..2247054
--- /dev/null
+++ b/tests.py
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/urls.py b/urls.py
new file mode 100644
index 0000000..eb64d93
--- /dev/null
+++ b/urls.py
@@ -0,0 +1,6 @@
+# This also imports the include function
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('changepw.views',
+ (r'^$', 'change_password'),
+)
diff --git a/views.py b/views.py
new file mode 100644
index 0000000..dd8f1d8
--- /dev/null
+++ b/views.py
@@ -0,0 +1,24 @@
+from changepw.models import ChangePasswordForm
+from django import forms
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.http import HttpResponseRedirect
+import subprocess
+
+def change_password(request):
+ if request.method == 'POST':
+ form = ChangePasswordForm(request.POST)
+ if form.is_valid():
+ new_password = form.cleaned_data['new_password']
+
+ # Magic for actually changing the password happens here
+ return_value = subprocess.check_call(['echo', new_password])
+ return render_to_response('changepw/change_password.html',
+ {'return_value': return_value},
+ context_instance=RequestContext(request))
+ else:
+ form = ChangePasswordForm()
+
+ return render_to_response('changepw/change_password.html',
+ {'form': form },
+ context_instance=RequestContext(request))