Fix: The lock was not working because:

1. The Account Types sent were sqlqueries and not integer literals
2. Voucher Types and Account Types were being compared to VoucherTypesSelected and AccountTypesSelected objects and not their ids.
This commit is contained in:
Amritanshu Agrawal 2021-09-10 21:46:47 +05:30
parent dbf0ecf0d8
commit ca352649f0
10 changed files with 127 additions and 65 deletions

View File

@ -46,20 +46,16 @@ def get_lock_info(
data = sorted(data, key=lambda d: d["index"], reverse=True) data = sorted(data, key=lambda d: d["index"], reverse=True)
for it in data: for it in data:
li: LockInformation = LockInformation( li: LockInformation = LockInformation(
voucherTypes=[ voucherTypes=[VoucherTypesSelected(id=vt["id_"], name=vt["name"]) for vt in it["voucher_types"]],
VoucherTypesSelected(id=vt["id_"], name=VoucherType.by_id(vt["id_"]).name) for vt in it["voucher_types"] accountTypes=[AccountTypesSelected(id=at["id_"], name=at["name"]) for at in it["account_types"]],
],
accountTypes=[
AccountTypesSelected(id=at["id_"], name=AccountType.by_id(at["id_"]).name) for at in it["account_types"]
],
start=LockDate(days=it["start"]["days"], date=it["start"]["date_"]), start=LockDate(days=it["start"]["days"], date=it["start"]["date_"]),
finish=LockDate(days=it["finish"]["days"], date=it["finish"]["date_"]), finish=LockDate(days=it["finish"]["days"], date=it["finish"]["date_"]),
index=it["index"], index=it["index"],
) )
# Data format # Data format
if len(li.voucher_types) != 0 and voucher_type not in li.voucher_types: if len(li.voucher_types) != 0 and voucher_type not in [vt.id_ for vt in li.voucher_types]:
continue continue
if len(li.account_types) != 0 and len(set(account_types) & set(li.account_types)) == 0: if len(li.account_types) != 0 and len(set(account_types) & set([at.id_ for at in li.account_types])) == 0:
continue continue
if li.start.days is not None: if li.start.days is not None:

View File

@ -61,10 +61,16 @@ def save_route(
def save(data: schema_in.EmployeeBenefitIn, date_: date, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.EmployeeBenefitIn, date_: date, user: UserToken, db: Session) -> Voucher:
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee.id_ for dj in data.employee_benefits])) db.execute(
select(distinct(AccountBase.type)).where(
AccountBase.id.in_([dj.employee.id_ for dj in data.employee_benefits])
)
)
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_).id, account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_423_LOCKED, status_code=status.HTTP_423_LOCKED,
@ -174,12 +180,16 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.EmployeeBenefitIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.EmployeeBenefitIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
AccountBase.id.in_( select(distinct(AccountBase.type)).where(
[dj.employee.id_ for dj in data.employee_benefits] + [vj.account_id for vj in voucher.journals] AccountBase.id.in_(
[dj.employee.id_ for dj in data.employee_benefits] + [vj.account_id for vj in voucher.journals]
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -61,10 +61,14 @@ def save_route(
def save(data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher:
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee_id for dj in data.incentives])) db.execute(
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.employee_id for dj in data.incentives]))
)
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_).id, account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_423_LOCKED, status_code=status.HTTP_423_LOCKED,
@ -137,10 +141,16 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.IncentiveIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
AccountBase.id.in_([dj.employee_id for dj in data.incentives] + [vj.account_id for vj in voucher.journals]) select(distinct(AccountBase.type)).where(
AccountBase.id.in_(
[dj.employee_id for dj in data.incentives] + [vj.account_id for vj in voucher.journals]
)
)
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -63,8 +63,10 @@ def save_route(
def save(data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]): def save(data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]):
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute(select(distinct(AccountBase.type)).where(AccountBase.id.in_(product_accounts))) account_types = (
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) db.execute(select(distinct(AccountBase.type)).where(AccountBase.id.in_(product_accounts))).scalars().all()
)
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_).id, account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_423_LOCKED, status_code=status.HTTP_423_LOCKED,
@ -189,13 +191,17 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]): def update_voucher(id_: uuid.UUID, data: schema_in.IssueIn, user: UserToken, db: Session) -> (Voucher, Optional[bool]):
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id.in_([vj.account_id for vj in voucher.journals]), or_(
AccountBase.id.in_(product_accounts), AccountBase.id.in_([vj.account_id for vj in voucher.journals]),
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -54,10 +54,14 @@ def save_route(
def save(data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher:
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.account.id_ for dj in data.journals])) db.execute(
select(distinct(AccountBase.type)).where(AccountBase.id.in_([dj.account.id_ for dj in data.journals]))
)
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_).id, account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_423_LOCKED, status_code=status.HTTP_423_LOCKED,
@ -114,10 +118,16 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.JournalIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
AccountBase.id.in_([dj.account.id_ for dj in data.journals] + [vj.account_id for vj in voucher.journals]) select(distinct(AccountBase.type)).where(
AccountBase.id.in_(
[dj.account.id_ for dj in data.journals] + [vj.account_id for vj in voucher.journals]
)
)
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -62,15 +62,19 @@ def save_route(
def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id == data.vendor.id_, or_(
AccountBase.id.in_(product_accounts), AccountBase.id == data.vendor.id_,
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_).id, account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_423_LOCKED, status_code=status.HTTP_423_LOCKED,
@ -170,13 +174,17 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]), or_(
AccountBase.id.in_(product_accounts), AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]),
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -62,15 +62,19 @@ def save_route(
def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def save(data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id == data.vendor.id_, or_(
AccountBase.id.in_(product_accounts), AccountBase.id == data.vendor.id_,
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_), account_types, db) allowed, message = get_lock_info([data.date_], VoucherType.by_name(data.type_).id, account_types, db)
if not allowed: if not allowed:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_423_LOCKED, status_code=status.HTTP_423_LOCKED,
@ -177,13 +181,17 @@ def update_route(
def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher: def update_voucher(id_: uuid.UUID, data: schema_in.PurchaseIn, user: UserToken, db: Session) -> Voucher:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories])) product_accounts = select(Product.account_id).where(Product.id.in_([i.product.id_ for i in data.inventories]))
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where( db.execute(
or_( select(distinct(AccountBase.type)).where(
AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]), or_(
AccountBase.id.in_(product_accounts), AccountBase.id.in_([data.vendor.id_] + [vj.account_id for vj in voucher.journals]),
AccountBase.id.in_(product_accounts),
)
) )
) )
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date, data.date_], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -41,8 +41,14 @@ def post_voucher(
try: try:
with SessionFuture() as db: with SessionFuture() as db:
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals])) db.execute(
select(distinct(AccountBase.type)).where(
AccountBase.id.in_([vj.account_id for vj in voucher.journals])
)
)
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db)
if not allowed: if not allowed:
@ -91,8 +97,12 @@ def delete_voucher(
voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one() voucher: Voucher = db.execute(select(Voucher).where(Voucher.id == id_)).scalar_one()
images = db.execute(select(DbImage).where(DbImage.resource_id == voucher.id)).scalars().all() images = db.execute(select(DbImage).where(DbImage.resource_id == voucher.id)).scalars().all()
check_delete_permissions(voucher, user) check_delete_permissions(voucher, user)
account_types = db.execute( account_types = (
select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals])) db.execute(
select(distinct(AccountBase.type)).where(AccountBase.id.in_([vj.account_id for vj in voucher.journals]))
)
.scalars()
.all()
) )
allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db) allowed, message = get_lock_info([voucher.date], voucher.type, account_types, db)
if not allowed: if not allowed:

View File

@ -7,9 +7,8 @@ RUN npm install --unsafe-perm && /app/overlord/node_modules/.bin/ng build
FROM python:latest FROM python:latest
LABEL maintainer="Amritanshu <docker@tanshu.com>" LABEL maintainer="Amritanshu <docker@tanshu.com>"
COPY --from=builder /app/brewman /app
COPY --from=builder /app/frontend /app/frontend ADD https://git.tanshu.com/tanshu/brewman/raw/tag/latest/brewman/pyproject.toml /app/pyproject.toml
WORKDIR /app
# Install Poetry # Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | POETRY_HOME=/opt/poetry python && \ RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | POETRY_HOME=/opt/poetry python && \
@ -17,10 +16,15 @@ RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/inst
ln -s /opt/poetry/bin/poetry && \ ln -s /opt/poetry/bin/poetry && \
poetry config virtualenvs.create false poetry config virtualenvs.create false
WORKDIR /app
# Allow installing dev dependencies to run tests # Allow installing dev dependencies to run tests
ARG INSTALL_DEV=false ARG INSTALL_DEV=false
RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi" RUN bash -c "if [ $INSTALL_DEV == 'true' ] ; then poetry install --no-root ; else poetry install --no-root --no-dev ; fi"
COPY --from=builder /app/brewman /app
COPY --from=builder /app/frontend /app/frontend
ENV PYTHONPATH=/app ENV PYTHONPATH=/app
EXPOSE 80 EXPOSE 80
VOLUME /frontend VOLUME /frontend
@ -30,4 +34,4 @@ RUN chmod 777 /app/docker-entrypoint.sh \
&& ln -s /app/docker-entrypoint.sh / && ln -s /app/docker-entrypoint.sh /
ENTRYPOINT ["docker-entrypoint.sh"] ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["python", "-m", "brewman"] CMD ["poetry", "run", "python", "-m", "brewman"]

View File

@ -2,7 +2,7 @@
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P ) parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P )
cd "$parent_path" || exit cd "$parent_path" || exit
if [ 1 -ne "$#" ] if [ 1 -eq "$#" ]
then then
echo "Version bump to $1" echo "Version bump to $1"
sed --in-place --regexp-extended 's/"([0-9].[0-9].[0-9])"/"'"$1"'"/g' brewman/brewman/__version__.py sed --in-place --regexp-extended 's/"([0-9].[0-9].[0-9])"/"'"$1"'"/g' brewman/brewman/__version__.py