list employees done
This commit is contained in:
parent
a8262fa689
commit
3df8aedbd4
64
app.py
64
app.py
@ -1,4 +1,4 @@
|
||||
from fastapi import FastAPI, Request, HTTPException, Form
|
||||
from fastapi import FastAPI, Request, HTTPException, Form, responses
|
||||
from fastapi.responses import RedirectResponse, HTMLResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
@ -9,8 +9,7 @@ from pydantic import BaseModel
|
||||
from sqlmodel import SQLModel, Relationship, Session, create_engine, select
|
||||
|
||||
import json
|
||||
from models import ordinateur, employee, employee_ordinateur
|
||||
from forms import EmployeeForm
|
||||
from models import ordinateur, employee
|
||||
|
||||
app = FastAPI()
|
||||
#ordi1 = ordinateur()
|
||||
@ -38,45 +37,48 @@ async def get_ordi1_info(request: Request, ordi_id: int):
|
||||
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(
|
||||
"employee_form.html", {"request" : request})
|
||||
|
||||
@app.post("/employee/add", response_class=HTMLResponse)
|
||||
async def submit_employee_form(
|
||||
first_name: str = Form(...),
|
||||
family_name: str = Form(...),
|
||||
badge_number: str = Form(...)
|
||||
):
|
||||
with Session(engine) as session:
|
||||
new_employee = employee(
|
||||
first_name=first_name,
|
||||
family_name=family_name,
|
||||
badge_number=badge_number
|
||||
)
|
||||
session.add(new_employee)
|
||||
session.commit()
|
||||
|
||||
return responses.RedirectResponse(
|
||||
url="/employees", status_code=303)
|
||||
|
||||
@app.get("/employees", response_class=HTMLResponse)
|
||||
async def get_employees(request: Request):
|
||||
with Session(engine) as session:
|
||||
statement= select(employee)
|
||||
employees= session.exec(statement).all()
|
||||
return templates.TemplateResponse(
|
||||
request=request, name="employees.html", context={"employees": employees}
|
||||
"employees.html", {"request" : request, "employees" : employees}
|
||||
)
|
||||
|
||||
@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=HTMLResponse)
|
||||
async def submit_employee_form(data: Annotated[EmployeeForm, Form()]):
|
||||
print(f"data obtained is {data}")
|
||||
@app.post("/employee/{e_id}/delete")
|
||||
async def delete_employee(e_id: int):
|
||||
with Session(engine) as session:
|
||||
employee = Employee(
|
||||
first_name=data.fname,
|
||||
family_name=data.lname,
|
||||
badge_number=data.badge
|
||||
)
|
||||
session.add(employee)
|
||||
db_e = session.get(employee, e_id)
|
||||
if not db_e:
|
||||
raise HTTPException(status_code=404, detail="Employé non trouvé")
|
||||
session.delete(db_e)
|
||||
session.commit()
|
||||
session.refresh(employee)
|
||||
return templates.TemplateResponse(
|
||||
request=request, name="employees.html", context={"employees": 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()'''
|
||||
return responses.RedirectResponse(url="/employees", status_code=303)
|
||||
|
||||
@app.post("/endpoint")
|
||||
async def receive_info(request: Request):
|
||||
|
||||
@ -9,9 +9,9 @@ class employee_ordinateur(SQLModel, table=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)
|
||||
first_name: str
|
||||
family_name: str
|
||||
badge_number: str
|
||||
|
||||
ordinateurs: list["ordinateur"] = Relationship(back_populates="employees", link_model=employee_ordinateur)
|
||||
|
||||
|
||||
@ -1,20 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.4/css/bulma.min.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container">
|
||||
|
||||
<h2>Employee form</h2>
|
||||
<div class="title pt-6">Employee form</div>
|
||||
|
||||
<form action="/employee/add" method="post">
|
||||
<label> First name:</label><br>
|
||||
<input class="input" type="text" name="first_name"><br>
|
||||
<label> Family name:</label><br>
|
||||
<input class ="input"type="text" name="family_name"><br>
|
||||
<label> Badge number:</label><br>
|
||||
<input class ="input"type="text" name="badge_number"><br> <br>
|
||||
|
||||
<form action="/employee/create" method="post" target="_blank">
|
||||
<label for="fname">First name:</label><br>
|
||||
<input type="text" id="fname" name="fname" value=""><br>
|
||||
<label for="lname">Family name:</label><br>
|
||||
<input type="text" id="lname" name="lname" value=""><br>
|
||||
<label for="badge">Badge number:</label><br>
|
||||
<input type="text" id="badge" name="badge" value=""><br>
|
||||
<label for="ordis">Ordinateurs</label><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
</html>
|
||||
@ -1,19 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.4/css/bulma.min.css">
|
||||
<title>Employees</title>
|
||||
</head>
|
||||
<body>
|
||||
<ul>{% for e in employees %}
|
||||
<li>{{ e.first_name}} {{ e.last_name }} {{ e.badge_number }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<ul>
|
||||
<li>uhdziuuadza</li>
|
||||
|
||||
</ul>
|
||||
<body>
|
||||
<div class="container">
|
||||
|
||||
<div class="title pt-6">Employee list</div>
|
||||
|
||||
<table class="table is-striped is-bordered is-fullwidth">
|
||||
|
||||
<tr class="is-link">
|
||||
<th>Id</th>
|
||||
<th>First name</th>
|
||||
<th>Family name</th>
|
||||
<th>Badge number</th>
|
||||
</tr>
|
||||
|
||||
|
||||
{% for e in employees %}
|
||||
<tr>
|
||||
<td>{{ e.id }}</td>
|
||||
<td>{{ e.first_name }}</td>
|
||||
<td>{{ e.family_name }}</td>
|
||||
<td>{{ e.badge_number }}</td>
|
||||
|
||||
|
||||
{% endfor %}
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user