From 391f025e0f1160a59cd330ac3477d781f090df47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Twoje=20Imi=C4=99=20Nazwisko?= Date: Thu, 24 Oct 2024 07:24:33 -0400 Subject: [PATCH] zoz model --- app/__pycache__/database.cpython-312.pyc | Bin 981 -> 0 bytes app/__pycache__/main.cpython-312.pyc | Bin 2935 -> 0 bytes app/__pycache__/models.cpython-312.pyc | Bin 933 -> 0 bytes app/__pycache__/schemas.cpython-312.pyc | Bin 1195 -> 0 bytes app/database.py | 2 +- app/main.py | 81 ++++++++++---------- app/models.py | 69 ++++++++++++----- app/schemas.py | 92 +++++++++++++++++++---- 8 files changed, 169 insertions(+), 75 deletions(-) delete mode 100644 app/__pycache__/database.cpython-312.pyc delete mode 100644 app/__pycache__/main.cpython-312.pyc delete mode 100644 app/__pycache__/models.cpython-312.pyc delete mode 100644 app/__pycache__/schemas.cpython-312.pyc diff --git a/app/__pycache__/database.cpython-312.pyc b/app/__pycache__/database.cpython-312.pyc deleted file mode 100644 index 15fbdf45c4c8f9b366b38e2c874690db0b0f4101..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 981 zcmZWn&rcIU82zUEtGl#-5oiSxK>3lP>4FGEVvMaqNGv9b9@r3?r905p-EQ683guKz zNHpOnBp$q)`UiMauUu+qB2GdgF&?0 zI~L%VyQnw529Bo@z+2EjLpm5pLCD1(-D5BcbBY$}+|30u+(@3DjFsBm`JU2WlJVrn+_%6d(4bJI-y@Aa#^9Z~ff*tTa-- z2{)#DE=JTn>sp$>==P$gs-?Mz>8lsx(tQ^>dOPXNCof)uVX)DP2f-X5+TZlAGIOwk zLJdc5a0n59!osS9<2bXN%3w+~YdE2+mg=Nl;`pL!yPVI)ww*FFhWZ>^YePofexYAK z9ks;?S+-2mnXoZ+a_$X}-wY4m4iANIO^l2TjmZwSov9E*BsJ%l3DYoA4iR0vr047; zVHZ;wjh?_dc-2{Jv9XR5PWM0(>$=&!Vp_U3@F;{Vubb>;B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Tt?>^pb|!ZFrkAESv<9Bu8ys4iTzj%{W8G=s zp}qd%(!I4S8+d(W7Z+LYao#gz%Odg|%mK8LO*M*%BIy7EF;Pz>Eu{@R2GE96Dse_X z!48mpqBEplx{Ep3MROZ=TOP+uux$w=qgPmQO0(;ebdu@J%BMp+)2otYKnGnQoc2l- zM@v*&J1S&3N5`XZ)_?W1Y-835o=m#43jo55SRmP??4s1ABk7X{Ba2~0xOBtAVMWmj z+Ux0GH#4`(ps=m;2{k{gu^uon8Fkt>lkVq=umP!)Aor2q1Xx9To!!gj47z%jAN`lX z1}^@^iaziQ0c0J?Ol4aaW}FZ&toaote*xK6rc0F8JNKdHShmc!*)TZEiE=6Y)D%Ve chOQnOSh=)u>kz@~u)|aa)}H@H@JcKG0}as)Q2+n{ diff --git a/app/database.py b/app/database.py index a754099..5a473bb 100644 --- a/app/database.py +++ b/app/database.py @@ -4,7 +4,7 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker #SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" # Dla SQLite -SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.19.0.4:3306/test" +SQLALCHEMY_DATABASE_URL = "mysql+mysqldb://root:secret@172.18.0.2:3306/test" engine = create_engine( SQLALCHEMY_DATABASE_URL, diff --git a/app/main.py b/app/main.py index f057015..4b5dea1 100644 --- a/app/main.py +++ b/app/main.py @@ -1,25 +1,15 @@ from fastapi import FastAPI, Depends, HTTPException from sqlalchemy.orm import Session -from fastapi.middleware.cors import CORSMiddleware -from app.database import SessionLocal, engine -import app.models as models -from app.schemas import Person, PersonCreate +from . import models, schemas +from .database import SessionLocal, engine +from datetime import datetime app = FastAPI() -origins = [ - "http://localhost:8080", - "http://mpabi.pl:8888", -] - -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) +# Tworzenie tabel w bazie danych +models.Base.metadata.create_all(bind=engine) +# Dependency do sesji z bazą danych def get_db(): db = SessionLocal() try: @@ -27,31 +17,42 @@ def get_db(): finally: db.close() -# Endpoint do tworzenia nowej osoby (POST) -@app.post("/persons/", response_model=Person) -def create_person(person: PersonCreate, db: Session = Depends(get_db)): - db_person = models.Person( - last_name=person.last_name, - first_name=person.first_name, - address=person.address, - city=person.city - ) - db.add(db_person) +# Endpoint do pobierania ról +@app.get("/roles/", response_model=list[schemas.Role]) +def read_roles(db: Session = Depends(get_db)): + return db.query(models.Role).all() + +# Endpoint do dodania roli +@app.post("/roles/", response_model=schemas.Role) +def create_role(role: schemas.RoleCreate, db: Session = Depends(get_db)): + db_role = models.Role(name=role.name) + db.add(db_role) db.commit() - db.refresh(db_person) - return db_person + db.refresh(db_role) + return db_role -# Endpoint do odczytu listy osób (GET) -@app.get("/persons/", response_model=list[Person]) -def read_persons(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): - persons = db.query(models.Person).offset(skip).limit(limit).all() - return persons +# Endpoint do pobierania zdarzeń +@app.get("/events/", response_model=list[schemas.Event]) +def read_events(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)): + return db.query(models.Event).offset(skip).limit(limit).all() -# Endpoint do odczytu osoby po ID (GET) -@app.get("/persons/{person_id}", response_model=Person) -def read_person(person_id: int, db: Session = Depends(get_db)): - person = db.query(models.Person).filter(models.Person.person_id == person_id).first() - if person is None: - raise HTTPException(status_code=404, detail="Person not found") - return person +# Endpoint do dodania zdarzenia +@app.post("/events/", response_model=schemas.Event) +def create_event(event: schemas.EventCreate, db: Session = Depends(get_db)): + db_event = models.Event( + role_id=event.role_id, + student_id=event.student_id, + criteria_id=event.criteria_id, + description=event.description, + event_date=event.event_date or datetime.now() + ) + db.add(db_event) + db.commit() + db.refresh(db_event) + return db_event + +# Endpoint do pobierania informacji o zdarzeniach dla konkretnego ucznia +@app.get("/events/student/{student_id}", response_model=list[schemas.Event]) +def read_student_events(student_id: int, db: Session = Depends(get_db)): + return db.query(models.Event).filter(models.Event.student_id == student_id).all() diff --git a/app/models.py b/app/models.py index 21d87c8..14d8995 100644 --- a/app/models.py +++ b/app/models.py @@ -1,25 +1,54 @@ -from sqlalchemy import Column, Integer, String -from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import Column, Integer, String, DateTime, ForeignKey +from sqlalchemy.orm import relationship +from .database import Base -Base = declarative_base() +class Criteria(Base): + __tablename__ = "criteria" + id = Column(Integer, primary_key=True, index=True) + name = Column(String(255)) -class Person(Base): - __tablename__ = "Persons" +class Role(Base): + __tablename__ = "roles" + id = Column(Integer, primary_key=True, index=True) + name = Column(String(255)) - person_id = Column("PersonID", Integer, primary_key=True, index=True, autoincrement=True) - last_name = Column("LastName", String(255), nullable=False) - first_name = Column("FirstName", String(255), nullable=True) - address = Column("Address", String(255), nullable=True) - city = Column("City", String(255), nullable=True) +class Student(Base): + __tablename__ = "students" + id = Column(Integer, primary_key=True, index=True) + first_name = Column(String(50)) + last_name = Column(String(50)) + grade = Column(String(50), default="Brak ocen") + points = Column(Integer, nullable=True) + status = Column(Integer) -""" -SQL Insert statements: -INSERT INTO Persons (PersonID, LastName, FirstName, Address, City) -VALUES -(1, 'Smith', 'John', '123 Maple St', 'New York'), -(2, 'Johnson', 'Emily', '456 Oak Ave', 'Los Angeles'), -(3, 'Williams', 'Michael', '789 Pine Dr', 'Chicago'), -(4, 'Brown', 'Sarah', '101 Birch Ln', 'Houston'), -(5, 'Jones', 'David', '202 Cedar Rd', 'Phoenix'); -""" +class ClassInfo(Base): + __tablename__ = "class_info" + id = Column(Integer, primary_key=True, index=True) + school = Column(String(255)) + city = Column(String(255)) + year = Column(String(50)) + semester = Column(String(50)) + subject = Column(String(255)) + level = Column(String(50)) + publisher = Column(String(255)) + class_name = Column(String(50)) + group = Column(String(50)) + profile = Column(String(255)) + max_points = Column(Integer) + teacher = Column(String(255)) + file_path = Column(String(255)) + student_count = Column(Integer) + +class Event(Base): + __tablename__ = "events" + id = Column(Integer, primary_key=True, index=True) + role_id = Column(Integer, ForeignKey("roles.id"), nullable=False) + student_id = Column(Integer, ForeignKey("students.id"), nullable=False) + criteria_id = Column(Integer, ForeignKey("criteria.id"), nullable=False) + description = Column(String(255)) + event_date = Column(DateTime) + + role = relationship("Role") + student = relationship("Student") + criteria = relationship("Criteria") diff --git a/app/schemas.py b/app/schemas.py index 2f499a8..7ca0324 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -1,19 +1,83 @@ -from pydantic import BaseModel, Field +from pydantic import BaseModel +from datetime import datetime -# Schemat do tworzenia nowej osoby (POST) -class PersonCreate(BaseModel): - last_name: str = Field(..., description="Nazwisko osoby", example="Kowalski") - first_name: str = Field(..., description="Imię osoby", example="Jan") - address: str | None = None - city: str | None = None +class CriteriaBase(BaseModel): + name: str -# Schemat reprezentujący osobę (GET) -class Person(BaseModel): - person_id: int - last_name: str - first_name: str - address: str | None = None - city: str | None = None +class CriteriaCreate(CriteriaBase): + pass + +class Criteria(CriteriaBase): + id: int + + class Config: + orm_mode = True + +class RoleBase(BaseModel): + name: str + +class RoleCreate(RoleBase): + pass + +class Role(RoleBase): + id: int + + class Config: + orm_mode = True + +class StudentBase(BaseModel): + first_name: str + last_name: str + grade: str + points: int + status: int + +class StudentCreate(StudentBase): + pass + +class Student(StudentBase): + id: int + + class Config: + orm_mode = True + +class ClassInfoBase(BaseModel): + school: str + city: str + year: str + semester: str + subject: str + level: str + publisher: str + class_name: str + group: str + profile: str + max_points: int + teacher: str + file_path: str + student_count: int + +class ClassInfoCreate(ClassInfoBase): + pass + +class ClassInfo(ClassInfoBase): + id: int + + class Config: + orm_mode = True + +class EventBase(BaseModel): + role_id: int + student_id: int + criteria_id: int + description: str + event_date: datetime + +class EventCreate(EventBase): + pass + +class Event(EventBase): + id: int class Config: orm_mode = True