def register(request): if request.user.is_authenticated(): return redirect(reverse('home')) if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): new_user = User( username=form.cleaned_data['username'], email=form.cleaned_data['email'], password=make_password(form.cleaned_data['password1']), is_active=False, ) new_user.save() if SERVER_EMAIL: messages.success(request, "Your account was successfully created. An activation link has been sent to your email address.") key = User.objects.make_random_password(length=32) activation = Activation(user=new_user, key=key) activation.save() send_mail('upTee registration', 'Thank you for your registration.\r\nClick the link below to activate your account:\r\n\r\nhttp://{0}/activate/{1}'.format(request.META['HTTP_HOST'], key), SERVER_EMAIL, [new_user.email], fail_silently=not DEBUG) else: messages.success(request, "Your account was successfully created. An Admin should contact you shortly.") if ADMINS: mail_admins('User registration', u'The following user just registered and wants to be activated:\r\n\r\n{0}'.format(form.cleaned_data['username']), fail_silently=not DEBUG) return redirect(reverse('home')) else: form = RegisterForm() challenge, response = captcha_settings.get_challenge()() store = CaptchaStore.objects.create(challenge=challenge, response=response) key = store.hashkey return render_to_response('accounts/register.html', { 'captcha': key, 'register_form': form, }, context_instance=RequestContext(request))
def password_recover(request, recover_key=None): if request.user.is_authenticated() or not SERVER_EMAIL: raise Http404 if not recover_key: form = RecoverPasswordForm() if request.method == 'POST': form = RecoverPasswordForm(request.POST) if form.is_valid(): user = User.objects.get(username=form.cleaned_data['username']) messages.success(request, "An email has been sent to your email address with further instructions.") key = User.objects.make_random_password(length=32) activation = Activation(user=user, key=key) activation.save() send_mail('upTee password recover', "The password recovery function was used for your account. If you didn't do that just ignore this email.\r\n\r\nPlease click the following link to reset your password:\r\nhttp://{0}/recoverpassword/{1}\r\n\r\nAnother mail will be sent with your new password.".format(request.META['HTTP_HOST'], key), SERVER_EMAIL, [user.email], fail_silently=not DEBUG) return redirect(reverse('home')) return render_to_response('accounts/password_recover.html', { 'form': form, }, context_instance=RequestContext(request)) recovery = get_object_or_404(Activation.objects.select_related(), key=recover_key) key = User.objects.make_random_password(length=12) recovery.user.password = make_password(key) recovery.user.save() recovery.delete() messages.success(request, "An email has been sent to your email address with your new password.") send_mail('upTee password recover', "The password recovery function was used for your account.\r\n\r\nYour new password is: {0}".format(key), SERVER_EMAIL, [recovery.user.email], fail_silently=not DEBUG) return redirect(reverse('home'))
def register(request): if request.user.is_authenticated(): return redirect(reverse('home')) if request.method == 'POST': register_form = RegisterForm(request.POST) testing_form = TestingForm(request.POST) if TESTING_STATE else None if register_form.is_valid() and (not testing_form or testing_form.is_valid()): if testing_form: testing_form.save() new_user = User( username=register_form.cleaned_data['username'], email=register_form.cleaned_data['email'], password=make_password( register_form.cleaned_data['password1']), is_active=False, ) new_user.save() if SERVER_EMAIL: messages.success( request, "Your account was successfully created. An activation link has been sent to your email address." ) key = User.objects.make_random_password(length=32) activation = Activation(user=new_user, key=key) activation.save() send_mail( 'upTee registration', 'Thank you for your registration.\r\nClick the link below to activate your account:\r\n\r\nhttp://{0}/activate/{1}' .format(request.META['HTTP_HOST'], key), SERVER_EMAIL, [new_user.email], fail_silently=not DEBUG) if ADMINS: mail_admins( 'User registration', u'The following user just registered and wants to be activated:\r\n\r\n{0}' .format(register_form.cleaned_data['username']), fail_silently=not DEBUG) else: messages.success( request, "Your account was successfully created. An Admin should contact you shortly." ) return redirect(reverse('home')) else: register_form = RegisterForm() testing_form = TestingForm() if TESTING_STATE else None return render_to_response('accounts/register.html', { 'register_form': register_form, 'testing_form': testing_form, }, context_instance=RequestContext(request))
def form_valid(self, form): user = self.request.user email = form.cleaned_data['email'] if settings.ENABLE_ACTIVATION_AFTER_EMAIL_CHANGE: code = get_random_string(20) act = Activation() act.code = code act.user = user act.email = email act.save() send_activation_change_email(self.request, email, code) messages.success( self.request, f'To complete the change of email address, click on the link sent to it.' ) else: user.email = email user.save() messages.success(self.request, f'Email successfully changed.') return redirect('SuperAdmin_change_email')
def password_recover(request, recover_key=None): if request.user.is_authenticated() or not SERVER_EMAIL: raise Http404 if not recover_key: form = RecoverPasswordForm() if request.method == 'POST': form = RecoverPasswordForm(request.POST) if form.is_valid(): user = User.objects.get(username=form.cleaned_data['username']) messages.success( request, "An email has been sent to your email address with further instructions." ) key = User.objects.make_random_password(length=32) activation = Activation(user=user, key=key) activation.save() send_mail( 'upTee password recover', "The password recovery function was used for your account. If you didn't do that just ignore this email.\r\n\r\nPlease click the following link to reset your password:\r\nhttp://{0}/recoverpassword/{1}\r\n\r\nAnother mail will be sent with your new password." .format(request.META['HTTP_HOST'], key), SERVER_EMAIL, [user.email], fail_silently=not DEBUG) return redirect(reverse('home')) return render_to_response('accounts/password_recover.html', { 'form': form, }, context_instance=RequestContext(request)) recovery = get_object_or_404(Activation.objects.select_related(), key=recover_key) key = User.objects.make_random_password(length=12) recovery.user.password = make_password(key) recovery.user.save() recovery.delete() messages.success( request, "An email has been sent to your email address with your new password.") send_mail( 'upTee password recover', "The password recovery function was used for your account.\r\n\r\nYour new password is: {0}" .format(key), SERVER_EMAIL, [recovery.user.email], fail_silently=not DEBUG) return redirect(reverse('home'))
def form_valid(self, form): request = self.request user = form.save(commit=False) if settings.DISABLE_USERNAME: # Set a temporary username user.username = get_random_string() else: user.username = form.cleaned_data['username'] if settings.ENABLE_USER_ACTIVATION: user.is_active = False # Create a user record user.save() # Change the username to the "user_ID" form if settings.DISABLE_USERNAME: user.username = f'user_{user.id}' user.save() if settings.ENABLE_USER_ACTIVATION: code = get_random_string(20) act = Activation() act.code = code act.user = user act.save() # activation user = act.user user.is_active = True user.save() # Remove the activation record act.delete() messages.success(request, _('You have successfully activated your account!')) # send_activation_email(request, user.email, code) else: raw_password = form.cleaned_data['password1'] user = authenticate(username=user.username, password=raw_password) login(request, user) messages.success(request, _('You are successfully signed up!')) return redirect('index')
def form_valid(self, form): request = self.request user = form.save(commit=False) if settings.DISABLE_USERNAME: # Set a temporary username user.username = get_random_string() else: user.username = form.cleaned_data['username'] if settings.ENABLE_USER_ACTIVATION: user.is_active = False # Create a user record user.owner = request.user user.save() # Change the username to the "user_ID" form if settings.DISABLE_USERNAME: user.username = f'user_{user.id}' user.save() if settings.ENABLE_USER_ACTIVATION: code = get_random_string(20) act = Activation() act.code = code act.user = user act.save() send_activation_email(request, user.email, code) messages.success( request, f'You are signed up. To activate the account, follow the link sent to the mail.' ) else: raw_password = form.cleaned_data['password1'] user = authenticate(username=user.username, password=raw_password) login(request, user) messages.success(request, f'You are successfully signed up!') return redirect('student_list')