def register(request): # If the user is already logged in, go to the dashboard page if request.user.is_authenticated(): return index(request) else: if request.POST and 'register' in request.POST: # Make sure everything checks out ... errors = [] username = request.POST.get('username') email = request.POST.get('email', '') password = request.POST.get('password') password_confirm = request.POST.get('password_confirm') university = request.POST.get('university', '').lower() # Now check all the possible errors if not university.startswith('mcgill'): errors.append("Anti-spam question wrong! Please enter the name of the university WikiNotes was made for.") if not username: errors.append("You didn't fill in your username!") if not password: errors.append("Please enter a password.") if len(password) < 6: errors.append("Your password is too short. Please keep it to at least 6 characters.") if password_confirm != password: errors.append("Passwords don't match!") # First check if the username is valid (might migrate to using the form from django.contrib.auth later) # Only allow alphanumeric chars for now, can change later if username and not validate_username(username): errors.append("Please only use alphanumeric characters and the underscore for your username.") # Now check if the username (any case combination) is already being used if User.objects.filter(username__iexact=username).count() > 0: errors.append("This username is already in use! Please find a new one.") data = { 'title': 'Create an account (errors)', 'errors': errors, 'username': username, 'email': email, 'password': password, 'university': university # so if there's an unrelated error user doesn't have to enter it again } if errors: return render(request, 'main/registration.html', data) else: # If the registration proceeded without errors # Create the user, then log the user in User.objects.create_user(username, email, password) new_user = authenticate(username=username, password=password) login(request, new_user) return index(request, show_welcome=True) else: return render(request, 'main/registration.html', {'title': 'Create an account'})
def register(request): # If the user is already logged in, go to the dashboard page if request.user.is_authenticated(): return index(request) else: if request.POST and 'register' in request.POST: # Make sure everything checks out ... errors = [] username = request.POST.get('username') email = request.POST.get('email', '') password = request.POST.get('password') password_confirm = request.POST.get('password_confirm') university = request.POST.get('university', '').lower() # Now check all the possible errors if not university.startswith(settings.UNIVERSITY_NAME): errors.append("Anti-spam question wrong.") if not username: errors.append("You didn't fill in your username!") if not password: errors.append("Please enter a password.") if len(password) < 6: errors.append( "Your password is too short. Please keep it to at least 6 characters." ) if password_confirm != password: errors.append("Passwords don't match!") # First check if the username is valid (might migrate to using the form from django.contrib.auth later) # Only allow alphanumeric chars for now, can change later if username and not validate_username(username): errors.append( "Please only use alphanumeric characters and the underscore for your username." ) # Now check if the username (any case combination) is already being used if User.objects.filter(username__iexact=username).count() > 0: errors.append( "This username is already in use! Please find a new one.") data = { 'title': 'Create an account (errors)', 'errors': errors, 'username': username, 'email': email, 'password': password, 'university': university # so if there's an unrelated error user doesn't have to enter it again } if errors: return render(request, 'main/registration.html', data) else: # If the registration proceeded without errors # Create the user, then log the user in User.objects.create_user(username, email, password) new_user = authenticate(username=username, password=password) login(request, new_user) return redirect('home') else: return render(request, 'main/registration.html', {'title': 'Create an account'})