def create_order(data) -> Order: order = Order() order.order_id = data["order_id"] order.weight = data["weight"] order.region = data["region"] order.delivery_hours = convert_to_str(data["delivery_hours"]) return order
def add_orders(): req_json = request.json['data'] db_sess = db_session.create_session() res = [] bad_id = [] is_ok = True already_in_base = [i.id for i in db_sess.query(Order).all()] for order_info in req_json: flag = False error_ans = [] try: OrderModel(**order_info, base=already_in_base) except pydantic.ValidationError as e: error_ans += json.loads(e.json()) flag = True if order_info['order_id'] in already_in_base: error_ans += [{ "loc": ["id"], "msg": "Invalid id: There is a order with the same id", "type": "value_error" }] if flag or order_info['order_id'] in already_in_base: is_ok = False bad_id.append({ "id": int(order_info['order_id']), 'errors': error_ans }) if not is_ok: continue order = Order() order.id = order_info['order_id'] order.weight = order_info['weight'] order.region = order_info['region'] order.orders_courier = 0 for i in list(order_info['delivery_hours']): dh = DH() dh.order_id = order.id dh.hours = i db_sess.add(dh) db_sess.add(order) res.append({"id": int(order_info['order_id'])}) if is_ok: db_sess.commit() return jsonify({"orders": res}), 201 pprint({"validation_error": bad_id}) print( '-------------------------------------------------------------------------' ) return jsonify({"validation_error": bad_id}), 400
def add_orders(): form = MakeOrderForm() if form.validate_on_submit(): req_json = [] db_sess = db_session.create_session() res = [] bad_id = [] is_ok = True already_in_base = [i.id for i in db_sess.query(Order).all()] req_json.append({ 'order_id': max(already_in_base) + 1, 'weight': form.weight.data, 'region': form.region.data, 'delivery_hours': form.dh.data.split(',') }) print(req_json[0]['delivery_hours']) for order_info in req_json: flag = False error_ans = [] try: OrderModel(**order_info, base=already_in_base) except pydantic.ValidationError as e: error_ans += json.loads(e.json()) flag = True if order_info['order_id'] in already_in_base: error_ans += [{ "loc": ["id"], "msg": "Invalid id: There is a order with the same id", "type": "value_error" }] if flag or order_info['order_id'] in already_in_base: is_ok = False bad_id.append({ "id": int(order_info['order_id']), 'errors': error_ans }) if not is_ok: continue order = Order() order.id = order_info['order_id'] order.weight = order_info['weight'] order.region = order_info['region'] order.orders_courier = 0 for i in list(order_info['delivery_hours']): dh = DH() dh.order_id = order.id dh.hours = i db_sess.add(dh) db_sess.add(order) res.append({"id": int(order_info['order_id'])}) if is_ok: db_sess.commit() return render_template('result.html', u=str({"orders": res})) # return jsonify({"orders": res}), 201 pprint({"validation_error": bad_id}) print( '-------------------------------------------------------------------------' ) return render_template('result.html', u=str({"validation_error": bad_id})) # return jsonify({"validation_error": bad_id}), 400 return render_template('new_order.html', title='Новый заказ', form=form)