def test_registration_form_valid(self): form = RegistrationForm(data={ "email":"*****@*****.**", "username": "******", "calories_plan": 999, "diet_type":"2", "password1":"test_password", "password2":"test_password" }) self.assertTrue(form.is_valid())
def register_view(request, *args, **kwargs): user = request.user if user.is_authenticated: return HttpResponse("You are already authenticated as " + str(user.email)) context = {} if request.POST: form = RegistrationForm(request.POST) if form.is_valid(): form.save() email = form.cleaned_data.get('email').lower() raw_password = form.cleaned_data.get('password1') account = authenticate(email=email, password=raw_password) login(request, account) destination = "" if destination: return redirect(destination) return redirect('home') else: context['registration_form'] = form else: form = RegistrationForm() context['registration_form'] = form return render(request, 'register.html', context)
def test_registration_form_not_valid(self): form = RegistrationForm(data={ "email":"*****@*****.**", "username": "******", "calories_plan": 999, "diet_type":"2", }) self.assertEquals(len(form.errors), 2)
def register(request): if request.method == "POST": user_form = RegistrationForm(request.POST) user_profile_from = UserProfileForm(request.POST) if user_form.is_valid(): new_user = user_form.save(False) new_user.set_password(user_form.cleaned_data["password"]) new_user.save() new_user_profile = user_profile_from.save(False) new_user_profile.user = new_user new_user_profile.save() new_user_info = UserInfo.objects.create(user=new_user) new_user_info.save() return redirect(reverse('Account:user_login')) else: return HttpResponse("Register failed!!!") elif request.method == "GET": form = RegistrationForm() user_profile_from = UserProfileForm() context = {"form": form, "user_profile_form": user_profile_from} return render(request, "Account/register.html", context)
def test_username_exist(self): form = RegistrationForm(data={ "email":self.email , "username": self.username, "calories_plan": 9, "diet_type":"2", "password1":"test_password", "password2":"test_password" }) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 2) TAG_RE = re.compile(r"<[^>]+>") self.assertEquals( TAG_RE.sub("", str(form.errors.get("email"))), f"Email {self.email} is already in use" ) self.assertEquals( TAG_RE.sub("", str(form.errors.get("username"))), f"This field cannot be null." )
def registration_view(request): context = {} if request.POST: form = RegistrationForm(request.POST) if form.is_valid(): form.save() email = form.cleaned_data.get('email') raw_password = form.cleaned_data.get('password1') account = authenticate(email=email, password=raw_password) login(request, account) return redirect('home') else: context['registration_form'] = form else: form = RegistrationForm() context['registration_form'] = form return render(request, 'account/register.html', context)