diff --git a/api_schemas/adventure_mission_schema.py b/api_schemas/adventure_mission_schema.py index b4fb62f0..cb06a6d4 100644 --- a/api_schemas/adventure_mission_schema.py +++ b/api_schemas/adventure_mission_schema.py @@ -1,5 +1,6 @@ from datetime import datetime from api_schemas.base_schema import BaseSchema +from helpers.types import MISSION_CATEGORIES class AdventureMissionCreate(BaseSchema): @@ -10,6 +11,7 @@ class AdventureMissionCreate(BaseSchema): max_points: int min_points: int nollning_week: int + mission_category: MISSION_CATEGORIES | None = None unlock_code: str | None = None unlock_hint_sv: str | None = None unlock_hint_en: str | None = None @@ -25,6 +27,7 @@ class AdventureMissionRead(BaseSchema): min_points: int nollning_id: int nollning_week: int + mission_category: str unlock_code: str | None = None unlock_hint_sv: str | None = None unlock_hint_en: str | None = None diff --git a/db_models/adventure_mission_model.py b/db_models/adventure_mission_model.py index 0588d8a8..24509a53 100644 --- a/db_models/adventure_mission_model.py +++ b/db_models/adventure_mission_model.py @@ -14,6 +14,7 @@ from .base_model import BaseModel_DB from sqlalchemy.orm import relationship, Mapped, mapped_column from .group_mission_model import GroupMission_DB +from helpers.types import MISSION_CATEGORIES if TYPE_CHECKING: from .nollning_model import Nollning_DB @@ -41,6 +42,8 @@ class AdventureMission_DB(BaseModel_DB): min_points: Mapped[int] = mapped_column() + mission_category: Mapped[Optional[MISSION_CATEGORIES]] = mapped_column(default="Spel") + group_missions: Mapped[list["GroupMission_DB"]] = ( relationship( # many-many relationship with groups requires this, so that groups can track which missions they have completed. back_populates="adventure_mission", cascade="all, delete-orphan", init=False diff --git a/helpers/types.py b/helpers/types.py index 23aedf38..640a1631 100644 --- a/helpers/types.py +++ b/helpers/types.py @@ -100,6 +100,8 @@ def force_utc(date: datetime): MISSION_CONFIRMED_TYPES = Literal["Accepted", "Failed", "Review"] +MISSION_CATEGORIES = Literal["Barbiedans", "Fadder", "Kreativ", "Resa", "Spel", "Tävling", "Älg"] + ASSETS_BASE_PATH = os.getenv("ASSETS_BASE_PATH") diff --git a/services/adventure_mission_service.py b/services/adventure_mission_service.py index be7ef76a..b2849985 100644 --- a/services/adventure_mission_service.py +++ b/services/adventure_mission_service.py @@ -58,6 +58,7 @@ def create_adventure_mission_(db: Session, data: AdventureMissionCreate, nollnin description_en=data.description_en, max_points=data.max_points, min_points=data.min_points, + mission_category=data.mission_category if data.mission_category is not None else "Spel", unlock_code=data.unlock_code, unlock_hint_sv=data.unlock_hint_sv, unlock_hint_en=data.unlock_hint_en, @@ -121,6 +122,15 @@ def edit_adventure_mission_(db: Session, id: int, data: AdventureMissionCreate): if not adventure_mission: raise HTTPException(404, detail="Mission not found") + if data.max_points < data.min_points: + raise HTTPException(400, detail="Max points cannot be lower than min points") + + if data.max_points < 1: + raise HTTPException(400, detail="Max points has to be atleast 1") + + if data.min_points < 0: + raise HTTPException(400, detail="Min points has to be atleast 0") + if data.unlock_code == "": # Easy guard against accidentally setting unlock_code to empty string data.unlock_code = None if data.unlock_hint_sv == "": @@ -128,6 +138,9 @@ def edit_adventure_mission_(db: Session, id: int, data: AdventureMissionCreate): if data.unlock_hint_en == "": data.unlock_hint_en = None + if data.mission_category is None: + data.mission_category = "Spel" + for var, value in vars(data).items(): # Allow for clearing of unlock_code by allowing setting attributes to None setattr(adventure_mission, var, value)