add abandoned status to distinguish given-up habits from completed ones
Completed and abandoned habits were both lumped under a single terminal state, making it impossible to tell "finished successfully" apart from "gave up" when reviewing habit history.
This commit is contained in:
+30
-3
@@ -118,6 +118,8 @@ def habits_page(request: Request, tab: str = "build", db: Session = Depends(get_
|
||||
|
||||
if tab == "completed":
|
||||
habits = habit_service.list_habits(db, current.id, status=HabitStatus.COMPLETED)
|
||||
elif tab == "abandoned":
|
||||
habits = habit_service.list_habits(db, current.id, status=HabitStatus.ABANDONED)
|
||||
else:
|
||||
if tab not in ("build", "quit"):
|
||||
tab = "build"
|
||||
@@ -139,6 +141,14 @@ def habits_page(request: Request, tab: str = "build", db: Session = Depends(get_
|
||||
)
|
||||
|
||||
|
||||
def _tab_for_habit(habit) -> str:
|
||||
if habit.status == HabitStatus.COMPLETED:
|
||||
return "completed"
|
||||
if habit.status == HabitStatus.ABANDONED:
|
||||
return "abandoned"
|
||||
return habit.habit_type.value
|
||||
|
||||
|
||||
@router.post("/habits/new")
|
||||
def create_habit_page(
|
||||
request: Request,
|
||||
@@ -208,7 +218,7 @@ def edit_habit_page(
|
||||
return HTMLResponse("입력값을 확인해주세요")
|
||||
|
||||
habit_service.update_habit(db, habit, data)
|
||||
tab = "completed" if habit.status == HabitStatus.COMPLETED else habit.habit_type.value
|
||||
tab = _tab_for_habit(habit)
|
||||
response = Response(status_code=200)
|
||||
response.headers["HX-Redirect"] = f"/habits?tab={tab}"
|
||||
return response
|
||||
@@ -230,6 +240,22 @@ def complete_habit_page(request: Request, habit_id: int, db: Session = Depends(g
|
||||
return response
|
||||
|
||||
|
||||
@router.post("/habits/{habit_id}/abandon")
|
||||
def abandon_habit_page(request: Request, habit_id: int, db: Session = Depends(get_db)):
|
||||
current = _current_user_or_redirect(request, db)
|
||||
if isinstance(current, RedirectResponse):
|
||||
return current
|
||||
|
||||
habit = habit_service.get_habit(db, habit_id, current.id)
|
||||
if habit is None:
|
||||
return Response(status_code=404)
|
||||
tab = habit.habit_type.value
|
||||
habit_service.abandon_habit(db, habit)
|
||||
response = Response(status_code=200)
|
||||
response.headers["HX-Redirect"] = f"/habits?tab={tab}"
|
||||
return response
|
||||
|
||||
|
||||
@router.post("/habits/{habit_id}/reactivate")
|
||||
def reactivate_habit_page(request: Request, habit_id: int, db: Session = Depends(get_db)):
|
||||
current = _current_user_or_redirect(request, db)
|
||||
@@ -239,9 +265,10 @@ def reactivate_habit_page(request: Request, habit_id: int, db: Session = Depends
|
||||
habit = habit_service.get_habit(db, habit_id, current.id)
|
||||
if habit is None:
|
||||
return Response(status_code=404)
|
||||
tab = _tab_for_habit(habit)
|
||||
habit_service.reactivate_habit(db, habit)
|
||||
response = Response(status_code=200)
|
||||
response.headers["HX-Redirect"] = "/habits?tab=completed"
|
||||
response.headers["HX-Redirect"] = f"/habits?tab={tab}"
|
||||
return response
|
||||
|
||||
|
||||
@@ -254,7 +281,7 @@ def delete_habit_page(request: Request, habit_id: int, db: Session = Depends(get
|
||||
habit = habit_service.get_habit(db, habit_id, current.id)
|
||||
if habit is None:
|
||||
return Response(status_code=404)
|
||||
tab = "completed" if habit.status == HabitStatus.COMPLETED else habit.habit_type.value
|
||||
tab = _tab_for_habit(habit)
|
||||
habit_service.delete_habit(db, habit)
|
||||
response = Response(status_code=200)
|
||||
response.headers["HX-Redirect"] = f"/habits?tab={tab}"
|
||||
|
||||
Reference in New Issue
Block a user