Exemplo n.º 1
0
def complete(
        product_id: int,
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> None:

    products_cruds.update(db=db,
                          product_id=product_id,
                          product=ProductsUpdate(approved="подтвержден"))

    product = products_cruds.get(db=db, product_id=product_id)

    cost = product.price * product.amount

    finance_cruds.create(
        db,
        user_id=user_id,
        finance=FinanceIn(type="расход",
                          cost=cost,
                          item=f"Попкупка {product.name}",
                          date=date.today()),
    )

    user = user_cruds.get(db, user_id)

    balance = user.balance - cost

    user_cruds.update(db, UserUpdate(balance=balance), user_id=user_id)
Exemplo n.º 2
0
def update(
        finance: FinanceUpdate,
        finance_id: int,
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> List[Finance]:
    old_finance = finance_cruds.get(db, user_id, finance_id)

    old_type = old_finance.type == "доход"
    old_cost = old_finance.cost

    finance_cruds.update(db, user_id, finance_id, finance)

    new_finance = finance_cruds.get(db, user_id, finance_id)

    new_type = new_finance.type == "доход"
    new_cost = new_finance.cost

    financial_diff = new_cost * (1 if new_type else
                                 -1) - old_cost * (1 if old_type else -1)

    # update user's balance
    user = user_cruds.get(db, user_id)
    balance = user.balance

    balance += financial_diff

    user_cruds.update(db, UserUpdate(balance=balance), user_id=user_id)
Exemplo n.º 3
0
def create(
        finance: FinanceIn,
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> List[Finance]:
    if finance.date > date.today():
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Date must be today or earlier",
        )

    id = finance_cruds.create(db=db, user_id=user_id, finance=finance)

    # update user's balance
    user = user_cruds.get(db, user_id)
    balance = user.balance

    if finance.type == "доход":
        balance += finance.cost
    else:
        balance -= finance.cost

    user_cruds.update(db, UserUpdate(balance=balance), user_id=user_id)

    return {"id": id}
Exemplo n.º 4
0
def get_balance(
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> None:
    user = user_cruds.get(db=db, user_id=user_id)

    return {
        "balance": user.balance,
    }
Exemplo n.º 5
0
def get_stress(
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> int:
    user = user_cruds.get(db=db, user_id=user_id)

    return {
        "current_stress": user.cur_stress_lvl,
        "max_stress": user.max_stress_lvl,
    }
Exemplo n.º 6
0
def complete_task(
    activity_id: int,
    _completed: ActivityComplition,
    user_id: int = Depends(get_user_id),
    db: Session = Depends(get_db),
):
    completed = _completed.completed

    activity = activity_cruds.get(db=db, user_id=user_id, activity_id=activity_id)

    if activity is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Activity with specified id not found",
        )

    if completed:
        if activity.completed == "выполнено":
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Activity already completed",
            )
    else:
        if activity.completed == "не выполнено":
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Activity haven't completed yet",
            )

    completed_text = "выполнено" if completed else "не выполнено"

    activity_cruds.update(
        activity_id=activity_id,
        activity=ActivityUpdate(completed=completed_text),
        user_id=user_id,
        db=db,
    )

    activity = activity_cruds.get(db=db, user_id=user_id, activity_id=activity_id)

    user = user_cruds.get(db=db, user_id=user_id)

    new_stress = user.cur_stress_lvl
    if completed:
        new_stress += activity.stress_points
    else:
        new_stress -= activity.stress_points

    user_cruds.update(
        db=db, user=UserUpdate(cur_stress_lvl=new_stress), user_id=user_id
    )

    return {"cur_stress_lvl": new_stress}
Exemplo n.º 7
0
def delete(
        finance_id: int,
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> None:
    finance = finance_cruds.get(db, user_id, finance_id)

    # update user's balance
    user = user_cruds.get(db, user_id)
    balance = user.balance

    type = finance.type == "доход"
    cost = finance.cost

    financial_diff = cost * (1 if type else -1)

    balance -= financial_diff

    user_cruds.update(db, UserUpdate(balance=balance), user_id=user_id)

    finance_cruds.delete(db, user_id, finance_id)
Exemplo n.º 8
0
                    continue

                if not plan[collision_b].important:
                    del plan[collision_b]
                    continue

                del test_plans[plan_name]

        # if plan without collisions save it
        plans[plan_name] = test_plans[plan_name]
        del test_plans[plan_name]

    if not plans:
        raise ValueError("Schedule have unresolvable collisions")

    max_stress = user_crud.get(db, user_id).max_stress_lvl

    # calculate stress
    stats = {}
    max_tasks = 0
    best_plan = None

    for plan_name in plans:
        stats[plan_name] = calculate_stats(plans[plan_name])
        if stats[plan_name][0] >= max_stress:
            if not restress_plan(plans, plan_name, max_stress):
                del plans[plan_name]
        elif stats[plan_name][1] > max_tasks:
            max_tasks = stats[plan_name][1]
            best_plan = plan_name