Example #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)
Example #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)
Example #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}
Example #4
0
def change_balance(
        _balance: UserBalanceIn,
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> None:
    user_cruds.update(db=db,
                      user=UserUpdate(**_balance.dict()),
                      user_id=user_id)
Example #5
0
def update_max_stress_lvl(
        _max_stress_lvl: UserStressIn,
        db: Session = Depends(get_db),
        user_id: int = Depends(get_user_id),
) -> None:
    user_cruds.update(db=db,
                      user=UserUpdate(**_max_stress_lvl.dict()),
                      user_id=user_id)
Example #6
0
async def get_issue(request: Request,
                    user_id: int,
                    session: session = Depends(get_db)):
    data = await request.form()
    session.execute(
        update(UserTable).where(UserTable.id == user_id).values(**UserUpdate(
            **data).dict(exclude_unset=True)))
    session.commit()

    return RedirectResponse(urljoin(str(request.base_url), "user"),
                            status_code=status.HTTP_303_SEE_OTHER)
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}
Example #8
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)
Example #9
0
def update(db: Session, user: schemas.UserUpdate, user_id: int) -> None:
    db.query(models.User).filter(models.User.id == user_id).update(
        user.dict(exclude_none=True))

    db.commit()