|
1 | 1 | import os |
2 | 2 | import sys |
| 3 | +from datetime import datetime, timezone |
| 4 | + |
| 5 | +try: |
| 6 | + from dotenv import load_dotenv |
| 7 | +except ModuleNotFoundError: |
| 8 | + load_dotenv = None |
3 | 9 |
|
4 | | -from dotenv import load_dotenv |
5 | 10 | from sqlalchemy.orm import Session |
6 | 11 |
|
7 | 12 | PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) |
8 | | -load_dotenv(os.path.join(PROJECT_ROOT, ".env")) |
| 13 | +if load_dotenv: |
| 14 | + load_dotenv(os.path.join(PROJECT_ROOT, ".env")) |
| 15 | +else: |
| 16 | + # fallback: load simple KEY=VALUE pairs into env if .env exists |
| 17 | + env_path = os.path.join(PROJECT_ROOT, ".env") |
| 18 | + if os.path.exists(env_path): |
| 19 | + with open(env_path, "r", encoding="utf-8") as f: |
| 20 | + for raw in f: |
| 21 | + line = raw.strip() |
| 22 | + if not line or line.startswith("#") or "=" not in line: |
| 23 | + continue |
| 24 | + k, v = line.split("=", 1) |
| 25 | + os.environ.setdefault(k.strip(), v.strip().strip('"').strip("'")) |
| 26 | + |
9 | 27 | sys.path.append(PROJECT_ROOT) |
10 | 28 |
|
11 | 29 | from app import database |
12 | 30 | from app.database import SessionLocal |
13 | 31 | from app.models import Certificate |
14 | 32 |
|
15 | 33 |
|
| 34 | +def _format_dt(dt: datetime) -> str: |
| 35 | + if not dt: |
| 36 | + return "-" |
| 37 | + # ensure timezone-aware |
| 38 | + if dt.tzinfo is None: |
| 39 | + dt = dt.replace(tzinfo=timezone.utc) |
| 40 | + # convert to local timezone for display |
| 41 | + try: |
| 42 | + local = dt.astimezone() |
| 43 | + except Exception: |
| 44 | + local = dt |
| 45 | + return local.strftime("%d.%m.%Y, %H:%M %Z") |
| 46 | + |
| 47 | + |
16 | 48 | def view_database(limit: int = 50): |
17 | 49 | # Ensure missing optional columns are added before selecting all model fields. |
18 | 50 | database.ensure_certificate_schema() |
@@ -43,7 +75,7 @@ def view_database(limit: int = 50): |
43 | 75 | print(f"Instructor : {cert.instructor}") |
44 | 76 | print(f"Course Link : {cert.course_link or '-'}") |
45 | 77 | print(f"Status : {getattr(cert, 'status', 'valid')}") |
46 | | - print(f"Created At : {cert.created_at}") |
| 78 | + print(f"Created At : {_format_dt(cert.created_at)}") |
47 | 79 | print("=" * 50) |
48 | 80 |
|
49 | 81 | except Exception as e: |
|
0 commit comments