Files
Journal/db.py
SimolZimol 0e7efe4b74 new file: .env.example
new file:   .gitignore
	new file:   Dockerfile
	new file:   README.md
	new file:   app.py
	new file:   auth.py
	new file:   config.py
	new file:   db.py
	new file:   db_helpers.py
	new file:   docker-compose.yml
	new file:   requirements.txt
	new file:   routes/__init__.py
	new file:   routes/admin_routes.py
	new file:   routes/auth_routes.py
	new file:   routes/journal_routes.py
	new file:   static/css/style.css
	new file:   static/js/main.js
	new file:   templates/admin/dashboard.html
	new file:   templates/admin/users.html
	new file:   templates/base.html
	new file:   templates/day.html
	new file:   templates/future.html
	new file:   templates/index.html
	new file:   templates/login.html
	new file:   templates/week.html
2026-08-02 20:41:24 +02:00

77 lines
2.3 KiB
Python

"""Datenbankzugriff (PyMySQL) + automatisches Schema-Init.
Verwendung:
from db import get_connection, init_db
"""
import pymysql
from pymysql.cursors import DictCursor
from config import Config
def get_connection():
"""Öffnet eine neue Verbindung zur MySQL-Datenbank."""
return pymysql.connect(
host=Config.DB_HOST,
port=Config.DB_PORT,
user=Config.DB_USER,
password=Config.DB_PASSWORD,
database=Config.DB_DATABASE,
charset="utf8mb4",
cursorclass=DictCursor,
autocommit=True,
)
SCHEMA = """
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(255),
salt VARCHAR(128) NOT NULL,
password_hash VARCHAR(512) NOT NULL,
is_admin TINYINT(1) NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS notes (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
note_date DATE NOT NULL,
bullet_type ENUM('.', 'x', '-', '?') NOT NULL DEFAULT '-',
note_text TEXT NOT NULL,
priority TINYINT NOT NULL DEFAULT 0,
status ENUM('open', 'done', 'moved') NOT NULL DEFAULT 'open',
moved_to_date DATE NULL,
moved_to_future TINYINT(1) NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT fk_notes_user FOREIGN KEY (user_id)
REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS future_log (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
month_date DATE NOT NULL,
note_text TEXT NOT NULL,
status ENUM('open', 'done') NOT NULL DEFAULT 'open',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_future_user FOREIGN KEY (user_id)
REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""
def init_db():
"""Erstellt das Schema, falls es noch nicht existiert."""
conn = get_connection()
try:
with conn.cursor() as cur:
for statement in SCHEMA.split(";"):
stmt = statement.strip()
if stmt:
cur.execute(stmt)
finally:
conn.close()