def add_couriers(): req_json = request.json['data'] db_sess = db_session.create_session() res = [] bad_id = [] already_in_base = [i.id for i in db_sess.query(Courier).all()] is_ok = True for courier_info in req_json: flag = False error_ans = [] try: CourierModel(**courier_info, base=already_in_base) except pydantic.ValidationError as e: error_ans += json.loads(e.json()) flag = True if courier_info['courier_id'] in already_in_base: error_ans += [{ "loc": ["id"], "msg": "Invalid id: There is a courier with the same id", "type": "value_error" }] if flag or courier_info['courier_id'] in already_in_base: is_ok = False bad_id.append({ "id": int(courier_info['courier_id']), 'errors': error_ans }) if not is_ok: continue courier = Courier() courier.id = courier_info['courier_id'] courier.maxw = c_type[courier_info['courier_type']] for i in list((courier_info['regions'])): reg = Region() reg.courier_id = courier.id reg.region = i db_sess.add(reg) for i in list(courier_info['working_hours']): wh = WH() wh.courier_id = courier.id wh.hours = i db_sess.add(wh) db_sess.add(courier) res.append({"id": courier_info['courier_id']}) if is_ok: db_sess.commit() return jsonify({"couriers": res}), 201 pprint({"validation_error": bad_id}) print( '-------------------------------------------------------------------------' ) return jsonify({"validation_error": bad_id}), 400
def edit_courier(): if current_user.user_type != 2: return redirect('/') courier_id = current_user.c_id db_sess = db_session.create_session() courier = db_sess.query(Courier).filter(Courier.id == courier_id).first() if not courier: return jsonify({'message': 'no courier with this id'}), 404 form = EditInfoForm() if form.validate_on_submit(): try: req_json = from_few_fields_to_json(form.courier_type.data, form.regions.data, form.working_hours.data) except Exception as e: return render_template('result.html', u=str({'errors': e})) # req_json = request.json print(req_json) try: EditCourierModel(**req_json) except pydantic.ValidationError as e: print({'errors': json.loads(e.json())}) return render_template('result.html', u=str({'errors': json.loads(e.json())})) # return jsonify({'errors': json.loads(e.json())}), 400 for k, v in dict(req_json).items(): if k == 'courier_type': courier.maxw = c_type[v] elif k == 'regions': db_sess.query(Region).filter( Region.courier_id == courier.id).delete() for i in v: reg = Region() reg.courier_id = courier.id reg.region = i db_sess.add(reg) elif k == 'working_hours': db_sess.query(WH).filter(WH.courier_id == courier.id).delete() for i in v: wh = WH() wh.courier_id = courier.id wh.hours = i db_sess.add(wh) db_sess.commit() res = { 'courier_id': courier_id, 'courier_type': rev_c_type[courier.maxw] } a = db_sess.query(WH).filter(WH.courier_id == courier.id).all() res['working_hours'] = [i.hours for i in a] b = [ i.region for i in db_sess.query(Region).filter( Region.courier_id == courier.id).all() ] res['regions'] = b for i in db_sess.query(Order).filter( Order.orders_courier == courier_id).all(): dh = db_sess.query(DH).filter(DH.order_id == i.id).all() if i.complete_time: continue if i.region not in res['regions'] or not is_t_ok(dh, a): i.orders_courier = 0 db_sess.commit() ords = list( db_sess.query(Order).filter(Order.orders_courier == courier_id, Order.complete_time == '').all()) for i in ords: i.orders_courier = 0 db_sess.commit() courier.currentw = 0 inds = choose_orders(list(map(lambda u: u.weight, ords)), courier.maxw) for i in inds: order = ords[i] courier.currentw += order.weight order.orders_courier = courier_id db_sess.commit() return render_template('result.html', u=str(res)) # return jsonify(res), 200 # form.courier_type.process_data(rev_c_type[courier.maxw]) form.courier_type.data = rev_c_type[courier.maxw] form.regions.data = ','.join( list( map( lambda x: str(x.region), db_sess.query(Region).filter( Region.courier_id == courier_id).all()))) form.working_hours.data = ','.join( list( map(lambda x: x.hours, db_sess.query(WH).filter(WH.courier_id == courier_id).all()))) return render_template('edit_courier.html', form=form, title='Изменить информацию')
def add_couriers(): if current_user.user_type != 3: return redirect('/') form = NewCourierForm() db_sess = db_session.create_session() candidates = db_sess.query(User).filter(User.user_type == 1).all() form.couriers.choices = [(i.id, i.name) for i in candidates] if form.validate_on_submit(): req_json = form_couriers_json(form.couriers.data, db_sess)['data'] already_in_base = [i.id for i in db_sess.query(Courier).all()] res = [] bad_id = [] is_ok = True if not req_json: return render_template('result.html', u='invalid courier(s) information') for courier_info in req_json: flag = False error_ans = [] try: CourierModel(**courier_info, base=already_in_base) except pydantic.ValidationError as e: error_ans += json.loads(e.json()) flag = True if courier_info['courier_id'] in already_in_base: error_ans += [{ "loc": ["id"], "msg": "Invalid id: There is a courier with the same id", "type": "value_error" }] if flag or courier_info['courier_id'] in already_in_base: is_ok = False bad_id.append({ "id": int(courier_info['courier_id']), 'errors': error_ans }) if not is_ok: continue courier = Courier() courier.id = courier_info['courier_id'] user = db_sess.query(User).filter( User.id == int(courier_info['user_id'])).first() user.c_id = courier.id user.user_type = 2 print(user) courier.maxw = c_type[courier_info['courier_type']] for i in list((courier_info['regions'])): reg = Region() reg.courier_id = courier.id reg.region = i db_sess.add(reg) for i in list(courier_info['working_hours']): wh = WH() wh.courier_id = courier.id wh.hours = i db_sess.add(wh) db_sess.add(courier) res.append({"id": courier_info['courier_id']}) if is_ok: db_sess.commit() return render_template('result.html', u=str({"couriers": res})) # return jsonify({"couriers": 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('available_couriers.html', title='Новый курьер', form=form)
with open('yandex_statistics.json', 'r') as stats: data = stats.read() data = json.loads(data) regions = data['data']['items'][180:] print(regions) db_session.global_init("../db/database.sqlite") session = db_session.create_session() for region in regions: name = region['name'] reg = session.query(Region).filter(Region.name == name).first() if reg: reg.cases = region['cases'] reg.cured = region['cured'] reg.deaths = region['deaths'] reg.lon = region['coordinates'][0] reg.lat = region['coordinates'][1] session.commit() else: reg = Region() reg.name = region['name'] reg.cases = region['cases'] reg.cured = region['cured'] reg.deaths = region['deaths'] reg.lon = region['coordinates'][0] reg.lat = region['coordinates'][1] session.add(reg) session.commit()
def edit_courier(courier_id): db_sess = db_session.create_session() courier = db_sess.query(Courier).filter(Courier.id == courier_id).first() if not courier: return jsonify({'message': 'no courier with this id'}), 404 if request.method == 'PATCH': req_json = request.json try: EditCourierModel(**req_json) except pydantic.ValidationError as e: print({'errors': json.loads(e.json())}) return jsonify({'errors': json.loads(e.json())}), 400 for k, v in dict(req_json).items(): if k == 'courier_type': courier.maxw = c_type[v] elif k == 'regions': db_sess.query(Region).filter( Region.courier_id == courier.id).delete() for i in v: reg = Region() reg.courier_id = courier.id reg.region = i db_sess.add(reg) elif k == 'working_hours': db_sess.query(WH).filter(WH.courier_id == courier.id).delete() for i in v: wh = WH() wh.courier_id = courier.id wh.hours = i db_sess.add(wh) db_sess.commit() res = { 'courier_id': courier_id, 'courier_type': rev_c_type[courier.maxw] } a = db_sess.query(WH).filter(WH.courier_id == courier.id).all() res['working_hours'] = [i.hours for i in a] b = [ i.region for i in db_sess.query(Region).filter( Region.courier_id == courier.id).all() ] res['regions'] = b for i in db_sess.query(Order).filter( Order.orders_courier == courier_id).all(): dh = db_sess.query(DH).filter(DH.order_id == i.id).all() if i.complete_time: continue if i.region not in res['regions'] or not is_t_ok(dh, a): i.orders_courier = 0 db_sess.commit() ords = list( db_sess.query(Order).filter(Order.orders_courier == courier_id, Order.complete_time == '').all()) for i in ords: i.orders_courier = 0 db_sess.commit() courier.currentw = 0 inds = choose_orders(list(map(lambda u: u.weight, ords)), courier.maxw) for i in inds: order = ords[i] courier.currentw += order.weight order.orders_courier = courier_id db_sess.commit() return jsonify(res), 200 elif request.method == 'GET': res = { 'courier_id': courier_id, 'courier_type': rev_c_type[courier.maxw] } a = [ i.hours for i in db_sess.query(WH).filter( WH.courier_id == courier.id).all() ] res['working_hours'] = a b = [ i.region for i in db_sess.query(Region).filter( Region.courier_id == courier.id).all() ] res['regions'] = b if not db_sess.query(Order).filter(Order.orders_courier == courier_id, Order.complete_time == '').all(): courier.earnings += courier.last_pack_cost courier.last_pack_cost = 0 res['earnings'] = courier.earnings if not courier.earnings: return jsonify(res), 200 try: t = min([ i.summa / i.q for i in db_sess.query(Region).filter( Region.courier_id == courier.id).all() if i.q != 0 ]) except ValueError: t = 60 * 60 res['rating'] = round((60 * 60 - min(t, 60 * 60)) / (60 * 60) * 5, 2) return jsonify(res), 200