1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
"""
PodcastItLater Admin Interface.
Admin pages and functionality for managing users and queue items.
"""
# : out podcastitlater-admin
# : dep ludic
# : dep httpx
# : dep starlette
# : dep pytest
# : dep pytest-asyncio
# : dep pytest-mock
import Biz.PodcastItLater.Core as Core
import Biz.PodcastItLater.UI as UI
import ludic.catalog.layouts as layouts
import ludic.html as html
# i need to import these unused because bild cannot get local transitive python
# dependencies yet
import Omni.App as App # noqa: F401
import Omni.Log as Log # noqa: F401
import Omni.Test as Test # noqa: F401
import sys
import typing
from ludic.attrs import Attrs
from ludic.components import Component
from ludic.types import AnyChildren
from ludic.web import Request
from ludic.web.datastructures import FormData
from ludic.web.responses import Response
from typing import override
class AdminUsersAttrs(Attrs):
"""Attributes for AdminUsers component."""
users: list[dict[str, typing.Any]]
class StatusBadgeAttrs(Attrs):
"""Attributes for StatusBadge component."""
status: str
count: int | None
class StatusBadge(Component[AnyChildren, StatusBadgeAttrs]):
"""Display a status badge with optional count."""
@override
def render(self) -> html.span:
status = self.attrs["status"]
count = self.attrs.get("count", None)
text = f"{status.upper()}: {count}" if count is not None else status
badge_class = self.get_status_badge_class(status)
return html.span(
text,
classes=["badge", badge_class, "me-3" if count is not None else ""],
)
@staticmethod
def get_status_badge_class(status: str) -> str:
"""Get Bootstrap badge class for status."""
return {
"pending": "bg-warning text-dark",
"processing": "bg-primary",
"completed": "bg-success",
"active": "bg-success",
"error": "bg-danger",
"cancelled": "bg-secondary",
"disabled": "bg-danger",
}.get(status, "bg-secondary")
class TruncatedTextAttrs(Attrs):
"""Attributes for TruncatedText component."""
text: str
max_length: int
max_width: str
class TruncatedText(Component[AnyChildren, TruncatedTextAttrs]):
"""Display truncated text with tooltip."""
@override
def render(self) -> html.div:
text = self.attrs["text"]
max_length = self.attrs["max_length"]
max_width = self.attrs.get("max_width", "200px")
truncated = (
text[:max_length] + "..." if len(text) > max_length else text
)
return html.div(
truncated,
title=text,
classes=["text-truncate"],
style={"max-width": max_width},
)
class ActionButtonsAttrs(Attrs):
"""Attributes for ActionButtons component."""
job_id: int
status: str
class ActionButtons(Component[AnyChildren, ActionButtonsAttrs]):
"""Render action buttons for queue items."""
@override
def render(self) -> html.div:
job_id = self.attrs["job_id"]
status = self.attrs["status"]
buttons = []
if status != "completed":
buttons.append(
html.button(
html.i(classes=["bi", "bi-arrow-clockwise", "me-1"]),
"Retry",
hx_post=f"/queue/{job_id}/retry",
hx_target="body",
hx_swap="outerHTML",
classes=["btn", "btn-sm", "btn-success", "me-1"],
disabled=status == "completed",
),
)
buttons.append(
html.button(
html.i(classes=["bi", "bi-trash", "me-1"]),
"Delete",
hx_delete=f"/queue/{job_id}",
hx_confirm="Are you sure you want to delete this queue item?",
hx_target="body",
hx_swap="outerHTML",
classes=["btn", "btn-sm", "btn-danger"],
),
)
return html.div(
*buttons,
classes=["btn-group"],
)
class QueueTableRowAttrs(Attrs):
"""Attributes for QueueTableRow component."""
item: dict[str, typing.Any]
class QueueTableRow(Component[AnyChildren, QueueTableRowAttrs]):
"""Render a single queue table row."""
@override
def render(self) -> html.tr:
item = self.attrs["item"]
return html.tr(
html.td(str(item["id"])),
html.td(
TruncatedText(
text=item["url"],
max_length=Core.TITLE_TRUNCATE_LENGTH,
max_width="300px",
),
),
html.td(
TruncatedText(
text=item.get("title") or "-",
max_length=Core.TITLE_TRUNCATE_LENGTH,
),
),
html.td(item["email"] or "-"),
html.td(StatusBadge(status=item["status"])),
html.td(str(item.get("retry_count", 0))),
html.td(html.small(item["created_at"], classes=["text-muted"])),
html.td(
TruncatedText(
text=item["error_message"] or "-",
max_length=Core.ERROR_TRUNCATE_LENGTH,
)
if item["error_message"]
else html.span("-", classes=["text-muted"]),
),
html.td(ActionButtons(job_id=item["id"], status=item["status"])),
)
class EpisodeTableRowAttrs(Attrs):
"""Attributes for EpisodeTableRow component."""
episode: dict[str, typing.Any]
class EpisodeTableRow(Component[AnyChildren, EpisodeTableRowAttrs]):
"""Render a single episode table row."""
@override
def render(self) -> html.tr:
episode = self.attrs["episode"]
return html.tr(
html.td(str(episode["id"])),
html.td(
TruncatedText(
text=episode["title"],
max_length=Core.TITLE_TRUNCATE_LENGTH,
),
),
html.td(
html.a(
html.i(classes=["bi", "bi-play-circle", "me-1"]),
"Listen",
href=episode["audio_url"],
target="_blank",
classes=["btn", "btn-sm", "btn-outline-primary"],
),
),
html.td(
f"{episode['duration']}s" if episode["duration"] else "-",
),
html.td(
f"{episode['content_length']:,} chars"
if episode["content_length"]
else "-",
),
html.td(html.small(episode["created_at"], classes=["text-muted"])),
)
class UserTableRowAttrs(Attrs):
"""Attributes for UserTableRow component."""
user: dict[str, typing.Any]
class UserTableRow(Component[AnyChildren, UserTableRowAttrs]):
"""Render a single user table row."""
@override
def render(self) -> html.tr:
user = self.attrs["user"]
return html.tr(
html.td(user["email"]),
html.td(html.small(user["created_at"], classes=["text-muted"])),
html.td(StatusBadge(status=user.get("status", "pending"))),
html.td(
html.select(
html.option(
"Pending",
value="pending",
selected=user.get("status") == "pending",
),
html.option(
"Active",
value="active",
selected=user.get("status") == "active",
),
html.option(
"Disabled",
value="disabled",
selected=user.get("status") == "disabled",
),
name="status",
hx_post=f"/admin/users/{user['id']}/status",
hx_trigger="change",
hx_target="body",
hx_swap="outerHTML",
classes=["form-select", "form-select-sm"],
),
),
)
def create_table_header(columns: list[str]) -> html.thead:
"""Create a table header with given column names."""
return html.thead(
html.tr(*[html.th(col, scope="col") for col in columns]),
classes=["table-light"],
)
class AdminUsers(Component[AnyChildren, AdminUsersAttrs]):
"""Admin view for managing users."""
@override
def render(self) -> html.html:
users = self.attrs["users"]
return html.html(
html.head(
html.meta(charset="utf-8"),
html.meta(
name="viewport",
content="width=device-width, initial-scale=1",
),
html.meta(
name="color-scheme",
content="light dark",
),
html.title("PodcastItLater - User Management"),
UI.create_htmx_script(),
),
html.body(
UI.create_bootstrap_styles(),
# Auto dark mode CSS (must come after Bootstrap)
UI.create_auto_dark_mode_style(),
html.div(
html.h1(
"PodcastItLater - User Management",
classes=["mb-4"],
),
html.div(
html.a(
html.i(classes=["bi", "bi-arrow-left", "me-2"]),
"Back to Admin",
href="/admin",
classes=["btn", "btn-outline-primary", "mb-3"],
),
),
self._render_users_table(users),
id="admin-users-content",
classes=["container", "my-4"],
),
),
)
@staticmethod
def _render_users_table(
users: list[dict[str, typing.Any]],
) -> html.div:
"""Render users table."""
return html.div(
html.h2("All Users", classes=["mb-3"]),
html.div(
html.table(
create_table_header([
"Email",
"Created At",
"Status",
"Actions",
]),
html.tbody(*[UserTableRow(user=user) for user in users]),
classes=["table", "table-hover", "table-striped"],
),
classes=["table-responsive"],
),
)
class AdminViewAttrs(Attrs):
"""Attributes for AdminView component."""
queue_items: list[dict[str, typing.Any]]
episodes: list[dict[str, typing.Any]]
status_counts: dict[str, int]
class AdminView(Component[AnyChildren, AdminViewAttrs]):
"""Admin view showing all queue items and episodes in tables."""
@override
def render(self) -> html.html:
queue_items = self.attrs["queue_items"]
episodes = self.attrs["episodes"]
status_counts = self.attrs.get("status_counts", {})
return html.html(
html.head(
html.meta(charset="utf-8"),
html.meta(
name="viewport",
content="width=device-width, initial-scale=1",
),
html.meta(
name="color-scheme",
content="light dark",
),
html.title("PodcastItLater - Admin Queue Status"),
UI.create_htmx_script(),
),
html.body(
UI.create_bootstrap_styles(),
# Auto dark mode CSS (must come after Bootstrap)
UI.create_auto_dark_mode_style(),
html.div(
AdminView._render_content(
queue_items,
episodes,
status_counts,
),
id="admin-content",
hx_get="/admin",
hx_trigger="every 10s",
hx_swap="innerHTML",
hx_target="#admin-content",
classes=["container", "my-4"],
),
),
)
@staticmethod
def _render_content(
queue_items: list[dict[str, typing.Any]],
episodes: list[dict[str, typing.Any]],
status_counts: dict[str, int],
) -> html.div:
"""Render the main content of the admin page."""
return html.div(
html.h1(
"PodcastItLater Admin - Queue Status",
classes=["mb-4"],
),
AdminView.render_navigation(),
AdminView.render_status_summary(status_counts),
AdminView.render_queue_table(queue_items),
AdminView.render_episodes_table(episodes),
)
@staticmethod
def render_navigation() -> html.div:
"""Render navigation links."""
return html.div(
html.a(
html.i(classes=["bi", "bi-arrow-left", "me-2"]),
"Back to Home",
href="/",
classes=["btn", "btn-outline-primary", "btn-sm", "me-2"],
),
html.a(
html.i(classes=["bi", "bi-people", "me-2"]),
"Manage Users",
href="/admin/users",
classes=["btn", "btn-outline-secondary", "btn-sm"],
),
classes=["mb-3"],
)
@staticmethod
def render_status_summary(status_counts: dict[str, int]) -> html.div:
"""Render status summary section."""
return html.div(
html.h2("Status Summary", classes=["mb-3"]),
html.div(
*[
StatusBadge(status=status, count=count)
for status, count in status_counts.items()
],
classes=["mb-4"],
),
)
@staticmethod
def render_queue_table(
queue_items: list[dict[str, typing.Any]],
) -> html.div:
"""Render queue items table."""
return html.div(
html.h2("Queue Items", classes=["mb-3"]),
html.div(
html.table(
create_table_header([
"ID",
"URL",
"Title",
"Email",
"Status",
"Retries",
"Created",
"Error",
"Actions",
]),
html.tbody(*[
QueueTableRow(item=item) for item in queue_items
]),
classes=["table", "table-hover", "table-sm"],
),
classes=["table-responsive", "mb-5"],
),
)
@staticmethod
def render_episodes_table(
episodes: list[dict[str, typing.Any]],
) -> html.div:
"""Render episodes table."""
return html.div(
html.h2("Completed Episodes", classes=["mb-3"]),
html.div(
html.table(
create_table_header([
"ID",
"Title",
"Audio URL",
"Duration",
"Content Length",
"Created",
]),
html.tbody(*[
EpisodeTableRow(episode=episode) for episode in episodes
]),
classes=["table", "table-hover", "table-sm"],
),
classes=["table-responsive"],
),
)
def admin_queue_status(request: Request) -> AdminView | Response | html.div:
"""Return admin view showing all queue items and episodes."""
# Check if user is logged in
user_id = request.session.get("user_id")
if not user_id:
# Redirect to login
return Response(
"",
status_code=302,
headers={"Location": "/"},
)
user = Core.Database.get_user_by_id(
user_id,
)
if not user:
# Invalid session
return Response(
"",
status_code=302,
headers={"Location": "/"},
)
# Check if user is admin
if not Core.is_admin(user):
# Forbidden - redirect to home with error
return Response(
"",
status_code=302,
headers={"Location": "/?error=forbidden"},
)
# Admins can see all data (excluding completed items)
all_queue_items = [
item
for item in Core.Database.get_all_queue_items(None)
if item.get("status") != "completed"
]
all_episodes = Core.Database.get_all_episodes(
None,
)
# Get overall status counts for all users
status_counts: dict[str, int] = {}
for item in all_queue_items:
status = item.get("status", "unknown")
status_counts[status] = status_counts.get(status, 0) + 1
# Check if this is an HTMX request for auto-update
if request.headers.get("HX-Request") == "true":
# Return just the content div for HTMX updates
AdminView(
queue_items=all_queue_items,
episodes=all_episodes,
status_counts=status_counts,
)
content = layouts.Stack(
html.h1("PodcastItLater Admin - Queue Status"),
AdminView.render_navigation(),
AdminView.render_status_summary(status_counts),
AdminView.render_queue_table(all_queue_items),
AdminView.render_episodes_table(all_episodes),
)
return html.div(
content,
hx_get="/admin",
hx_trigger="every 10s",
hx_swap="innerHTML",
)
return AdminView(
queue_items=all_queue_items,
episodes=all_episodes,
status_counts=status_counts,
)
def retry_queue_item(request: Request, job_id: int) -> Response:
"""Retry a failed queue item."""
try:
# Check if user owns this job
user_id = request.session.get("user_id")
if not user_id:
return Response("Unauthorized", status_code=401)
job = Core.Database.get_job_by_id(
job_id,
)
if job is None or job.get("user_id") != user_id:
return Response("Forbidden", status_code=403)
Core.Database.retry_job(job_id)
# Redirect back to admin view
return Response(
"",
status_code=200,
headers={"HX-Redirect": "/admin"},
)
except (ValueError, KeyError) as e:
return Response(
f"Error retrying job: {e!s}",
status_code=500,
)
def delete_queue_item(request: Request, job_id: int) -> Response:
"""Delete a queue item."""
try:
# Check if user owns this job or is admin
user_id = request.session.get("user_id")
if not user_id:
return Response("Unauthorized", status_code=401)
job = Core.Database.get_job_by_id(
job_id,
)
if job is None:
return Response("Job not found", status_code=404)
# Check ownership or admin status
user = Core.Database.get_user_by_id(user_id)
if job.get("user_id") != user_id and not Core.is_admin(user):
return Response("Forbidden", status_code=403)
Core.Database.delete_job(job_id)
# Check if request is from admin page via referer header
is_from_admin = "/admin" in request.headers.get("referer", "")
# Redirect to admin if from admin page, trigger update otherwise
if is_from_admin:
return Response(
"",
status_code=200,
headers={"HX-Redirect": "/admin"},
)
return Response(
"",
status_code=200,
headers={"HX-Trigger": "queue-updated"},
)
except (ValueError, KeyError) as e:
return Response(
f"Error deleting job: {e!s}",
status_code=500,
)
def admin_users(request: Request) -> AdminUsers | Response:
"""Admin page for managing users."""
# Check if user is logged in and is admin
user_id = request.session.get("user_id")
if not user_id:
return Response(
"",
status_code=302,
headers={"Location": "/"},
)
user = Core.Database.get_user_by_id(
user_id,
)
if not user or not Core.is_admin(user):
return Response(
"",
status_code=302,
headers={"Location": "/?error=forbidden"},
)
# Get all users
with Core.Database.get_connection() as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT id, email, created_at, status FROM users "
"ORDER BY created_at DESC",
)
rows = cursor.fetchall()
users = [dict(row) for row in rows]
return AdminUsers(users=users)
def update_user_status(
request: Request,
user_id: int,
data: FormData,
) -> Response:
"""Update user account status."""
# Check if user is logged in and is admin
session_user_id = request.session.get("user_id")
if not session_user_id:
return Response("Unauthorized", status_code=401)
user = Core.Database.get_user_by_id(
session_user_id,
)
if not user or not Core.is_admin(user):
return Response("Forbidden", status_code=403)
# Get new status from form data
new_status_raw = data.get("status", "pending")
new_status = (
new_status_raw if isinstance(new_status_raw, str) else "pending"
)
if new_status not in {"pending", "active", "disabled"}:
return Response("Invalid status", status_code=400)
# Update user status
Core.Database.update_user_status(
user_id,
new_status,
)
# Redirect back to users page
return Response(
"",
status_code=200,
headers={"HX-Redirect": "/admin/users"},
)
def main() -> None:
"""Admin tests are currently in Web."""
if "test" in sys.argv:
sys.exit(0)
|