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 _change_password(request): """Change a user's password.""" try: new_password = get_pronounceable_pass(3, 2) request.user.set_password(new_password) request.user.save() send_mail( subject=Notifications.CONTENT['CHANGED_PASSWORD']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['CHANGED_PASSWORD']['content'].format( new_password)), from_email=settings.EMAIL_HOST_USER, recipient_list=[request.user.email], ) message = 'Your new password has been successfully set.' except: message = 'Failure: Your new password could not be created.' template_context = { 'change_email_form': ChangeEmailForm(), '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 _change_password(request): """Change a user's password.""" try: new_password = get_pronounceable_pass(3,2) request.user.set_password(new_password) request.user.save() send_mail( subject=Notifications.CONTENT['CHANGED_PASSWORD']['subject'], message=Notifications.HEADER_FOOTER.format( Notifications.CONTENT['CHANGED_PASSWORD']['content'] .format(new_password) ), from_email=settings.EMAIL_HOST_USER, recipient_list=[request.user.email], ) message = 'Your new password has been successfully set.' except: message = 'Failure: Your new password could not be created.' template_context = { 'change_email_form': ChangeEmailForm(), '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 _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 _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 _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