def post(self, request): if request.session.get('user_id'): return redirect('home') form = CustomerForm(request.POST) if form.is_valid(): customer = form.save(commit=False) customer.password = make_password(customer.password) customer.save() messages.success(request, f'Account Created') return redirect('customer-login') return render(request, 'account/signup.html', {'form': form})
def test_CustomerForm_valid_data(self): form= CustomerForm(data={ 'name':'hari', 'email':'*****@*****.**', 'phone':'0778877953 ', 'profile_pic':'' }) response =self.client.post(reverse('accountSettings'),{'form':{ 'name':'hari', 'email':'*****@*****.**', 'phone':'0778877953 ', 'profile_pic':'' } }) self.assertEquals(response.status_code,302) self.assertTrue(form.is_valid())
def index(request): if request.method == "POST": form = CustomerForm(request.POST) if form.is_valid(): try: form.save() return redirect("sample") except: pass else: form = CustomerForm() return render(request, 'index.html', {'form': form})
def customer_profile_view(request): if request.method == 'POST': user_form = UserForm(request.POST, prefix='UF') profile_form = CustomerForm(request.POST, prefix='PF') if user_form.is_valid() and profile_form.is_valid( ) and user_form.cleaned_data['password1'] == user_form.cleaned_data[ 'password2'] and len(user_form.cleaned_data['password1']) > 6: user = user_form.save(commit=False) user.set_password(user_form.cleaned_data['password1']) user.is_active = True user.is_customer = True user.save() user.customer.address = profile_form.cleaned_data.get('address') user.customer.city = profile_form.cleaned_data.get('city') user.customer.phonenumber = profile_form.cleaned_data.get( 'phonenumber') user.customer.save() cart = Cart(user=user) cart.save() messages.success(request, 'Uzytkownik stworzony') return redirect('login') elif user_form.cleaned_data['password1'] != user_form.cleaned_data[ 'password2']: user_form.add_error('password2', 'Hasła nie są takie same') elif len(user_form.cleaned_data['password1']) <= 6: user_form.add_error('password1', 'Hasło za krótkie') else: user_form = UserForm(prefix='UF') profile_form = CustomerForm(prefix='PF') return render(request, 'account/register.html', { 'user_form': user_form, 'profile_form': profile_form, })
def customer_profile_view(request): if request.method == 'POST': user_form = UserForm(request.POST, prefix='UF') profile_form = CustomerForm(request.POST, prefix='PF') if user_form.is_valid() and profile_form.is_valid( ) and user_form.cleaned_data['password1'] == user_form.cleaned_data[ 'password2'] and len(user_form.cleaned_data['password1']) > 5: user = user_form.save(commit=False) user.set_password(user_form.cleaned_data['password1']) user.is_active = True user.is_customer = True user.save() user.customer.birth_date = profile_form.cleaned_data.get( 'birth_date') if user.customer.over_eighteen(): user.customer.save() cart = Cart(user=user) cart.save() messages.success(request, 'Uzytkownik stworzony') return redirect('home') else: user.delete() profile_form.add_error('birth_date', 'Nie masz 18 lat') elif user_form.cleaned_data['password1'] != user_form.cleaned_data[ 'password2']: user_form.add_error('password2', 'Hasła nie są takie same') elif len(user_form.cleaned_data['password1']) < 6: user_form.add_error('password1', 'Hasło za krótkie') else: user_form = UserForm(prefix='UF') profile_form = CustomerForm(prefix='PF') return render(request, 'account/register.html', { 'user_form': user_form, 'profile_form': profile_form, })
def get(self, request): if request.session.get('user_id'): return redirect('home') form = CustomerForm() return render(request, 'account/signup.html', {'form': form})
def test_registration_form_customer_nodata(self): form=CustomerForm(data={}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors),2)
def test_registration_form_customer_data(self): form=CustomerForm(data={'nickname':'przykladowa','birth_date':'1990-10-10'}) self.assertTrue(form.is_valid())