def customer_create(): params = request.json required_fields = ['fullname', 'phone'] for rf in required_fields: if not params.has_key(rf): raise BadRequest("Need `%s` field" % rf, jsonify({'errCode': 'ERROR_FIELD_REQUIRED'})) if not len(params['fullname']): raise BadRequest("Fullname not null", jsonify({'errCode': 'ERROR_FULLNAME_IS_NULL'})) phone = params['phone'] email = params['email'] note = params.get('note', "") if not re.match(PHONE_PATTERN, phone): raise BadRequest("Phone is not true format", response=jsonify({'errCode': 'ERROR_PHONE_FORMAT'})) if not re.match(EMAIL_PATTERN, email): raise BadRequest("Email is not true format", jsonify({'errCode': 'ERROR_EMAIL_FORMAT'})) if len(note) > 1000: raise BadRequest("Note is too long", jsonify({'errCode': 'ERROR_NOTE_TOO_LONG'})) conds = (Customer.phone == params['phone']) if params.get('email'): conds |= (Customer.email == params['email']) customer_obj = Customer.query.filter(conds).first() if customer_obj: raise SecurityError("Customer %s is existed!" % params['fullname'], jsonify({'errCode': 'ERROR_CUSTOMER_IS_EXISTED'})) #TODO verify email and phone customer_obj = Customer() customer_obj.fullname = params['fullname'] customer_obj.email = email customer_obj.phone = phone customer_obj.note = note customer_obj.birthday = params.get('birthday') customer_obj.created_at = datetime.datetime.utcnow() customer_obj.updated_at = datetime.datetime.utcnow() db.session.add(customer_obj) db.session.commit() customer_obj = Customer.query.filter(Customer.phone == phone).first() return jsonify(customer_obj.to_dict())
def regidter(): form = CustomerRegForm() if request.method == 'POST': if form.validate(): # 从表单上取出数据添加到Customer数据模型对象 new_customer = Customer() new_customer.id = form.userid.data new_customer.name = form.name.data new_customer.password = form.password.data new_customer.address = form.address.data new_customer.birthday = form.birthday.data new_customer.phone = form.phone.data db.session.add(new_customer) db.session.commit() print('注册成功') return render_template('customer_reg_success.html', form=form) # else: # render_template('customer_reg.html', form=form) return render_template('customer_reg.html', form=form)