Exemplo n.º 1
0
async def session_middleware(request: Request, call_text):
    response = Response('请求页面不存在...', status_code=500)
    try:
        request.state.db = SessionLocal()
        response = await call_text(request)
    finally:
        request.state.db.close()
    return response
Exemplo n.º 2
0
def init() -> None:
    try:
        db = SessionLocal()
        # Try to create session to check if DB is awake
        db.execute('SELECT 1')
    except Exception as e:
        logger.error(e)
        raise e
Exemplo n.º 3
0
async def db_session_middleware(request: Request, call_next):
    response = Response("Internal server error", status_code=500)
    try:
        request.state.db = SessionLocal()
        response = await call_next(request)
    finally:
        request.state.db.close()
    return response
Exemplo n.º 4
0
def get_db() -> Generator:
    """
    获取sqlalchemy会话对象
    :return:
    """
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
Exemplo n.º 5
0
def init() -> None:
    db = SessionLocal()

    create_user(
        db,
        UserCreate(
            email="*****@*****.**",
            password="******",
            is_active=True,
            is_superuser=True,
        ),
    )
def init() -> None:
    db = SessionLocal()

    create_pessoa(
        db,
        PessoaCreate(
            email="*****@*****.**",
            senha="admin",
            usuario="admin",
            ativo=True,
            superusuario=True,
        ),
    )
Exemplo n.º 7
0
def get_pakete(db: Session = Depends(get_db)):
    db = SessionLocal()
    answer = db.query(Pakete, Ort).filter(
        Pakete.wunschort_id != None).join(Ort, Pakete.wunschort_id == Ort.id).all()
    pakete_list = []

    for row in answer:
        paket = row[0]
        ort = row[1]
        paket_rep = PaketRep(id=paket.id, groesse=paket.groesse,
                             fach_nummer=paket.fach_nummer, empfaenger_id=paket.empfaenger_id,  absender_id=paket.absender_id, wunschort_id=paket.wunschort_id, long=ort.long, lat=ort.lat, interaktion_von=paket.interaktion_von, interaktion_bis=paket.interaktion_bis)
        pakete_list.append(paket_rep)
        if (DEBUG):
            paket.interaktion_von = datetime.now(
                timezone.utc)
            paket.interaktion_bis = datetime.now(
                timezone.utc) + timedelta(minutes=45)
            db.add(paket)
    db.commit()
    return pakete_list
Exemplo n.º 8
0
def redistribute_old_and_new_pakete_to_zustellbasen():
    pakete_list = get_pakete()
    start = ceil_dt(datetime.now(timezone.utc), timedelta(minutes=30)
                    )
    end = ceil_dt(datetime.now(timezone.utc), timedelta(minutes=30)
                  ) + timedelta(minutes=(30))
    print(f"Alle Pakete ohne Packstation: {len(pakete_list)}")
    pakete_for_next_timeslot = []
    print(start)
    print(end)
    for paket in pakete_list:
        print(paket.interaktion_bis)
        if (paket.zustellbasis_id is not None) or ((paket.interaktion_von is not None and paket.interaktion_bis is not None) and ((start <= paket.interaktion_von <= end) or (start <= paket.interaktion_bis <= end) or (paket.interaktion_von < start and end < paket.interaktion_bis))):
            pakete_for_next_timeslot.append(paket)
    print(f"Alle Pakete für den Timeslot: {len(pakete_for_next_timeslot)}")
    coordinates = [(paket.lat, paket.long) for paket in pakete_list]
    center_point = (51.514244, 7.468429)
    coord_array = np.array(coordinates)
    distance_array = check_if_long_lat_array_inside_circe(
        coord_array, center_point[0], center_point[1], 2)
    filtered_list = list(compress(pakete_list, distance_array))
    pakete_coordinates = np.array(
        [(paket.lat, paket.long) for paket in filtered_list])
    kmeans = KMeans(n_clusters=4, random_state=0)
    kmeans_result = kmeans.fit_predict(pakete_coordinates)
    print(kmeans.cluster_centers_[0][1])
    pakete_with_cluster = []
    db = SessionLocal()
    for i in range(4):
        zustellbasis = db.query(Zustellbasis).get(i+1)
        zustellbasis.lat = kmeans.cluster_centers_[i][0]
        zustellbasis.long = kmeans.cluster_centers_[i][1]
        db.add(zustellbasis)
    db.commit()
    for i, (paket, cluster) in enumerate(zip(filtered_list, kmeans_result)):
        db_paket = db.query(Pakete).get(paket.id)
        db_paket.zustellbasis_id = int(cluster + 1)
        db.add(db_paket)
    db.commit()
def db() -> Generator:
    yield SessionLocal()
Exemplo n.º 10
0
            password=user_info["password"],
            authority_id=user_info["authority_id"],  # 权限id
            avatar=user_info["avatar"])
        user = curd_user.create(db, obj_in=user_in)  # noqa: F841
        print(f"用户-{user.nickname}-创建成功")


def init_casbin():
    """
    初始化casbin的基本API数据

    把 api_v1_router 分组的所有路由都添加到 casbin里面
    :return:
    """
    e = get_casbin()

    for route in api_v1_router.routes:
        if route.name == "登录":
            # 登录不验证权限
            continue
        for method in route.methods:
            # 添加casbin规则
            e.add_policy("999", route.path, method)


db = SessionLocal()

init_authority(db)
init_user(db)
init_casbin()
Exemplo n.º 11
0
def get_db() -> Generator:  # pragma: no cover
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
Exemplo n.º 12
0
def get_db() -> Generator:
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
Exemplo n.º 13
0
def init_data() -> None:
    db = SessionLocal()
    init_db(db)
Exemplo n.º 14
0
async def db_session_middleware(request: Request, call_next):
    request.state.db = SessionLocal()
    response = await call_next(request)
    request.state.db.close()
    return response
Exemplo n.º 15
0
def get_db():
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
Exemplo n.º 16
0
def db():
    yield SessionLocal()