Spaces:
Runtime error
Runtime error
feat(classes): add full edit and delete support for classes and curriculum subjects with interactive modals
Browse files- app/api/classes.py +27 -0
- app/api/subjects.py +23 -0
- app/schemas/schemas.py +10 -0
app/api/classes.py
CHANGED
|
@@ -43,6 +43,33 @@ def get_class_students(class_id: int, db: Session = Depends(get_db), token: dict
|
|
| 43 |
results.append(s_out)
|
| 44 |
return results
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@router.delete("/{class_id}")
|
| 47 |
def delete_class(class_id: int, db: Session = Depends(get_db), token: dict = Depends(get_current_user_token)):
|
| 48 |
cls = db.query(Class).filter(Class.id == class_id).first()
|
|
|
|
| 43 |
results.append(s_out)
|
| 44 |
return results
|
| 45 |
|
| 46 |
+
@router.put("/{class_id}", response_model=ClassOut)
|
| 47 |
+
@router.patch("/{class_id}", response_model=ClassOut)
|
| 48 |
+
def update_class(
|
| 49 |
+
class_id: int,
|
| 50 |
+
req: ClassUpdate,
|
| 51 |
+
db: Session = Depends(get_db),
|
| 52 |
+
token: dict = Depends(get_current_user_token)
|
| 53 |
+
):
|
| 54 |
+
cls = db.query(Class).filter(Class.id == class_id).first()
|
| 55 |
+
if not cls:
|
| 56 |
+
raise HTTPException(status_code=404, detail="Class not found")
|
| 57 |
+
|
| 58 |
+
if req.name is not None:
|
| 59 |
+
cls.name = req.name
|
| 60 |
+
if req.section is not None:
|
| 61 |
+
cls.section = req.section
|
| 62 |
+
if req.academic_year is not None:
|
| 63 |
+
cls.academic_year = req.academic_year
|
| 64 |
+
|
| 65 |
+
db.commit()
|
| 66 |
+
db.refresh(cls)
|
| 67 |
+
|
| 68 |
+
student_count = db.query(Student).filter(Student.class_id == cls.id, Student.active == True).count()
|
| 69 |
+
cls_out = ClassOut.model_validate(cls)
|
| 70 |
+
cls_out.student_count = student_count
|
| 71 |
+
return cls_out
|
| 72 |
+
|
| 73 |
@router.delete("/{class_id}")
|
| 74 |
def delete_class(class_id: int, db: Session = Depends(get_db), token: dict = Depends(get_current_user_token)):
|
| 75 |
cls = db.query(Class).filter(Class.id == class_id).first()
|
app/api/subjects.py
CHANGED
|
@@ -27,6 +27,29 @@ def create_subject(req: SubjectCreate, db: Session = Depends(get_db), token: dic
|
|
| 27 |
db.refresh(subject)
|
| 28 |
return subject
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@router.delete("/{subject_id}")
|
| 31 |
def delete_subject(subject_id: int, db: Session = Depends(get_db), token: dict = Depends(get_current_user_token)):
|
| 32 |
subject = db.query(Subject).filter(Subject.id == subject_id).first()
|
|
|
|
| 27 |
db.refresh(subject)
|
| 28 |
return subject
|
| 29 |
|
| 30 |
+
@router.put("/{subject_id}", response_model=SubjectOut)
|
| 31 |
+
@router.patch("/{subject_id}", response_model=SubjectOut)
|
| 32 |
+
def update_subject(
|
| 33 |
+
subject_id: int,
|
| 34 |
+
req: SubjectUpdate,
|
| 35 |
+
db: Session = Depends(get_db),
|
| 36 |
+
token: dict = Depends(get_current_user_token)
|
| 37 |
+
):
|
| 38 |
+
subject = db.query(Subject).filter(Subject.id == subject_id).first()
|
| 39 |
+
if not subject:
|
| 40 |
+
raise HTTPException(status_code=404, detail="Subject not found")
|
| 41 |
+
|
| 42 |
+
if req.name is not None:
|
| 43 |
+
subject.name = req.name
|
| 44 |
+
if req.code is not None:
|
| 45 |
+
subject.code = req.code
|
| 46 |
+
if req.class_id is not None:
|
| 47 |
+
subject.class_id = req.class_id
|
| 48 |
+
|
| 49 |
+
db.commit()
|
| 50 |
+
db.refresh(subject)
|
| 51 |
+
return subject
|
| 52 |
+
|
| 53 |
@router.delete("/{subject_id}")
|
| 54 |
def delete_subject(subject_id: int, db: Session = Depends(get_db), token: dict = Depends(get_current_user_token)):
|
| 55 |
subject = db.query(Subject).filter(Subject.id == subject_id).first()
|
app/schemas/schemas.py
CHANGED
|
@@ -47,6 +47,11 @@ class ClassCreate(BaseModel):
|
|
| 47 |
section: str
|
| 48 |
academic_year: str
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
class ClassOut(BaseModel):
|
| 51 |
model_config = ConfigDict(from_attributes=True)
|
| 52 |
|
|
@@ -62,6 +67,11 @@ class SubjectCreate(BaseModel):
|
|
| 62 |
code: str
|
| 63 |
class_id: int
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
class SubjectOut(BaseModel):
|
| 66 |
model_config = ConfigDict(from_attributes=True)
|
| 67 |
|
|
|
|
| 47 |
section: str
|
| 48 |
academic_year: str
|
| 49 |
|
| 50 |
+
class ClassUpdate(BaseModel):
|
| 51 |
+
name: Optional[str] = None
|
| 52 |
+
section: Optional[str] = None
|
| 53 |
+
academic_year: Optional[str] = None
|
| 54 |
+
|
| 55 |
class ClassOut(BaseModel):
|
| 56 |
model_config = ConfigDict(from_attributes=True)
|
| 57 |
|
|
|
|
| 67 |
code: str
|
| 68 |
class_id: int
|
| 69 |
|
| 70 |
+
class SubjectUpdate(BaseModel):
|
| 71 |
+
name: Optional[str] = None
|
| 72 |
+
code: Optional[str] = None
|
| 73 |
+
class_id: Optional[int] = None
|
| 74 |
+
|
| 75 |
class SubjectOut(BaseModel):
|
| 76 |
model_config = ConfigDict(from_attributes=True)
|
| 77 |
|