Beispiel #1
0
def create_account(request):
    """
    Respond to posts for creating an account.

    Only takes POSTs.
    """
    # if next is passed, get it
    next = request.GET.get('next', reverse('news.views.news_items.index'))
    next = request.POST.get('next', next)
    assert_or_404(valid_next_redirect(next))

    assert_or_404(request.method == 'POST')

    username = get_from_POST_or_404(request, 'username')
    password = get_from_POST_or_404(request, 'password')

    # make sure it's a valid username
    if not valid_username(username):
        request.session['create_account_error'] = 'Username can only ' + \
            'consist of letters, numbers, and underscores, and must be ' + \
            'less than 30 characters'
        request.session['create_account_username'] = username
        return HttpResponseRedirect(reverse('news.views.login.login_view') +
                "?next=" + next)

    # make sure no other users have this username
    if User.objects.filter(username=username):
        request.session['create_account_error'] = \
                'Username ' + username + ' taken'
        request.session['create_account_username'] = username
        return HttpResponseRedirect(reverse('news.views.login.login_view') +
                "?next=" + next)

    # make sure it's a valid password
    if not valid_password(password):
        request.session['create_account_error'] = \
                 'Password cannot be blank and must be less than 30 characters'
        request.session['create_account_username'] = username
        return HttpResponseRedirect(reverse('news.views.login.login_view') +
                "?next=" + next)

    # create the user and userprofile
    user = User.objects.create_user(username, '', password)
    UserProfile.objects.create(user=user)

    return login_view(request)
Beispiel #2
0
    def testValidUsername(self):
        """ Test valid_username(). """

        # usernames must match \w+
        self.assert_(valid_username('user'))
        self.assert_(valid_username('32__328LALAuser'))
        self.assert_(valid_username('_'))
        self.assert_(valid_username('a' * NEWS_MAX_USERNAME_LENGTH))

        self.assertFalse(valid_username('!#^#$'))
        self.assertFalse(valid_username(''))
        self.assertFalse(valid_username('\n'))
        self.assertFalse(valid_username('66.66'))
        self.assertFalse(valid_username('doctor user'))
        self.assertFalse(valid_username('日本語'))

        # usernames must not be too long
        self.assertFalse(valid_username('a' * (NEWS_MAX_USERNAME_LENGTH + 1)))