Exemplo n.º 1
0
def register(request):
    def valid_username_chars(name):
        return re.match(r'^[a-zA-Z0-9_\-. ]+$', name) != None

    def valid_username_spaces(name):
        return not name.startswith(' ') and not name.endswith(' ')

    username = request.POST['username']
    email = request.POST['email']
    password = request.POST['password']

    if len(username) == 0:
        return HttpResponse('Please enter a name.')
    elif len(username) < 3:
        return HttpResponse('Name must contain at least 3 characters.')
    elif len(username) > 30:
        return HttpResponse('Name must contain 30 characters or fewer.')
    elif not valid_username_chars(username):
        return HttpResponse(
            'Name must contain only ASCII letters, numbers, underscores, hypens, periods and spaces. Sorry about the temporary lack of Unicode support.'
        )
    elif not valid_username_spaces(username):
        return HttpResponse('Name cannot start or end with a space.')
    elif len(User.objects.filter(username=username)) != 0:
        return HttpResponse('Name already in use.')

    if len(email) == 0:
        return HttpResponse('Please enter an email address.')
    elif not '.' in email or not '@' in email:
        return HttpResponse('Please enter a valid email address.')
    elif len(User.objects.filter(email=email)) != 0:
        return HttpResponse('Email already in use.')

    if len(password) == 0:
        return HttpResponse('Please enter a password.')
    elif len(password) < 6:
        return HttpResponse('Password must contain at least 6 characters.')

    user = User.objects.create_user(username, email, password)
    user.save()

    authUser = authenticate(username=username, password=password)
    auth_login(request, authUser)

    return HttpResponse(str(user.id))
Exemplo n.º 2
0
def register(request):
  def valid_username_chars(name):
    return re.match(r'^[a-zA-Z0-9_\-. ]+$', name) != None

  def valid_username_spaces(name):
    return not name.startswith(' ') and not name.endswith(' ')

  username = request.POST['username']
  email    = request.POST['email']
  password = request.POST['password']

  if len(username) == 0:
    return HttpResponse('Please enter a name.')
  elif len(username) < 3:
    return HttpResponse('Name must contain at least 3 characters.')
  elif len(username) > 30:
    return HttpResponse('Name must contain 30 characters or fewer.')
  elif not valid_username_chars(username):
    return HttpResponse('Name must contain only ASCII letters, numbers, underscores, hypens, periods and spaces. Sorry about the temporary lack of Unicode support.')
  elif not valid_username_spaces(username):
    return HttpResponse('Name cannot start or end with a space.')
  elif len(User.objects.filter(username=username)) != 0:
    return HttpResponse('Name already in use.')

  if len(email) == 0:
    return HttpResponse('Please enter an email address.')
  elif not '.' in email or not '@' in email:
    return HttpResponse('Please enter a valid email address.')
  elif len(User.objects.filter(email=email)) != 0:
    return HttpResponse('Email already in use.')

  if len(password) == 0:
    return HttpResponse('Please enter a password.')
  elif len(password) < 6:
    return HttpResponse('Password must contain at least 6 characters.')

  user = User.objects.create_user(username, email, password)
  user.save()

  authUser = authenticate(username=username, password=password)
  auth_login(request, authUser)

  return HttpResponse(str(user.id))
Exemplo n.º 3
0
def login(request):
    username = request.POST['username']
    password = request.POST['password']

    if '@' in username:
        try:
            username = User.objects.get(email=username).username
        except:
            return HttpResponse('Incorrect username, email or password.')

    print 'authenticating user'
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            print 'logging user in'
            auth_login(request, user)
            print 'successfully logged in'
            return HttpResponse('success')
        else:
            return HttpResponse('Account disabled.')
    else:
        return HttpResponse('Incorrect username, email or password.')
Exemplo n.º 4
0
def login(request):
  username = request.POST['username']
  password = request.POST['password']

  if '@' in username:
    try:
      username = User.objects.get(email=username).username
    except:
      return HttpResponse('Incorrect username, email or password.')

  print 'authenticating user'
  user = authenticate(username=username, password=password)
  if user is not None:
    if user.is_active:
      print 'logging user in'
      auth_login(request, user)
      print 'successfully logged in'
      return HttpResponse('success')
    else:
      return HttpResponse('Account disabled.')
  else:
    return HttpResponse('Incorrect username, email or password.')