def addCustomer(): form = AddCustomerForm() form.recommender.choices = [(0, '')] poss_customers = Customer.query\ .filter(Customer.customer_type == CUSTOMER_TYPES['TYPE_CUSTOMER']).all() for cust in poss_customers: form.recommender.choices.append((cust.id, cust.name)) if form.validate_on_submit(): customer = Customer() customer.name = form.name.data customer.email = form.email.data customer.base_discount = int(form.base_discount.data)/100.0 if form.recommender.data and form.recommender.data > 0: customer.recommender_id = form.recommender.data customer.company_name = form.company_name.data if form.post_code1.data and form.post_code2.data: customer.post_code = int(str(form.post_code1.data) + str(form.post_code2.data)) else: customer.post_code = None customer.address1 = form.address1.data customer.address2 = form.address2.data customer.address3 = form.address3.data db.session.add(customer) db.session.commit() flash(gettext("New customer successfully added.")) return redirect(url_for("customers")) return render_template('settings/addCustomer.html', title=gettext("Add New Customer"), form=form)
def post(self, request): form = AddCustomerForm(request.POST) if form.is_valid(): newcustomername = form.cleaned_data['New_Customer_Name'] newcustomernumber = form.cleaned_data['New_Customer_Number'] if SortedCustomerList.objects.filter( Sorted_Customer_Number=newcustomernumber).exists(): print 'Error. Previously entered Customer Number entered.' else: sortedcustomer = SortedCustomerList( Sorted_Customer_Name=newcustomername, Sorted_Customer_Number=newcustomernumber) sortedcustomer.save() form = AddCustomerForm() return render(request, self.template_name, {'form': form})
def add_customer(): form = AddCustomerForm(request.form) if form.validate() and request.method == 'POST': success = add_customer_controller(form) if success: flash("Added new customer to database.") return redirect(url_for('add_customer')) else: flash( "Failed to add new customer to database. Is the username unique?" ) return render_template('/customerreptasks/addcustomer.html', form=form) print("not implemented") else: form = AddCustomerForm() return render_template('/customerreptasks/addcustomer.html', form=form)
def editCustomer(id=0): customer = Customer.query.filter_by(id=id).first() if customer == None: flash(gettext('Customer not found.')) return redirect(url_for('customers')) form = AddCustomerForm(obj=customer) form.recommender.choices = [(0, '')] poss_customers = Customer.query\ .filter(Customer.customer_type == CUSTOMER_TYPES['TYPE_CUSTOMER'])\ .filter(Customer.id != customer.id).all() for cust in poss_customers: form.recommender.choices.append((cust.id, cust.name)) if form.is_submitted(): #delete customer if 'delete' in request.form: #db.session.delete(customer) #db.session.commit() flash(gettext('This operation is not allowed at the moment.')) return redirect(url_for("customers")) if form.validate(): #update customer customer.name = form.name.data customer.email = form.email.data customer.base_discount = int(form.base_discount.data)/100.0 nohinsho_letter = form.next_nohinsho_letter.data if nohinsho_letter and len(nohinsho_letter) == 1 and re.match("^[A-Z]+$", nohinsho_letter): customer.order_no = ord(nohinsho_letter) - 65 if form.recommender.data and form.recommender.data > 0: customer.recommender_id = form.recommender.data else: customer.recommender_id = None customer.company_name = form.company_name.data if form.post_code1.data and form.post_code2.data: customer.post_code = int(str(form.post_code1.data) + str(form.post_code2.data)) else: customer.post_code = None customer.address1 = form.address1.data customer.address2 = form.address2.data customer.address3 = form.address3.data db.session.add(customer) db.session.commit() flash(gettext("Customer successfully changed.")) return redirect(url_for("customers")) form.base_discount.data = int(customer.base_discount*100) if customer.base_discount else 0 if customer.order_no: form.next_nohinsho_letter.data = chr(customer.order_no + 65) else: form.next_nohinsho_letter.data = 'A' selected = customer.recommender_id if customer.recommender_id else 0 if customer.post_code: post_code = str(customer.post_code) else: post_code = '' form.post_code1.data = post_code[:3] form.post_code2.data = post_code[3:] return render_template('settings/editCustomer.html', title=gettext("Edit Customer"), customer=customer, selected=selected, form=form)
def get(self, request): form = AddCustomerForm() return render(request, self.template_name, { 'form': form, })
def customer(action): """ 用户列表 """ if action == 'add': form = AddCustomerForm() if form.validate_on_submit(): name = form.data['name'] sex = form.data['sex'] phone = form.data['phone'] email = form.data['email'] company = form.data['company'] addr = form.data['addr'] note = form.data['note'] tax_id = form.data['tax_id'] CustomerCtl.add(name, sex, phone, email, company, addr, note, tax_id, current_user.id) flash(u'添加成功', 'success') return redirect(url_for('Common.customer', action='list')) return render_template('customer_add.html', form=form) if action == 'edit': form = EditCustomerForm() if form.validate_on_submit(): uid = form.data['uid'] name = form.data['name'] sex = form.data['sex'] phone = form.data['phone'] email = form.data['email'] company = form.data['company'] addr = form.data['addr'] note = form.data['note'] tax_id = form.data['tax_id'] customer = CustomerCtl.update(uid, name, sex, phone, email, company, addr, note, tax_id, current_user.id) if customer: flash(u'编辑成功', 'success') else: flash(u'编辑失败', 'error') return redirect(url_for('Common.customer', action='list')) uid = request.args.get('uid', '0') customer = CustomerCtl.get_by_id(uid) if customer: form.uid.data = customer.id form.name.data = customer.name form.sex.data = customer.sex form.phone.data = customer.phone form.email.data = customer.email form.company.data = customer.company form.addr.data = customer.addr form.note.data = customer.note form.tax_id.data = customer.tax_id else: flash(u'找不到该客户', 'error') return redirect(url_for('Common.customer', action='list')) return render_template('customer_edit.html', form=form) if action == 'list': result = CustomerCtl.get_all() owner_dict = {} for i in result: try: owner_dict[i.owner] = AdminCtl.get(i.owner).name except: # 如果用户被删除,则AdminCtl.get可能引发异常 pass kwargs = { 'result': result, 'STATUS_LIST': settings.CUSTOMER_STATUS_DICT, 'owner_dict': owner_dict } return render_template("customer.html", **kwargs) return render_template("404.html")