Moved the session to core from routers

This commit is contained in:
tanshu 2020-05-12 09:55:33 +05:30
parent d62119882d
commit 8dcda9cb56
23 changed files with 141 additions and 137 deletions

@ -1,30 +1,30 @@
from datetime import date, timedelta
def session_current_date(session):
def get_date(session) -> str:
if "date" not in session:
session["date"] = date.today().strftime("%d-%b-%Y")
return session["date"]
def session_current_date_set(session, date_):
def set_date(session, date_):
session["date"] = date_
return session["date"]
def session_period_start(session):
def get_start_date(session):
if "start" not in session:
session["start"] = get_first_day(date.today()).strftime("%d-%b-%Y")
return session["start"]
def session_period_finish(session):
def get_finish_date(session):
if "finish" not in session:
session["finish"] = get_last_day(date.today()).strftime("%d-%b-%Y")
return session["finish"]
def session_period_set(start, finish, session):
def set_period(start, finish, session):
session["start"] = (
start if isinstance(start, str) else start.strftime("%d-%b-%Y")
)

@ -9,9 +9,9 @@ from ..schemas.auth import UserToken
from ..models.master import Employee
from ..models.voucher import Attendance
from ..routers.fingerprint import get_prints
from ..routers.services.session import session_current_date
from ..db.session import SessionLocal
from ..core.security import get_current_active_user as get_user
from ..core.session import get_date
from fastapi import APIRouter
@ -29,7 +29,7 @@ def get_db():
@router.get("/")
def attendance_blank(db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["attendance"])):
return {"date": session_current_date(request), "body": []}
return {"date": get_date(request), "body": []}
@router.get("/{date}") # "Attendance"

@ -3,7 +3,7 @@ from sqlalchemy import or_
from ..models.master import AttendanceType, Employee
from ..models.voucher import Attendance
from .attendance import daterange
from .services.session import session_period_start, session_period_finish
from ..core.session import get_start_date, get_finish_date
from fastapi import APIRouter
@ -11,8 +11,8 @@ router = APIRouter()
@router.get("/") # "Attendance", renderer="csv"
def get_report(request):
start_date = request.GET.get("StartDate", session_period_start(request))
finish_date = request.GET.get("FinishDate", session_period_finish(request))
start_date = request.GET.get("StartDate", get_start_date(request))
finish_date = request.GET.get("FinishDate", get_finish_date(request))
start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y")
finish_date = datetime.datetime.strptime(finish_date, "%d-%b-%Y")
header, report = attendance_record(start_date, finish_date, request.dbsession)

@ -7,10 +7,9 @@ from brewman.models.master import Employee
from brewman.models.validation_exception import ValidationError
from brewman.models.voucher import Attendance
from brewman.routers.fingerprint import get_prints
from brewman.routers.services.session import (
session_period_start,
session_period_finish,
session_current_date,
from ..core.session import (
get_start_date,
get_finish_date,
)
from fastapi import APIRouter
@ -21,8 +20,8 @@ router = APIRouter()
@router.get("/") # "Attendance"
def employee_attendance_blank(request):
return {
"startDate": session_period_start(request),
"finishDate": session_period_finish(request),
"startDate": get_start_date(request),
"finishDate": get_finish_date(request),
"employee": None,
"body": [],
}
@ -37,8 +36,8 @@ def employee_attendance_report(request):
)
if employee is None:
raise ValidationError("Employee id is wrong")
start_date = request.GET.get("s", session_period_start(request))
finish_date = request.GET.get("f", session_period_finish(request))
start_date = request.GET.get("s", get_start_date(request))
finish_date = request.GET.get("f", get_finish_date(request))
info = {
"startDate": start_date,
"finishDate": finish_date,

@ -7,10 +7,10 @@ from sqlalchemy import desc, or_, func
from brewman.models.master import Recipe, Product, RecipeItem, CostCentre
from brewman.models.validation_exception import ValidationError
from brewman.models.voucher import Voucher, Inventory, VoucherType, Journal
from brewman.routers.services.session import (
session_period_start,
session_period_finish,
session_period_set,
from ..core.session import (
get_start_date,
get_finish_date,
set_period,
)
from fastapi import APIRouter
@ -95,7 +95,7 @@ def save(request):
save_recipe(recipe, request.dbsession)
transaction.commit()
session_period_set(valid_from, valid_to, request)
set_period(valid_from, valid_to, request)
return recipe_info(recipe.id, request)
@ -328,8 +328,8 @@ def recipe_info(id_, request):
if id_ is None:
info = {
"Quantity": 1,
"ValidFrom": session_period_start(request),
"ValidTo": session_period_finish(request),
"ValidFrom": get_start_date(request),
"ValidTo": get_finish_date(request),
"RecipeItems": [],
}
else:

@ -11,10 +11,10 @@ from brewman.models.master import AccountType, AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.reports.closing_stock import get_closing_stock
from brewman.routers.reports.profit_loss import get_accumulated_profit
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,7 +34,7 @@ def report_blank(
request: Request,
user: UserToken = Security(get_user, scopes=["balance-sheet"]),
):
return {"date": session_period_finish(request.session), "body": [], "footer": []}
return {"date": get_finish_date(request.session), "body": [], "footer": []}
@router.get("/{date}")
@ -45,7 +45,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["balance-sheet"]),
):
body, footer = build_balance_sheet(date, db)
session_period_set(session_period_start(request.session), date, request.session)
set_period(get_start_date(request.session), date, request.session)
return {"date": date, "body": body, "footer": footer}

@ -10,10 +10,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase, AccountType
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,8 +34,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["cash-flow"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
"footer": {},
}
@ -50,7 +50,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["cash-flow"]),
):
body, footer = build_report(s, f, db)
session_period_set(s, f, request.session)
set_period(s, f, request.session)
return {
"startDate": s,
"finishDate": f,
@ -69,7 +69,7 @@ def report_id(
user: UserToken = Security(get_user, scopes=["cash-flow"]),
):
details, footer = build_report_id(id_, s, f, db)
session_period_set(s, f, request.session)
set_period(s, f, request.session)
return {
"startDate": s,
"finishDate": f,

@ -9,10 +9,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import Product, CostCentre
from brewman.models.voucher import Voucher, Journal, Inventory
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -32,7 +32,7 @@ def report_blank(
request: Request,
user: UserToken = Security(get_user, scopes=["closing-stock"]),
):
return {"date": session_period_finish(request.session), "body": []}
return {"date": get_finish_date(request.session), "body": []}
@router.get("/{date}")
@ -42,7 +42,7 @@ def report_data(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["closing-stock"]),
):
session_period_set(session_period_start(request.session), date, request.session)
set_period(get_start_date(request.session), date, request.session)
return {"date": date, "body": build_report(date, db)}

@ -7,10 +7,10 @@ from ...schemas.auth import UserToken
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -31,8 +31,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["daybook"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
}
@ -46,7 +46,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["daybook"]),
):
body = build_report(start, finish, db)
session_period_set(start, finish, request.session)
set_period(start, finish, request.session)
return {"startDate": start, "finishDate": finish, "body": body}

@ -10,10 +10,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,8 +34,8 @@ def show_blank(
user: UserToken = Security(get_user, scopes=["ledger"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"account": None,
"body": [],
}
@ -51,10 +51,10 @@ def show_data(
user: UserToken = Security(get_user, scopes=["ledger"]),
):
account = db.query(AccountBase).filter(AccountBase.id == id_).first()
start_date = s if s is not None else session_period_start(request.session)
finish_date = f if f is not None else session_period_finish(request.session)
start_date = s if s is not None else get_start_date(request.session)
finish_date = f if f is not None else get_finish_date(request.session)
body = build_report(account.id, start_date, finish_date, db)
session_period_set(start_date, finish_date, request.session)
set_period(start_date, finish_date, request.session)
return {
"startDate": start_date,
"finishDate": finish_date,

@ -9,10 +9,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -33,8 +33,8 @@ def show_blank(
user: UserToken = Security(get_user, scopes=["net-transactions"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
}
@ -47,7 +47,7 @@ def show_data(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["net-transactions"]),
):
session_period_set(start, finish, request.session)
set_period(start, finish, request.session)
return {
"startDate": start,
"finishDate": finish,

@ -10,10 +10,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import Product, CostCentre
from brewman.models.voucher import Voucher, Journal, VoucherType, Inventory
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,8 +34,8 @@ def show_blank(
user: UserToken = Security(get_user, scopes=["product-ledger"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"product": None,
"body": [],
}
@ -51,10 +51,10 @@ def show_data(
user: UserToken = Security(get_user, scopes=["product-ledger"]),
):
product = db.query(Product).filter(Product.id == id_).first()
start_date = s if s is not None else session_period_start(request.session)
finish_date = f if f is not None else session_period_finish(request.session)
start_date = s if s is not None else get_start_date(request.session)
finish_date = f if f is not None else get_finish_date(request.session)
body = build_report(product.id, start_date, finish_date, db)
session_period_set(start_date, finish_date, request.session)
set_period(start_date, finish_date, request.session)
return {
"startDate": start_date,
"finishDate": finish_date,

@ -10,10 +10,10 @@ from ...db.session import SessionLocal
from brewman.models.master import AccountType, AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.reports.closing_stock import get_opening_stock, get_closing_stock
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,8 +34,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["profit-&-loss"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
"footer": {},
}
@ -50,7 +50,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["profit-&-loss"]),
):
body, footer = build_profit_loss(start, finish, db)
session_period_set(start, finish, request.session)
set_period(start, finish, request.session)
return {
"startDate": start,
"finishDate": finish,

@ -7,10 +7,10 @@ from ...schemas.auth import UserToken
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.voucher import Voucher, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -31,8 +31,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["purchase-entries"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
}
@ -46,7 +46,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["net-transactions"]),
):
body = build_report(start, finish, db)
session_period_set(start, finish, request.session)
set_period(start, finish, request.session)
return {"startDate": start, "finishDate": finish, "body": body}

@ -10,10 +10,10 @@ from ...db.session import SessionLocal
from brewman.models.master import CostCentre, Product
from brewman.models.voucher import Voucher, Journal, Inventory, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,8 +34,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["purchases"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
"footer": {},
}
@ -50,7 +50,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["purchases"]),
):
body, footer = build_report(start, finish, db)
session_period_set(start, finish, request.session)
set_period(start, finish, request.session)
return {
"startDate": start,
"finishDate": finish,

@ -10,10 +10,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase, CostCentre, Product, ProductGroup
from brewman.models.voucher import Voucher, Journal, Inventory
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -34,8 +34,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["raw-material-cost"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
"footer": {},
}
@ -50,7 +50,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["raw-material-cost"]),
):
body, footer = build_report(s, f, db)
session_period_set(s, f, request.session)
set_period(s, f, request.session)
return {
"startDate": s,
"finishDate": f,
@ -69,7 +69,7 @@ def report_id(
user: UserToken = Security(get_user, scopes=["raw-material-cost"]),
):
body = build_report_id(id_, s, f, db)
session_period_set(s, f, request.session)
set_period(s, f, request.session)
return {
"id": id_,
"startDate": s,

@ -11,10 +11,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -35,8 +35,8 @@ def show_blank(
user: UserToken = Security(get_user, scopes=["reconcile"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"account": None,
"body": [],
}
@ -52,10 +52,10 @@ def show_data(
user: UserToken = Security(get_user, scopes=["reconcile"]),
):
account = db.query(AccountBase).filter(AccountBase.id == id_).first()
start_date = s if s is not None else session_period_start(request.session)
finish_date = f if f is not None else session_period_finish(request.session)
start_date = s if s is not None else get_start_date(request.session)
finish_date = f if f is not None else get_finish_date(request.session)
body = build_report(account.id, start_date, finish_date, db)
session_period_set(start_date, finish_date, request.session)
set_period(start_date, finish_date, request.session)
return {
"startDate": start_date,
"finishDate": finish_date,
@ -168,8 +168,8 @@ def save(
user: UserToken = Security(get_user, scopes=["reconcile"]),
):
account = db.query(AccountBase).filter(AccountBase.id == id_).first()
start_date = s if s is not None else session_period_start(request.session)
finish_date = f if f is not None else session_period_finish(request.session)
start_date = s if s is not None else get_start_date(request.session)
finish_date = f if f is not None else get_finish_date(request.session)
raise Exception("not fixed")
# for item in request.json_body["body"]:
# if "id" not in item or item["id"] is None:

@ -9,10 +9,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import Product, CostCentre
from brewman.models.voucher import Voucher, Journal, VoucherType, Inventory
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -33,8 +33,8 @@ def report_blank(
user: UserToken = Security(get_user, scopes=["stock-movement"]),
):
return {
"startDate": session_period_start(request.session),
"finishDate": session_period_finish(request.session),
"startDate": get_start_date(request.session),
"finishDate": get_finish_date(request.session),
"body": [],
}
@ -48,7 +48,7 @@ def report_data(
user: UserToken = Security(get_user, scopes=["stock-movement"]),
):
body = build_stock_movement(start, finish, db)
session_period_set( start, finish, request.session)
set_period(start, finish, request.session)
return {"startDate": start, "finishDate": finish, "body": body}

@ -9,10 +9,10 @@ from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from brewman.models.master import AccountBase
from brewman.models.voucher import Voucher, Journal, VoucherType
from brewman.routers.services.session import (
session_period_set,
session_period_start,
session_period_finish,
from ...core.session import (
set_period,
get_start_date,
get_finish_date,
)
router = APIRouter()
@ -32,7 +32,7 @@ def report_blank(
request: Request,
user: UserToken = Security(get_user, scopes=["trial-balance"]),
):
return {"date": session_period_finish(request.session), "body": []}
return {"date": get_finish_date(request.session), "body": []}
@router.get("/{date}")
@ -42,7 +42,7 @@ def report_data(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["trial-balance"]),
):
session_period_set(session_period_start(request.session), date, request.session)
set_period(get_start_date(request.session), date, request.session)
return {"date": date, "body": build_report(date, db)}

@ -28,7 +28,12 @@ from .service_charge import service_charge_create_voucher, service_charge_update
from ..session import get_first_day
@router.post("/api/voucher/{id}", request_param="p") # "Post Vouchers"
from fastapi import APIRouter, Depends, Security, Request
router = APIRouter()
@router.post("/api/voucher/{id}") # , request_param="p" "Post Vouchers"
def voucher_post(request):
user = (
request.dbsession.query(User)

@ -1,4 +1,4 @@
from brewman.routers.services.session import session_current_date
from ....core.session import get_date
from brewman.routers.services.voucher import blank_voucher
@ -66,7 +66,7 @@ class EmptyVoucher(object):
voucher_type = self.request.GET.get("t", None)
if additional_info is None:
additional_info = {}
additional_info["date"] = session_current_date(self.request)
additional_info["date"] = get_date(self.request)
additional_info["type"] = voucher_type
return blank_voucher(additional_info, self.request.dbsession)

@ -5,7 +5,7 @@ from brewman.models.auth import User
from brewman.models.validation_exception import ValidationError
from brewman.models.voucher import Voucher
from brewman.routers import get_lock_info
from brewman.routers.services.session import session_current_date_set
from ....core.session import set_date
from . import (
voucher_info,
journal_create_voucher,
@ -114,7 +114,7 @@ class SaveVoucher(object):
self.json, self.user, self.request.dbsession
)
transaction.commit()
session_current_date_set(self.request, self.json["date"])
set_date(self.request, self.json["date"])
new_voucher = (
self.request.dbsession.query(Voucher)
.filter(Voucher.id == voucher.id)

@ -7,7 +7,7 @@ from pyramid.response import Response
from brewman.models.auth import User
from brewman.models.voucher import Voucher
from brewman.routers import get_lock_info
from brewman.routers.services.session import session_current_date_set
from ....core.session import set_date
from . import (
voucher_info,
issue_update_voucher,
@ -150,7 +150,7 @@ class UpdateVoucher(object):
self.voucher, self.json, self.user, self.request.dbsession
)
transaction.commit()
session_current_date_set(self.request, self.json["date"])
set_date(self.request, self.json["date"])
new_voucher = (
self.request.dbsession.query(Voucher)
.filter(Voucher.id == voucher.id)