init: Flask CRUD app v1.0.0 — same design as Lucee, PG params, CREATE IF NOT EXISTS
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
.venv/
|
||||
venv/
|
||||
.env
|
||||
*.egg-info/
|
||||
dist/
|
||||
*.log
|
||||
.DS_Store
|
||||
.idea/
|
||||
.vscode/
|
||||
@@ -0,0 +1,116 @@
|
||||
"""
|
||||
app.py — Flask CRUD-интерфейс для PostgreSQL.
|
||||
|
||||
Переменные окружения (из json_env):
|
||||
- PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD — подключение к БД
|
||||
- TABLE_NAME — имя таблицы (по умолчанию crud_items)
|
||||
|
||||
При старте: CREATE TABLE IF NOT EXISTS (БЕЗ DROP).
|
||||
"""
|
||||
|
||||
import os
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
from flask import Flask, render_template, request, redirect, url_for, g
|
||||
|
||||
VERSION = "1.0.0"
|
||||
TABLE_NAME = os.environ.get("TABLE_NAME", "crud_items")
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
def get_db():
|
||||
"""Ленивое подключение к PostgreSQL. Один коннект на запрос."""
|
||||
if "db" not in g:
|
||||
g.db = psycopg2.connect(
|
||||
host=os.environ.get("PGHOST", "127.0.0.1"),
|
||||
port=os.environ.get("PGPORT", "5432"),
|
||||
dbname=os.environ.get("PGDATABASE", "postgres"),
|
||||
user=os.environ.get("PGUSER", "postgres"),
|
||||
password=os.environ.get("PGPASSWORD", ""),
|
||||
)
|
||||
g.db.autocommit = True
|
||||
return g.db
|
||||
|
||||
|
||||
def init_db():
|
||||
"""CREATE TABLE IF NOT EXISTS — никогда не дропает существующую таблицу."""
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute(
|
||||
f"""
|
||||
CREATE TABLE IF NOT EXISTS {TABLE_NAME} (
|
||||
id SERIAL PRIMARY KEY,
|
||||
value TEXT NOT NULL DEFAULT ''
|
||||
)
|
||||
"""
|
||||
)
|
||||
cur.close()
|
||||
|
||||
|
||||
@app.teardown_appcontext
|
||||
def close_db(exc):
|
||||
db = g.pop("db", None)
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
def index():
|
||||
db = get_db()
|
||||
cur = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
|
||||
# --- Обработка POST (create / update / delete) — POST-redirect-GET ---
|
||||
if request.method == "POST":
|
||||
action = request.form.get("action", "")
|
||||
|
||||
if action == "create":
|
||||
cur.execute(
|
||||
f"INSERT INTO {TABLE_NAME} (value) VALUES (%s)",
|
||||
(request.form.get("value", ""),),
|
||||
)
|
||||
|
||||
elif action == "update":
|
||||
cur.execute(
|
||||
f"UPDATE {TABLE_NAME} SET value = %s WHERE id = %s",
|
||||
(request.form.get("value", ""), int(request.form.get("id", 0))),
|
||||
)
|
||||
|
||||
elif action == "delete":
|
||||
cur.execute(
|
||||
f"DELETE FROM {TABLE_NAME} WHERE id = %s",
|
||||
(int(request.form.get("id", 0)),),
|
||||
)
|
||||
|
||||
cur.close()
|
||||
return redirect(url_for("index"))
|
||||
|
||||
# --- GET: список + форма редактирования ---
|
||||
edit_row = {}
|
||||
edit_id = request.args.get("edit")
|
||||
if edit_id:
|
||||
cur.execute(
|
||||
f"SELECT id, value FROM {TABLE_NAME} WHERE id = %s",
|
||||
(int(edit_id),),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if row:
|
||||
edit_row = dict(row)
|
||||
|
||||
# Список всех записей
|
||||
cur.execute(f"SELECT id, value FROM {TABLE_NAME} ORDER BY id")
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
|
||||
return render_template(
|
||||
"index.html",
|
||||
table_name=TABLE_NAME,
|
||||
rows=rows,
|
||||
edit_row=edit_row,
|
||||
version=VERSION,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)), debug=False)
|
||||
@@ -0,0 +1,3 @@
|
||||
Flask>=3.0
|
||||
psycopg2-binary>=2.9
|
||||
gunicorn>=21.2
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<svg
|
||||
width="57"
|
||||
height="57"
|
||||
xml:space="preserve"
|
||||
overflow="hidden"
|
||||
version="1.1"
|
||||
id="svg8"
|
||||
sodipodi:docname="U_v3.svg"
|
||||
inkscape:version="1.3.2 (091e20e, 2023-11-25, custom)"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"><sodipodi:namedview
|
||||
id="namedview8"
|
||||
pagecolor="#ffffff"
|
||||
bordercolor="#000000"
|
||||
borderopacity="0.25"
|
||||
inkscape:showpageshadow="2"
|
||||
inkscape:pageopacity="0.0"
|
||||
inkscape:pagecheckerboard="0"
|
||||
inkscape:deskcolor="#d1d1d1"
|
||||
inkscape:zoom="16.226667"
|
||||
inkscape:cx="27.146672"
|
||||
inkscape:cy="28.317584"
|
||||
inkscape:window-width="2560"
|
||||
inkscape:window-height="1494"
|
||||
inkscape:window-x="-11"
|
||||
inkscape:window-y="-11"
|
||||
inkscape:window-maximized="1"
|
||||
inkscape:current-layer="svg8" /><defs
|
||||
id="defs2"><clipPath
|
||||
id="clip0"><rect
|
||||
x="652"
|
||||
y="420"
|
||||
width="64"
|
||||
height="75"
|
||||
id="rect1" /></clipPath><clipPath
|
||||
id="clip1"><rect
|
||||
x="652"
|
||||
y="420"
|
||||
width="64"
|
||||
height="70"
|
||||
id="rect2" /></clipPath></defs><g
|
||||
clip-path="url(#clip0)"
|
||||
transform="matrix(1.0135748,0,0,1.0135748,-664.97034,-440.30567)"
|
||||
id="g8"><g
|
||||
clip-path="url(#clip1)"
|
||||
id="g7"><g
|
||||
id="g6"><path
|
||||
d="m 114.028,43.1492 v 7.6592 c 0,3.7843 -3.08,6.8632 -6.866,6.8632 L 85.3367,57.5419 c -3.7853,0 -6.8655,-3.0805 -6.8655,-6.8643 v -2.5279 l 0.0555,0.009 V 15.8148 l -10.3823,2.0127 0.0045,3.8772 -0.0045,0.0015 v 28.971 c 0,9.481 7.7132,17.1923 17.1867,17.1923 l 21.8359,0.1313 c 9.474,0 17.187,-7.7117 17.187,-17.1928 v -2.6541 l 0.027,0.0045 V 15.8145 l -10.382,2.0126 0.028,25.3214 z"
|
||||
fill="#001c34"
|
||||
fill-rule="evenodd"
|
||||
transform="matrix(1,0,0,1.01337,587.92,420.059)"
|
||||
id="path6" /></g></g></g></svg>
|
||||
|
After Width: | Height: | Size: 2.0 KiB |
Executable
+25
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<!-- Creator: CorelDRAW -->
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="82.3711mm" height="18.2443mm" version="1.1" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
|
||||
viewBox="0 0 8221.93 1821.07"
|
||||
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
xmlns:xodm="http://www.corel.com/coreldraw/odm/2003">
|
||||
<defs>
|
||||
<style type="text/css">
|
||||
<![CDATA[
|
||||
.fil0 {fill:#001C34}
|
||||
]]>
|
||||
</style>
|
||||
</defs>
|
||||
<g id="Слой_x0020_1">
|
||||
<metadata id="CorelCorpID_0Corel-Layer"/>
|
||||
<g id="_2283355517888">
|
||||
<path class="fil0" d="M477.49 479.69l-413.51 0 -63.98 276.48 178.44 0 1.17 1028.71 0 26.75 0.03 0 276.39 0 0 -871.72c0,-101.28 82.43,-183.69 183.75,-183.69l589.25 3.44c101.34,0 183.76,82.46 183.76,183.74l0 871.72 276.44 0 0 -871.72c0,-253.77 -206.46,-460.15 -460.05,-460.15l-589.52 -3.53 -162.17 0.45 0 -0.48z"/>
|
||||
<path class="fil0" d="M6664.65 1813.37l1181.04 0c212.14,0 376.24,-175.01 376.24,-387.08 0,-212.13 -172.55,-384.68 -384.69,-384.68l-653.66 0c-60.22,0 -109.16,-48.94 -109.16,-109.18l0 -66.18c0,-60.19 48.94,-109.14 109.16,-109.14l695.76 0 63.74 -275.49 -759.5 0c-212.13,0 -384.66,172.53 -384.66,384.63l0 66.18c0,212.13 172.53,384.67 384.66,384.67l653.66 0c60.2,0 109.2,49 109.2,109.19 0,60.13 -49,116.1 -109.2,116.1l-1118.9 0 -53.68 270.98z"/>
|
||||
<path class="fil0" d="M6200.79 483.5l-723.21 0c-215.88,0 -391.47,175.6 -391.47,391.51l0 485.5c0,248.38 202.05,450.4 450.4,450.4l989.38 0 62.13 -268.51 -1051.51 0c-96.41,0 -174.95,-85.37 -174.95,-181.88l-1.5 -39.46 923.62 0 308.51 -1.84 0 -237.95 0 -206.26c0,-215.91 -175.59,-391.51 -391.41,-391.51zm115.96 560.28l-955.19 0 0 -168.77c0,-63.96 52.07,-116.05 116.02,-116.05l723.21 0c63.91,0 115.96,52.08 115.96,116.05l0 168.77z"/>
|
||||
<path class="fil0" d="M4683.49 1194.24l0 168.28c0,100.92 -81.7,178.37 -182.67,178.37l-589.87 1.23c-93.18,0 -170.28,-69.96 -181.63,-160.09l0 -458.13c11.36,-90.1 88.46,-160.07 181.63,-160.07l589.44 3.5c100.97,0 183.1,82.18 183.1,183.1l0 30.42 0 213.4zm-1230.2 -345.53l-0.89 -795.03 276.91 -53.68 0 526.07c55.74,-24.16 117.03,-37.74 181.5,-37.74l589.71 3.5c252.69,0 458.42,205.72 458.42,458.59l0 30.42 0 213.4 0 168.28c0,252.86 -205.73,458.54 -458.42,458.54l-589.71 -3.5c-252.7,0 -458.41,-205.67 -458.41,-458.56l0 -171.61 0 -240.48 0.89 -98.2z"/>
|
||||
<path class="fil0" d="M3041.25 1150.84l0 204.28c0,100.93 -82.15,183.05 -183.11,183.05l-582.11 -3.46c-100.96,0 -183.11,-82.16 -183.11,-183.08l0 -67.42 1.48 0.24 0 -862.65 -276.91 53.68 0.12 103.41 -0.12 0.04 0 772.69c0,252.87 205.72,458.54 458.39,458.54l582.4 3.5c252.67,0 458.4,-205.68 458.4,-458.55l0 -70.79 0.71 0.12 0 -862.65 -276.91 53.68 0.76 675.35z"/>
|
||||
</g>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.8 KiB |
@@ -0,0 +1,111 @@
|
||||
:root {
|
||||
--brand-primary: #2563eb;
|
||||
--brand-primary-dark: #1d4ed8;
|
||||
--brand-gray: #d1d5db;
|
||||
--brand-grey-light: #f3f4f6;
|
||||
--brand-destructive: #ef4444;
|
||||
--brand-success: #22c55e;
|
||||
--text-primary: #1a1a1a;
|
||||
--text-muted: #6b7280;
|
||||
--bg-card: #ffffff;
|
||||
--radius: 12px;
|
||||
}
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body {
|
||||
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
|
||||
font-size: 14px;
|
||||
color: var(--text-primary);
|
||||
background: var(--brand-grey-light);
|
||||
min-height: 100vh;
|
||||
padding: 40px 16px;
|
||||
}
|
||||
.container { max-width: 720px; margin: 0 auto; }
|
||||
|
||||
/* Header */
|
||||
.header {
|
||||
display: flex; align-items: center; gap: 12px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
.header img { height: 36px; }
|
||||
.header h1 { font-size: 20px; font-weight: 600; color: #001C34; }
|
||||
|
||||
/* Card */
|
||||
.card {
|
||||
background: var(--bg-card);
|
||||
border-radius: var(--radius);
|
||||
border: 1px solid var(--brand-gray);
|
||||
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
|
||||
overflow: hidden;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
.card-header {
|
||||
background: var(--brand-grey-light);
|
||||
padding: 12px 16px;
|
||||
font-weight: 600;
|
||||
font-size: 16px;
|
||||
}
|
||||
.card-body { padding: 16px; }
|
||||
|
||||
/* Table */
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th {
|
||||
background: var(--brand-grey-light);
|
||||
text-transform: uppercase;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
color: var(--text-muted);
|
||||
padding: 8px 12px;
|
||||
text-align: left;
|
||||
border-right: 1px solid var(--brand-gray);
|
||||
}
|
||||
th:last-child { border-right: none; text-align: center; width: 60px; }
|
||||
td {
|
||||
padding: 8px 12px;
|
||||
border-bottom: 1px solid var(--brand-gray);
|
||||
border-right: 1px solid var(--brand-gray);
|
||||
}
|
||||
td:last-child { border-right: none; text-align: center; width: 60px; }
|
||||
tr:hover { background: rgba(243,244,246,0.5); }
|
||||
tr:last-child td { border-bottom: none; }
|
||||
|
||||
/* Buttons */
|
||||
.btn {
|
||||
display: inline-flex; align-items: center; gap: 4px;
|
||||
height: 32px; padding: 0 12px;
|
||||
border: 1px solid var(--brand-gray);
|
||||
border-radius: 6px;
|
||||
background: var(--bg-card);
|
||||
font-size: 14px; font-family: inherit;
|
||||
cursor: pointer; white-space: nowrap;
|
||||
text-decoration: none;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
.btn:hover { background: var(--brand-grey-light); }
|
||||
.btn-primary {
|
||||
background: var(--brand-primary); color: #fff; border-color: var(--brand-primary);
|
||||
}
|
||||
.btn-primary:hover { background: var(--brand-primary-dark); }
|
||||
.btn-sm { height: 28px; padding: 0 8px; font-size: 13px; }
|
||||
|
||||
/* Form */
|
||||
.form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 12px;
|
||||
}
|
||||
.form-row {
|
||||
display: flex; gap: 8px; align-items: flex-end;
|
||||
}
|
||||
.form-row input { flex: 1; }
|
||||
input[type="text"] {
|
||||
height: 36px; padding: 0 12px;
|
||||
border: 1px solid var(--brand-gray);
|
||||
border-radius: 6px; font-size: 14px; font-family: inherit;
|
||||
}
|
||||
input[type="text"]:focus { outline: none; border-color: var(--brand-primary); box-shadow: 0 0 0 2px rgba(37,99,235,0.15); }
|
||||
|
||||
/* Footer */
|
||||
.footer { text-align: center; padding: 16px; color: #9ca3af; font-size: 12px; }
|
||||
|
||||
/* Empty state */
|
||||
.empty-cell { color: var(--text-muted); padding: 24px; text-align: center; }
|
||||
@@ -0,0 +1,108 @@
|
||||
<!---
|
||||
index.html — CRUD-интерфейс для таблицы в PostgreSQL (Flask + Jinja2).
|
||||
|
||||
Логика:
|
||||
1. Если POST-запрос с action — выполняем create/update/delete
|
||||
и редиректим на GET (POST-redirect-GET паттерн)
|
||||
2. Если GET-запрос с ?edit=ID — загружаем запись для редактирования
|
||||
3. Всегда показываем список всех записей + форму добавления/редактирования
|
||||
|
||||
Безопасность:
|
||||
- Параметризованные запросы (%s) в app.py → защита от SQL-инъекций
|
||||
- redirect после POST → защита от повторной отправки формы
|
||||
- Jinja2 автоэкранирование → XSS-защита
|
||||
|
||||
Переменные (из app.py):
|
||||
- table_name — имя таблицы
|
||||
- rows — список записей [(id, value), ...]
|
||||
- edit_row — запись для редактирования {} или None
|
||||
- version — версия приложения
|
||||
--->
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>CRUD Flask Example — {{ table_name }}</title>
|
||||
<link rel="icon" type="image/svg+xml" href="{{ url_for('static', filename='favicon.svg') }}" />
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
|
||||
<div class="header">
|
||||
<img src="{{ url_for('static', filename='logo.svg') }}" alt="Nubes" />
|
||||
<h1>CRUD Flask Example</h1>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header">Записи</div>
|
||||
<div class="card-body" style="padding: 0;">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>VALUE</th>
|
||||
<th style="text-align:center;"></th>
|
||||
<th style="text-align:center;"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for row in rows %}
|
||||
<tr>
|
||||
<td>{{ row.id }}</td>
|
||||
<td>{{ row.value }}</td>
|
||||
<td style="text-align:center;">
|
||||
<a href="?edit={{ row.id }}" class="btn btn-sm">✏️</a>
|
||||
</td>
|
||||
<td style="text-align:center;">
|
||||
<form method="post" style="display:inline">
|
||||
<input type="hidden" name="action" value="delete" />
|
||||
<input type="hidden" name="id" value="{{ row.id }}" />
|
||||
<button type="submit" class="btn btn-sm" onclick="return confirm('Удалить {{ row.id }}?')">🗑️</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% if not rows %}
|
||||
<div class="empty-cell">Нет записей. Создайте первую.</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
{% if edit_row %}
|
||||
<div class="card-header">Редактировать #{{ edit_row.id }}</div>
|
||||
<div class="card-body">
|
||||
<form method="post" class="form-grid">
|
||||
<input type="hidden" name="action" value="update" />
|
||||
<input type="hidden" name="id" value="{{ edit_row.id }}" />
|
||||
<div class="form-row">
|
||||
<input type="text" name="value" value="{{ edit_row.value }}" required placeholder="Значение" />
|
||||
<button type="submit" class="btn btn-primary">Сохранить</button>
|
||||
<a href="/" class="btn">Отмена</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="card-header">Добавить</div>
|
||||
<div class="card-body">
|
||||
<form method="post" class="form-grid">
|
||||
<input type="hidden" name="action" value="create" />
|
||||
<div class="form-row">
|
||||
<input type="text" name="value" placeholder="Значение" required />
|
||||
<button type="submit" class="btn btn-primary">Добавить</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="footer">
|
||||
v{{ version }} — {{ table_name }}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user