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 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()
# 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', # products=[product_1], customer=daniel, current_order=False)