feat: Start/Ende fuer Fixposten in der GUI + Datumsvalidierung
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@ from decimal import Decimal
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -28,6 +28,13 @@ class RecurringIn(BaseModel):
|
||||
end_date: date | None = None
|
||||
category_id: int | None = None
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _ende_nicht_vor_start(self):
|
||||
if (self.start_date is not None and self.end_date is not None
|
||||
and self.end_date < self.start_date):
|
||||
raise ValueError("Ende darf nicht vor Start liegen")
|
||||
return self
|
||||
|
||||
|
||||
class RecurringOut(RecurringIn):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
@@ -90,6 +97,10 @@ def patch_recurring(item_id: int, data: RecurringPatch,
|
||||
_check_category(session, fields["category_id"])
|
||||
for key, value in fields.items():
|
||||
setattr(item, key, value)
|
||||
if (item.start_date is not None and item.end_date is not None
|
||||
and item.end_date < item.start_date):
|
||||
session.rollback()
|
||||
raise HTTPException(422, "Ende darf nicht vor Start liegen")
|
||||
session.commit()
|
||||
session.refresh(item)
|
||||
return RecurringOut.model_validate(item)
|
||||
|
||||
Reference in New Issue
Block a user