new fuctions

This commit is contained in:
Mateusz Gruszczyński
2025-08-11 23:44:01 +02:00
parent dc8bfacdf6
commit 9d5630bde3
4 changed files with 88 additions and 11 deletions

54
app.py
View File

@@ -1182,6 +1182,10 @@ def file_mtime_filter(path):
# return datetime.utcnow()
return datetime.now(timezone.utc)
@app.template_filter("todatetime")
def to_datetime_filter(s):
return datetime.strptime(s, "%Y-%m-%d")
@app.template_filter("filesizeformat")
def filesizeformat_filter(path):
@@ -2126,20 +2130,59 @@ def crop_receipt_user():
@login_required
@admin_required
def admin_panel():
now = datetime.now(timezone.utc)
month_str = request.args.get("month")
show_all = (month_str == "all")
if not show_all:
try:
if month_str:
year, month = map(int, month_str.split("-"))
now = datetime(year, month, 1, tzinfo=timezone.utc)
else:
now = datetime.now(timezone.utc)
month_str = now.strftime("%Y-%m")
except Exception:
now = datetime.now(timezone.utc)
month_str = now.strftime("%Y-%m")
start = now
end = (start + timedelta(days=31)).replace(day=1)
else:
now = datetime.now(timezone.utc)
start = end = None
# Liczniki globalne
user_count = User.query.count()
list_count = ShoppingList.query.count()
item_count = Item.query.count()
all_lists = ShoppingList.query.options(
base_query = ShoppingList.query.options(
joinedload(ShoppingList.owner),
joinedload(ShoppingList.items),
joinedload(ShoppingList.receipts),
joinedload(ShoppingList.expenses),
joinedload(ShoppingList.categories),
).all()
)
if not show_all and start and end:
base_query = base_query.filter(ShoppingList.created_at >= start, ShoppingList.created_at < end)
all_lists = base_query.all()
# tylko listy z danych miesięcy
if db.engine.name == "sqlite":
month_col = func.strftime('%Y-%m', ShoppingList.created_at)
else:
month_col = func.to_char(ShoppingList.created_at, 'YYYY-MM')
active_months = (
db.session.query(month_col.label("month"))
.filter(ShoppingList.created_at != None)
.distinct()
.order_by("month") # bez min()
.all()
)
month_options = [row.month for row in active_months]
all_ids = [l.id for l in all_lists]
@@ -2213,6 +2256,7 @@ def admin_panel():
)
purchased_items_count = Item.query.filter_by(purchased=True).count()
expense_summary = get_admin_expense_summary()
process = psutil.Process(os.getpid())
app_mem = process.memory_info().rss // (1024 * 1024)
@@ -2248,6 +2292,10 @@ def admin_panel():
table_count=table_count,
record_total=record_total,
uptime_minutes=uptime_minutes,
timedelta=timedelta,
show_all=show_all,
month_str=month_str,
month_options=month_options,
)