diff options
24 files changed, 293 insertions, 78 deletions
diff --git a/apps/patchwork/forms.py b/apps/patchwork/forms.py index 1ff2bd0..f83c27a 100644 --- a/apps/patchwork/forms.py +++ b/apps/patchwork/forms.py @@ -22,34 +22,33 @@ from django.contrib.auth.models import User from django import forms from patchwork.models import Patch, State, Bundle, UserProfile -from registration.forms import RegistrationFormUniqueEmail -from registration.models import RegistrationProfile -class RegistrationForm(RegistrationFormUniqueEmail): +class RegistrationForm(forms.Form): first_name = forms.CharField(max_length = 30, required = False) last_name = forms.CharField(max_length = 30, required = False) - username = forms.CharField(max_length=30, label=u'Username') + username = forms.RegexField(regex = r'^\w+$', max_length=30, + label=u'Username') email = forms.EmailField(max_length=100, label=u'Email address') password = forms.CharField(widget=forms.PasswordInput(), label='Password') - password1 = forms.BooleanField(required = False) - password2 = forms.BooleanField(required = False) - - def save(self, profile_callback = None): - user = RegistrationProfile.objects.create_inactive_user( \ - username = self.cleaned_data['username'], - password = self.cleaned_data['password'], - email = self.cleaned_data['email'], - profile_callback = profile_callback) - user.first_name = self.cleaned_data.get('first_name', '') - user.last_name = self.cleaned_data.get('last_name', '') - user.save() - - # saving the userprofile causes the firstname/lastname to propagate - # to the person objects. - user.get_profile().save() - - return user + + def clean_username(self): + value = self.cleaned_data['username'] + try: + user = User.objects.get(username__iexact = value) + except User.DoesNotExist: + return self.cleaned_data['username'] + raise forms.ValidationError('This username is already taken. ' + \ + 'Please choose another.') + + def clean_email(self): + value = self.cleaned_data['email'] + try: + user = User.objects.get(email__iexact = value) + except User.DoesNotExist: + return self.cleaned_data['email'] + raise forms.ValidationError('This email address is already in use ' + \ + 'for the account "%s".\n' % user.username) def clean(self): return self.cleaned_data diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index ee6748f..806875b 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -21,6 +21,7 @@ from django.db import models from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.contrib.sites.models import Site +from django.conf import settings from patchwork.parser import hash_patch import re @@ -374,9 +375,10 @@ class BundlePatch(models.Model): ordering = ['order'] class EmailConfirmation(models.Model): - validity = datetime.timedelta(days = 30) + validity = datetime.timedelta(days = settings.CONFIRMATION_VALIDITY_DAYS) type = models.CharField(max_length = 20, choices = [ ('userperson', 'User-Person association'), + ('registration', 'Registration'), ]) email = models.CharField(max_length = 200) user = models.ForeignKey(User, null = True) diff --git a/apps/patchwork/tests/__init__.py b/apps/patchwork/tests/__init__.py index 9618d1f..db096d8 100644 --- a/apps/patchwork/tests/__init__.py +++ b/apps/patchwork/tests/__init__.py @@ -24,3 +24,5 @@ from patchwork.tests.mboxviews import * from patchwork.tests.updates import * from patchwork.tests.filters import * from patchwork.tests.confirm import * +from patchwork.tests.registration import * +from patchwork.tests.user import * diff --git a/apps/patchwork/tests/registration.py b/apps/patchwork/tests/registration.py new file mode 100644 index 0000000..18b781f --- /dev/null +++ b/apps/patchwork/tests/registration.py @@ -0,0 +1,150 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2010 Jeremy Kerr <jk@ozlabs.org> +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest +from django.test import TestCase +from django.test.client import Client +from django.core import mail +from django.core.urlresolvers import reverse +from django.contrib.auth.models import User +from patchwork.models import EmailConfirmation, Person +from patchwork.tests.utils import create_user + +def _confirmation_url(conf): + return reverse('patchwork.views.confirm', kwargs = {'key': conf.key}) + +class TestUser(object): + firstname = 'Test' + lastname = 'User' + username = 'testuser' + email = 'test@example.com' + password = 'foobar' + +class RegistrationTest(TestCase): + def setUp(self): + self.user = TestUser() + self.client = Client() + self.default_data = {'username': self.user.username, + 'first_name': self.user.firstname, + 'last_name': self.user.lastname, + 'email': self.user.email, + 'password': self.user.password} + self.required_error = 'This field is required.' + self.invalid_error = 'Enter a valid value.' + + def testRegistrationForm(self): + response = self.client.get('/register/') + self.assertEquals(response.status_code, 200) + self.assertTemplateUsed(response, 'patchwork/registration_form.html') + + def testBlankFields(self): + for field in ['username', 'email', 'password']: + data = self.default_data.copy() + del data[field] + response = self.client.post('/register/', data) + self.assertEquals(response.status_code, 200) + self.assertFormError(response, 'form', field, self.required_error) + + def testInvalidUsername(self): + data = self.default_data.copy() + data['username'] = 'invalid user' + response = self.client.post('/register/', data) + self.assertEquals(response.status_code, 200) + self.assertFormError(response, 'form', 'username', self.invalid_error) + + def testExistingUsername(self): + user = create_user() + data = self.default_data.copy() + data['username'] = user.username + response = self.client.post('/register/', data) + self.assertEquals(response.status_code, 200) + self.assertFormError(response, 'form', 'username', + 'This username is already taken. Please choose another.') + + def testExistingEmail(self): + user = create_user() + data = self.default_data.copy() + data['email'] = user.email + response = self.client.post('/register/', data) + self.assertEquals(response.status_code, 200) + self.assertFormError(response, 'form', 'email', + 'This email address is already in use ' + \ + 'for the account "%s".\n' % user.username) + + def testValidRegistration(self): + response = self.client.post('/register/', self.default_data) + self.assertEquals(response.status_code, 200) + self.assertContains(response, 'confirmation email has been sent') + + # check for presence of an inactive user object + users = User.objects.filter(username = self.user.username) + self.assertEquals(users.count(), 1) + user = users[0] + self.assertEquals(user.username, self.user.username) + self.assertEquals(user.email, self.user.email) + self.assertEquals(user.is_active, False) + + # check for confirmation object + confs = EmailConfirmation.objects.filter(user = user, + type = 'registration') + self.assertEquals(len(confs), 1) + conf = confs[0] + self.assertEquals(conf.email, self.user.email) + + # check for a sent mail + self.assertEquals(len(mail.outbox), 1) + msg = mail.outbox[0] + self.assertEquals(msg.subject, 'Patchwork account confirmation') + self.assertTrue(self.user.email in msg.to) + self.assertTrue(_confirmation_url(conf) in msg.body) + + # ...and that the URL is valid + response = self.client.get(_confirmation_url(conf)) + self.assertEquals(response.status_code, 200) + +class RegistrationConfirmationTest(TestCase): + + def setUp(self): + self.user = TestUser() + self.default_data = {'username': self.user.username, + 'first_name': self.user.firstname, + 'last_name': self.user.lastname, + 'email': self.user.email, + 'password': self.user.password} + + def testRegistrationConfirmation(self): + self.assertEqual(EmailConfirmation.objects.count(), 0) + response = self.client.post('/register/', self.default_data) + self.assertEquals(response.status_code, 200) + self.assertContains(response, 'confirmation email has been sent') + + self.assertEqual(EmailConfirmation.objects.count(), 1) + conf = EmailConfirmation.objects.filter()[0] + self.assertFalse(conf.user.is_active) + self.assertTrue(conf.active) + + response = self.client.get(_confirmation_url(conf)) + self.assertEquals(response.status_code, 200) + self.assertTemplateUsed(response, 'patchwork/registration-confirm.html') + + conf = EmailConfirmation.objects.get(pk = conf.pk) + self.assertTrue(conf.user.is_active) + self.assertFalse(conf.active) + + diff --git a/apps/patchwork/tests/user.py b/apps/patchwork/tests/user.py index c9e5be3..e96e6c5 100644 --- a/apps/patchwork/tests/user.py +++ b/apps/patchwork/tests/user.py @@ -22,9 +22,9 @@ from django.test import TestCase from django.test.client import Client from django.core import mail from django.core.urlresolvers import reverse +from django.conf import settings from django.contrib.auth.models import User from patchwork.models import EmailConfirmation, Person -from patchwork.utils import userprofile_register_callback def _confirmation_url(conf): return reverse('patchwork.views.confirm', kwargs = {'key': conf.key}) @@ -39,7 +39,6 @@ class TestUser(object): self.password = User.objects.make_random_password() self.user = User.objects.create_user(self.username, self.email, self.password) - userprofile_register_callback(self.user) class UserPersonRequestTest(TestCase): def setUp(self): @@ -119,3 +118,11 @@ class UserPersonConfirmTest(TestCase): # need to reload the confirmation to check this. conf = EmailConfirmation.objects.get(pk = self.conf.pk) self.assertEquals(conf.active, False) + +class UserLoginRedirectTest(TestCase): + + def testUserLoginRedirect(self): + url = '/user/' + response = self.client.get(url) + self.assertRedirects(response, settings.LOGIN_URL + '?next=' + url) + diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py index f1c95e8..1cb5dfb 100644 --- a/apps/patchwork/tests/utils.py +++ b/apps/patchwork/tests/utils.py @@ -59,7 +59,7 @@ class defaults(object): _user_idx = 1 def create_user(): global _user_idx - userid = 'test-%d' % _user_idx + userid = 'test%d' % _user_idx email = '%s@example.com' % userid _user_idx += 1 diff --git a/apps/patchwork/urls.py b/apps/patchwork/urls.py index 27c79fd..6810e3e 100644 --- a/apps/patchwork/urls.py +++ b/apps/patchwork/urls.py @@ -19,6 +19,7 @@ from django.conf.urls.defaults import * from django.conf import settings +from django.contrib.auth import views as auth_views urlpatterns = patterns('', # Example: @@ -46,6 +47,23 @@ urlpatterns = patterns('', (r'^user/link/$', 'patchwork.views.user.link'), (r'^user/unlink/(?P<person_id>[^/]+)/$', 'patchwork.views.user.unlink'), + # password change + url(r'^user/password-change/$', auth_views.password_change, + name='auth_password_change'), + url(r'^user/password-change/done/$', auth_views.password_change_done, + name='auth_password_change_done'), + + # login/logout + url(r'^user/login/$', auth_views.login, + {'template_name': 'patchwork/login.html'}, + name = 'auth_login'), + url(r'^user/logout/$', auth_views.logout, + {'template_name': 'patchwork/logout.html'}, + name = 'auth_logout'), + + # registration + (r'^register/', 'patchwork.views.user.register'), + # public view for bundles (r'^bundle/(?P<username>[^/]*)/(?P<bundlename>[^/]*)/$', 'patchwork.views.bundle.public'), diff --git a/apps/patchwork/views/base.py b/apps/patchwork/views/base.py index 1539472..590a3b6 100644 --- a/apps/patchwork/views/base.py +++ b/apps/patchwork/views/base.py @@ -62,6 +62,7 @@ def confirm(request, key): import patchwork.views.user views = { 'userperson': patchwork.views.user.link_confirm, + 'registration': patchwork.views.user.register_confirm, } conf = get_object_or_404(EmailConfirmation, key = key) diff --git a/apps/patchwork/views/user.py b/apps/patchwork/views/user.py index 759a6e3..3d28f4b 100644 --- a/apps/patchwork/views/user.py +++ b/apps/patchwork/views/user.py @@ -21,9 +21,12 @@ from django.contrib.auth.decorators import login_required from patchwork.requestcontext import PatchworkRequestContext from django.shortcuts import render_to_response, get_object_or_404 +from django.contrib import auth +from django.contrib.sites.models import Site from django.http import HttpResponseRedirect from patchwork.models import Project, Bundle, Person, EmailConfirmation, State -from patchwork.forms import UserProfileForm, UserPersonLinkForm +from patchwork.forms import UserProfileForm, UserPersonLinkForm, \ + RegistrationForm from patchwork.filters import DelegateFilter from patchwork.views import generic_list from django.template.loader import render_to_string @@ -31,6 +34,55 @@ from django.conf import settings from django.core.mail import send_mail import django.core.urlresolvers +def register(request): + context = PatchworkRequestContext(request) + if request.method == 'POST': + form = RegistrationForm(request.POST) + if form.is_valid(): + data = form.cleaned_data + # create inactive user + user = auth.models.User.objects.create_user(data['username'], + data['email'], + data['password']) + user.is_active = False; + user.first_name = data.get('first_name', '') + user.last_name = data.get('last_name', '') + user.save() + + # create confirmation + conf = EmailConfirmation(type = 'registration', user = user, + email = user.email) + conf.save() + + # send email + mail_ctx = {'site': Site.objects.get_current(), + 'confirmation': conf} + + subject = render_to_string('patchwork/activation_email_subject.txt', + mail_ctx).replace('\n', ' ').strip() + + message = render_to_string('patchwork/activation_email.txt', + mail_ctx) + + send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, + [conf.email]) + + # setting 'confirmation' in the template indicates success + context['confirmation'] = conf + + else: + form = RegistrationForm() + + return render_to_response('patchwork/registration_form.html', + { 'form': form }, + context_instance=context) + +def register_confirm(request, conf): + conf.user.is_active = True + conf.user.save() + conf.deactivate() + return render_to_response('patchwork/registration-confirm.html') + @login_required def profile(request): context = PatchworkRequestContext(request) diff --git a/apps/settings.py b/apps/settings.py index f56da70..8f091d0 100644 --- a/apps/settings.py +++ b/apps/settings.py @@ -64,7 +64,7 @@ MIDDLEWARE_CLASSES = ( ROOT_URLCONF = 'apps.urls' -LOGIN_URL = '/accounts/login' +LOGIN_URL = '/user/login/' LOGIN_REDIRECT_URL = '/user/' # If you change the ROOT_DIR setting in your local_settings.py, you'll need to @@ -96,13 +96,12 @@ INSTALLED_APPS = ( 'django.contrib.sites', 'django.contrib.admin', 'patchwork', - 'registration', ) DEFAULT_PATCHES_PER_PAGE = 100 DEFAULT_FROM_EMAIL = 'Patchwork <patchwork@patchwork.example.com>' -ACCOUNT_ACTIVATION_DAYS = 7 +CONFIRMATION_VALIDITY_DAYS = 7 # Set to True to enable the Patchwork XML-RPC interface ENABLE_XMLRPC = False diff --git a/apps/urls.py b/apps/urls.py index 3894708..4ddef9e 100644 --- a/apps/urls.py +++ b/apps/urls.py @@ -23,9 +23,6 @@ from django.conf.urls.defaults import * from django.conf import settings from django.contrib import admin -from registration.views import register -from patchwork.forms import RegistrationForm - admin.autodiscover() htdocs = os.path.join(settings.ROOT_DIR, 'htdocs') @@ -34,13 +31,6 @@ urlpatterns = patterns('', # Example: (r'^', include('patchwork.urls')), - # override the default registration form - url(r'^accounts/register/$', - register, {'form_class': RegistrationForm}, - name='registration_register'), - - (r'^accounts/', include('registration.urls')), - # Uncomment this for admin: (r'^admin/', include(admin.site.urls)), diff --git a/docs/INSTALL b/docs/INSTALL index 4c178ef..6a1a0bf 100644 --- a/docs/INSTALL +++ b/docs/INSTALL @@ -81,17 +81,6 @@ in brackets): cd ../python ln -s ../packages/django/django ./django - We also use the django-registration infrastructure from - http://bitbucket.org/ubernostrum/django-registration/. Your distro - may provide the django-registration python module (in Ubuntu/Debian it's - called 'python-django-registration'). If not, download the module - and symlink it to lib/python/ : - - cd lib/packages/ - hg clone http://bitbucket.org/ubernostrum/django-registration/ - cd ../python - ln -s ../packages/django-registration/registration ./registration - We also use some Javascript libraries: cd lib/packages diff --git a/lib/sql/grant-all.mysql.sql b/lib/sql/grant-all.mysql.sql index f60c6b8..a3d758c 100644 --- a/lib/sql/grant-all.mysql.sql +++ b/lib/sql/grant-all.mysql.sql @@ -22,7 +22,6 @@ GRANT SELECT, UPDATE, INSERT, DELETE ON patchwork_project TO 'www-data'@localhos GRANT SELECT, UPDATE, INSERT, DELETE ON patchwork_bundle TO 'www-data'@localhost; GRANT SELECT, UPDATE, INSERT, DELETE ON patchwork_bundle_patches TO 'www-data'@localhost; GRANT SELECT, UPDATE, INSERT, DELETE ON patchwork_patch TO 'www-data'@localhost; -GRANT SELECT, UPDATE, INSERT, DELETE ON registration_registrationprofile TO 'www-data'@localhost; -- allow the mail user (in this case, 'nobody') to add patches GRANT INSERT, SELECT ON patchwork_patch TO 'nobody'@localhost; diff --git a/lib/sql/grant-all.postgres.sql b/lib/sql/grant-all.postgres.sql index 47c4ad3..591ffd0 100644 --- a/lib/sql/grant-all.postgres.sql +++ b/lib/sql/grant-all.postgres.sql @@ -22,8 +22,7 @@ GRANT SELECT, UPDATE, INSERT, DELETE ON patchwork_project, patchwork_bundle, patchwork_bundlepatch, - patchwork_patch, - registration_registrationprofile + patchwork_patch TO "www-data"; GRANT SELECT, UPDATE ON auth_group_id_seq, @@ -45,8 +44,7 @@ GRANT SELECT, UPDATE ON patchwork_state_id_seq, patchwork_emailconfirmation_id_seq, patchwork_userprofile_id_seq, - patchwork_userprofile_maintainer_projects_id_seq, - registration_registrationprofile_id_seq + patchwork_userprofile_maintainer_projects_id_seq TO "www-data"; -- allow the mail user (in this case, 'nobody') to add patches diff --git a/lib/sql/migration/009-drop-registrationprofile.sql b/lib/sql/migration/009-drop-registrationprofile.sql new file mode 100644 index 0000000..f1c2b43 --- /dev/null +++ b/lib/sql/migration/009-drop-registrationprofile.sql @@ -0,0 +1,27 @@ +BEGIN; + +DELETE FROM registration_registrationprofile; + +-- unlink users who have contributed + +UPDATE patchwork_person SET user_id = NULL + WHERE user_id IN (SELECT id FROM auth_user WHERE is_active = False) + AND id IN (SELECT DISTINCT submitter_id FROM patchwork_comment); + +-- remove persons who only have a user linkage + +DELETE FROM patchwork_person WHERE user_id IN + (SELECT id FROM auth_user WHERE is_active = False); + +-- delete profiles + +DELETE FROM patchwork_userprofile WHERE user_id IN + (SELECT id FROM auth_user WHERE is_active = False); + +-- delete inactive users + +DELETE FROM auth_user WHERE is_active = False; + +DROP TABLE registration_registrationprofile; + +COMMIT; diff --git a/templates/base.html b/templates/base.html index e14470e..9e80dca 100644 --- a/templates/base.html +++ b/templates/base.html @@ -30,7 +30,7 @@ {% else %} <a href="{% url auth_login %}">login</a> <br/> - <a href="{% url registration_register %}">register</a> + <a href="{% url patchwork.views.user.register %}">register</a> {% endif %} </div> <div style="clear: both;"></div> diff --git a/templates/registration/activation_email.txt b/templates/patchwork/activation_email.txt index 6b1477d..e918e5f 100644 --- a/templates/registration/activation_email.txt +++ b/templates/patchwork/activation_email.txt @@ -3,7 +3,7 @@ Hi, This email is to confirm your account on the patchwork patch-tracking system. You can activate your account by visiting the url: - http://{{site.domain}}{% url registration_activate activation_key=activation_key %} + http://{{site.domain}}{% url patchwork.views.confirm key=confirmation.key %} If you didn't request a user account on patchwork, then you can ignore this mail. diff --git a/templates/registration/activation_email_subject.txt b/templates/patchwork/activation_email_subject.txt index c409f38..c409f38 100644 --- a/templates/registration/activation_email_subject.txt +++ b/templates/patchwork/activation_email_subject.txt diff --git a/templates/patchwork/help/about.html b/templates/patchwork/help/about.html index edc381e..0d784d7 100644 --- a/templates/patchwork/help/about.html +++ b/templates/patchwork/help/about.html @@ -11,10 +11,6 @@ <p>Patchwork is built on the <a href="http://djangoproject.com/">django</a> web framework.</p> -<p>Patchwork includes the <a -href="http://code.google.com/p/django-registration/">django-registration</a> -application.</p> - <p>Icons from the <a href="http://sweetie.sublink.ca/">Sweetie</a> icon set.</a> {% endblock %} diff --git a/templates/registration/login.html b/templates/patchwork/login.html index 2dfc2a7..2dfc2a7 100644 --- a/templates/registration/login.html +++ b/templates/patchwork/login.html diff --git a/templates/registration/logout.html b/templates/patchwork/logout.html index f030aee..f030aee 100644 --- a/templates/registration/logout.html +++ b/templates/patchwork/logout.html diff --git a/templates/registration/activate.html b/templates/patchwork/registration-confirm.html index f0cc39f..f0cc39f 100644 --- a/templates/registration/activate.html +++ b/templates/patchwork/registration-confirm.html diff --git a/templates/registration/registration_form.html b/templates/patchwork/registration_form.html index e2b17c1..3a314b8 100644 --- a/templates/registration/registration_form.html +++ b/templates/patchwork/registration_form.html @@ -6,12 +6,11 @@ {% block body %} -{% if request and not error %} +{% if confirmation and not error %} <p>Registration successful!</p> - <p>A confirmation email has been sent to {{ request.email }}. You'll + <p>A confirmation email has been sent to {{ confirmation.email }}. You'll need to visit the link provided in that email to confirm your registration.</p> - <pre>{{email}}</pre> </p> {% else %} <p>By creating a patchwork account, you can:<p> diff --git a/templates/registration/registration_complete.html b/templates/registration/registration_complete.html deleted file mode 100644 index a89c116..0000000 --- a/templates/registration/registration_complete.html +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "base.html" %} - -{% block title %}Registration{% endblock %} -{% block heading %}Registration{% endblock %} - -{% block body %} - - <p>Registration successful!</p> - <p>A confirmation email has been sent to your email address. You'll - need to visit the link provided in that email to activate your - patchwork account.</p> - -{% endblock %} |