diff options
-rw-r--r-- | recover/__init__.py | 0 | ||||
-rw-r--r-- | recover/forms.py | 5 | ||||
-rw-r--r-- | recover/models.py | 6 | ||||
-rw-r--r-- | recover/tests.py | 16 | ||||
-rw-r--r-- | recover/urls.py | 5 | ||||
-rw-r--r-- | recover/views.py | 61 | ||||
-rw-r--r-- | settings/10-base.conf | 1 | ||||
-rw-r--r-- | templates/signup.html | 2 | ||||
-rw-r--r-- | urls.py | 1 |
9 files changed, 96 insertions, 1 deletions
diff --git a/recover/__init__.py b/recover/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/recover/__init__.py diff --git a/recover/forms.py b/recover/forms.py new file mode 100644 index 0000000..dec1842 --- /dev/null +++ b/recover/forms.py @@ -0,0 +1,5 @@ +from django import forms + +class RecoverForm(forms.Form): + email = forms.EmailField(max_length = 254, label = 'Email: ') + username = forms.CharField(max_length = 100, label = 'Username:')
\ No newline at end of file diff --git a/recover/models.py b/recover/models.py new file mode 100644 index 0000000..3a5c563 --- /dev/null +++ b/recover/models.py @@ -0,0 +1,6 @@ +from django.db import models + +class RecoverPassword(models.Model): + email = models.EmailField() + user = models.CharField(max_length = 100) + url = models.CharField(max_length = 30, blank = True, null = True)
\ No newline at end of file diff --git a/recover/tests.py b/recover/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/recover/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this 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.assertEqual(1 + 1, 2) diff --git a/recover/urls.py b/recover/urls.py new file mode 100644 index 0000000..8368084 --- /dev/null +++ b/recover/urls.py @@ -0,0 +1,5 @@ +from django.conf.urls.defaults import patterns + +urlpatterns = patterns('okupy.recover.views', + (r'^$', 'recover'), +) diff --git a/recover/views.py b/recover/views.py new file mode 100644 index 0000000..b1403f6 --- /dev/null +++ b/recover/views.py @@ -0,0 +1,61 @@ +from django.shortcuts import render_to_response +from django.template import RequestContext +from okupy.libraries.exception import OkupyException, log_extra_data +from okupy.libraries.ldap_wrappers import ldap_user_search +from okupy.libraries.verification import sendConfirmationEmail +from okupy.recover.forms import RecoverForm +from okupy.recover.models import RecoverPassword +import logging + +logger = logging.getLogger('okupy') + +def checkUserEmail(username, email): + ''' + Check if the username exists + ''' + user = ldap_user_search(username) + if not user: + return False + ''' + Check if the email belongs to the above username + ''' + if email not in user[0][1]['mail']: + return False + ''' + Check if the user has already requested for a + password reset + ''' + # TODO + # What to do here? Options: + # 1) Remove previous entry from the DB + # 2) Print error + return user + +def recover(request): + ''' + Recover password. User fills in username and email in a simple form, + and he gets a temporary URL where he can update the password + ''' + msg = '' + form = '' + email = '' + if request.method == 'POST': + form = RecoverForm(request.POST) + if form.is_valid(): + username = form.cleaned_data['username'] + email = form.cleaned_data['email'] + try: + user = checkUserEmail(username, email) + if not user: + raise OkupyException('User not found') + else: + user = user[0][1] + sendConfirmationEmail(request, form, RecoverPassword) + except OkupyException as error: + msg = error.value + logger.error(msg, extra = log_extra_data(request, form)) + else: + form = RecoverForm() + return render_to_response('recover/recover.html', + {'msg': msg, 'form': form, 'email': email}, + context_instance = RequestContext(request)) diff --git a/settings/10-base.conf b/settings/10-base.conf index c97e6a8..5294955 100644 --- a/settings/10-base.conf +++ b/settings/10-base.conf @@ -68,6 +68,7 @@ INSTALLED_APPS = ( # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'okupy.accounts', + 'okupy.recover', 'okupy.verification', ) diff --git a/templates/signup.html b/templates/signup.html index 5795346..22a6090 100644 --- a/templates/signup.html +++ b/templates/signup.html @@ -1,4 +1,4 @@ -{% if username %} +{% if email %} You will soon recieve a mail at {{ email }} to verify your account {% else %} <form action="." method="post">{% csrf_token %} @@ -11,6 +11,7 @@ urlpatterns = patterns('', (r'^admin/', include(admin.site.urls)), (r'^login/$', mylogin), (r'^logout/$', mylogout), + (r'^recover/$', include('recover.urls')), (r'^signup/', include('signup.urls')), (r'^verification/', include('verification.urls')), ) |