Ejemplo n.º 1
0
 def check_username(request):
     """ Checks if a username exists. """
     try:
         User.objects.get(username=request.REQUEST[username])
     except User.DoesNotExist:
         return Result(available=not is_reserved_path(username+'/'))
     else:
         return Result(available=False)
Ejemplo n.º 2
0
    def create_account(request):
        """ Creates a new account. """

        get = request.REQUEST.get
        username = get('username')
        password = get('password')
        email = get('email')

        if is_reserved_path(username+'/'):
            raise APIError('usernamereserved', "Username is reserved")

        try:
            new_user = User.objects.create( username = username,
                                            password = password,
                                            email = email )
        except:
            # TODO: Catch specific exceptions
            raise APIError('usernameexists', "Username exists")
        else:
            new_profile = Profile.objects.create(user = new_user)
            user = authenticate(username=username, password=password)
            assert user.pk == new_user.pk, "Authenticated user is not the new user"
            login(request, user)
Ejemplo n.º 3
0
def _create(request, view_name='create_account'):

    td = {}
    td['form_url'] = reverse(view_name)

    form = request.POST.get
    errors = defaultdict(list)

    invite = slugify(request.GET.get('invite', '').strip())
    if invite:
        td['invite'] = invite

    def add_error(field, error):
        errors[field].append(error)

    if request.method == "POST":

        invite = form('invite', '').strip()
        first_name = form('first_name', '').strip()
        last_name = form('last_name', '').strip()
        email = form('email', '').strip()
        username = slugify(form('username', '').strip().lower())
        password = form('password', '')
        password_check = form('password_check', '')

        if settings.REQUIRE_INVITE:
            if not invite:
                add_error('invite', 'Invite code is required')
            else:
                try:
                    Invite.check(invite)
                except InviteError, e:
                    add_error('invite', str(e))

        if not first_name:
            add_error('first_name', "First name is required")

        if not email:
            add_error('email', 'Email is required')
        elif not email_re.match(email):
            add_error('email', "Doesn't look like an email")

        if email:
            try:
                user = User.objects.get(email=email)
            except User.DoesNotExist:
                pass
            else:
                add_error('email', 'An account with this email address already exists')

        if username:
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                pass
            else:
                add_error('username', 'An account with this username address already exists')

        if not username:
            add_error('username', 'Username is required')
        elif len(username) <= 3:
            add_error('username', 'Username is too short (must be greater than 3 characters)')
        elif is_reserved_path(username):
            add_error('username', 'Sorry, this username is reserved')

        if not password:
            add_error('password', 'Password is required')

        elif len(password) <= 3:
            add_error('password', 'Password is too short (must be greater than 3 characters)')

        if password != password_check:
            add_error('password_check', 'Passwords do not match')
            password = password_check = ''


        td.update( invite=invite,
                   first_name=first_name,
                   last_name=last_name,
                   email=email,
                   username=username,
                   password=password,
                   password_check=password_check )

        if not errors:
            try:
                if settings.REQUIRE_INVITE:
                    Invite.use(invite)
                new_profile = Profile.create( username,
                                              password,
                                              email,
                                              first_name=first_name,
                                              last_name=last_name )

            except InviteError, e:
                add_error('invite', str(e))

            except ValueError, e:
                field, msg = str(e).split(':')
                add_error(field, msg)