Skip to content

Commit

Permalink
[bug 778095] Register as contributor
Browse files Browse the repository at this point in the history
* A migration to add new "Registered as contributor" group
* Add a checkbox to the registration form
* If checkbox is checked, user gets added to the group
  • Loading branch information
rlr committed Aug 9, 2012
1 parent b225c49 commit b964490
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 8 deletions.
Expand Up @@ -130,7 +130,7 @@ <h2>{{ _('Before you continue, we ask you to create a support account so we can
<div id="register_form" class="slidebox">
{# TODO: these may have regressed the layout, h2 -> h3. Check! #}
<h3 class="slide">{{ _('Create an account') }}</h3>
{{ register_form(register, csrf=csrf, action='') }}
{{ register_form(register, csrf=csrf, action='', show_checkbox=False) }}
</div>

<div id="login_form" class="slidebox">
Expand Down
5 changes: 5 additions & 0 deletions apps/users/forms.py
Expand Up @@ -106,6 +106,11 @@ class RegisterForm(forms.ModelForm):
help_text=_lazy(u'Enter the same password as '
'above, for verification.'))

interested = forms.BooleanField(
label=_lazy(u'I am interested in volunteering to help other '
'Mozilla users'),
required=False)

class Meta(object):
model = User
fields = ('username', 'password', 'password2', 'email')
Expand Down
9 changes: 7 additions & 2 deletions apps/users/models.py
Expand Up @@ -5,7 +5,7 @@
import re

from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.models import User, Group
from django.contrib.sites.models import Site
from django.core import mail
from django.db import models
Expand Down Expand Up @@ -208,7 +208,7 @@ def activate_user(self, activation_key, request=None):
def create_inactive_user(self, username, password, email,
locale=settings.LANGUAGE_CODE,
email_template=None, email_subject=None,
email_data=None):
email_data=None, volunteer_interest=False):
"""
Create a new, inactive ``User`` and ``Profile``, generates a
``RegistrationProfile`` and email its activation key to the
Expand All @@ -224,6 +224,11 @@ def create_inactive_user(self, username, password, email,
self.send_confirmation_email(registration_profile, email_template,
email_subject, email_data)

if volunteer_interest:
statsd.incr('user.registered-as-contributor')
group = Group.objects.get(name='Registered as contributor')
new_user.groups.add(group)

return new_user

def send_confirmation_email(self, registration_profile,
Expand Down
14 changes: 11 additions & 3 deletions apps/users/templates/users/includes/macros.html
Expand Up @@ -38,15 +38,23 @@ <h3>{{ _('Login Problems?') }}</h3>
</div>
{%- endmacro %}

{% macro register_form(form, csrf, action=None) -%}
{% macro register_form(form, csrf, action=None, show_checkbox=True) -%}
{{ errorlist(form) }}
<form method="post" action="{{ action if action != None else
url('users.register') }}">
{{ csrf|safe }}
<ul>
{% for field in form %}
<li>{{ field|label_with_help }} {{ field|safe }}</li>
{% for field in form if field.name != 'interested' %}
<li>{{ field|label_with_help }} {{ field }}</li>
{% endfor %}
{% if show_checkbox %}
<li class="checkbox">
<label>
{{ form.interested }}
{{ form.interested.field.label }}
</label>
</li>
{% endif %}
</ul>
<div class="submit">
<input type="submit" name="register" class="btn" value="{{ _('Register') }}" />
Expand Down
23 changes: 22 additions & 1 deletion apps/users/tests/test_views.py
Expand Up @@ -15,7 +15,7 @@
from sumo.urlresolvers import reverse
from users import ERROR_SEND_EMAIL
from users.models import Profile, RegistrationProfile, EmailChange, Setting
from users.tests import profile, user
from users.tests import profile, user, group


class RegisterTests(TestCase):
Expand Down Expand Up @@ -46,6 +46,9 @@ def test_new_user(self, get_current):
key = RegistrationProfile.objects.all()[0].activation_key
assert mail.outbox[0].body.find('activate/%s/%s' % (u.id, key)) > 0

# By default, users aren't added to any groups
eq_(0, len(u.groups.all()))

# Now try to log in
u.is_active = True
u.save()
Expand Down Expand Up @@ -197,6 +200,24 @@ def test_old_activation_url(self, get_current):
user = User.objects.get(pk=user.pk)
assert user.is_active

@mock.patch.object(Site.objects, 'get_current')
def test_new_contributor(self, get_current):
"""Verify that interested contributors are added to group."""
get_current.return_value.domain = 'su.mo.com'
group_name = 'Registered as contributor'
group(name=group_name, save=True)
data = {
'username': 'newbie',
'email': 'newbie@example.com',
'password': 'foobar22',
'password2': 'foobar22',
'interested': 'yes'}
response = self.client.post(reverse('users.register', locale='en-US'),
data, follow=True)
eq_(200, response.status_code)
u = User.objects.get(username='newbie')
eq_(group_name, u.groups.all()[0].name)


class ChangeEmailTestCase(TestCase):
fixtures = ['users.json']
Expand Down
3 changes: 2 additions & 1 deletion apps/users/utils.py
Expand Up @@ -47,7 +47,8 @@ def handle_register(request, email_template=None, email_subject=None,
locale=request.locale,
email_template=email_template,
email_subject=email_subject,
email_data=email_data)
email_data=email_data,
volunteer_interest=form.cleaned_data['interested'])
if not form.is_valid():
# Delete user if form is not valid, i.e. email was not sent.
# This is in a POST request and so always pinned to master,
Expand Down
9 changes: 9 additions & 0 deletions media/css/users.css
Expand Up @@ -42,6 +42,15 @@ article.main form label {
text-align: left;
}

article.main form li.checkbox label {
padding: 0 5px 0 168px;
width: auto;
}

article.main form li.checkbox input {
margin: 0 7px 0 0;
}

article.main form li input {
margin-top: -8px;
width: 200px;
Expand Down
1 change: 1 addition & 0 deletions migrations/163-registered-as-contributor-group.sql
@@ -0,0 +1 @@
INSERT INTO `auth_group` (`name`) VALUES ('Registered as contributor');

0 comments on commit b964490

Please sign in to comment.