aboutsummaryrefslogtreecommitdiff
path: root/generalresearch/models/thl/contest/io.py
blob: d11080f031690ea86ff3f326cef94f26a5b75703 (plain)
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
from __future__ import annotations

from datetime import UTC, datetime
from uuid import uuid4

from generalresearch.models.thl.contest.definitions import ContestType
from generalresearch.models.thl.contest.leaderboard import (
    LeaderboardContest,
    LeaderboardContestCreate,
    LeaderboardContestUserView,
)
from generalresearch.models.thl.contest.milestone import (
    MilestoneContest,
    MilestoneContestCreate,
    MilestoneUserView,
)
from generalresearch.models.thl.contest.raffle import (
    RaffleContest,
    RaffleContestCreate,
    RaffleUserView,
)

model_cls = {
    ContestType.RAFFLE: RaffleContest,
    ContestType.MILESTONE: MilestoneContest,
    ContestType.LEADERBOARD: LeaderboardContest,
}
user_model_cls = {
    ContestType.RAFFLE: RaffleUserView,
    ContestType.MILESTONE: MilestoneUserView,
    ContestType.LEADERBOARD: LeaderboardContestUserView,
}
ContestCreate = RaffleContestCreate | LeaderboardContestCreate | MilestoneContestCreate
from generalresearch.models.thl.contest.contest import Contest


def contest_create_to_contest(
    product_id: str, contest_create: ContestCreate
) -> Contest:
    now = datetime.now(tz=UTC)
    d = contest_create.model_dump(mode="json")
    d["uuid"] = uuid4().hex
    d["product_id"] = product_id
    d["created_at"] = now
    d["updated_at"] = now
    return model_cls[contest_create.contest_type].model_validate(d)