ajout employee
This commit is contained in:
parent
f5a4b58668
commit
c2467efef7
44
app.py
44
app.py
@ -1,12 +1,16 @@
|
|||||||
from fastapi import FastAPI, Request, HTTPException
|
from fastapi import FastAPI, Request, HTTPException, Form
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import RedirectResponse, HTMLResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
from sqlmodel import SQLModel, Session, create_engine, select
|
from typing import Annotated
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from sqlmodel import SQLModel, Relationship, Session, create_engine, select
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from models import ordinateur
|
from models import ordinateur
|
||||||
|
from forms import EmployeeForm
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
#ordi1 = ordinateur()
|
#ordi1 = ordinateur()
|
||||||
@ -20,6 +24,9 @@ sqlite_url = f"sqlite:///mydb.db"
|
|||||||
|
|
||||||
engine = create_engine(sqlite_url)
|
engine = create_engine(sqlite_url)
|
||||||
|
|
||||||
|
def create_db_and_tables():
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
SQLModel.metadata.create_all(engine)
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
@app.get("/ordi/{ordi_id}", response_class=HTMLResponse)
|
@app.get("/ordi/{ordi_id}", response_class=HTMLResponse)
|
||||||
@ -31,6 +38,34 @@ async def get_ordi1_info(request: Request, ordi_id: int):
|
|||||||
request=request, name="item.html", context={"ordi": this_ordi}
|
request=request, name="item.html", context={"ordi": this_ordi}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@app.get("/employee/create", response_class=HTMLResponse)
|
||||||
|
async def get_employee_form(request: Request):
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
request=request, name="employee_form.html", success= True )
|
||||||
|
|
||||||
|
@app.post("/employee/create", response_class=RedirectResponse)
|
||||||
|
async def submit_employee_form(data: Annotated[EmployeeForm, Form()]):
|
||||||
|
print(f"data obtained is {data}")
|
||||||
|
with Session(engine) as session:
|
||||||
|
employee = Employee(
|
||||||
|
first_name=data.first_name,
|
||||||
|
family_name=data.family_name
|
||||||
|
)
|
||||||
|
session.add(employee)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(employee)
|
||||||
|
return RedirectResponse("/employees")
|
||||||
|
|
||||||
|
|
||||||
|
'''@app.post("/employee/delete", response=RedirectResponse)
|
||||||
|
async def delete_employee(request: Request):
|
||||||
|
with Session(engine) as session:
|
||||||
|
statement = select(ordinateur)
|
||||||
|
results = session.exec(statement)
|
||||||
|
ordi1 = results.one()
|
||||||
|
|
||||||
|
session.delete(ordi1)
|
||||||
|
session.commit()'''
|
||||||
|
|
||||||
@app.post("/endpoint")
|
@app.post("/endpoint")
|
||||||
async def receive_info(request: Request):
|
async def receive_info(request: Request):
|
||||||
@ -54,6 +89,7 @@ async def receive_info(request: Request):
|
|||||||
statement = select(ordinateur).where(ordinateur.mac_address == hardware.get("mac_address", ""))
|
statement = select(ordinateur).where(ordinateur.mac_address == hardware.get("mac_address", ""))
|
||||||
ordi1 = session.exec(statement).first()
|
ordi1 = session.exec(statement).first()
|
||||||
|
|
||||||
|
|
||||||
is_new = False
|
is_new = False
|
||||||
if not ordi1:
|
if not ordi1:
|
||||||
ordi1 = ordinateur()
|
ordi1 = ordinateur()
|
||||||
@ -87,6 +123,8 @@ async def receive_info(request: Request):
|
|||||||
|
|
||||||
if is_new:
|
if is_new:
|
||||||
session.add(ordi1)
|
session.add(ordi1)
|
||||||
|
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
#session.close()
|
#session.close()
|
||||||
|
|
||||||
|
|||||||
19
models.py
19
models.py
@ -1,7 +1,19 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import configparser
|
import configparser
|
||||||
import requests
|
import requests
|
||||||
from sqlmodel import Field, SQLModel
|
from sqlmodel import Field, SQLModel, Relationship
|
||||||
|
|
||||||
|
class employee_ordinateur(SQLModel, table=True):
|
||||||
|
employee_id: int | None = Field(default=None, foreign_key="employee.id", primary_key=True)
|
||||||
|
ordinateur_id: int | None = Field(default=None, foreign_key="ordinateur.id", primary_key=True)
|
||||||
|
|
||||||
|
class employee(SQLModel, table=True):
|
||||||
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
first_name: str = Field(index=True)
|
||||||
|
family_name: str = Field(index=True)
|
||||||
|
badge_number: str = Field(index=True)
|
||||||
|
|
||||||
|
ordinateurs: list["ordinateur"] = Relationship(back_populates="employees", link_model=employee_ordinateur)
|
||||||
|
|
||||||
class ordinateur(SQLModel, table=True):
|
class ordinateur(SQLModel, table=True):
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
@ -29,10 +41,7 @@ class ordinateur(SQLModel, table=True):
|
|||||||
wm: str = Field(index=True)
|
wm: str = Field(index=True)
|
||||||
kernel: str = Field(index=True)
|
kernel: str = Field(index=True)
|
||||||
|
|
||||||
class employee(SQLModel, tablel=True):
|
employees: list["employee"] = Relationship(back_populates="ordinateurs", link_model=employee_ordinateur)
|
||||||
id: int | None = Field(default=None, primary_key=True)
|
|
||||||
first_name: str = Field(index=True)
|
|
||||||
family_name: str = Field(index=True)
|
|
||||||
|
|
||||||
def shutdown():
|
def shutdown():
|
||||||
return
|
return
|
||||||
|
|||||||
@ -12,6 +12,7 @@ Jinja2==3.1.6
|
|||||||
MarkupSafe==3.0.3
|
MarkupSafe==3.0.3
|
||||||
pydantic==2.12.5
|
pydantic==2.12.5
|
||||||
pydantic_core==2.41.5
|
pydantic_core==2.41.5
|
||||||
|
python-multipart==0.0.22
|
||||||
requests==2.32.5
|
requests==2.32.5
|
||||||
SQLAlchemy==2.0.46
|
SQLAlchemy==2.0.46
|
||||||
sqlmodel==0.0.32
|
sqlmodel==0.0.32
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user