Match Lucee UI and dedupe inserts
This commit is contained in:
parent
93a0acf36a
commit
eed3e1fcde
116
site/app.py
116
site/app.py
@ -1,3 +1,4 @@
|
|||||||
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
@ -39,35 +40,70 @@ PAGE = """
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>Flask + Postgres Demo</title>
|
<title>Flask + Postgres Demo</title>
|
||||||
|
<link rel="icon" href="https://nubes.ru/themes/custom/nubes/images/nubes-ico.svg" type="image/svg+xml">
|
||||||
|
<style>
|
||||||
|
:root { --nubes-blue: #005BFF; --nubes-dark: #1A1A1A; --nubes-grey: #F8F9FA; --nubes-border: #E5E7EB; }
|
||||||
|
body { font-family: 'Segoe UI', Tahoma, sans-serif; margin: 0; padding: 0; background: var(--nubes-grey); color: var(--nubes-dark); }
|
||||||
|
.header-bg { position: sticky; top: 0; z-index: 1000; background: #fff; border-bottom: 1px solid var(--nubes-border); padding: 15px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
|
||||||
|
.container { max-width: 1000px; margin: auto; padding: 0 20px; }
|
||||||
|
.header-content { display: flex; align-items: center; justify-content: space-between; }
|
||||||
|
.logo { height: 40px; }
|
||||||
|
.main-content { padding: 40px 0; }
|
||||||
|
.card { background: #fff; padding: 32px; border-radius: 16px; box-shadow: 0 4px 20px rgba(0,0,0,0.04); }
|
||||||
|
.btn { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; padding: 12px 24px; border: none; border-radius: 8px; font-weight: 600; font-size: 14px; }
|
||||||
|
.btn-primary { background: var(--nubes-blue); color: #fff; }
|
||||||
|
.btn-action { padding: 12px; background: #fff; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 18px; line-height: 1; cursor: pointer; min-width: 44px; }
|
||||||
|
.btn-action:hover { background: var(--nubes-grey); border-color: var(--nubes-blue); }
|
||||||
|
.input-group { display: flex; gap: 12px; margin-bottom: 32px; }
|
||||||
|
input[type="text"] { flex-grow: 1; width: 100%; padding: 12px 16px; border: 1px solid var(--nubes-border); border-radius: 8px; font-size: 14px; }
|
||||||
|
table { width: 100%; border-collapse: collapse; }
|
||||||
|
th { text-align: left; padding: 16px; font-size: 12px; text-transform: uppercase; color: #6B7280; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
td { padding: 16px; border-bottom: 1px solid var(--nubes-border); }
|
||||||
|
tbody tr:nth-child(even) { background-color: #FAFBFC; }
|
||||||
|
tbody tr:hover { background-color: #F3F4F6; }
|
||||||
|
.id-cell { font-family: monospace; color: #9CA3AF; width: 60px; }
|
||||||
|
.actions-cell { display: flex; gap: 12px; width: 130px; }
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h3>Flask + Postgres Demo</h3>
|
<div class="header-bg">
|
||||||
{% if error %}<p style="color:red">{{ error }}</p>{% endif %}
|
<div class="container header-content">
|
||||||
<form method="post">
|
<img src="https://nubes.ru/themes/custom/nubes_2025/logo.svg" alt="Nubes" class="logo">
|
||||||
<input type="text" name="txt_content" placeholder="Новое сообщение...">
|
<div style="font-size: 14px; color: var(--nubes-blue); font-weight: 600;">Flask + Postgres Demo</div>
|
||||||
<button type="submit">Добавить</button>
|
</div>
|
||||||
</form>
|
</div>
|
||||||
<table border="1" cellpadding="6" cellspacing="0">
|
<div class="container main-content">
|
||||||
<tr><th>ID</th><th>Содержимое</th><th>Действия</th></tr>
|
<div class="card">
|
||||||
{% for row in rows %}
|
{% if error %}<p style="color:red">{{ error }}</p>{% endif %}
|
||||||
<tr>
|
<form method="post" class="input-group" onsubmit="const btn=this.querySelector('button'); if(btn){btn.disabled=true; btn.textContent='Добавляем...';}">
|
||||||
<td>{{ row[0] }}</td>
|
<input type="text" name="txt_content" placeholder="Новое сообщение..." required>
|
||||||
<td>
|
<button type="submit" class="btn btn-primary">Добавить</button>
|
||||||
<form method="post" action="/update">
|
</form>
|
||||||
<input type="hidden" name="id" value="{{ row[0] }}">
|
<table>
|
||||||
<input type="text" name="txt_content" value="{{ row[1] }}">
|
<thead><tr><th>ID</th><th>Содержимое</th><th>Действия</th></tr></thead>
|
||||||
<button type="submit">💾</button>
|
<tbody>
|
||||||
</form>
|
{% for row in rows %}
|
||||||
</td>
|
<tr>
|
||||||
<td>
|
<td class="id-cell">{{ row[0] }}</td>
|
||||||
<form method="post" action="/delete" onsubmit="return confirm('Удалить?')">
|
<td>
|
||||||
<input type="hidden" name="id" value="{{ row[0] }}">
|
<form method="post" action="/update" id="upd_{{ row[0] }}" style="margin:0">
|
||||||
<button type="submit">🗑</button>
|
<input type="hidden" name="id" value="{{ row[0] }}">
|
||||||
</form>
|
<input type="text" name="txt_content" value="{{ row[1] }}" style="width:100%; border:none; background:transparent;">
|
||||||
</td>
|
</form>
|
||||||
</tr>
|
</td>
|
||||||
{% endfor %}
|
<td class="actions-cell">
|
||||||
</table>
|
<button type="submit" form="upd_{{ row[0] }}" class="btn-action">💾</button>
|
||||||
|
<form method="post" action="/delete" style="margin:0" onsubmit="return confirm('Удалить?')">
|
||||||
|
<input type="hidden" name="id" value="{{ row[0] }}">
|
||||||
|
<button type="submit" class="btn-action">🗑</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
"""
|
"""
|
||||||
@ -81,10 +117,28 @@ def index():
|
|||||||
content = request.form.get("txt_content") or "Это Flask сделал"
|
content = request.form.get("txt_content") or "Это Flask сделал"
|
||||||
try:
|
try:
|
||||||
with get_conn() as conn, conn.cursor() as cur:
|
with get_conn() as conn, conn.cursor() as cur:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"INSERT INTO nubes_test_table (test_data) VALUES (%s)",
|
"SELECT test_data, created_at FROM nubes_test_table ORDER BY id DESC LIMIT 1"
|
||||||
(content,),
|
)
|
||||||
)
|
last = cur.fetchone()
|
||||||
|
is_duplicate = False
|
||||||
|
if last:
|
||||||
|
last_text, last_created = last
|
||||||
|
now = (
|
||||||
|
datetime.datetime.now(last_created.tzinfo)
|
||||||
|
if isinstance(last_created, datetime.datetime)
|
||||||
|
else datetime.datetime.utcnow()
|
||||||
|
)
|
||||||
|
is_duplicate = (
|
||||||
|
last_text == content
|
||||||
|
and (now - last_created).total_seconds() < 3
|
||||||
|
)
|
||||||
|
|
||||||
|
if not is_duplicate:
|
||||||
|
cur.execute(
|
||||||
|
"INSERT INTO nubes_test_table (test_data) VALUES (%s)",
|
||||||
|
(content,),
|
||||||
|
)
|
||||||
return redirect("/")
|
return redirect("/")
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
error = str(exc)
|
error = str(exc)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user