diff options
Diffstat (limited to 'account')
-rw-r--r-- | account/forms.py | 40 | ||||
-rw-r--r-- | account/models.py | 57 | ||||
-rw-r--r-- | account/urls.py | 47 | ||||
-rw-r--r-- | account/views.py | 6 |
4 files changed, 148 insertions, 2 deletions
diff --git a/account/forms.py b/account/forms.py new file mode 100644 index 0000000..6a566e9 --- /dev/null +++ b/account/forms.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- + +""" +account/forms.py for skaschool +""" + +from django import forms +from registration.forms import RegistrationFormUniqueEmail +from django.utils.translation import ugettext_lazy as _ + + +class UserRegForm(RegistrationFormUniqueEmail): + """ + based on 'django-registration' RegistrationFormUniqueEmail + add fields 'realname', 'gender', 'institute' and 'captcha' + """ + # XXX: keep consistent with GENDERS in 'models.UserProfile' + GENDERS = ( + ('M', _("Male")), + ('F', _("Female")), + ('X', _("Secret")), + ) + realname = forms.CharField(max_length=30, label=_("Name")) + gender = forms.ChoiceField(choices=GENDERS, label=_("Gender")) + institute = forms.CharField(max_length=100, label=_("Institute")) + + def __init__(self, *args, **kw): + super(UserRegForm, self).__init__(*args, **kw) + # order form fields + self.fields.keyOrder = [ + 'username', + 'email', + 'password1', + 'password2', + 'realname', + 'gender', + 'institute', + ] + + diff --git a/account/models.py b/account/models.py index a3c2b0a..04f255c 100644 --- a/account/models.py +++ b/account/models.py @@ -1,12 +1,65 @@ # -*- coding: utf-8 -*- # # app 'account' models -# registration, login, etc. +# registration # from django.db import models from django.contrib.auth.models import User +from django.contrib import admin + +from django.utils.translation import ugettext_lazy as _ + +from registration.signals import user_registered + class UserProfile(models.Model): - user = models.OneToOneField(User) + """ + custom user profile + connected with signal 'user_registered' sent by 'django-registration' + """ + # XXX: keep consistent with GENDERS in 'forms.UserRegForm' + GENDERS = ( + ('M', _("Male")), + ('F', _("Female")), + ('X', _("Secret")), + ) + user = models.ForeignKey(User, unique=True, verbose_name=_("Username")) + realname = models.CharField(_("Name"), max_length=30) + gender = models.CharField(_("Gender"), max_length=1, choices=GENDERS) + institute = models.CharField(_("Institute"), max_length=100) + # store the infomation about approval and sponsorship + is_approved = models.BooleanField(_("Is approved"), default=False) + is_sponsored = models.BooleanField(_("Is sponsored"), default=False) + + class Meta: + verbose_name = _('user profile') + verbose_name_plural = _('user profiles') + + def __unicode__(self): + return u'UserProfile for %s' % self.user + + +###### signal callback ###### +def user_registered_callback(sender, user, request, **kwargs): + """ + callback of signal 'user_registered' from 'django-registration' + to create custom user profile + ref: http://johnparsons.net/index.php/2013/06/28/creating-profiles-with-django-registration/ + """ + profile = UserProfile(user = user) + profile.realname = request.POST['realname'] + profile.gender = request.POST['gender'] + profile.institute = request.POST['institute'] + profile.save() + +### connect 'user_registered_callback' to signal +user_registered.connect(user_registered_callback) + + +### add to adim +admin.site.register([ + UserProfile, +]) + diff --git a/account/urls.py b/account/urls.py new file mode 100644 index 0000000..3ae8d12 --- /dev/null +++ b/account/urls.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +""" +urls.py for app 'account' +customize 'registration.backends.default.urls' to use custom form +""" + +from django.conf.urls import patterns, include, url +from django.views.generic.base import TemplateView + +from registration.backends.default.views import ActivationView +from registration.backends.default.views import RegistrationView + +from account.forms import UserRegForm + + +urlpatterns = patterns('', + ## django-registration + # 0. registration_disallowed + url(r'^register/closed/$', + TemplateView.as_view(template_name='registration/registration_closed.html'), + name='registration_disallowed'), + # 1. registration_register + url(r'^register/$', + RegistrationView.as_view(form_class=UserRegForm), + name='registration_register'), + # 2. registration_complete + url(r'^register/complete/$', + TemplateView.as_view(template_name='registration/registration_complete.html'), + name='registration_complete'), + # 4. registration_activation_complete + url(r'^activate/complete/$', + TemplateView.as_view(template_name='registration/activation_complete.html'), + name='registration_activation_complete'), + # 3. registration_activate + # Activation keys get matched by \w+ instead of the more specific + # [a-fA-F0-9]{40} because a bad activation key should still get to the view; + # that way it can return a sensible "invalid key" message instead of a + # confusing 404. + url(r'^activate/(?P<activation_key>\w+)/$', + ActivationView.as_view(), + name='registration_activate'), + ## django auth + #(r'', include('registration.auth_urls')), +) + + diff --git a/account/views.py b/account/views.py index 91ea44a..2419fe6 100644 --- a/account/views.py +++ b/account/views.py @@ -1,3 +1,9 @@ +# -*- coding: utf-8 -*- + +""" +views.py of app 'account' +""" + from django.shortcuts import render # Create your views here. |