def put(self, _id): data = self.parser.parse_args() user = CustomerModel.find_by_id(_id) if user: user.first_name = data["first_name"] # if data["first_name"] else user.first_name user.middle_name = data["middle_name"] user.last_name = data["last_name"] user.date_of_birth = date(*data["date_of_birth"]) user.gender = data["gender"] user.email = data["email"] user.phone_No_1 = data["phone_No_1"] user.phone_No_2 = data["phone_No_2"] user.phone_No_3 = data["phone_No_3"] user.address = data["address"] user.social_status = data["social_status"] user.national_id = data["national_id"] user.has_smartphone = data["has_smartphone"] user.has_computer = data["has_computer"] user.has_internet = data["has_internet"] else: valid_data = {key: val for key, val in data.items() if val is not None} user = CustomerModel(**valid_data) try: user.save_to_db() except: return {"message": "An error occurred while saving to the database."}, 500 return user.json()
def post(self, related_user_id): data = Customer.parser.parse_args() theCustomer = CustomerModel( data['credit_limit_balance'], EducationType(data['education']), MaritalStatusType(data['marriage']), data['age'], data['repayment_status_month_1'], data['repayment_status_month_2'], data['repayment_status_month_3'], data['repayment_status_month_4'], data['repayment_status_month_5'], data['repayment_status_month_6'], data['bill_amount_month_1'], data['bill_amount_month_2'], data['bill_amount_month_3'], data['bill_amount_month_4'], data['bill_amount_month_5'], data['bill_amount_month_6'], data['payment_amount_month_1'], data['payment_amount_month_2'], data['payment_amount_month_3'], data['payment_amount_month_4'], data['payment_amount_month_5'], data['payment_amount_month_6'], related_user_id, data['customer_result_id']) # OR USE: theCustomer = CustomerModel(related_user_id, **data) # **kwargs is a dictionary (key word args) try: theCustomer.save_to_db() except: return {"message": "An error occurred inserting the item"}, 500 return theCustomer.json(), 201
def customer_reg(): if request.method == 'POST': try: name = request.form['name'] email = request.form['email'] phone = request.form['phone'] password = request.form['password'] confirmpass = request.form['confirmpass'] if password != confirmpass: flash('Passwords dont match!', 'danger') return redirect(url_for('customer_reg')) elif (CustomerModel.check_email_exist(email)): flash('Email already in use', 'danger') return redirect(url_for('customer_reg')) else: # protect the password by harshing it hashedpass = bcrypt.generate_password_hash(password, 10).decode('utf-8') # add the customer to the database customer = CustomerModel(name=name, email=email, phone_number=phone, password=hashedpass) customer.insert_record() flash( 'Your account has been successfully created.Please Login', 'success') return redirect(url_for('customer_login')) except Exception as e: print(e) return render_template('customerreg.html')
def post(self): data = CustomerRegister.parser.parse_args() print('data', data['address']) if UserModel.find_by_username(data['username']): return {'message': 'username already exists'}, 400 if AgentModel.find_by_agent_id(data['agent_id']) is None: return {'message': 'Invalid agent Id'}, 400 user = UserModel(data['username'], data['password'], usertype='customer') user.save_to_db() agent = AgentModel.find_by_agent_id(data['agent_id']) d, m, y = data['dob'].split('-') converted_dob = datetime(int(y), int(m), int(d)) customer = CustomerModel(customer_id=user.id, agent=agent, name=data['name'], dob=converted_dob, email=data['email'], address=data['address'], contact_num=data['contact_num'], alternate_num=data['alternate_num']) print('got it', customer.json()) customer.save_to_db() return {'message': 'customer Registered Successfully'}, 201
def put(self): data = Customer.parser.parse_args() customer = CustomerModel.find_by_full_name(data["f_name"], data["l_name"]) if customer: customer.email = data["email"] customer.active = data["active"] customer.created_at = data["created_at"] else: customer = CustomerModel(**data) customer.save_to_db() return customer.json()
def doRegister(self, request): """买家注册""" self.response_["type"] = BaseView.RESPONSE_TYPE_JSON customerKeys = ("name", "password", "mobile", "email") customer = {} for key in customerKeys: customer[key] = request.POST.get(key) # 正则验证数据 regex = Regex() if regex.validateUsername(customer["name"]) is False: self.context = { "code": 403, "msg": "用户名须为6-30位字母数字字符组成", "data": {} } elif regex.validatePassword(customer["password"]) is False: self.context = { "codd": 404, "msg": "密码须位8-30位字母数字字符组成", "data": {} } elif regex.validateMobile(customer["mobile"]) is False: self.context = {"codd": 405, "msg": "手机格式验证错误", "data": {}} elif regex.validateEmail(customer["email"]) is False: self.context = {"codd": 406, "msg": "邮箱格式验证错误", "data": {}} else: # 数据库查重 if CustomerModel.objects.filter(name=customer["name"]).exists(): self.context = {"code": 407, "msg": "用户名已存在", "data": {}} elif CustomerModel.objects.filter( mobile=customer["mobile"]).exists(): self.context = {"code": 408, "msg": "手机已存在", "data": {}} elif CustomerModel.objects.filter( email=customer["email"]).exists(): self.context = {"code": 409, "msg": "邮箱已存在", "data": {}} else: # 插入新数据 customer = CustomerModel( name=customer["name"], password=hashlib.sha512( customer["password"].encode("utf-8")).hexdigest(), mobile=customer["mobile"], email=customer["email"]) customer.save() self.context = { "code": 200, "msg": "注册成功", "data": { "id": customer.id } }
def put(self, tax_id): data = Customer.parser.parse_args() customer = CustomerModel.find_by_taxid(tax_id) if customer: customer.debtor_value = data['debtor_value'] customer.customer_defaulter = data['customer_defaulter'] else: customer = CustomerModel(tax_id, data['debtor_value']) customer.save_to_db() return customer.json()
def post(self): data = self.parser.parse_args() user = CustomerModel.find_by_email(data["email"]) if user: return {'message': "The email {} already exists.".format(data["email"])}, 400 valid_data = {key: val for key, val in data.items() if val is not None} user = CustomerModel(**valid_data) try: user.save_to_db() except: return {"message": "An error occurred while saving to the database."}, 500 return user.json(), 201
def mutate(root, info, campaign_id, customer_data): campaign = CampaignModel.find_by_id(campaign_id) if not campaign: raise Exception("Campaign not found!") company = campaign.company.fetch() if not company: raise Exception("Company not found!") customer = CustomerModel(**customer_data, source=campaign, company=company) customer.save() return AddCustomer(customer=customer)
def put(self, related_user_id): data = Customer.parser.parse_args() theCustomer = CustomerModel.find_recent_by_related_user_id( related_user_id) if theCustomer: # update if exists theCustomer.update_customer( data['credit_limit_balance'], EducationType(data['education']), MaritalStatusType(data['marriage']), data['age'], data['repayment_status_month_1'], data['repayment_status_month_2'], data['repayment_status_month_3'], data['repayment_status_month_4'], data['repayment_status_month_5'], data['repayment_status_month_6'], data['bill_amount_month_1'], data['bill_amount_month_2'], data['bill_amount_month_3'], data['bill_amount_month_4'], data['bill_amount_month_5'], data['bill_amount_month_6'], data['payment_amount_month_1'], data['payment_amount_month_2'], data['payment_amount_month_3'], data['payment_amount_month_4'], data['payment_amount_month_5'], data['payment_amount_month_6'], related_user_id, data['customer_result_id']) else: # create if dne theCustomer = CustomerModel( data['credit_limit_balance'], EducationType(data['education']), MaritalStatusType(data['marriage']), data['age'], data['repayment_status_month_1'], data['repayment_status_month_2'], data['repayment_status_month_3'], data['repayment_status_month_4'], data['repayment_status_month_5'], data['repayment_status_month_6'], data['bill_amount_month_1'], data['bill_amount_month_2'], data['bill_amount_month_3'], data['bill_amount_month_4'], data['bill_amount_month_5'], data['bill_amount_month_6'], data['payment_amount_month_1'], data['payment_amount_month_2'], data['payment_amount_month_3'], data['payment_amount_month_4'], data['payment_amount_month_5'], data['payment_amount_month_6'], related_user_id, data['customer_result_id']) # OR USE: theCustomer = CustomerModel(related_user_id, **data) theCustomer.save_to_db() return theCustomer.json()
def create_customer(): request_data = request.get_json() name = request_data['name'] description = request_data['description'] if CustomerModel.get_by_name(name): return {'message': f"customer with name '{name}' already exists"}, 400 customer = CustomerModel(name, description) try: customer.save() except Exception: return {"message", "Error creating the customer"}, 500 return customer.json()
def post(self): data = CustomerRegister.parser.parse_args() error_validation = validators.customer_register_validator(**data) if error_validation['error validation']: return error_validation if CustomerModel.find_by_username( data['username']) or UserModel.find_by_username( data['username']): return { "message": "An account with that username already exists" }, 400 customer = CustomerModel( **data) # CustomerModel(data['username'], data['password'] ...) customer.save_to_db() # return {'customer': customer.fake_json()}, 201 # return {'customers': [customer.short_json() for customer in CustomerModel.query.all()]}, 201 return {"message": "Account created successfully."}, 201
def post(self): data = Customer.parser.parse_args() customer = CustomerModel.find_by_email(data["email"]) if customer: return ( { "message": f"a member with the same email already exists" }, 400, ) new_customer = CustomerModel(**data) try: new_customer.save_to_db() except: return ( { "message": "An error occured adding a member" }, 500, ) return new_customer.json(), 201
def post(self, tax_id): if CustomerModel.find_by_taxid(tax_id): return { 'message': "An customer with cpf '{}' already exists.".format( tax_id)}, 400 data = Customer.parser.parse_args() customer = CustomerModel( tax_id, data['debtor_value'], data['score_id'], data['name'], data['customer_defaulter']) try: customer.save_to_db() except: return { "message": "An error occurred inserting the customer."}, 500 return customer.json(), 201
def post(self): data = request.form print(data) if not CustomerModel.find_by_id(data['customer_id']): CustomerModel(data['customer_id'], data['password'], data['full_name'], data['address'], data['gender'], data['marital_status'], data['customer_type']).save_to_db() print("customer saved!") msg = "Customer enrolled successfully!" print(CustomerModel.query.filter(CustomerModel.customer_id == 10000001).first()) else: msg = "Customer ID has exist!" def save_home(): print("start saving home insurance") h_start_date = datetime.strptime(data['h_start_date'], "%Y-%m-%d").date() h_end_date = datetime.strptime(data['h_end_date'], "%Y-%m-%d").date() h_invoice_date = datetime.now() h_pay_due_date = datetime.strptime('2020-12-31', "%Y-%m-%d").date() purchase_date = datetime.strptime(data['purchase_date'], "%Y-%m-%d").date() print("date changed") HomeInsuranceModel(data['customer_id'], h_start_date, h_end_date, data['h_premium_amount'], data['h_policy_status']).save_to_db() print("h ins saved") HomeInvoiceModel(h_invoice_date, h_pay_due_date, data['h_premium_amount'], data['customer_id']).save_to_db() print("h inv saved") HomeModel(purchase_date, data['purchase_value'], data['area_sqft'], data['home_type'], data['auto_fire_notif'], data['security_sys'], data['swimming_pool'], data['basement'], data['customer_id']).save_to_db() print("home saved") def save_auto(): print("start saving home insurance") a_start_date = datetime.strptime(data['a_start_date'], "%Y-%m-%d").date() a_end_date = datetime.strptime(data['a_end_date'], "%Y-%m-%d").date() a_invoice_date = datetime.now() a_pay_due_date = datetime.strptime('2020-12-31', "%Y-%m-%d").date() driver_birthday = datetime.strptime(data['driver_birthday'], "%Y-%m-%d").date() print("date changed") AutoInsuranceModel(data['customer_id'], a_start_date, a_end_date, data['a_premium_amount'], data['a_policy_status']).save_to_db() print("a ins saved") AutoInvoiceModel(a_invoice_date, a_pay_due_date, data['a_premium_amount'], data['customer_id']).save_to_db() print("a inv saved") VehicleModel(data['vin'], data['model_year'], data['vehicle_status'], data['customer_id']).save_to_db() print("vehicle saved") new_vehicle = VehicleModel.find_by_vin(data['vin']) DriverModel(data['license_num'], data['driver_name'], driver_birthday, new_vehicle.vehicle_id).save_to_db() print("driver saved") if data['customer_type'] == "A": save_auto() print("auto insurance saved!") elif data['customer_type'] == "H": save_home() print("home insurance saved!") elif data['customer_type'] == "B": save_auto() save_home() print("both insurance saved!") return redirect("/information/"+data['customer_id'])
[get_product(name, url) for name, url in bestsellers_list] # product_1 = ProductModel( # title = 'test', # rating = 4.6, # category = 'test', # price = 3.99, # currency = 'GBP', # symbol = '$', # image = 'dlkjfal' # ) # # product_1.save() daniel = CustomerModel( username="******", email="*****@*****.**", password="******", # products=[product_1] ) daniel.save() mitty = CustomerModel( username="******", email="*****@*****.**", password="******", # products=[product_1] ) # mitty.save() order_1 = OrderModel( total_amount=100, order_status='Confirmed',
from classes.order import Order # create prodcut model pm = ProductModel(db) # create a product # product = Product("Pinon",5000,"Togo") # insert the product # product = pm.insertProduct(product) products = pm.getProducts() product_list = [] for product in products: product_list.append( Product(product['name'], product['price'], product['country'], product['_id'])) cm = CustomerModel(db) customer = Customer("Serge", "Kossi", "M", "92639417") customer = cm.insertCustomer(customer) om = OrderModel(db) order = Order(customer) for product in product_list: order.add_product(product) order = om.insertOrder(order) print(order)