|
| 1 | +"""Result of a permission check from the Allow2 service API.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from typing import Any, Dict, List, Optional |
| 7 | + |
| 8 | +from allow2_service.models.activity import Activity |
| 9 | +from allow2_service.models.day_type import DayType |
| 10 | + |
| 11 | + |
| 12 | +@dataclass(frozen=True) |
| 13 | +class CheckResult: |
| 14 | + """Result of a permission check from the Allow2 service API. |
| 15 | +
|
| 16 | + Contains the overall allowed status, per-activity breakdown, |
| 17 | + and current/upcoming day type information. |
| 18 | +
|
| 19 | + Attributes: |
| 20 | + allowed: Whether the child is globally allowed right now. |
| 21 | + activities: Per-activity permission breakdown. |
| 22 | + today_day_type: The current day type. |
| 23 | + tomorrow_day_type: The upcoming day type (if provided). |
| 24 | + raw: The raw API response for advanced use. |
| 25 | + """ |
| 26 | + |
| 27 | + allowed: bool |
| 28 | + activities: List[Activity] |
| 29 | + today_day_type: Optional[DayType] |
| 30 | + tomorrow_day_type: Optional[DayType] |
| 31 | + raw: Dict[str, Any] = field(default_factory=dict) |
| 32 | + |
| 33 | + def get_activity(self, activity_id: int) -> Optional[Activity]: |
| 34 | + """Get a specific activity by ID, or None if not present. |
| 35 | +
|
| 36 | + Args: |
| 37 | + activity_id: The activity ID to look up. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + The matching Activity, or None. |
| 41 | + """ |
| 42 | + for activity in self.activities: |
| 43 | + if activity.id == activity_id: |
| 44 | + return activity |
| 45 | + return None |
| 46 | + |
| 47 | + def is_activity_allowed(self, activity_id: int) -> bool: |
| 48 | + """Check whether a specific activity is allowed. |
| 49 | +
|
| 50 | + Args: |
| 51 | + activity_id: The activity ID to check. |
| 52 | +
|
| 53 | + Returns: |
| 54 | + True if the activity is present and allowed. |
| 55 | + """ |
| 56 | + activity = self.get_activity(activity_id) |
| 57 | + return activity is not None and activity.allowed |
| 58 | + |
| 59 | + def get_remaining_seconds(self, activity_id: int) -> int: |
| 60 | + """Get remaining seconds for a specific activity. |
| 61 | +
|
| 62 | + Args: |
| 63 | + activity_id: The activity ID to check. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Remaining seconds, or 0 if activity not found. |
| 67 | + """ |
| 68 | + activity = self.get_activity(activity_id) |
| 69 | + return activity.remaining if activity is not None else 0 |
| 70 | + |
| 71 | + @staticmethod |
| 72 | + def from_api_response(response: Dict[str, Any]) -> CheckResult: |
| 73 | + """Build from the raw API response. |
| 74 | +
|
| 75 | + Args: |
| 76 | + response: The decoded JSON response from /serviceapi/check. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + A CheckResult instance. |
| 80 | + """ |
| 81 | + activities: List[Activity] = [] |
| 82 | + raw_activities = response.get("activities", []) |
| 83 | + |
| 84 | + for act_data in raw_activities: |
| 85 | + activities.append(Activity.from_array(act_data)) |
| 86 | + |
| 87 | + today_day_type: Optional[DayType] = None |
| 88 | + if "dayType" in response: |
| 89 | + today_day_type = DayType.from_array(response["dayType"]) |
| 90 | + elif "today" in response: |
| 91 | + today_day_type = DayType.from_array(response["today"]) |
| 92 | + |
| 93 | + tomorrow_day_type: Optional[DayType] = None |
| 94 | + if "tomorrowDayType" in response: |
| 95 | + tomorrow_day_type = DayType.from_array(response["tomorrowDayType"]) |
| 96 | + elif "tomorrow" in response: |
| 97 | + tomorrow_day_type = DayType.from_array(response["tomorrow"]) |
| 98 | + |
| 99 | + # Global "allowed" is true only if ALL activities are allowed |
| 100 | + allowed = True |
| 101 | + for act in activities: |
| 102 | + if not act.allowed: |
| 103 | + allowed = False |
| 104 | + break |
| 105 | + |
| 106 | + # Override with explicit server value if present |
| 107 | + if "allowed" in response: |
| 108 | + allowed = bool(response["allowed"]) |
| 109 | + |
| 110 | + return CheckResult( |
| 111 | + allowed=allowed, |
| 112 | + activities=activities, |
| 113 | + today_day_type=today_day_type, |
| 114 | + tomorrow_day_type=tomorrow_day_type, |
| 115 | + raw=response, |
| 116 | + ) |
0 commit comments