forked from cartwheelweb/packaginator
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working in django-registration. Some prep work for django-export scripts
- Loading branch information
Showing
16 changed files
with
309 additions
and
13 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" Your average django-registration backend """ | ||
|
||
from django.conf import settings | ||
from django.contrib.sites.models import RequestSite | ||
from django.contrib.sites.models import Site | ||
|
||
from registration import signals | ||
from registration.models import RegistrationProfile | ||
|
||
from accounts.forms import RegistrationForm | ||
|
||
from profiles.models import Profile | ||
|
||
class DjangoPackagesRegistrationBackend(object): | ||
|
||
def register(self, request, **kwargs): | ||
""" | ||
Assumes that the profiles copies the fields found in handstand.profiles.models.Profile | ||
""" | ||
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1'] | ||
if Site._meta.installed: | ||
site = Site.objects.get_current() | ||
else: | ||
site = RequestSite(request) | ||
|
||
new_user = RegistrationProfile.objects.create_inactive_user(username, email, | ||
password, site, send_email=settings.ACCOUNTS_ACTIVATION_EMAIL) | ||
|
||
signals.user_registered.send(sender=self.__class__, | ||
user=new_user, | ||
request=request) | ||
|
||
profile = Profile.objects.create( | ||
user=new_user, | ||
) | ||
profile.save() | ||
|
||
return new_user | ||
|
||
def activate(self, request, activation_key): | ||
""" | ||
Given an an activation key, look up and activate the user | ||
account corresponding to that key (if possible). | ||
After successful activation, the signal | ||
``registration.signals.user_activated`` will be sent, with the | ||
newly activated ``User`` as the keyword argument ``user`` and | ||
the class of this backend as the sender. | ||
""" | ||
activated = RegistrationProfile.objects.activate_user(activation_key) | ||
if activated: | ||
signals.user_activated.send(sender=self.__class__, | ||
user=activated, | ||
request=request) | ||
return activated | ||
|
||
def registration_allowed(self, request): | ||
""" | ||
Indicate whether account registration is currently permitted, | ||
based on the value of the setting ``REGISTRATION_OPEN``. This | ||
is determined as follows: | ||
* If ``REGISTRATION_OPEN`` is not specified in settings, or is | ||
set to ``True``, registration is permitted. | ||
* If ``REGISTRATION_OPEN`` is both specified and set to | ||
``False``, registration is not permitted. | ||
""" | ||
return getattr(settings, 'REGISTRATION_OPEN', True) | ||
|
||
def get_form_class(self, request): | ||
""" | ||
Return the default form class used for user registration. | ||
""" | ||
return RegistrationForm | ||
|
||
def post_registration_redirect(self, request, user): | ||
""" | ||
Return the name of the URL to redirect to after successful | ||
user registration. | ||
""" | ||
return ('registration_complete', (), {}) | ||
|
||
def post_activation_redirect(self, request, user): | ||
""" | ||
Return the name of the URL to redirect to after successful | ||
account activation. | ||
""" | ||
return ('registration_activation_complete', (), {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from django.contrib.auth.models import User | ||
from django import forms | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from uni_form.helpers import FormHelper, Submit, Layout, Row | ||
|
||
attrs_dict = {'class': 'required'} | ||
|
||
class RegistrationForm(forms.Form): | ||
""" | ||
Form for registering a new user account. | ||
Validates that the requested username is not already in use, and | ||
requires the password to be entered twice to catch typos. | ||
""" | ||
username = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, | ||
maxlength=30)), | ||
label=_("User name")) | ||
|
||
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, | ||
maxlength=75)), | ||
label=_("Email address")) | ||
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False), | ||
label=_("Password")) | ||
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False), | ||
label=_("Password (again)")) | ||
|
||
helper = FormHelper() | ||
layout = Layout( | ||
'username', | ||
'email', | ||
Row('password1','password2'), | ||
) | ||
|
||
helper.add_layout(layout) | ||
|
||
submit = Submit('register','Register') | ||
helper.add_input(submit) | ||
|
||
def clean(self): | ||
""" | ||
Verifiy that the values entered into the two password fields | ||
match. Note that an error here will end up in | ||
``non_field_errors()`` because it doesn't apply to a single | ||
field. | ||
""" | ||
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: | ||
if self.cleaned_data['password1'] != self.cleaned_data['password2']: | ||
raise forms.ValidationError(_("The two password fields didn't match.")) | ||
return self.cleaned_data | ||
|
||
def clean_username(self): | ||
""" | ||
Validate that the supplied email address is unique for the | ||
site. | ||
""" | ||
if User.objects.filter(username__exact=self.cleaned_data['username']): | ||
raise forms.ValidationError(_("This username is already in use. Please supply a different username.")) | ||
return self.cleaned_data['username'] | ||
|
||
def clean_email(self): | ||
""" | ||
Validate that the supplied email address is unique for the | ||
site. | ||
""" | ||
if User.objects.filter(email__iexact=self.cleaned_data['email']): | ||
raise forms.ValidationError(_("This email address is already in use. Please supply a different email address.")) | ||
return self.cleaned_data['email'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.conf import settings | ||
from django.conf.urls.defaults import patterns, include, url | ||
from django.views.generic.simple import direct_to_template | ||
|
||
from registration.views import activate | ||
from registration.views import register | ||
|
||
from accounts.forms import RegistrationForm | ||
|
||
|
||
urlpatterns = patterns('', | ||
|
||
url(r'^register/$', | ||
register, | ||
{'backend': "accounts.backends.DjangoPackagesRegistrationBackend", | ||
'form_class': RegistrationForm}, | ||
name='registration_register'), | ||
url(r'^activate/complete/$', | ||
direct_to_template, | ||
{'template': 'registration/activation_complete.html'}, | ||
name='registration_activation_complete'), | ||
url(r'^activate/(?P<activation_key>\w+)/$', | ||
activate, | ||
{'backend': "accounts.backends.StockRegistrationBackend"}, | ||
name='registration_activate'), | ||
url(r'^register/complete/$', | ||
direct_to_template, | ||
{'template': 'registration/registration_complete.html'}, | ||
name='registration_complete'), | ||
url(r'^register/closed/$', | ||
direct_to_template, | ||
{'template': 'registration/registration_closed.html'}, | ||
name='registration_disallowed'), | ||
(r'', include('registration.auth_urls')), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Create your views here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.