mirror of
https://github.com/complexcaresolutions/dak.c2s.git
synced 2026-03-17 17:13:42 +00:00
fix: upsert report metadata to avoid duplicate key error on re-generation
When generating a report for the same jahr/kw, update the existing record instead of inserting a duplicate (which caused IntegrityError). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
db963a8e12
commit
4467e1b1cb
1 changed files with 20 additions and 9 deletions
|
|
@ -119,17 +119,28 @@ def generate_report(
|
||||||
with open(filepath, "wb") as f:
|
with open(filepath, "wb") as f:
|
||||||
f.write(xlsx_bytes)
|
f.write(xlsx_bytes)
|
||||||
|
|
||||||
# Save report metadata to DB
|
# Upsert report metadata (replace if same jahr/kw exists)
|
||||||
report = WeeklyReport(
|
report = (
|
||||||
jahr=jahr,
|
db.query(WeeklyReport)
|
||||||
kw=kw,
|
.filter(WeeklyReport.jahr == jahr, WeeklyReport.kw == kw)
|
||||||
report_date=date.today(),
|
.first()
|
||||||
report_data=report_data,
|
|
||||||
generated_by=user.id,
|
|
||||||
)
|
)
|
||||||
report.report_file_path = filepath
|
if report:
|
||||||
|
report.report_date = date.today()
|
||||||
|
report.report_data = report_data
|
||||||
|
report.report_file_path = filepath
|
||||||
|
report.generated_by = user.id
|
||||||
|
else:
|
||||||
|
report = WeeklyReport(
|
||||||
|
jahr=jahr,
|
||||||
|
kw=kw,
|
||||||
|
report_date=date.today(),
|
||||||
|
report_data=report_data,
|
||||||
|
generated_by=user.id,
|
||||||
|
)
|
||||||
|
report.report_file_path = filepath
|
||||||
|
db.add(report)
|
||||||
|
|
||||||
db.add(report)
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(report)
|
db.refresh(report)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue