def registery(): if (not 'captcha' in session.keys()) or (session['captcha'] == None): return jsonify(result=False, reason="Please reload captcha first"), 400 pipeline = Pipeline(request) pipeline.add(ensureJson) pipeline.add(ensureCaptcha, [request, session]) pipeline.add( ensureParam, [request, 'username', lambda: invalidateSession(session, 'captcha')]) pipeline.add( ensureParam, [request, 'password', lambda: invalidateSession(session, 'captcha')]) broken, retvs = pipeline.run() if broken: return retvs _, _, username, password = retvs sess = DBSession() user = User(username) user.setPassword(password) sess.add(user) sess.commit() session['captcha'] = None return jsonify(result=True), 200
def createProduct(): sess = DBSession() current_user = get_jwt_identity() user = sess.query(User).filter_by(id=current_user).first() #manager = sess.query(User).filter_by(id=current_user,isManager=True).first() if not user.isManager: return jsonify({"msg": "No Permission"}), 401 if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 title = request.json.get('title') if not title: return jsonify({"msg": "Missing title parameter"}), 400 category = request.json.get('category') if not category: return jsonify({"msg": "Missing category parameter"}), 400 storehouse_id = request.json.get('storehouse_id') if not storehouse_id: return jsonify({"msg": "Missing storehouse_id parameter"}), 400 dictdata = request.json.get('dictdata') if not dictdata: return jsonify({"msg": "Missing dictdata parameter"}), 400 product = Product(title,category,storehouse_id) product.update(dictdata) sess.add(product) sess.commit() return jsonify(result=True, productId=product.id)
def __init__(self, id, compulsory, question): if (len(question) < 1 or len(id) < 1): raise ValueError session = DBSession() self.id = id self.type = "TextQuestion" self.compulsory = compulsory self.question = question session.add(self) session.commit()
def __init__(self, id, compulsory, question, responses): if (responses == None or len(responses) < 1 or len(question) < 1 or len(id) < 1): raise ValueError session = DBSession() self.id = id self.type = "MCQ" self.compulsory = compulsory self.question = question self.responses = responses session.add(self) session.commit()
def createOrder(): sess = DBSession() current_user = get_jwt_identity() if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 user = sess.query(User).filter_by(id=current_user).first() ids = request.json.get('ids') if not ids: return jsonify({"msg": "Missing ids parameter"}), 400 receiver = request.json.get('receiver', '') phonenumber = request.json.get('phonenumber', '') address = request.json.get('address', '') carts = sess.query(Cart).filter_by(creator_id=current_user, removed=False).all() vir = Order(current_user) vir.setAddress(address, receiver, phonenumber) sess.add(vir) sess.commit() #rders = [] created = [] for cart in carts: if cart.product_id in ids: product = sess.query(Product).filter_by(id=cart.product_id, shelved=True).first() # 限购暂未实现 #print(product.remain, cart.count) #if (not product) or (product.remain < cart.count): # orders.append([False,cart.id]) # continue #product.remain = product.remain - cart.count order = Order(current_user, False) order.fill(cart.product_id, cart.count, product.price, vir.id) sess.add(order) cart.removed = True sess.commit() created.append(cart.product_id) #orders.append([True,cart.id,cart.product_id,cart.count,product.price]) #return jsonify(orders=orders,price=vir.cost()), 200 return jsonify(result=True, created=created), 200
def add(): current_user = get_jwt_identity() pid = request.json['id'] if request.is_json and ( 'id' in request.json.keys()) else None count = request.json['count'] if request.is_json and ( 'count' in request.json.keys()) else None sess = DBSession() cart = sess.query(Cart).filter_by(creator_id=current_user, product_id=pid, removed=False).first() if cart: cart.count += count if cart.count <= 0: cart.removed = True else: cart = Cart(current_user, pid, count) sess.add(cart) sess.commit() return jsonify(result=True), 200
def add(): current_user = get_jwt_identity() pipeline = Pipeline(request) pipeline.add(ensureJson) pipeline.add(ensureParam, [request, 'receiver']) pipeline.add(ensureParam, [request, 'phonenumber']) pipeline.add(ensureParam, [request, 'address']) broken, retvs = pipeline.run() if broken: return retvs _, receiver, phonenumber, address = retvs addr = Address(current_user, receiver, phonenumber, address) sess = DBSession() sess.add(addr) sess.commit() user = sess.query(User).filter_by(id=current_user).first() if user.default_address_id is None: user.default_address_id = addr.id sess.commit() return jsonify(result=True, id=addr.id), 200
def createSupplierOrder(): sess = DBSession() current_user = get_jwt_identity() if not request.is_json: return jsonify(result=False, msg="Missing JSON in request"), 400 if current_user: user = sess.query(User).filter_by(id=current_user).first() if user.isManager or user.isOperator: product_id = request.json.get('product_id') if not product_id: return jsonify({"msg": "Missing product_id parameter"}), 400 count = request.json.get('count') if not count: return jsonify({"msg": "Missing count parameter"}), 400 storehouse_id = request.json.get('storehouse_id') if not storehouse_id: return jsonify({"msg": "Missing storehouse_id parameter"}), 400 product = sess.query(Product).filter_by(id=product_id).first() if not product: return jsonify({"msg": "Bad productId"}), 401 supplierOrder = SupplierOrder(current_user) supplierOrder.fill(product_id, storehouse_id, count) sess.add(supplierOrder) sess.commit() return jsonify(sorderid=supplierOrder.id), 200 else: return jsonify({"msg": "No Permission"}), 403 else: return jsonify({"msg": "Please login"}), 401