Пример #1
0
def registration(request):
    if request.method == 'POST':  # If the form has been submitted...
        form = UserRegistrationForm(request.POST)
        if form.is_valid():  # All validation rules pass
            # create user from cleaned_data
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password1']

            new_user = User.objects.create_user(username, email, password)
            new_user.is_active = False  # Users created inactive by default

            # Add users to ionusers group, for a default set of permissions
            try:
                group = Group.objects.get(name='ionusers')
                new_user.groups.add(group)
            except Group.DoesNotExist:
                logger.warn("Group ionusers not found. " +
                         "New user %s will lack permission to do anything beyond look!", username)
            new_user.save()

            # Send global message notifying of pending registration
            msg = "New pending user registration for '%s'. " + \
                "Please visit <a href='%s'>Account Management</a> (as an admin user) to review."
            Message.warn(msg % (username, urlresolvers.reverse('configure_account')))

            # Otherwise redirect to a success page. Awaiting approval
            return shortcuts.redirect(urlresolvers.reverse('signup_pending'))
    else:
        # Blank Form
        form = UserRegistrationForm()

    context = template.RequestContext(request, {'form': form, })
    return shortcuts.render_to_response("rundb/login/ion_account_reg.html",
                                        context_instance=context)
Пример #2
0
def registration(request):
    if request.method == 'POST':  # If the form has been submitted...
        form = UserRegistrationForm(request.POST)
        if form.is_valid():  # All validation rules pass
            # create user from cleaned_data
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password1']

            new_user = User.objects.create_user(username, email, password)
            new_user.is_active = False  # Users created inactive by default

            # Add users to ionusers group, for a default set of permissions
            try:
                group = Group.objects.get(name='ionusers')
                new_user.groups.add(group)
            except Group.DoesNotExist:
                logger.warn("Group ionusers not found. " +
                         "New user %s will lack permission to do anything beyond look!", username)
            new_user.save()

            # Send global message notifying of pending registration
            msg = "New pending user registration for '%s'. " + \
                "Please visit <a href='%s'>Account Management</a> (as an admin user) to review."
            Message.warn(msg % (username, urlresolvers.reverse('configure_account')))

            # Otherwise redirect to a success page. Awaiting approval
            return shortcuts.redirect(urlresolvers.reverse('signup_pending'))
    else:
        # Blank Form
        form = UserRegistrationForm()

    context = template.RequestContext(request, {'form': form, })
    return shortcuts.render_to_response("rundb/login/ion_account_reg.html",
                                        context_instance=context)
Пример #3
0
 def test_form_username_already_used(self):
     valid = {'username': '******', 'email': '*****@*****.**', 'password1': '12345', 'password2': '12345'}
     form = UserRegistrationForm(valid)
     self.assertTrue(form.is_bound)
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors, {'username': [u'A user with that username already exists.']})
     self.assertEqual(form.errors.keys(), ['username'], 'Only username should have an error')
Пример #4
0
 def test_form_password_mismatch(self):
     mismatch = {
         'username': '******',
         'email': '*****@*****.**',
         'password1': '12345',
         'password2': '12346'
     }
     form = UserRegistrationForm(mismatch)
     self.assertTrue(form.is_bound)
     self.assertEqual(
         form.errors,
         {'__all__': [u"The two password fields didn't match."]})
     self.assertFalse(form.is_valid())
Пример #5
0
def registration(request):
    if request.method == "POST":  # If the form has been submitted...
        form = UserRegistrationForm(request.POST)
        if form.is_valid():  # All validation rules pass
            # create user from cleaned_data
            username = form.cleaned_data["username"]
            email = form.cleaned_data["email"]
            password = form.cleaned_data["password1"]

            new_user = User.objects.create_user(username, email, password)
            new_user.is_active = False  # Users created inactive by default

            # Add users to ionusers group, for a default set of permissions
            try:
                group = Group.objects.get(name="ionusers")
                new_user.groups.add(group)
            except Group.DoesNotExist:
                logger.warn(
                    "Group ionusers not found. " +
                    "New user %s will lack permission to do anything beyond look!",
                    username,
                )
            new_user.save()

            # update UserProfile to indicate this account needs activation
            new_user.userprofile.needs_activation = True
            new_user.userprofile.save()

            # Send global message notifying of pending registration
            msg = (
                "New pending user registration for '%s'. " +
                "Please visit <a href='%s'>Account Management</a> to review."
            )  # TODO: i18n ?
            Message.warn(
                msg % (username, urlresolvers.reverse("configure_account")),
                route=Message.USER_STAFF,
            )

            # Otherwise redirect to a success page. Awaiting approval
            return shortcuts.redirect(urlresolvers.reverse("signup_pending"))
    else:
        # Blank Form
        form = UserRegistrationForm()

    context = template.RequestContext(request, {"form": form})
    return shortcuts.render_to_response("rundb/login/ion_account_reg.html",
                                        context_instance=context)
Пример #6
0
    def test_form(self):
        form = UserRegistrationForm({'username': '******', 'email': '*****@*****.**', 'password1': '12345', 'password2': '12345'})
        self.assertTrue(form.is_bound)
        self.assertEqual(form.errors, {})
        self.assertTrue(form.is_valid())
        self.assertHTMLEqual(form.errors.as_ul(), '')
        self.assertEqual(form.errors.as_text(), '')
        self.assertEqual(form.cleaned_data["username"], 'abc')
        self.assertEqual(form.cleaned_data["email"], '*****@*****.**')
        self.assertEqual(form.cleaned_data["password1"], '12345')
        self.assertEqual(form.cleaned_data["password2"], '12345')
        self.assertHTMLEqual(str(form['username']), '<input type="text" maxlength="30" name="username" value="abc" id="id_username" />')
        self.assertHTMLEqual(str(form['email']), '<input type="text" maxlength="75" name="email" value="*****@*****.**" id="id_email" />')
        self.assertHTMLEqual(str(form['password1']), '<input type="password" name="password1" id="id_password1" />')
        self.assertHTMLEqual(str(form['password2']), '<input type="password" name="password2" id="id_password2" />')
        try:
            form['nonexistentfield']
            self.fail('Attempts to access non-existent fields should fail.')
        except KeyError:
            pass

        form_output = []

        for boundfield in form:
            form_output.append(str(boundfield))

        self.assertHTMLEqual('\n'.join(form_output), """<input type="text" maxlength="30" name="username" value="abc" id="id_username" />
<input type="text" maxlength="75" name="email" value="*****@*****.**" id="id_email" />
<input type="password" name="password1" id="id_password1" />
<input type="password" name="password2" id="id_password2" />""")

        form_output = []

        for boundfield in form:
            form_output.append([boundfield.label, boundfield.data])

        self.assertEqual(form_output, [
            ['Username', 'abc'],
            ['E-mail', '*****@*****.**'],
            ['Password', '12345'],
            ['Password (again)', '12345']
        ])
        self.assertHTMLEqual(str(form), """<tr><th><label for="id_username">Username:</label></th><td><input type="text" maxlength="30" name="username" value="abc" id="id_username" /></td></tr>
<tr><th><label for="id_email">E-mail:</label></th><td><input type="text" maxlength="75" name="email" value="*****@*****.**" id="id_email" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input type="password" name="password1" id="id_password1" /></td></tr>
<tr><th><label for="id_password2">Password (again):</label></th><td><input type="password" name="password2" id="id_password2" /></td></tr>""")
Пример #7
0
 def test_form_username_is_valid(self):
     valid = {'username': '******', 'email': '*****@*****.**', 'password1': '12345', 'password2': '12345'}
     form = UserRegistrationForm(valid)
     self.assertTrue(form.is_bound)
     self.assertEqual(form.errors, {})
     self.assertTrue(form.is_valid())
     
     invalid = dict(valid)
     invalid['username'] = '******'
     form = UserRegistrationForm(invalid)
     self.assertTrue(form.is_bound)
     self.assertEqual(form.errors['username'], ['This value may contain only letters, numbers and @/./+/-/_ characters.'])
     self.assertEqual(form.errors.keys(), ['username'], 'Only username should have an error')
     self.assertFalse(form.is_valid())
Пример #8
0
 def test_form_password_mismatch(self):
     mismatch = {'username': '******', 'email': '*****@*****.**', 'password1': '12345', 'password2': '12346'}
     form = UserRegistrationForm(mismatch)
     self.assertTrue(form.is_bound)
     self.assertEqual(form.errors, {'__all__': [u"The two password fields didn't match."]})
     self.assertFalse(form.is_valid())