Passed
Push — main ( 810f99...fafd4d )
by Jochen
04:18
created

PresenceTimeSlot.from_()   A

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 13
nop 5
dl 0
loc 14
ccs 2
cts 3
cp 0.6667
crap 1.037
rs 9.75
c 0
b 0
f 0
1
"""
2
byceps.services.orga_presence.models
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2014-2023 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from __future__ import annotations
10 1
from dataclasses import dataclass
11 1
from datetime import datetime
12 1
from enum import Enum
13 1
from uuid import UUID
14
15 1
from ..party.models import Party
16 1
from ..user.models.user import User
17
18 1
from ...util.datetime.range import DateTimeRange
19
20
21 1
TimeSlotType = Enum('TimeSlotType', ['party', 'presence', 'task'])
22
23
24 1
@dataclass(frozen=True)
25 1
class TimeSlot:
26 1
    type: TimeSlotType
27 1
    starts_at: datetime
28 1
    ends_at: datetime
29
30 1
    @property
31 1
    def range(self) -> DateTimeRange:
32
        return DateTimeRange(self.starts_at, self.ends_at)
33
34
35 1
@dataclass(frozen=True)
36 1
class PartyTimeSlot(TimeSlot):
37 1
    party: Party
38
39 1
    @classmethod
40 1
    def from_party(cls, party: Party) -> PartyTimeSlot:
41
        return cls(
42
            type=TimeSlotType.party,
43
            starts_at=party.starts_at,
44
            ends_at=party.ends_at,
45
            party=party,
46
        )
47
48
49 1
@dataclass(frozen=True)
50 1
class PresenceTimeSlot(TimeSlot):
51 1
    id: UUID
52 1
    orga: User
53
54 1
    @classmethod
55 1
    def from_(
56
        cls,
57
        time_slot_id: UUID,
58
        orga: User,
59
        starts_at: datetime,
60
        ends_at: datetime,
61
    ) -> PresenceTimeSlot:
62
        return cls(
63
            id=time_slot_id,
64
            type=TimeSlotType.presence,
65
            starts_at=starts_at,
66
            ends_at=ends_at,
67
            orga=orga,
68
        )
69
70
71 1
@dataclass(frozen=True)
72 1
class TaskTimeSlot(TimeSlot):
73 1
    id: UUID
74 1
    title: str
75
76 1
    @classmethod
77 1
    def from_(
78
        cls,
79
        time_slot_id: UUID,
80
        title: str,
81
        starts_at: datetime,
82
        ends_at: datetime,
83
    ) -> TaskTimeSlot:
84
        return cls(
85
            id=time_slot_id,
86
            type=TimeSlotType.task,
87
            starts_at=starts_at,
88
            ends_at=ends_at,
89
            title=title,
90
        )
91