def shipping(request): """ Renders checkout shipping page with navbar and footer removed to fit with conventions of online shops. If user not logged in getting to this stage is blocked, and user is asked to login. """ customer = None if request.user.is_authenticated(): customer = Customer.objects.filter(user=request.user).first() else: messages.error(request, "Need to be logged in to do checkout") return redirect(reverse("login")) if request.method == "POST": form = CustomerForm(request.POST, instance=customer) if form.is_valid(): customer = form.save(commit=False) customer.user = request.user customer.save() return redirect("checkout") else: form = CustomerForm(instance=customer) return render( request, "shipping.html", { "customer": customer, "form": form, "disable_header": True, "disable_footer": True, }, )
def user_profile(request): """ Renders profile page for user with a form to update their information. When posted creates a new customer. """ customer = Customer.objects.filter(user=request.user).first() if request.method == "POST": form = CustomerForm(request.POST, instance=customer) if form.is_valid(): customer = form.save(commit=False) customer.user = request.user customer.save() messages.success(request, "You have updated your profile") else: form = CustomerForm(instance=customer) has_order = False if customer: has_order = Order.objects.filter(customer=customer).exists() return render( request, "profile.html", { "user": request.user, "form": form, "has_order": has_order }, )
def shipping(request): """ Renders checkout shipping page with navbar and footer ifnot logged in getting user is prompted to login. """ customer = None if request.user.is_authenticated(): customer = Customer.objects.filter(user=request.user).first() else: messages.error(request, "Need to be logged in to do checkout") return redirect(reverse("login")) if request.method == "POST": form = CustomerForm(request.POST, instance=customer) if form.is_valid(): customer = form.save(commit=False) customer.user = request.user customer.save() return redirect("checkout") else: form = CustomerForm(instance=customer) return render( request, "shipping.html", { "customer": customer, "form": form, }, )
def accountSettings(r): customer=r.user.customer form=CustomerForm(instance=customer) if r.method=="POST": form=CustomerForm(r.POST,r.FILES,instance=customer) if form.is_valid(): form.save() return render(r,"accounts/account_settings.html",{'form':form})
def account_setting(request): customer = request.user.customers form = CustomerForm(instance=customer) if request.method == "POST": form = CustomerForm(request.POST, request.FILES, instance=customer) if form.is_valid(): form.save() return render(request, 'crmaccounts/account_setting.html', {'form': form})
def account_settings(request): customer = request.user.customer form = CustomerForm(instance=customer) if request.method == 'POST': form = CustomerForm(request.POST, request.FILES, instance=customer) if form.is_valid(): form.save() context = {'form': form} return render(request, 'accounts/account_settings.html', context)
def create_customer(request): if request.method == 'POST': form = CustomerForm(request.POST) if form.is_valid(): form.save() return redirect('/') #redirect to dashboard else: form = CustomerForm() return render(request, 'accounts/customer_form.html', {'form':form})
def account_settings(request): # customer = Customer.objects.get(id=pk) customer = request.user.customer form = CustomerForm(instance=customer) if request.method == "POST": form = CustomerForm(request.POST, request.FILES, instance=customer) if form.is_valid(): form.save() context = {"form": form} return render(request, "accounts/account_settings.html", context=context)
def create_customer(request): form = CustomerForm() if request.method == 'POST': form = CustomerForm(request.POST) if form.is_valid(): form.save() return redirect('/') context = { 'form': form} return render(request, 'accounts/create_customer.html', context)
def update_customer(request, pk): customer = Customers.objects.get(id=pk) form = CustomerForm(instance=customer) if request.method == "POST": form = CustomerForm(request.POST, instance=customer) if form.is_valid(): form.save() return redirect('/') else: context = {'form': form} return render(request, 'crmaccounts/update_customer.html', context)
def createcustomer(request): form = CustomerForm() if request.method == 'POST': #same save function as above form = CustomerForm(request.POST) #again saves that specific instance instead of making a new one. if form.is_valid(): form.save() return redirect('/') context = { 'form': form, } return render(request, 'accounts/customerform.html', context)
def user_account(request): customer = request.user.customer if request.method == 'POST': form = CustomerForm(request.POST, request.FILES, instance=customer) if form.is_valid(): form.save() return redirect('/user') else: form = CustomerForm(instance=customer) context = {'form': form} return render(request, 'users/userAccount.html', context)
def customer_settings(request): customer = request.user.customer form = CustomerForm(instance=customer) if request.method == 'POST': form = CustomerForm(request.POST, request.FILES, instance=customer) if form.is_valid(): form.save() messages.success(request, "Profile Updated Successfully") else: messages.error(request, 'There is error in form submission') context = {'form': form} return render(request, 'accounts/customer_settings.html', context)
def updateCustomer(request, pk): customer = Customer.objects.get(id=pk) form = CustomerForm(instance=customer) if request.method == 'POST': form = CustomerForm(request.POST, instance=customer) if form.is_valid(): form.save() logger.info("customer info changed by admin") return redirect('customer', pk) context = {'form': form} return render(request, 'accounts/update_customer.html', context)
def updateCustomer(request, pk): customer = Customer.objects.get(id=pk) form = CustomerForm(instance=customer) if request.method == 'POST': form = CustomerForm(request.POST, instance=customer) if form.is_valid(): form.save() return redirect('/') context = {'form': form} return render(request, 'customer/customer_update.html', context)
def get(self, request, *args, **kwargs): self.object = None form_class = self.get_form_class() user_form = self.get_form(form_class) cust_form = CustomerForm() return self.render_to_response( self.get_context_data(form1=user_form, form2=cust_form))
def updatecustomer(request, pk): customer = Customer.objects.get(id=pk) form = CustomerForm(instance = customer) if request.method == 'POST': #same save function as above form = CustomerForm(request.POST, instance= customer) #again saves that specific instance instead of making a new one. if form.is_valid(): form.save() return redirect('/') context = { 'form': form, 'customer': customer } return render(request, 'accounts/customerform.html', context)
def test_required_fields(self): form = CustomerForm({ 'first_name': '', 'last_name': '', 'street_address': '', 'country': '', 'city': '', 'postal': '', 'email': '' }) self.assertFalse(form.is_valid()) self.assertIn('first_name', form.errors.keys()) self.assertIn('last_name', form.errors.keys()) self.assertIn('street_address', form.errors.keys()) self.assertIn('country', form.errors.keys()) self.assertIn('city', form.errors.keys()) self.assertIn('postal', form.errors.keys()) self.assertIn('email', form.errors.keys()) self.assertEqual(form.errors['first_name'][0], 'This field is required.') self.assertEqual(form.errors['last_name'][0], 'This field is required.') self.assertEqual(form.errors['street_address'][0], 'This field is required.') self.assertEqual(form.errors['country'][0], 'This field is required.') self.assertEqual(form.errors['city'][0], 'This field is required.') self.assertEqual(form.errors['postal'][0], 'This field is required.') self.assertEqual(form.errors['email'][0], 'This field is required.')
def post(self, request, *args, **kwargs): self.object = None form_class = self.get_form_class() user_form = self.get_form(form_class) cust_form = CustomerForm(self.request.POST, self.request.FILES) if (user_form.is_valid() and cust_form.is_valid()): return self.form_valid(user_form, cust_form) else: return self.form_invalid(user_form, cust_form)
def SignUp2(request): if request.method == 'POST': form1 = CustomerForm(request.POST) form2 = UserForm(request.POST) if all([form1.is_valid(), form2.is_valid()]): customer = form1.save() user = form2.save() success_url = reverse_lazy('login') return HttpResponseRedirect(success_url) else: form1 = CustomerForm() form2 = UserForm() return render(request, 'signup.html', { 'customer_form': form1, 'user_form': form2, })
def update(self, request, msisdn): try: customer = Customer.objects.get(msisdn=msisdn) except Customer.DoesNotExist: return rc.NOT_FOUND form = CustomerForm(data=request.data, instance=customer) if not form.is_valid(): raise FormValidationError(form) return form.save()
def order_confirmation(request): customer = Customer.objects.filter(user=request.user).first() order_history = request.session.get('order_history') product = request.session.get("product_name") quantity = request.session.get("quantity") form = CustomerForm(instance=customer) return render( request, "order_confirmation.html", { "order_history": order_history, "product": product, "quantity": quantity, "form": form, }, )
def save_info_settings(self): info = CustomerForm(self.request.POST, instance=self.customer) if info.is_valid(): self.save_customer_info_data(info)
def test_company_name_is_required(self): form = CustomerForm({'company_name': ''}) self.assertFalse(form.is_valid()) self.assertIn('company_name', form.errors.keys()) self.assertEqual(form.errors['company_name'][0], 'This field is required.')