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 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, })