def signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.is_active = False user.save() current_site = get_current_site(request) subject = 'Activate Your MySite Account' message = render_to_string( 'prabhav/registration/account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) from_email = [settings.EMAIL_HOST_USER] to_email = [user.email] send_mail(subject=subject, from_email=from_email, recipient_list=to_email, message=message, fail_silently=False) return redirect('account_activation_sent') else: form = SignUpForm() return render(request, 'prabhav/registration/signup.html', {'form': form})
def signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.is_active = False user.save() current_site = get_current_site(request) message = render_to_string( 'registration/account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes( user.pk)).decode("utf-8"), 'token': account_activation_token.make_token(user), }) subject = 'Activate Your MySite Account' user.email_user(subject, message) return render(request, 'registration/account_activation_sent.html', {'user': user}) else: form = SignUpForm() return render(request, 'registration/signup.html', {'form': form})
def test_activation_email(self): u1 = User.objects.create_user(username='******',password='******') page = self.page page.live_server_url = self.live_server_url page.register_valid_details() self.assertEqual(page.get_help_blocks(), None) self.assertEqual(page.get_message_box_text(), page.confirm_email_message) uid = urlsafe_base64_encode(force_bytes(u1.pk)) token = account_activation_token.make_token(u1) response = self.client.get(reverse('registration:activate', args=[uid,token])) self.assertEqual(response.status_code, 200)
def test_activation_email(self): u1 = User.objects.create_user(username='******', password='******') page = self.page page.live_server_url = self.live_server_url page.register_valid_details() self.assertEqual(page.get_help_blocks(), None) self.assertEqual(page.get_message_box_text(), page.CONFIRM_EMAIL_MESSAGE) uid = urlsafe_base64_encode(force_bytes(u1.pk)) token = account_activation_token.make_token(u1) response = self.client.get( reverse('registration:activate', args=[uid, token])) self.assertEqual(response.status_code, 200)
def send_activation_email(req, user): current_site = get_current_site(req) message = render_to_string( 'registration/account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(str( user.pk))).decode("utf-8"), 'token': account_activation_token.make_token(user), }) send_mail('Please confirm your email address', message, settings.EMAIL_HOST_USER, [user.email]) return 'Activation link email sent to user successfully'
def signup(request): """Create a new unactivated user in the db for a potential new user.""" if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save(commit=False) # This is needed to force the user to rely on the email # confirmation. Otherwise, the user can log in without the # confirmation email. user.is_active = False # user_name = f"{user.first_name}_{user.last_name}" # Creates new user object in the database. user.save() # Load the profile instance created by the signal # Needed to save the profile details of the user. user.refresh_from_db() # Save again, to save the new changes made to the user's profile user.save() # Without this the user will not belong to any group. # Newly signed up users can only be customers. Otherwise, they have # to have an account created to them by the admin/staff manually. group = Group.objects.get(name="Inquirers") group.user_set.add(user) current_site = get_current_site(request) subject = 'Activate Your Questions & Answers Account' message = render_to_string('account_activation_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(), 'token': account_activation_token.make_token(user), }) user.email_user(subject, message) return redirect("account_activation_sent") else: form = SignUpForm() return render(request, 'signup.html', {'form': form})
def form_valid(self, form): user = form.save(commit=False) user.is_active = False user.save() current_site = get_current_site(self.request) send_email_task.delay(recipients=[user.email], subject="[Rolorex] Activate your account", template="registration/mail/activate", context={ 'user': user.username, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) return super().form_valid(form)
def form_valid(self, form): user_creation = form.save(commit=False) user_creation.is_active = False user_creation.save() self.user = User.objects.get(email=self.email) current_site = get_current_site(self.request) subject = "Activacion de cuenta en Suger" message = render_to_string('registration/account_activation_email.html', { 'user': self.user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(self.user.pk)), 'token': account_activation_token.make_token(self.user), }) send_mail( subject, message, '*****@*****.**', [self.email], fail_silently=False, ) return HttpResponseRedirect(reverse_lazy('newPerfil', kwargs={'pk': user_creation.pk}))
def post(self, request): if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") alumni_form = AlumniForm(request.POST, request.FILES, prefix="alu") if user_form.is_valid() and alumni_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_alumni.html', { 'user_form': user_form, 'alumni_form': alumni_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'country_list': self.country_list, 'city_list': self.city_list, 'region_list': self.region_list, }) try: alu_country_name = request.POST.get('current_country') alu_country = Country.objects.get(name=alu_country_name) except ObjectDoesNotExist: alu_country = None try: alu_state_name = request.POST.get('current_state') alu_state = Region.objects.get(name=alu_state_name) except ObjectDoesNotExist: alu_state = None try: alu_city_name = request.POST.get('current_city') alu_city = City.objects.get(name=alu_city_name) except ObjectDoesNotExist: alu_city = None alu_phone = request.POST.get('alu-phone_number') if alu_country and alu_phone: if not validate_phone(alu_country, alu_phone): self.phone_error = True return render( request, 'registration/signup_alumni.html', { 'user_form': user_form, 'alumni_form': alumni_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'country_list': self.country_list, 'city_list': self.city_list, 'region_list': self.region_list, }) user = user_form.save(commit=False) user.set_password(user.password) user.is_active = False user.save() alumni = alumni_form.save(commit=False) alumni.user = user alumni.country = alu_country alumni.city = alu_city alumni.state = alu_state alumni.save() print('nhi nhi yha pahooncha') current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': force_text(urlsafe_base64_encode(force_bytes( user.pk))), 'token': account_activation_token.make_token(user), }) to_email = alumni_form.cleaned_data.get('email') send_mail(mail_subject, message, settings.EMAIL_HOST_USER, [to_email]) print('reached') return render(request, 'HomePage/email_ask_confirm.html') else: print('yha pahooncha kya') return render( request, 'registration/signup_alumni.html', { 'user_form': user_form, 'alumni_form': alumni_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'country_list': self.country_list, 'city_list': self.city_list, 'region_list': self.region_list, })
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") volunteer_form = VolunteerForm( request.POST, request.FILES, prefix="vol") if user_form.is_valid() and volunteer_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) vol_country = request.POST.get('vol-country') vol_phone = request.POST.get('vol-phone_number') if (vol_country and vol_phone): if not validate_phone(vol_country, vol_phone): self.phone_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) if 'resume_file' in request.FILES: my_file = volunteer_form.cleaned_data['resume_file'] if not validate_file(my_file): return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() volunteer = volunteer_form.save(commit=False) volunteer.user = user # if an organization isn't chosen from the dropdown, # then organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer.organization = organization else: unlisted_org = request.POST.get('vol-unlisted_organization') org = Organization.objects.create(name=unlisted_org, approved_status=False) org.save() volunteer.organization = org volunteer.reminder_days = 1 volunteer.save() current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = volunteer_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return render(request, 'home/email_ask_confirm.html') else: return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def post(self, request): data = request.data print(data) username = data['username'] email = data['email'] password = data['password'] password2 = data['password1'] username_error = '' email_error = '' password_error = '' password2_error = '' error = False if username == '': error = True username_error = "Username can't be empty" if error == False: user_set = User.objects.filter(username=username) if len(user_set) > 0: error = True username_error = "Username already exists" if email == '': error = True email_error = "Email can't be empty" if password == '': error = True password_error = "Password can't be empty" if password != password2: error = True password2_error = "Passwords didn't match" if error == False: user = User.objects.create_user( username=username, email=email, ) user.set_password(password) user.is_active = False user.save() mail = email print(mail) current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes( user.pk)).decode(), 'token': account_activation_token.make_token(user), }) send_mail(mail_subject, message, '*****@*****.**', [mail]) errors = { 'username_error': username_error, 'email_error': email_error, 'password_error': password_error, 'password2_error': password2_error, 'error': error } print(errors) return Response(errors)
def post(self, request): organization_list = get_organizations_ordered_by_name() if organization_list: if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") volunteer_form = VolunteerForm(request.POST, request.FILES, prefix="vol") if user_form.is_valid() and volunteer_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': self.organization_list, }) vol_country = request.POST.get('vol-country') vol_phone = request.POST.get('vol-phone_number') if (vol_country and vol_phone): if not validate_phone(vol_country, vol_phone): self.phone_error = True return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) if 'resume_file' in request.FILES: my_file = volunteer_form.cleaned_data['resume_file'] if not validate_file(my_file): return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) user = user_form.save() user.set_password(user.password) user.save() volunteer = volunteer_form.save(commit=False) volunteer.user = user # if an organization isn't chosen from the dropdown, # then organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: volunteer.organization = organization else: unlisted_org = request.POST.get( 'vol-unlisted_organization') org = Organization.objects.create( name=unlisted_org, approved_status=False) org.save() volunteer.organization = org volunteer.reminder_days = 1 volunteer.save() current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = volunteer_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return render(request, 'home/email_ask_confirm.html') else: return render( request, 'registration/signup_volunteer.html', { 'user_form': user_form, 'volunteer_form': volunteer_form, 'registered': self.registered, 'phone_error': self.phone_error, 'organization_list': self.organization_list, }) else: return render(request, 'home/home.html', {'error': True})
def post(self, request): organization_list = get_organizations_ordered_by_name() country_list = Country.objects.all() if request.method == 'POST': user_form = UserForm(request.POST, prefix="usr") administrator_form = AdministratorForm( request.POST, prefix="admin") if user_form.is_valid() and administrator_form.is_valid(): password1 = request.POST.get('usr-password') password2 = request.POST.get('usr-confirm_password') if not match_password(password1, password2): self.match_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, }) try: admin_country_name = request.POST.get('country') admin_country = Country.objects.get(name=admin_country_name) except ObjectDoesNotExist: admin_country = None try: admin_state_name = request.POST.get('state') admin_state = Region.objects.get(name=admin_state_name) except ObjectDoesNotExist: admin_state = None try: admin_city_name = request.POST.get('city') admin_city = City.objects.get(pk=admin_city_name) except ObjectDoesNotExist: admin_city = None admin_phone = request.POST.get('admin-phone_number') if (admin_country and admin_phone): if not validate_phone(admin_country, admin_phone): self.phone_error = True return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, }) user = user_form.save() user.set_password(user.password) user.save() administrator = administrator_form.save(commit=False) administrator.user = user # if organization isn't chosen from dropdown, # the organization_id will be 0 organization_id = request.POST.get('organization_name') organization = get_organization_by_id(organization_id) if organization: administrator.organization = organization else: unlisted_org = request.POST.get('admin-unlisted_' 'organization') org = create_organization(unlisted_org) administrator.organization = org administrator.country = admin_country administrator.state = admin_state administrator.city = admin_city administrator.save() current_site = get_current_site(request) mail_subject = 'Activate your account.' message = render_to_string( 'registration/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': account_activation_token.make_token(user), }) to_email = administrator_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() return render(request, 'home/email_ask_confirm.html') else: return render( request, 'registration/signup_administrator.html', { 'user_form': user_form, 'administrator_form': administrator_form, 'registered': self.registered, 'phone_error': self.phone_error, 'match_error': self.match_error, 'organization_list': organization_list, 'country_list': self.country_list, })