Skip to content

Commit 8f3de02

Browse files
committed
ID Already Exists form reset.
Fixes #2 Form data is preserved and the error message stays longer and shows the next id.
1 parent b85ae59 commit 8f3de02

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

app/blueprints/miniatures.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def add():
8383
"notes": form.get("notes"),
8484
}
8585
# Prevent duplicate (series, unique_id) combination
86-
from sqlalchemy import and_
86+
from sqlalchemy import and_, func
8787

8888
from ..extensions import session_scope
8989
from ..models.miniature import Miniature
@@ -95,8 +95,27 @@ def add():
9595
.first()
9696
)
9797
if existing:
98-
flash(f"Unique ID {unique_id} already exists in Series {series}", "danger")
99-
return render_template("miniatures/add.html")
98+
# Calculate next available unique_id in this series
99+
max_unique = (
100+
session.query(func.max(Miniature.unique_id))
101+
.filter(Miniature.series == series)
102+
.scalar()
103+
) or 0
104+
next_unique = max_unique + 1
105+
106+
# Preserve all form fields and suggest next ID
107+
prefill = {
108+
"series": series,
109+
"unique_id": next_unique,
110+
"prefix": form.get("prefix"),
111+
"chassis": form.get("chassis"),
112+
"type": form.get("type"),
113+
"status": form.get("status"),
114+
"tray_id": form.get("tray_id"),
115+
"notes": form.get("notes"),
116+
}
117+
flash(f"Unique ID {unique_id} already exists in Series {series}. Suggested next ID: {next_unique}", "danger")
118+
return render_template("miniatures/add.html", prefill=prefill)
100119

101120
add_miniature(data)
102121
flash("Miniature added", "success")

app/templates/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
{% endfor %}
2727
</div>
2828
<script>
29-
// Auto-fade alerts after 3 seconds
29+
// Auto-fade alerts after 6 seconds
3030
setTimeout(() => {
3131
document.querySelectorAll('.alert').forEach(alert => {
3232
const bsAlert = new bootstrap.Alert(alert);
3333
bsAlert.close();
3434
});
35-
}, 3000);
35+
}, 6000);
3636
</script>
3737
{% endif %}
3838
{% endwith %}

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)