Clients should now work.
Blacked a lot of files
This commit is contained in:
parent
06fd0db3f6
commit
9dff72aaed
@ -14,7 +14,7 @@ from .routers import (
|
|||||||
product,
|
product,
|
||||||
product_group,
|
product_group,
|
||||||
recipe,
|
recipe,
|
||||||
login
|
login,
|
||||||
)
|
)
|
||||||
from .routers.auth import client, user, role
|
from .routers.auth import client, user, role
|
||||||
from .db.base_class import Base
|
from .db.base_class import Base
|
||||||
@ -31,18 +31,30 @@ app.include_router(login.router, tags=["login"])
|
|||||||
app.include_router(account.router, prefix="/api/accounts", tags=["accounts"])
|
app.include_router(account.router, prefix="/api/accounts", tags=["accounts"])
|
||||||
app.include_router(account_types.router, prefix="/api/account-types", tags=["accounts"])
|
app.include_router(account_types.router, prefix="/api/account-types", tags=["accounts"])
|
||||||
app.include_router(attendance.router, prefix="/api/attendance", tags=["attendance"])
|
app.include_router(attendance.router, prefix="/api/attendance", tags=["attendance"])
|
||||||
app.include_router(attendance_types.router, prefix="/api/attendance-types", tags=["attendance"])
|
app.include_router(
|
||||||
app.include_router(employee_attendance.router, prefix="/api/employee-attendance", tags=["attendance"])
|
attendance_types.router, prefix="/api/attendance-types", tags=["attendance"]
|
||||||
app.include_router(employee_attendance.router, prefix="/api/employee-attendance", tags=["attendance"])
|
)
|
||||||
app.include_router(attendance_report.router, prefix="/api/attendance-report", tags=["attendance"])
|
app.include_router(
|
||||||
app.include_router(cost_centre.router, prefix="/api/cost-centres", tags=["cost-centres"])
|
employee_attendance.router, prefix="/api/employee-attendance", tags=["attendance"]
|
||||||
|
)
|
||||||
|
app.include_router(
|
||||||
|
employee_attendance.router, prefix="/api/employee-attendance", tags=["attendance"]
|
||||||
|
)
|
||||||
|
app.include_router(
|
||||||
|
attendance_report.router, prefix="/api/attendance-report", tags=["attendance"]
|
||||||
|
)
|
||||||
|
app.include_router(
|
||||||
|
cost_centre.router, prefix="/api/cost-centres", tags=["cost-centres"]
|
||||||
|
)
|
||||||
app.include_router(employee.router, prefix="/api/employees", tags=["employees"])
|
app.include_router(employee.router, prefix="/api/employees", tags=["employees"])
|
||||||
app.include_router(fingerprint.router, prefix="/api/fingerprint", tags=["employees"])
|
app.include_router(fingerprint.router, prefix="/api/fingerprint", tags=["employees"])
|
||||||
app.include_router(product.router, prefix="/api/products", tags=["products"])
|
app.include_router(product.router, prefix="/api/products", tags=["products"])
|
||||||
app.include_router(product_group.router, prefix="/api/product-groups", tags=["products"])
|
app.include_router(
|
||||||
|
product_group.router, prefix="/api/product-groups", tags=["products"]
|
||||||
|
)
|
||||||
app.include_router(recipe.router, prefix="/api/recipes", tags=["products"])
|
app.include_router(recipe.router, prefix="/api/recipes", tags=["products"])
|
||||||
|
|
||||||
app.include_router(client.router, prefix="/api/clients", tags=["users"])
|
app.include_router(client.router, prefix="/api/clients", tags=["clients"])
|
||||||
app.include_router(role.router, prefix="/api/roles", tags=["users"])
|
app.include_router(role.router, prefix="/api/roles", tags=["users"])
|
||||||
app.include_router(user.router, prefix="/api/users", tags=["users"])
|
app.include_router(user.router, prefix="/api/users", tags=["users"])
|
||||||
|
|
||||||
|
@ -29,7 +29,13 @@ class Client(Base):
|
|||||||
login_history = relationship("LoginHistory", backref="client")
|
login_history = relationship("LoginHistory", backref="client")
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, code=None, name=None, enabled=False, otp=None, creation_date=None, id_=None
|
self,
|
||||||
|
code=None,
|
||||||
|
name=None,
|
||||||
|
enabled=False,
|
||||||
|
otp=None,
|
||||||
|
creation_date=None,
|
||||||
|
id_=None,
|
||||||
):
|
):
|
||||||
self.code = code
|
self.code = code
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -32,10 +32,7 @@ class Product(Base):
|
|||||||
fraction_units = Column("fraction_units", Unicode(255), nullable=False)
|
fraction_units = Column("fraction_units", Unicode(255), nullable=False)
|
||||||
product_yield = Column("product_yield", Numeric, nullable=False)
|
product_yield = Column("product_yield", Numeric, nullable=False)
|
||||||
product_group_id = Column(
|
product_group_id = Column(
|
||||||
"product_group_id",
|
"product_group_id", GUID(), ForeignKey("product_groups.id"), nullable=False,
|
||||||
GUID(),
|
|
||||||
ForeignKey("product_groups.id"),
|
|
||||||
nullable=False,
|
|
||||||
)
|
)
|
||||||
account_id = Column("account_id", GUID(), ForeignKey("accounts.id"), nullable=False)
|
account_id = Column("account_id", GUID(), ForeignKey("accounts.id"), nullable=False)
|
||||||
price = Column("cost_price", Numeric, nullable=False)
|
price = Column("cost_price", Numeric, nullable=False)
|
||||||
@ -174,8 +171,7 @@ class RecipeItem(Base):
|
|||||||
__table_args__ = (UniqueConstraint("recipe_id", "product_id"),)
|
__table_args__ = (UniqueConstraint("recipe_id", "product_id"),)
|
||||||
|
|
||||||
id = Column("recipe_item_id", GUID(), primary_key=True, default=uuid.uuid4)
|
id = Column("recipe_item_id", GUID(), primary_key=True, default=uuid.uuid4)
|
||||||
recipe_id = Column(
|
recipe_id = Column("recipe_id", GUID(), ForeignKey("recipes.id"), nullable=False)
|
||||||
"recipe_id", GUID(), ForeignKey("recipes.id"), nullable=False)
|
|
||||||
product_id = Column("product_id", GUID(), ForeignKey("products.id"), nullable=False)
|
product_id = Column("product_id", GUID(), ForeignKey("products.id"), nullable=False)
|
||||||
quantity = Column("quantity", Integer, nullable=False)
|
quantity = Column("quantity", Integer, nullable=False)
|
||||||
price = Column("price", Integer, nullable=False)
|
price = Column("price", Integer, nullable=False)
|
||||||
@ -265,7 +261,9 @@ class AccountBase(Base):
|
|||||||
is_starred = Column("is_starred", Boolean, nullable=False)
|
is_starred = Column("is_starred", Boolean, nullable=False)
|
||||||
is_active = Column("is_active", Boolean, nullable=False)
|
is_active = Column("is_active", Boolean, nullable=False)
|
||||||
is_reconcilable = Column("is_reconcilable", Boolean, nullable=False)
|
is_reconcilable = Column("is_reconcilable", Boolean, nullable=False)
|
||||||
cost_centre_id = Column("cost_centre_id", GUID(), ForeignKey("cost_centres.id"), nullable=False)
|
cost_centre_id = Column(
|
||||||
|
"cost_centre_id", GUID(), ForeignKey("cost_centres.id"), nullable=False
|
||||||
|
)
|
||||||
is_fixture = Column("is_fixture", Boolean, nullable=False)
|
is_fixture = Column("is_fixture", Boolean, nullable=False)
|
||||||
|
|
||||||
__mapper_args__ = {"polymorphic_on": account_type}
|
__mapper_args__ = {"polymorphic_on": account_type}
|
||||||
|
@ -1,51 +1,92 @@
|
|||||||
|
import traceback
|
||||||
import uuid
|
import uuid
|
||||||
from sqlalchemy import desc
|
|
||||||
from brewman.models.auth import Client, LoginHistory
|
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, HTTPException, status, Depends, Security
|
||||||
|
from sqlalchemy import desc
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ...schemas.auth import UserToken
|
||||||
|
from ...core.security import get_current_active_user as get_user
|
||||||
|
from ...db.session import SessionLocal
|
||||||
|
from ...models.auth import Client, LoginHistory
|
||||||
|
import brewman.schemas.auth as schemas
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{id}") # "Clients"
|
# Dependency
|
||||||
def update(request):
|
def get_db():
|
||||||
item = (
|
try:
|
||||||
request.dbsession.query(Client)
|
db = SessionLocal()
|
||||||
.filter(Client.id == uuid.UUID(request.matchdict["id"]))
|
yield db
|
||||||
.first()
|
finally:
|
||||||
)
|
db.close()
|
||||||
item.enabled = request.json_body["enabled"]
|
|
||||||
if item.enabled:
|
|
||||||
item.otp = None
|
|
||||||
item.name = request.json_body["name"]
|
|
||||||
transaction.commit()
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{id}") # "Clients"
|
@router.put("/{id_}", response_model=schemas.Client)
|
||||||
def delete(request):
|
def update(
|
||||||
id_ = request.matchdict.get("id", None)
|
id_: uuid.UUID,
|
||||||
if id_ is None:
|
data: schemas.ClientIn,
|
||||||
response = Response("Client not found")
|
db: Session = Depends(get_db),
|
||||||
response.status_int = 500
|
user: UserToken = Security(get_user, scopes=["clients"]),
|
||||||
return response
|
):
|
||||||
|
try:
|
||||||
client = request.dbsession.query(Client).filter(Client.id == uuid.UUID(id_)).first()
|
item: Client = db.query(Client).filter(Client.id == id_).first()
|
||||||
request.dbsession.execute(
|
item.enabled = data.enabled
|
||||||
LoginHistory.__table__.delete(LoginHistory.client_id == client.id)
|
if item.enabled:
|
||||||
)
|
item.otp = None
|
||||||
request.dbsession.delete(client)
|
item.name = data.name
|
||||||
transaction.commit()
|
db.commit()
|
||||||
return {}
|
return {}
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=traceback.format_exc(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/") # "Clients"
|
@router.delete("/{id_}")
|
||||||
async def show_list(l: bool):
|
def delete(
|
||||||
list_ = request.dbsession.query(Client).order_by(Client.name).all()
|
id_: uuid.UUID,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
user: UserToken = Security(get_user, scopes=["clients"]),
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
item: Client = db.query(Client).filter(Client.id == id_).first()
|
||||||
|
db.execute(LoginHistory.__table__.delete(LoginHistory.client_id == item.id))
|
||||||
|
db.delete(item)
|
||||||
|
db.commit()
|
||||||
|
return {}
|
||||||
|
except SQLAlchemyError as e:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=traceback.format_exc(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/list")
|
||||||
|
async def show_list(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
user: UserToken = Security(get_user, scopes=["clients"]),
|
||||||
|
):
|
||||||
|
list_ = db.query(Client).order_by(Client.name).all()
|
||||||
clients = []
|
clients = []
|
||||||
for item in list_:
|
for item in list_:
|
||||||
last_login = (
|
last_login = (
|
||||||
request.dbsession.query(LoginHistory)
|
db.query(LoginHistory)
|
||||||
.filter(LoginHistory.client_id == item.id)
|
.filter(LoginHistory.client_id == item.id)
|
||||||
.order_by(desc(LoginHistory.date))
|
.order_by(desc(LoginHistory.date))
|
||||||
.first()
|
.first()
|
||||||
@ -69,13 +110,13 @@ async def show_list(l: bool):
|
|||||||
return clients
|
return clients
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{id}") # "Clients"
|
@router.get("/{id_}")
|
||||||
def show_id(request):
|
def show_id(
|
||||||
item = (
|
id_: uuid.UUID,
|
||||||
request.dbsession.query(Client)
|
db: Session = Depends(get_db),
|
||||||
.filter(Client.id == uuid.UUID(request.matchdict["id"]))
|
user: UserToken = Security(get_user, scopes=["clients"]),
|
||||||
.first()
|
):
|
||||||
)
|
item: Client = db.query(Client).filter(Client.id == id_).first()
|
||||||
return {
|
return {
|
||||||
"id": item.id,
|
"id": item.id,
|
||||||
"code": item.code,
|
"code": item.code,
|
||||||
|
@ -39,8 +39,7 @@ def save(
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
detail=str(e),
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -66,8 +65,7 @@ def update(
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
detail=str(e),
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -82,7 +80,9 @@ def add_permissions(role: Role, permissions: List[schemas.PermissionItem], db):
|
|||||||
gp = [p for p in role.permissions if p.id == permission.id_]
|
gp = [p for p in role.permissions if p.id == permission.id_]
|
||||||
gp = None if len(gp) == 0 else gp[0]
|
gp = None if len(gp) == 0 else gp[0]
|
||||||
if permission.enabled and gp is None:
|
if permission.enabled and gp is None:
|
||||||
role.permissions.append(db.query(Permission).filter(Permission.id == permission.id_).one())
|
role.permissions.append(
|
||||||
|
db.query(Permission).filter(Permission.id == permission.id_).one()
|
||||||
|
)
|
||||||
elif not permission.enabled and gp:
|
elif not permission.enabled and gp:
|
||||||
role.permissions.remove(gp)
|
role.permissions.remove(gp)
|
||||||
|
|
||||||
@ -122,9 +122,18 @@ def show_blank(
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/list", response_model=List[schemas.RoleList])
|
@router.get("/list", response_model=List[schemas.RoleList])
|
||||||
async def show_list(db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["users"])):
|
async def show_list(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
user: UserToken = Security(get_user, scopes=["users"]),
|
||||||
|
):
|
||||||
return [
|
return [
|
||||||
{"id": item.id, "name": item.name, "permissions": [p.name for p in sorted(item.permissions, key=lambda p: p.name)]}
|
{
|
||||||
|
"id": item.id,
|
||||||
|
"name": item.name,
|
||||||
|
"permissions": [
|
||||||
|
p.name for p in sorted(item.permissions, key=lambda p: p.name)
|
||||||
|
],
|
||||||
|
}
|
||||||
for item in db.query(Role).order_by(Role.name).all()
|
for item in db.query(Role).order_by(Role.name).all()
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -135,7 +144,7 @@ def show_id(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
user: UserToken = Security(get_user, scopes=["users"]),
|
user: UserToken = Security(get_user, scopes=["users"]),
|
||||||
):
|
):
|
||||||
item = db.query(Role).filter(Role.id == id_).first()
|
item: Role = db.query(Role).filter(Role.id == id_).first()
|
||||||
return role_info(item, db)
|
return role_info(item, db)
|
||||||
|
|
||||||
|
|
||||||
@ -146,7 +155,7 @@ def role_info(item: Optional[Role], db):
|
|||||||
"permissions": [
|
"permissions": [
|
||||||
{"id": p.id, "name": p.name, "enabled": False}
|
{"id": p.id, "name": p.name, "enabled": False}
|
||||||
for p in db.query(Permission).order_by(Permission.name).all()
|
for p in db.query(Permission).order_by(Permission.name).all()
|
||||||
]
|
],
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
@ -159,5 +168,5 @@ def role_info(item: Optional[Role], db):
|
|||||||
"enabled": True if item in item.permissions else False,
|
"enabled": True if item in item.permissions else False,
|
||||||
}
|
}
|
||||||
for item in db.query(Role).order_by(Role.name).all()
|
for item in db.query(Role).order_by(Role.name).all()
|
||||||
]
|
],
|
||||||
}
|
}
|
||||||
|
@ -31,11 +31,7 @@ def save(
|
|||||||
user: UserToken = Security(get_user, scopes=["users"]),
|
user: UserToken = Security(get_user, scopes=["users"]),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
item = User(
|
item = User(name=data.name, password=data.password, locked_out=data.locked_out)
|
||||||
name=data.name,
|
|
||||||
password=data.password,
|
|
||||||
locked_out=data.locked_out
|
|
||||||
)
|
|
||||||
db.add(item)
|
db.add(item)
|
||||||
add_roles(item, data.roles, db)
|
add_roles(item, data.roles, db)
|
||||||
db.commit()
|
db.commit()
|
||||||
@ -43,8 +39,7 @@ def save(
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
detail=str(e),
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -56,8 +51,7 @@ def save(
|
|||||||
|
|
||||||
@router.get("/me", response_model=schemas.User)
|
@router.get("/me", response_model=schemas.User)
|
||||||
def show_me(
|
def show_me(
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db), user: UserToken = Depends(get_user),
|
||||||
user: UserToken = Depends(get_user),
|
|
||||||
):
|
):
|
||||||
item = db.query(User).filter(User.id == user.id_).first()
|
item = db.query(User).filter(User.id == user.id_).first()
|
||||||
return user_info(item, db, user)
|
return user_info(item, db, user)
|
||||||
@ -82,8 +76,7 @@ def update_me(
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
detail=str(e),
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -112,8 +105,7 @@ def update(
|
|||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
detail=str(e),
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -135,9 +127,9 @@ def add_roles(user: User, roles: List[schemas.RoleItem], db: Session):
|
|||||||
|
|
||||||
@router.delete("/{id_}")
|
@router.delete("/{id_}")
|
||||||
def delete(
|
def delete(
|
||||||
id_: uuid.UUID,
|
id_: uuid.UUID,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
user: UserToken = Security(get_user, scopes=["users"]),
|
user: UserToken = Security(get_user, scopes=["users"]),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
item: User = db.query(User).filter(User.id == id_).first()
|
item: User = db.query(User).filter(User.id == id_).first()
|
||||||
@ -168,15 +160,25 @@ def show_blank(
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/list", response_model=List[schemas.UserList])
|
@router.get("/list", response_model=List[schemas.UserList])
|
||||||
async def show_list(db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["users"])):
|
async def show_list(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
user: UserToken = Security(get_user, scopes=["users"]),
|
||||||
|
):
|
||||||
return [
|
return [
|
||||||
{"id": item.id, "name": item.name, "lockedOut": item.locked_out, "roles": [p.name for p in sorted(item.roles, key=lambda p: p.name)]}
|
{
|
||||||
|
"id": item.id,
|
||||||
|
"name": item.name,
|
||||||
|
"lockedOut": item.locked_out,
|
||||||
|
"roles": [p.name for p in sorted(item.roles, key=lambda p: p.name)],
|
||||||
|
}
|
||||||
for item in db.query(User).order_by(User.name).all()
|
for item in db.query(User).order_by(User.name).all()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@router.get("/active")
|
@router.get("/active")
|
||||||
async def show_active(db: Session = Depends(get_db), user: UserToken = Depends(get_user)):
|
async def show_active(
|
||||||
|
db: Session = Depends(get_db), user: UserToken = Depends(get_user)
|
||||||
|
):
|
||||||
return [
|
return [
|
||||||
{"name": item.name}
|
{"name": item.name}
|
||||||
for item in db.query(User).filter(User.locked_out == False).order_by(User.name)
|
for item in db.query(User).filter(User.locked_out == False).order_by(User.name)
|
||||||
@ -196,10 +198,12 @@ def show_id(
|
|||||||
def user_info(item: Optional[User], db: Session, user: UserToken):
|
def user_info(item: Optional[User], db: Session, user: UserToken):
|
||||||
if item is None:
|
if item is None:
|
||||||
return {
|
return {
|
||||||
"name": "", "lockedOut": False, "roles": [
|
"name": "",
|
||||||
|
"lockedOut": False,
|
||||||
|
"roles": [
|
||||||
{"id": r.id, "name": r.name, "enabled": False}
|
{"id": r.id, "name": r.name, "enabled": False}
|
||||||
for r in db.query(Role).order_by(Role.name).all()
|
for r in db.query(Role).order_by(Role.name).all()
|
||||||
]
|
],
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return {
|
return {
|
||||||
@ -214,5 +218,7 @@ def user_info(item: Optional[User], db: Session, user: UserToken):
|
|||||||
"enabled": True if r in item.roles else False,
|
"enabled": True if r in item.roles else False,
|
||||||
}
|
}
|
||||||
for r in db.query(Role).order_by(Role.name).all()
|
for r in db.query(Role).order_by(Role.name).all()
|
||||||
] if "advanced-delete" in user.permissions else [],
|
]
|
||||||
|
if "advanced-delete" in user.permissions
|
||||||
|
else [],
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ async def login_for_access_token(
|
|||||||
)
|
)
|
||||||
),
|
),
|
||||||
"user_id": str(user.id),
|
"user_id": str(user.id),
|
||||||
"locked_out": user.locked_out
|
"locked_out": user.locked_out,
|
||||||
},
|
},
|
||||||
expires_delta=access_token_expires,
|
expires_delta=access_token_expires,
|
||||||
)
|
)
|
||||||
|
@ -28,9 +28,9 @@ def get_db():
|
|||||||
|
|
||||||
@router.post("/", response_model=schemas.Product)
|
@router.post("/", response_model=schemas.Product)
|
||||||
def save(
|
def save(
|
||||||
data: schemas.ProductIn,
|
data: schemas.ProductIn,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
user: UserToken = Security(get_user, scopes=["products"]),
|
user: UserToken = Security(get_user, scopes=["products"]),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
item = Product(
|
item = Product(
|
||||||
@ -45,15 +45,14 @@ def save(
|
|||||||
sale_price=data.sale_price,
|
sale_price=data.sale_price,
|
||||||
is_active=data.is_active,
|
is_active=data.is_active,
|
||||||
is_purchased=data.is_purchased,
|
is_purchased=data.is_purchased,
|
||||||
is_sold=data.is_sold
|
is_sold=data.is_sold,
|
||||||
).create(db)
|
).create(db)
|
||||||
db.commit()
|
db.commit()
|
||||||
return product_info(item.id, db)
|
return product_info(item.id, db)
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
|
||||||
detail=str(e),
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -127,7 +126,8 @@ def delete(
|
|||||||
|
|
||||||
@router.get("/") # "Products"
|
@router.get("/") # "Products"
|
||||||
def show_blank(
|
def show_blank(
|
||||||
db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["products"])
|
db: Session = Depends(get_db),
|
||||||
|
user: UserToken = Security(get_user, scopes=["products"]),
|
||||||
):
|
):
|
||||||
return product_info(None, db)
|
return product_info(None, db)
|
||||||
|
|
||||||
@ -151,7 +151,11 @@ def show_list(db: Session = Depends(get_db), user: UserToken = Depends(get_user)
|
|||||||
"productYield": item.product_yield,
|
"productYield": item.product_yield,
|
||||||
"isFixture": item.is_fixture,
|
"isFixture": item.is_fixture,
|
||||||
}
|
}
|
||||||
for item in db.query(Product).order_by(desc(Product.is_active)).order_by(Product.product_group_id).order_by(Product.name).all()
|
for item in db.query(Product)
|
||||||
|
.order_by(desc(Product.is_active))
|
||||||
|
.order_by(Product.product_group_id)
|
||||||
|
.order_by(Product.name)
|
||||||
|
.all()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -180,9 +184,9 @@ async def show_term(
|
|||||||
"productYield": item.product_yield,
|
"productYield": item.product_yield,
|
||||||
"isSold": item.is_sold,
|
"isSold": item.is_sold,
|
||||||
"salePrice": item.sale_price,
|
"salePrice": item.sale_price,
|
||||||
} if extended else {
|
|
||||||
"id": item.id, "name": item.full_name, "price": item.price
|
|
||||||
}
|
}
|
||||||
|
if extended
|
||||||
|
else {"id": item.id, "name": item.full_name, "price": item.price}
|
||||||
)
|
)
|
||||||
if count is not None and index == count - 1:
|
if count is not None and index == count - 1:
|
||||||
break
|
break
|
||||||
@ -231,13 +235,9 @@ def product_info(id_: Optional[uuid.UUID], db: Session):
|
|||||||
|
|
||||||
def delete_with_data(product: Product, db: Session):
|
def delete_with_data(product: Product, db: Session):
|
||||||
suspense_product = (
|
suspense_product = (
|
||||||
db.query(Product)
|
db.query(Product).filter(Product.id == Product.suspense()).first()
|
||||||
.filter(Product.id == Product.suspense())
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
suspense_batch = (
|
|
||||||
db.query(Batch).filter(Batch.id == Batch.suspense()).first()
|
|
||||||
)
|
)
|
||||||
|
suspense_batch = db.query(Batch).filter(Batch.id == Batch.suspense()).first()
|
||||||
query = (
|
query = (
|
||||||
db.query(Voucher)
|
db.query(Voucher)
|
||||||
.options(joinedload_all(Voucher.inventories, Inventory.product, innerjoin=True))
|
.options(joinedload_all(Voucher.inventories, Inventory.product, innerjoin=True))
|
||||||
@ -264,11 +264,15 @@ def delete_with_data(product: Product, db: Session):
|
|||||||
prod_inv.tax = 0
|
prod_inv.tax = 0
|
||||||
prod_inv.discount = 0
|
prod_inv.discount = 0
|
||||||
prod_inv.batch = suspense_batch
|
prod_inv.batch = suspense_batch
|
||||||
voucher.narration += f"\nSuspense \u20B9{prod_inv.amount:,.2f} is {product.name}"
|
voucher.narration += (
|
||||||
|
f"\nSuspense \u20B9{prod_inv.amount:,.2f} is {product.name}"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
sus_inv.quantity += prod_inv.amount
|
sus_inv.quantity += prod_inv.amount
|
||||||
db.delete(prod_inv)
|
db.delete(prod_inv)
|
||||||
voucher.narration += f"\nDeleted \u20B9{prod_inv.amount:,.2f} of {product.name}"
|
voucher.narration += (
|
||||||
|
f"\nDeleted \u20B9{prod_inv.amount:,.2f} of {product.name}"
|
||||||
|
)
|
||||||
for batch in product.batches:
|
for batch in product.batches:
|
||||||
db.delete(batch)
|
db.delete(batch)
|
||||||
db.delete(product)
|
db.delete(product)
|
||||||
|
@ -5,16 +5,19 @@ from pydantic import BaseModel
|
|||||||
|
|
||||||
|
|
||||||
def to_camel(string: str) -> str:
|
def to_camel(string: str) -> str:
|
||||||
first, *others = string.split('_')
|
first, *others = string.split("_")
|
||||||
return ''.join([first] + [word.capitalize() for word in others])
|
return "".join([first] + [word.capitalize() for word in others])
|
||||||
|
|
||||||
|
|
||||||
class Client(BaseModel):
|
class ClientIn(BaseModel):
|
||||||
id_: uuid.UUID
|
|
||||||
code: int
|
|
||||||
name: str
|
name: str
|
||||||
enabled: bool
|
enabled: bool
|
||||||
otp: int
|
otp: int
|
||||||
|
|
||||||
|
|
||||||
|
class Client(ClientIn):
|
||||||
|
id_: uuid.UUID
|
||||||
|
code: int
|
||||||
creation_date: datetime
|
creation_date: datetime
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +28,7 @@ class LoginHistory(BaseModel):
|
|||||||
date: datetime
|
date: datetime
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
anystr_strip_whitespace = True
|
anystr_strip_whitespace = True
|
||||||
alias_generator = to_camel
|
alias_generator = to_camel
|
||||||
|
|
||||||
@ -36,7 +39,7 @@ class PermissionItem(BaseModel):
|
|||||||
enabled: bool
|
enabled: bool
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
|
|
||||||
|
|
||||||
class RoleIn(BaseModel):
|
class RoleIn(BaseModel):
|
||||||
@ -44,7 +47,7 @@ class RoleIn(BaseModel):
|
|||||||
permissions: List[PermissionItem]
|
permissions: List[PermissionItem]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
anystr_strip_whitespace = True
|
anystr_strip_whitespace = True
|
||||||
|
|
||||||
|
|
||||||
@ -58,7 +61,7 @@ class RoleList(BaseModel):
|
|||||||
permissions: List[str]
|
permissions: List[str]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
anystr_strip_whitespace = True
|
anystr_strip_whitespace = True
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +71,7 @@ class RoleItem(BaseModel):
|
|||||||
enabled: bool
|
enabled: bool
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
|
|
||||||
|
|
||||||
class UserIn(BaseModel):
|
class UserIn(BaseModel):
|
||||||
@ -78,7 +81,7 @@ class UserIn(BaseModel):
|
|||||||
roles: List[RoleItem]
|
roles: List[RoleItem]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
anystr_strip_whitespace = True
|
anystr_strip_whitespace = True
|
||||||
alias_generator = to_camel
|
alias_generator = to_camel
|
||||||
|
|
||||||
@ -93,7 +96,7 @@ class UserList(BaseModel):
|
|||||||
roles: List[str]
|
roles: List[str]
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
fields = {'id_': 'id'}
|
fields = {"id_": "id"}
|
||||||
anystr_strip_whitespace = True
|
anystr_strip_whitespace = True
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user