def test_signup_pass(self): form = SignUpForm( data={ 'username': self.USERNAME, 'password1': self.PASSWORD, 'password2': self.PASSWORD }) self.assertTrue(form.is_valid())
def test_signup_password_fail(self): """ password fail """ form = SignUpForm( data={ 'username': self.USERNAME, 'password1': self.PASSWORD, 'password2': self.NEW_PASS, }) self.assertFalse(form.is_valid())
def test_signup_email_fail_2(self): """ Email already exists! """ form = SignUpForm( data={ 'username': self.owner_one.email, 'password1': self.PASSWORD, 'password2': self.PASSWORD }) self.assertFalse(form.is_valid())
def test_signup_email_fail(self): """ Email and Email as username already exists! """ form = SignUpForm( data={ 'username': self.test_user, 'password1': self.PASSWORD, 'password2': self.PASSWORD }) self.assertFalse(form.is_valid())
def late_registration(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): user = form.save() user.refresh_from_db( ) # load the profile instance created by the signal user.member.preferred_name = form.cleaned_data.get( 'preferred_name') user.member.first_name = form.cleaned_data.get('first_name') user.member.last_name = form.cleaned_data.get('last_name') user.member.email = form.cleaned_data.get('email') user.member.classification = form.cleaned_data.get( 'classification') user.member.major = form.cleaned_data.get('major') username = form.cleaned_data.get('username') user.save() raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return redirect('members:member_home') else: form = SignUpForm() return render(request, 'members/signup_form.html', {'form': form})
def signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): # Create user user = form.save(commit=False) user.is_active = False user.password = make_password(form.cleaned_data['password']) user.save() # Send email of new user current_site = get_current_site(request) mail_subject = 'A new account has been created and required your attention.' print("Generated token: ", account_activation_token.make_token(user)) message = render_to_string( '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), }) to_email = os.environ.get('EMAIL_HOST_RECEIVER') email = EmailMessage(mail_subject, message, to=[to_email]) logger.info(f"NEW USER: Sending email to {to_email}") email.send() return render(request, 'registration/registration_complete.html') else: form = SignUpForm() return render(request, 'signup.html', {'form': form})
def form_valid(self, form: SignUpForm) -> HttpResponseRedirect: self.object = form.save() send_activate_email(self.request, self.object) return super().form_valid(form)
def facebook_login(request): client_app_id = settings.FACEBOOK_APP_ID client_secret = settings.FACEBOOK_SECRET_CODE url_access_token = 'https://graph.facebook.com/v2.12/oauth/access_token' url_me = 'https://graph.facebook.com/v2.12/me' code = request.GET['code'] redirect_uri = 'https://zirmi.net/facebook-login/' params_access_token = { 'client_id': client_app_id, 'redirect_uri': redirect_uri, 'client_secret': client_secret, 'code': code, } response = requests.get(url_access_token, params_access_token) response_dict = response.json() params = { 'access_token': response_dict['access_token'], 'fields': ','.join([ 'id', 'name', 'email', 'age_range', 'gender', ]) } response = requests.get(url_me, params) response_dict = response.json() facebook_id = response_dict.get('id', None) facebook_email = response_dict.get('email', None) try: facebook_age_range = response_dict['age_range']['min'] except KeyError: facebook_generation = '' else: if facebook_age_range < 12: facebook_generation = '1' elif facebook_age_range < 22: facebook_generation = '2' elif facebook_age_range < 32: facebook_generation = '3' elif facebook_age_range < 42: facebook_generation = '4' else: facebook_generation = '5' try: facebook_gender = response_dict['gender'] except KeyError: facebook_gender = '' else: if facebook_gender == 'male': facebook_gender = 'm' elif facebook_gender == 'femal': facebook_gender = 'f' try: user = User.objects.get(email=facebook_email) except User.DoesNotExist: form = SignUpForm() context = { 'signup_form': form, 'facebook_id': facebook_id, 'facebook_email': facebook_email, 'facebook_generation': facebook_generation, 'facebook_gender': facebook_gender, } return render(request, 'members/facebook_signup.html', context) else: login(request, user, backend='members.backends.FacebookBackend') return redirect('index')