transfer from private server/git server
This commit is contained in:
59
web/models/block_model.py
Normal file
59
web/models/block_model.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
block_model.py — Accès aux données pour l'entité "block" (widgets du dashboard).
|
||||
|
||||
Module historique de GestHub, migré de Flask-SQLAlchemy vers PyMySQL pour
|
||||
rester cohérent avec le reste de la couche models/ (accès direct, sans ORM,
|
||||
requêtes paramétrées).
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
|
||||
def _row_to_dict(row):
|
||||
if row is None:
|
||||
return None
|
||||
return {
|
||||
"id": row["id"],
|
||||
"type": row["block_type"],
|
||||
"column": row["column_name"],
|
||||
"position": row["position"],
|
||||
"data": json.loads(row["data"]) if row["data"] else {},
|
||||
}
|
||||
|
||||
|
||||
def fetch_all(db):
|
||||
query = (
|
||||
"SELECT id, block_type, column_name, position, data FROM blocks "
|
||||
"ORDER BY column_name, position"
|
||||
)
|
||||
rows = db.fetch_all(query)
|
||||
return [_row_to_dict(r) for r in rows]
|
||||
|
||||
|
||||
def insert(db, block_type, column_name, data, position=99):
|
||||
query = (
|
||||
"INSERT INTO blocks (block_type, column_name, position, data) "
|
||||
"VALUES (%s, %s, %s, %s)"
|
||||
)
|
||||
new_id = db.execute(query, (block_type, column_name, position, json.dumps(data)))
|
||||
return _row_to_dict({
|
||||
"id": new_id,
|
||||
"block_type": block_type,
|
||||
"column_name": column_name,
|
||||
"position": position,
|
||||
"data": json.dumps(data),
|
||||
})
|
||||
|
||||
|
||||
def update_position(db, block_id, column_name, position):
|
||||
query = "UPDATE blocks SET column_name = %s, position = %s WHERE id = %s"
|
||||
with db.cursor(commit=True) as cur:
|
||||
cur.execute(query, (column_name, position, block_id))
|
||||
return cur.rowcount
|
||||
|
||||
|
||||
def delete(db, block_id):
|
||||
query = "DELETE FROM blocks WHERE id = %s"
|
||||
with db.cursor(commit=True) as cur:
|
||||
cur.execute(query, (block_id,))
|
||||
return cur.rowcount
|
||||
Reference in New Issue
Block a user