From 3e13bc798b277823e4bb3ab0992fbb6fa5eb99ec Mon Sep 17 00:00:00 2001 From: Sergio Oliveira Date: Wed, 25 Sep 2013 11:28:18 -0300 Subject: [PATCH] Refactorign signup process --- src/accounts/forms.py | 60 +++++++++++++++++------------------------------------------- src/accounts/models.py | 14 +++++++------- src/accounts/templates/accounts/account_message.html | 8 -------- src/accounts/templates/accounts/email_signup-email-confirmation.html | 9 --------- src/accounts/urls.py | 5 ++--- src/accounts/views.py | 146 ++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------- src/colab/custom_settings.py | 9 ++++++++- src/colab/deprecated/templates/base.html | 13 +++++++++++-- 8 files changed, 87 insertions(+), 177 deletions(-) delete mode 100644 src/accounts/templates/accounts/account_message.html delete mode 100644 src/accounts/templates/accounts/email_signup-email-confirmation.html diff --git a/src/accounts/forms.py b/src/accounts/forms.py index 6c7ec1a..15e24d2 100644 --- a/src/accounts/forms.py +++ b/src/accounts/forms.py @@ -1,57 +1,31 @@ # -*- coding: utf-8 -*- from django import forms -from django.contrib.auth.models import User +from django.contrib.auth import get_user_model from django.contrib.auth.forms import UserCreationForm as UserCreationForm_ from django.utils.translation import ugettext_lazy as _ from super_archives.models import MailingList -from super_archives.validators import UniqueValidator -LISTS_NAMES = [] -for list_ in MailingList.objects.iterator(): - choice = (list_.name, list_.name) - LISTS_NAMES.append(choice) +User = get_user_model() -class UserCreationForm(UserCreationForm_): - first_name = forms.CharField(max_length=30, label=_(u'Name'), - widget=forms.TextInput(attrs={'class':'form-control'})) - last_name = forms.CharField(max_length=30, label=_(u'Last name'), - widget=forms.TextInput(attrs={'class':'form-control'})) - email = forms.EmailField(validators=[UniqueValidator(User, 'email')], - widget=forms.TextInput(attrs={'class':'form-control'})) - lists = forms.MultipleChoiceField(label=u'Listas', - required=False, - widget=forms.CheckboxSelectMultiple, - choices=LISTS_NAMES) - +class NewUserForm(forms.ModelForm): + class Meta: + model = User + fields = ('first_name', 'last_name', 'email', 'username') def __init__(self, *args, **kwargs): - super(UserCreationForm, self).__init__(*args, **kwargs) - self.fields.pop('password1') - self.fields.pop('password2') - - -class UserUpdateForm(UserCreationForm): - institution= forms.CharField(max_length=120, label=_(u'Institution'), required=False, - widget=forms.TextInput(attrs={'class':'form-control'})) - role = forms.CharField(max_length=60, label=_(u'Role'), required=False, - widget=forms.TextInput(attrs={'class':'form-control'})) - twitter = forms.URLField(label=_(u'Twitter'), required=False, - widget=forms.TextInput(attrs={'class':'form-control'})) - facebook = forms.URLField(label=_(u'Facebook'), required=False, - widget=forms.TextInput(attrs={'class':'form-control'})) - google_talk = forms.EmailField(label=_(u'Google Talk'), required=False, - widget=forms.TextInput(attrs={'class':'form-control'})) - webpage = forms.URLField(label=_(u'Personal Website/Blog'), required=False, - widget=forms.TextInput(attrs={'class':'form-control'})) + super(NewUserForm, self).__init__(*args, **kwargs) + for field in self.fields.values(): + field.widget.attrs.update({'class': 'form-control'}) + field.required = True - def __init__(self, *args, **kwargs): - super(UserUpdateForm, self).__init__(*args, **kwargs) - self.fields.pop('username') - self.fields.pop('first_name') - self.fields.pop('last_name') - self.fields.pop('email') - self.fields.pop('lists') + +class ListsForm(forms.Form): + LISTS_NAMES = ((list.name, list.name) for list in MailingList.objects.all()) + lists = forms.MultipleChoiceField(label=_(u'Mailing lists'), + required=False, + widget=forms.CheckboxSelectMultiple, + choices=LISTS_NAMES) diff --git a/src/accounts/models.py b/src/accounts/models.py index 7a4edba..f5effec 100644 --- a/src/accounts/models.py +++ b/src/accounts/models.py @@ -4,13 +4,13 @@ from django.contrib.auth.models import AbstractUser class User(AbstractUser): - institution = models.CharField(max_length=128, null=True) - role = models.CharField(max_length=128, null=True) - twitter = models.CharField(max_length=128, null=True) - facebook = models.CharField(max_length=128, null=True) - google_talk = models.EmailField(null=True) - webpage = models.CharField(max_length=256, null=True) - verification_hash = models.CharField(max_length=32, null=True) + institution = models.CharField(max_length=128, null=True, blank=True) + role = models.CharField(max_length=128, null=True, blank=True) + twitter = models.CharField(max_length=128, null=True, blank=True) + facebook = models.CharField(max_length=128, null=True, blank=True) + google_talk = models.EmailField(null=True, blank=True) + webpage = models.CharField(max_length=256, null=True, blank=True) + verification_hash = models.CharField(max_length=32, null=True, blank=True) # We need to have `email` field set as unique but Django does not # support field overriding (at least not until 1.6). diff --git a/src/accounts/templates/accounts/account_message.html b/src/accounts/templates/accounts/account_message.html deleted file mode 100644 index dedb08d..0000000 --- a/src/accounts/templates/accounts/account_message.html +++ /dev/null @@ -1,8 +0,0 @@ -{% extends "base.html" %} -{% load i18n %} - -{% block main-content %} - -
{% trans msg %}
- -{% endblock %} diff --git a/src/accounts/templates/accounts/email_signup-email-confirmation.html b/src/accounts/templates/accounts/email_signup-email-confirmation.html deleted file mode 100644 index 61c7a67..0000000 --- a/src/accounts/templates/accounts/email_signup-email-confirmation.html +++ /dev/null @@ -1,9 +0,0 @@ -{% load i18n %} - -{% trans "Welcome to the Colab!" %} - -{% trans "To activate your account, please confirm your mail's activation by accessing the following link:" %} - - - http://{{ server_name }}{% url 'email_verification' hash %} - diff --git a/src/accounts/urls.py b/src/accounts/urls.py index d6d455e..aff2db9 100644 --- a/src/accounts/urls.py +++ b/src/accounts/urls.py @@ -5,10 +5,9 @@ from .views import UserProfileDetailView urlpatterns = patterns('', - url(r'^$', 'accounts.views.signup', name='signup'), + #url(r'^$', 'accounts.views.signup', name='signup'), - url(r'^verify/(?P[\w]{32})/$', - 'accounts.views.verify_email', name='email_verification'), + url(r'^register/$', 'accounts.views.signup', name='signup'), url(r'^(?P[\w@+.-]+)/?$', UserProfileDetailView.as_view(), name='user_profile'), diff --git a/src/accounts/views.py b/src/accounts/views.py index 6f60dc8..f0d7a86 100644 --- a/src/accounts/views.py +++ b/src/accounts/views.py @@ -1,118 +1,18 @@ #!/usr/bin/env python # encoding: utf-8 -import uuid -from colab.deprecated import signup as signup_ +from django.contrib import messages -from django.template import RequestContext from django.contrib.auth import get_user_model from django.views.generic import DetailView from django.utils.translation import ugettext as _ -from django.shortcuts import render, get_object_or_404 +from django.shortcuts import render, redirect from colab.deprecated import solrutils +from colab.deprecated import signup as signup_ -from .forms import UserCreationForm from super_archives.models import EmailAddress, Message - - -# helper -def get_field_set(form): - fieldsets = ( - (_('Personal Information'), ( - form['first_name'], - form['last_name'], - form['email'], - form['username'], - ) - ), - (_('Subscribe to mail lists'), ( - form['lists'], - ) - ), - ) - return fieldsets - - -def signup(request): - - # If the request method is GET just return the form - if request.method == 'GET': - form = UserCreationForm() - return render(request, 'accounts/signup-form.html', - {'form': form, 'fieldsets': get_field_set(form)}) - - # If the request method is POST try to store data - form = UserCreationForm(request.POST) - - # If there is validation errors give the form back to the user - if not form.is_valid(): - return render(request, 'accounts/signup-form.html', - {'form': form, 'fieldsets': get_field_set(form)}) - - user = User( - username=form.cleaned_data.get('username'), - email=form.cleaned_data.get('email'), - first_name=form.cleaned_data.get('first_name'), - last_name=form.cleaned_data.get('last_name'), - is_active=False, - ) - user.set_password(form.cleaned_data.get('password1')) - user.save() - - profile = UserProfile( - user=user, - institution=form.cleaned_data.get('institution'), - role=form.cleaned_data.get('role'), - twitter=form.cleaned_data.get('twitter'), - facebook=form.cleaned_data.get('facebook'), - google_talk=form.cleaned_data.get('google_talk'), - webpage=form.cleaned_data.get('webpage'), - verification_hash=uuid.uuid4().get_hex(), - ) - profile.save() - - signup_.send_verification_email(request, user) - - mailing_lists = form.cleaned_data.get('lists') - if mailing_lists: - signup_.send_email_lists(user, mailing_lists) - - - # Check if the user's email have been used previously - # in the mainling lists to link the user to old messages - email_addr, created = EmailAddress.objects.get_or_create(address=user.email) - if created: - email_addr.real_name = user.get_full_name() - - email_addr.user = user - email_addr.save() - - template_data = { - 'msg': _(u'Registration completed successfully. Please visit your email address to validate it.'), - 'msg_css_class': 'alert-success', - } - - return render(request, 'accounts/account_message.html', template_data) - - -def verify_email(request, hash): - """Verify hash and activate user's account""" - - profile = get_object_or_404(UserProfile, verification_hash=hash) - - profile.verification_hash = 'verified' - profile.save() - - profile.user.is_active = True - profile.user.save() - - template_data = { - 'msg': _(u'E-mail validated correctly.'), - 'msg_css_class': 'alert-success', - } - - return render(request, 'accounts/account_message.html', template_data) +from .forms import NewUserForm, ListsForm class UserProfileDetailView(DetailView): @@ -141,5 +41,43 @@ class UserProfileDetailView(DetailView): query = query.order_by('-received_time') context['emails'] = query[:10] + context.update(kwargs) return super(UserProfileDetailView, self).get_context_data(**context) + +def signup(request): + # If the request method is GET just return the form + if request.method == 'GET': + user_form = NewUserForm() + lists_form = ListsForm() + return render(request, 'registration/registration_form.html', + {'user_form': user_form, 'lists_form': lists_form}) + + user_form = NewUserForm(request.POST) + lists_form = ListsForm(request.POST) + + if not user_form.is_valid() or not lists_form.is_valid(): + return render(request, 'registration/registration_form.html', + {'user_form': user_form, 'lists_form': lists_form}) + + user = user_form.save() + + mailing_lists = lists_form.cleaned_data.get('lists') + if mailing_lists: + signup_.send_email_lists(user, mailing_lists) + + # Check if the user's email have been used previously + # in the mainling lists to link the user to old messages + email_addr, created = EmailAddress.objects.get_or_create(address=user.email) + if created: + email_addr.real_name = user.get_full_name() + + email_addr.user = user + email_addr.save() + + messages.success(request, _('Your profile has been created!')) + messages.warning(request, _('You must login to validated your profile. ' + 'Profiles not validated are deleted in 24h.')) + + return redirect('user_profile', username=user.username) + diff --git a/src/colab/custom_settings.py b/src/colab/custom_settings.py index bc5c0c5..86c20ca 100644 --- a/src/colab/custom_settings.py +++ b/src/colab/custom_settings.py @@ -23,7 +23,6 @@ INSTALLED_APPS = INSTALLED_APPS + ( 'cliauth', 'django_browserid', 'conversejs', - 'registration', # Own apps 'super_archives', @@ -145,6 +144,14 @@ STATIC_ROOT = os.path.join(BASE_DIR, '..', 'www', 'static') AUTH_USER_MODEL = 'accounts.User' +from django.contrib.messages import constants as messages +MESSAGE_TAGS = { + messages.INFO: 'alert-info', + messages.SUCCESS: 'alert-success', + messages.WARNING: 'alert-warning', + messages.ERROR: 'alert-danger', +} + ### Proxy configuration SOCKS_SERVER = None diff --git a/src/colab/deprecated/templates/base.html b/src/colab/deprecated/templates/base.html index f78ad82..637fe67 100644 --- a/src/colab/deprecated/templates/base.html +++ b/src/colab/deprecated/templates/base.html @@ -47,8 +47,6 @@
- {% block header %}{% endblock %} - + {% block messages %} + {% for message in messages %} +
+ + {{ message }} +
+ {% endfor %} + {% endblock %} + + {% block header %}{% endblock %} +
{% block main-content %} {% endblock %}
-- libgit2 0.21.2