Introduce product details table
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import datetime
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, File, Form, HTTPException, Request
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from starlette import status
|
||||
from starlette.responses import RedirectResponse
|
||||
@@ -13,6 +14,7 @@ from allmende_payment_system.models import (
|
||||
Area,
|
||||
Permission,
|
||||
Product,
|
||||
ProductDetails,
|
||||
Transaction,
|
||||
User,
|
||||
UserGroup,
|
||||
@@ -234,9 +236,14 @@ async def edit_product_post(
|
||||
select(Product).where(Product.id == product_id)
|
||||
).scalar_one()
|
||||
|
||||
for field_name, data in product_data.model_dump().items():
|
||||
setattr(product, field_name, data)
|
||||
new_details = ProductDetails(
|
||||
valid_from=func.now(),
|
||||
)
|
||||
|
||||
for field_name, data in product_data.model_dump().items():
|
||||
setattr(new_details, field_name, data)
|
||||
|
||||
product.details_history.append(new_details)
|
||||
session.flush()
|
||||
|
||||
return RedirectResponse(
|
||||
@@ -273,12 +280,15 @@ async def new_product_post(
|
||||
if not user.has_permission("product", "edit"):
|
||||
raise HTTPException(status_code=403, detail="Insufficient permissions")
|
||||
|
||||
product = Product()
|
||||
product = Product(area_id=product_data.area_id)
|
||||
session.add(product)
|
||||
session.flush()
|
||||
|
||||
product_details = ProductDetails()
|
||||
product.details_history.append(product_details)
|
||||
|
||||
for field_name, data in product_data.model_dump().items():
|
||||
setattr(product, field_name, data)
|
||||
|
||||
session.add(product)
|
||||
setattr(product_details, field_name, data)
|
||||
|
||||
return RedirectResponse(
|
||||
url="/admin/products", status_code=status.HTTP_303_SEE_OTHER
|
||||
|
||||
Reference in New Issue
Block a user