add contract page with dynamic form elements
This commit is contained in:
@@ -58,7 +58,6 @@ def callback(code: str, session: Session = Depends(get_session)):
|
||||
email=decoded_token.get("email"),
|
||||
name=decoded_token.get("preferred_username")
|
||||
)
|
||||
print(user_create)
|
||||
user = service.get_or_create_user(session, user_create)
|
||||
return {
|
||||
"access_token": token_data["access_token"],
|
||||
|
||||
@@ -11,7 +11,7 @@ def get_all(
|
||||
statement = statement.where(models.Form.season.in_(seasons))
|
||||
if len(productors) > 0:
|
||||
statement = statement.join(models.Productor).where(models.Productor.name.in_(productors))
|
||||
return session.exec(statement).all()
|
||||
return session.exec(statement.order_by(models.Form.name)).all()
|
||||
|
||||
def get_one(session: Session, form_id: int) -> models.FormPublic:
|
||||
return session.get(models.Form, form_id)
|
||||
|
||||
@@ -33,7 +33,12 @@ class ProductorPublic(ProductorBase):
|
||||
class Productor(ProductorBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
products: list["Product"] = Relationship(back_populates='productor')
|
||||
products: list["Product"] = Relationship(
|
||||
back_populates='productor',
|
||||
sa_relationship_kwargs={
|
||||
"order_by": "Product.name"
|
||||
},
|
||||
)
|
||||
|
||||
class ProductorUpdate(SQLModel):
|
||||
name: str | None
|
||||
@@ -60,9 +65,10 @@ class ShipmentProductLink(SQLModel, table=True):
|
||||
class ProductBase(SQLModel):
|
||||
name: str
|
||||
unit: Unit
|
||||
price: float
|
||||
price: float | None
|
||||
price_kg: float | None
|
||||
weight: float | None
|
||||
quantity: float | None
|
||||
quantity_unit: str | None
|
||||
type: ProductType
|
||||
productor_id: int | None = Field(default=None, foreign_key="productor.id")
|
||||
|
||||
@@ -81,12 +87,13 @@ class ProductUpdate(SQLModel):
|
||||
unit: Unit | None
|
||||
price: float | None
|
||||
price_kg: float | None
|
||||
weight: float | None
|
||||
quantity: float | None
|
||||
quantity_unit: str | None
|
||||
productor_id: int | None
|
||||
shipment_ids: list[int] | None = []
|
||||
type: ProductType | None
|
||||
|
||||
class ProductCreate(ProductBase):
|
||||
shipment_ids: list[int] | None = []
|
||||
pass
|
||||
|
||||
class FormBase(SQLModel):
|
||||
name: str
|
||||
@@ -106,7 +113,13 @@ class Form(FormBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
productor: Optional['Productor'] = Relationship()
|
||||
referer: Optional['User'] = Relationship()
|
||||
shipments: list["Shipment"] = Relationship(back_populates="form", cascade_delete=True)
|
||||
shipments: list["Shipment"] = Relationship(
|
||||
back_populates="form",
|
||||
cascade_delete=True,
|
||||
sa_relationship_kwargs={
|
||||
"order_by": "Shipment.name"
|
||||
},
|
||||
)
|
||||
|
||||
class FormUpdate(SQLModel):
|
||||
name: str | None
|
||||
@@ -161,7 +174,13 @@ class ShipmentPublic(ShipmentBase):
|
||||
|
||||
class Shipment(ShipmentBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
products: list[Product] = Relationship(back_populates="shipments", link_model=ShipmentProductLink)
|
||||
products: list[Product] = Relationship(
|
||||
back_populates="shipments",
|
||||
link_model=ShipmentProductLink,
|
||||
sa_relationship_kwargs={
|
||||
"order_by": "Product.name"
|
||||
},
|
||||
)
|
||||
form: Optional[Form] = Relationship(back_populates="shipments")
|
||||
|
||||
class ShipmentUpdate(SQLModel):
|
||||
|
||||
@@ -7,7 +7,7 @@ def get_all(session: Session, names: list[str], types: list[str]) -> list[models
|
||||
statement = statement.where(models.Productor.name.in_(names))
|
||||
if len(types) > 0:
|
||||
statement = statement.where(models.Productor.type.in_(types))
|
||||
return session.exec(statement).all()
|
||||
return session.exec(statement.order_by(models.Productor.name)).all()
|
||||
|
||||
def get_one(session: Session, productor_id: int) -> models.ProductorPublic:
|
||||
return session.get(models.Productor, productor_id)
|
||||
|
||||
@@ -11,9 +11,10 @@ router = APIRouter(prefix='/products')
|
||||
def get_products(
|
||||
session: Session = Depends(get_session),
|
||||
names: list[str] = Query([]),
|
||||
types: list[str] = Query([]),
|
||||
productors: list[str] = Query([]),
|
||||
):
|
||||
return service.get_all(session, names, productors)
|
||||
return service.get_all(session, names, productors, types)
|
||||
|
||||
@router.get('/{id}', response_model=models.ProductPublic)
|
||||
def get_product(id: int, session: Session = Depends(get_session)):
|
||||
|
||||
@@ -4,23 +4,25 @@ import src.models as models
|
||||
def get_all(
|
||||
session: Session,
|
||||
names: list[str],
|
||||
productors: list[str]
|
||||
productors: list[str],
|
||||
types: list[str],
|
||||
|
||||
) -> list[models.ProductPublic]:
|
||||
statement = select(models.Product)
|
||||
if len(names) > 0:
|
||||
statement = statement.where(models.Product.name.in_(names))
|
||||
if len(productors) > 0:
|
||||
statement = statement.join(models.Productor).where(models.Productor.name.in_(productors))
|
||||
return session.exec(statement).all()
|
||||
if len(types) > 0:
|
||||
statement = statement.where(models.Product.type.in_(types))
|
||||
return session.exec(statement.order_by(models.Product.name)).all()
|
||||
|
||||
def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||
return session.get(models.Product, product_id)
|
||||
|
||||
def create_one(session: Session, product: models.ProductCreate) -> models.ProductPublic:
|
||||
shipments = session.exec(select(models.Shipment).where(models.Shipment.id.in_(product.shipment_ids))).all()
|
||||
|
||||
product_create = product.model_dump(exclude_unset=True, exclude={'shipment_ids'})
|
||||
new_product = models.Product(**product_create, shipments=shipments)
|
||||
product_create = product.model_dump(exclude_unset=True)
|
||||
new_product = models.Product(**product_create)
|
||||
session.add(new_product)
|
||||
session.commit()
|
||||
session.refresh(new_product)
|
||||
@@ -32,13 +34,7 @@ def update_one(session: Session, id: int, product: models.ProductUpdate) -> mode
|
||||
new_product = result.first()
|
||||
if not new_product:
|
||||
return None
|
||||
|
||||
shipments_to_add = session.exec(select(models.Shipment).where(models.Shipment.id.in_(product.shipment_ids))).all()
|
||||
new_product.shipments.clear()
|
||||
for add in shipments_to_add:
|
||||
new_product.shipments.append(add)
|
||||
|
||||
product_updates = product.model_dump(exclude_unset=True, exclude={"shipment_ids"})
|
||||
product_updates = product.model_dump(exclude_unset=True)
|
||||
for key, value in product_updates.items():
|
||||
setattr(new_product, key, value)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import src.models as models
|
||||
|
||||
def get_all(session: Session) -> list[models.ShipmentPublic]:
|
||||
statement = select(models.Shipment)
|
||||
return session.exec(statement).all()
|
||||
return session.exec(statement.order_by(models.Shipment.name)).all()
|
||||
|
||||
def get_one(session: Session, shipment_id: int) -> models.ShipmentPublic:
|
||||
return session.get(models.Shipment, shipment_id)
|
||||
|
||||
@@ -3,7 +3,7 @@ import src.models as models
|
||||
|
||||
def get_all(session: Session) -> list[models.TemplatePublic]:
|
||||
statement = select(models.Template)
|
||||
return session.exec(statement).all()
|
||||
return session.exec(statement.order_by(models.Template.name)).all()
|
||||
|
||||
def get_one(session: Session, template_id: int) -> models.TemplatePublic:
|
||||
return session.get(models.Template, template_id)
|
||||
|
||||
@@ -3,7 +3,7 @@ import src.models as models
|
||||
|
||||
def get_all(session: Session) -> list[models.UserPublic]:
|
||||
statement = select(models.User)
|
||||
return session.exec(statement).all()
|
||||
return session.exec(statement.order_by(models.User.name)).all()
|
||||
|
||||
def get_one(session: Session, user_id: int) -> models.UserPublic:
|
||||
return session.get(models.User, user_id)
|
||||
|
||||
Reference in New Issue
Block a user