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,
)

View File

@@ -328,6 +328,7 @@ input.form-control {
.ts-dropdown {
background-color: #212529 !important;
color: #fff !important;
z-index: 9999 !important;
}
.ts-dropdown .active {

View File

@@ -127,6 +127,41 @@
</div>
</div>
{% if not show_all %}
{% set current_date = now.replace(day=1) %}
{% set prev_month = (current_date - timedelta(days=1)).strftime('%Y-%m') %}
{% set next_month = (current_date + timedelta(days=31)).replace(day=1).strftime('%Y-%m') %}
{% endif %}
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
<div>
{% if not show_all %}
<a href="{{ url_for('admin_panel', month=prev_month) }}" class="btn btn-outline-light btn-sm">
← Poprzedni
</a>
<a href="{{ url_for('admin_panel', month=next_month) }}" class="btn btn-outline-light btn-sm">
Następny →
</a>
{% endif %}
</div>
<form method="get">
<div class="input-group input-group-sm">
<span class="input-group-text bg-secondary text-white">📅</span>
<select name="month" class="form-select bg-dark text-white border-secondary" onchange="this.form.submit()">
<option value="all" {% if show_all %}selected{% endif %}>Wszystkie miesiące</option>
{% for val in month_options %}
{% set date_obj = (val ~ '-01') | todatetime %}
<option value="{{ val }}" {% if month_str==val %}selected{% endif %}>
{{ date_obj.strftime('%B %Y')|capitalize }}
</option>
{% endfor %}
</select>
</div>
</form>
</div>
<div class="card bg-dark text-white mb-5">
<div class="card-body">
<h3 class="mt-4">📄 Wszystkie listy zakupowe</h3>

View File

@@ -13,7 +13,7 @@
<div class="card bg-dark text-white mb-5">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-dark table-striped table-hover align-middle mb-0">
<table class="table table-dark table-striped table-hover align-middle mb-0 sortable">
<thead>
<tr>
<th scope="col">ID</th>
@@ -57,11 +57,4 @@
{% block scripts %}
<script src="https://cdn.jsdelivr.net/npm/tom-select/dist/js/tom-select.complete.min.js"></script>
<script src="{{ url_for('static_bp.serve_js', filename='admin_mass_categories.js') }}"></script>
<style>
.ts-dropdown {
z-index: 9999 !important;
/* 🔹 dropdown zawsze na wierzchu */
}
</style>
{% endblock %}