notyfikacje

This commit is contained in:
2026-11-01 20:27:10 +01:00
parent e55af30644
commit b77b348d54
11 changed files with 375 additions and 18 deletions
+52 -12
View File
@@ -2,9 +2,10 @@
Two Flask services with separate responsibilities and separate databases:
- **main app** (`app.py`) — all user-facing views: login, home, notes, API docs
and the `/sensors` dashboard. It owns the **main app** database
(`main_app.db`: users and notes) and proxies data requests to the sensors API.
- **main app** (`app.py`) — all user-facing views: login, home, notes,
notifications, API docs and the `/sensors` dashboard. It owns the **main app**
database (`main_app.db`: users, notes and notifications) and proxies data
requests to the sensors API.
- **sensors app** (`sensors_app.py`) — API only. It owns the **sensors**
database (`sensors.db`, tables `sensor` and `sensor_readout`) and has no
knowledge of users. Its API is open to every user on the local machine.
@@ -34,7 +35,7 @@ server/
├── auth.py # Flask-Login setup
├── config.py # Config (databases, secret key, sensors URL)
├── models.py # Sensor, SensorReadout (sensors DB)
├── user_models.py # User, Note, NoteItem (main_app.db)
├── user_models.py # User, Note, NoteItem, Notification
├── seed.py # Sample sensor data
├── migrate_legacy_db.py # Import old combined data.db into split DBs
├── run.sh # Development launcher (main/sensors + port)
@@ -52,6 +53,7 @@ server/
├── home.html
├── login.html
├── notes.html
├── notifications.html
├── docs.html
└── sensors.html
```
@@ -102,12 +104,12 @@ After verifying the new databases you can archive or remove `data.db`.
## Users
User accounts and notes live only in `main_app.db` (main app). There is no
public registration; accounts are created by an administrator.
User accounts, notes and notifications live only in `main_app.db` (main app).
There is no public registration; accounts are created by an administrator.
Main app pages (`/`, `/home`, `/notes`, `/api/docs`, `/sensors`) and dashboard
read endpoints (`GET /api/sensors/:id/types`, `/readouts`, `/readouts/hourly`)
require login. Device endpoints (`GET/POST /api/sensors`,
Main app pages (`/`, `/home`, `/notes`, `/notifications`, `/api/docs`,
`/sensors`) and dashboard read endpoints (`GET /api/sensors/:id/types`,
`/readouts`, `/readouts/hourly`) require login. Device endpoints (`GET/POST /api/sensors`,
`POST /api/sensors/:id/readouts`) stay open so devices can push data without a
browser session.
@@ -178,6 +180,34 @@ automatically):
alembic -c alembic_users.ini upgrade head
```
## Notifications
Each user has their own short text notifications in `main_app.db`. A
notification is either **new** or **read**. The main navigation shows the number
of new notifications next to the `Notifications` link. Opening a notification
from the `/notifications` page marks it as read and moves it below the new ones.
Create notifications with the CLI (run it as the same user as the service):
```bash
cd /home/mateusz/prog/yggdrasil/server
# For one user
sudo -u mateusz .venv/bin/flask --app app create-notification \
--username admin --message "Backup finished."
# For every user
sudo -u mateusz .venv/bin/flask --app app create-notification \
--all --message "Planned maintenance tonight."
```
Apply the users database migration once (the production launcher does this
automatically):
```bash
alembic -c alembic_users.ini upgrade head
```
## Running
The first argument selects the app (`main` or `sensors`), the second the port.
@@ -291,7 +321,7 @@ deployment path or user is different.
| Service | Port | Routes |
|---|---|---|
| main | 8000 | `/`, `/login`, `/home`, `/notes`, `/sensors`, `/api/docs`, `/api/*` (proxy) |
| main | 8000 | `/`, `/login`, `/home`, `/notes`, `/notifications`, `/sensors`, `/api/docs`, `/api/*` (proxy) |
| sensors | 8001 | `/api/*` (sensors and readouts) |
### Tests
@@ -322,9 +352,9 @@ Owned by the sensors app. Contains only sensor data.
| `value` | Float | Measured value |
| `timestamp` | DateTime | Readout timestamp (UTC) |
### main_app.db (users and notes)
### main_app.db (users, notes and notifications)
Owned by the main app. Contains accounts and notes.
Owned by the main app. Contains accounts, notes and notifications.
**user**
| Column | Type | Description |
@@ -354,6 +384,16 @@ Owned by the main app. Contains accounts and notes.
| `is_done` | Boolean | Checked state (checked items render last, struck through) |
| `created_at` | DateTime | Creation timestamp (UTC) |
**notification**
| Column | Type | Description |
|---|---|---|
| `id` | Integer | Primary key |
| `user_id` | Integer | Foreign key to `user.id` (recipient) |
| `message` | String(500) | Short notification text |
| `is_read` | Boolean | `false` = new, `true` = read |
| `created_at` | DateTime | Creation timestamp (UTC) |
| `read_at` | DateTime | When the notification was read, or `NULL` |
### Inspecting the databases on Linux
The databases are ordinary SQLite files. Use the `sqlite3` CLI (install it with