-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddle_goal.py
120 lines (92 loc) · 4.13 KB
/
middle_goal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from fastapi import Request, Form, Depends
from fastapi.templating import Jinja2Templates
from fastapi import APIRouter
from typing import List, Optional
from sqlalchemy.orm import Session
from starlette.responses import RedirectResponse
from database import SessionLocal, engine
import models
# Templates (Jinja2)
templates = Jinja2Templates(directory="templates/")
# Router
router = APIRouter()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
models.Base.metadata.create_all(bind=engine)
@router.get("/middle_goal/{high_level_goal_id}")
async def middle_goal(request: Request, high_level_goal_id: int, refinement_type: str = "AND", db: Session = Depends(get_db)):
goal_with_outputs = db.query(models.Goal).filter(models.Goal.id == high_level_goal_id).first()
if not goal_with_outputs:
return RedirectResponse("/")
highlevelgoal = goal_with_outputs.goal_name
# Get all current subgoals linked to the high-level goal
subgoals = db.query(models.Goal).join(models.Hierarchy, models.Goal.id == models.Hierarchy.subgoal_id).filter(
models.Hierarchy.high_level_goal_id == high_level_goal_id,
models.Hierarchy.refinement == refinement_type
).all()
return templates.TemplateResponse('middle_goal.html', context={
'request': request,
'hlg_id': high_level_goal_id,
'refinement_type': refinement_type,
'highlevelgoal': highlevelgoal,
'subgoals': subgoals # Pass the subgoals to the template
})
@router.post("/middle_goal")
async def middle_goal(request: Request, goal_name: str = Form(...), goal_type: str = Form(...), hlg_id: int = Form(...),
refinement_type: str = Form(...), subgoal_id: List[str] = Form([]), db: Session = Depends(get_db)):
if hlg_id == -1:
return RedirectResponse(f"/goal_model_generation", status_code=302)
# Create the new mid-goal (MG)
new_middle_goal = models.Goal(goal_type=goal_type, goal_name=goal_name)
db.add(new_middle_goal)
db.commit()
db.refresh(new_middle_goal)
# If a subgoal (SG) is provided, link the MG between the high-level goal (HLG) and the SG
for sg in subgoal_id:
# --> Update the goal hierarchy <--
# Break the current hierarchy between the HLG and the SG
# Remove any existing relationships between the HLG and the SGs
old_hierarchy = db.query(models.Hierarchy).filter(
models.Hierarchy.high_level_goal_id == hlg_id,
models.Hierarchy.subgoal_id == sg,
models.Hierarchy.refinement == refinement_type
).first()
if old_hierarchy:
db.delete(old_hierarchy)
db.commit()
# Check if the new hierarchy (HLG -> MG) already exists to avoid redundancy
existing_hierarchy_hlg = db.query(models.Hierarchy).filter(
models.Hierarchy.high_level_goal_id == hlg_id,
models.Hierarchy.subgoal_id == new_middle_goal.id,
models.Hierarchy.refinement == refinement_type
).first()
if not existing_hierarchy_hlg:
# Insert the new relationship (HLG -> MG)
new_hierarchy_hlg = models.Hierarchy(
refinement=refinement_type,
high_level_goal_id=hlg_id,
subgoal_id=new_middle_goal.id
)
db.add(new_hierarchy_hlg)
# Check if the new hierarchy (MG -> SG) already exists to avoid redundancy
existing_hierarchy_sub = db.query(models.Hierarchy).filter(
models.Hierarchy.high_level_goal_id == new_middle_goal.id,
models.Hierarchy.subgoal_id == sg,
models.Hierarchy.refinement == refinement_type
).first()
if not existing_hierarchy_sub:
# Insert the new relationship (MG -> SG)
new_hierarchy_sub = models.Hierarchy(
refinement=refinement_type,
high_level_goal_id=new_middle_goal.id,
subgoal_id=sg
)
db.add(new_hierarchy_sub)
# Commit all changes after the loop
db.commit()
return RedirectResponse(f"/goal_model_generation", status_code=302)