def _register_user(request): """Register a new user in the database.""" registration_form = RegistrationForm(request.POST) if registration_form.is_valid(): email_address = registration_form.cleaned_data['email_address'] new_password = get_pronounceable_pass(3, 2) try: user = User.objects.get(email=email_address) user.set_password(new_password) user.save() send_mail( subject=Notifications.CONTENT['RECOVERED_PASSWORD']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['RECOVERED_PASSWORD'] ['content'].format(new_password)), from_email=settings.EMAIL_HOST_USER, recipient_list=[email_address], ) message = 'This email address is already registered in the '\ 'database. An email containing the password has been '\ 'sent to this address.' except User.DoesNotExist: user = User.objects.create_user( username='******', # dummy username just for now email=email_address, password=new_password) # now use id as username user.username = user.id user.save() send_mail( subject=Notifications.CONTENT['USER_REGISTERED']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['USER_REGISTERED']['content'].format( email_address, new_password)), from_email=settings.EMAIL_HOST_USER, recipient_list=[email_address], ) message = 'User "{0}" has been created successfully.'.format( email_address) else: message = 'User could not be created. Please try again.' template_context = { 'auth_form': AuthenticationForm(), 'registration_form': registration_form, 'password_recovery_form': EmailBaseForm(), 'message': message } return template_context
def _register_user(request): """Register a new user in the database.""" registration_form = RegistrationForm(request.POST) if registration_form.is_valid(): email_address = registration_form.cleaned_data["email_address"] new_password = get_pronounceable_pass(3, 2) try: user = User.objects.get(email=email_address) user.set_password(new_password) user.save() send_mail( subject=Notifications.CONTENT["RECOVERED_PASSWORD"]["subject"], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT["RECOVERED_PASSWORD"]["content"].format(new_password) ), from_email=settings.EMAIL_HOST_USER, recipient_list=[email_address], ) message = ( "This email address is already registered in the " "database. An email containing the password has been " "sent to this address." ) except User.DoesNotExist: user = User.objects.create_user( username="******", email=email_address, password=new_password # dummy username just for now ) # now use id as username user.username = user.id user.save() send_mail( subject=Notifications.CONTENT["USER_REGISTERED"]["subject"], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT["USER_REGISTERED"]["content"].format(email_address, new_password) ), from_email=settings.EMAIL_HOST_USER, recipient_list=[email_address], ) message = 'User "{0}" has been created successfully.'.format(email_address) else: message = "User could not be created. Please try again." template_context = { "auth_form": AuthenticationForm(), "registration_form": registration_form, "password_recovery_form": EmailBaseForm(), "message": message, } return template_context
def _log_user_in(request): """Log a user in.""" message = '' email_address = request.POST['username'] # this is actually the email password = request.POST['password'] # a little workarounce, because we are looking up users based on email try: # lookup the correct user object pre_auth_user = User.objects.get(email=email_address) except ObjectDoesNotExist: user = None else: # authenticate with name user = authenticate(username=pre_auth_user.username, password=password) if user is not None: if user.is_active: login(request, user) else: message = 'Your account has been disabled. ' \ 'Please contact the administrator.' else: message = 'Your username and password didn\'t match. Please try again.' template_context = { 'user_req_form': UserRequestForm(), 'auth_form': AuthenticationForm(), 'registration_form': RegistrationForm(), 'password_recovery_form': EmailBaseForm(), 'message': message, } return template_context
def _recover_password(request): """Recover the password for a particular user.""" password_recovery_form = EmailBaseForm(request.POST) if password_recovery_form.is_valid(): email_address = password_recovery_form.cleaned_data['email_address'] try: user = User.objects.get(email=email_address) new_password = get_pronounceable_pass(3, 2) user.set_password(new_password) user.save() send_mail( subject=Notifications.CONTENT['RECOVERED_PASSWORD']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['RECOVERED_PASSWORD'] ['content'].format(new_password)), from_email=settings.EMAIL_HOST_USER, recipient_list=[user.email]) message = 'The password for user "{0}" has been recovered '\ 'successfully and will be sent to your ' \ 'email address.'.format(email_address) except User.DoesNotExist: message = 'The user "{0}" does not exist in the ' \ 'database.'.format(email_address) else: message = 'You have not entered a valid email address.' template_context = { 'auth_form': AuthenticationForm(), 'registration_form': RegistrationForm(), 'password_recovery_form': password_recovery_form, 'message': message } return template_context
def process_main_page_forms(request): """Process the forms on the main page.""" if request.method == 'POST': if request.POST['form-type'] == u'login-form': template_context = _log_user_in(request) elif request.POST['form-type'] == u'user-request-form': template_context = _submit_user_request(request) elif request.POST['form-type'] == u'registration-form': template_context = _register_user(request) elif request.POST['form-type'] == u'password-recovery-form': template_context = _recover_password(request) else: template_context = { 'auth_form': AuthenticationForm(), 'registration_form': RegistrationForm(), 'user_req_form': UserRequestForm(), 'password_recovery_form': EmailBaseForm() } return render(request, 'bugex_webapp/main.html', template_context)
def _register_user(request): """Register a new user in the database.""" registration_form = RegistrationForm(request.POST) if registration_form.is_valid(): email_address = registration_form.cleaned_data['email_address'] new_password = get_pronounceable_pass(3,2) try: user = User.objects.get(email=email_address) user.set_password(new_password) user.save() send_mail( subject=Notifications.CONTENT['RECOVERED_PASSWORD']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['RECOVERED_PASSWORD']['content'] .format(new_password) ), from_email=settings.EMAIL_HOST_USER, recipient_list=[email_address], ) message = 'This email address is already registered in the '\ 'database. An email containing the password has been '\ 'sent to this address.' except User.DoesNotExist: user = User.objects.create_user( username='******', # dummy username just for now email=email_address, password=new_password ) # now use id as username user.username = user.id user.save() send_mail( subject=Notifications.CONTENT['USER_REGISTERED']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['USER_REGISTERED']['content'] .format( email_address, new_password ) ), from_email=settings.EMAIL_HOST_USER, recipient_list=[email_address], ) message = 'User "{0}" has been created successfully.'.format( email_address ) else: message = 'User could not be created. Please try again.' template_context = { 'auth_form': AuthenticationForm(), 'registration_form': registration_form, 'password_recovery_form': EmailBaseForm(), 'message': message } return template_context