Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api_schemas/adventure_mission_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class AdventureMissionCreate(BaseSchema):
max_points: int
min_points: int
nollning_week: int
unlock_code: str | None = None
unlock_hint_sv: str | None = None
unlock_hint_en: str | None = None


class AdventureMissionRead(BaseSchema):
Expand All @@ -22,4 +25,7 @@ class AdventureMissionRead(BaseSchema):
min_points: int
nollning_id: int
nollning_week: int
unlock_code: str | None = None
unlock_hint_sv: str | None = None
unlock_hint_en: str | None = None
created_at: datetime
23 changes: 18 additions & 5 deletions db_models/adventure_mission_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

from sqlalchemy import ForeignKey, String
from db_models.base_model import BaseModel_DB
from typing import TYPE_CHECKING

from helpers.constants import MAX_ADVENTURE_MISSION_DESC, MAX_ADVENTURE_MISSION_NAME
from typing import TYPE_CHECKING, Optional

from helpers.constants import (
MAX_ADVENTURE_MISSION_DESC,
MAX_ADVENTURE_MISSION_NAME,
MAX_ADVENTURE_MISSION_UNLOCK_CODE,
MAX_ADVENTURE_MISSION_UNLOCK_HINT,
)
from .base_model import BaseModel_DB
from sqlalchemy.orm import relationship, Mapped, mapped_column
from .group_mission_model import GroupMission_DB
Expand Down Expand Up @@ -36,8 +41,16 @@ class AdventureMission_DB(BaseModel_DB):

min_points: Mapped[int] = mapped_column()

group_missions: Mapped[list["GroupMission_DB"]] = relationship(
back_populates="adventure_mission", cascade="all, delete-orphan", init=False
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
)
)

# Not secret, passed along with all fetches which can be made by basically anyone.
unlock_code: Mapped[Optional[str]] = mapped_column(String(MAX_ADVENTURE_MISSION_UNLOCK_CODE), default=None)

unlock_hint_sv: Mapped[Optional[str]] = mapped_column(String(MAX_ADVENTURE_MISSION_UNLOCK_HINT), default=None)
unlock_hint_en: Mapped[Optional[str]] = mapped_column(String(MAX_ADVENTURE_MISSION_UNLOCK_HINT), default=None)

created_at: Mapped[datetime_utc] = created_at_column()
2 changes: 2 additions & 0 deletions helpers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
# Adventure mission
MAX_ADVENTURE_MISSION_NAME = 200
MAX_ADVENTURE_MISSION_DESC = 2000
MAX_ADVENTURE_MISSION_UNLOCK_CODE = 200
MAX_ADVENTURE_MISSION_UNLOCK_HINT = 200

# Nollning
MAX_NOLLNING_NAME = 200
Expand Down
20 changes: 19 additions & 1 deletion services/adventure_mission_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def create_adventure_mission_(db: Session, data: AdventureMissionCreate, nollnin
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 == "":
data.unlock_hint_sv = None
if data.unlock_hint_en == "":
data.unlock_hint_en = None

new_adventure_mission = AdventureMission_DB(
nollning_id=nollning_id,
nollning_week=data.nollning_week,
Expand All @@ -37,6 +44,9 @@ def create_adventure_mission_(db: Session, data: AdventureMissionCreate, nollnin
description_en=data.description_en,
max_points=data.max_points,
min_points=data.min_points,
unlock_code=data.unlock_code,
unlock_hint_sv=data.unlock_hint_sv,
unlock_hint_en=data.unlock_hint_en,
)

db.add(new_adventure_mission)
Expand Down Expand Up @@ -82,8 +92,16 @@ def edit_adventure_mission_(db: Session, id: int, data: AdventureMissionCreate):
if not adventure_mission:
raise HTTPException(404, detail="Mission not found")

if data.unlock_code == "": # Easy guard against accidentally setting unlock_code to empty string
data.unlock_code = None
if data.unlock_hint_sv == "":
data.unlock_hint_sv = None
if data.unlock_hint_en == "":
data.unlock_hint_en = None

for var, value in vars(data).items():
setattr(adventure_mission, var, value) if value is not None else None
# Allow for clearing of code by allowing setting attributes to None
setattr(adventure_mission, var, value)

db.commit()
db.refresh(adventure_mission)
Expand Down
Loading