Coverage for functions \ flipdare \ generated \ model \ issue \ restriction_model.py: 100%

0 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-05-08 12:22 +1000

1#!/usr/bin/env python 

2# 

3# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved. 

4# 

5# This file is part of Flipdare's proprietary software and contains 

6# confidential and copyrighted material. Unauthorised copying, 

7# modification, distribution, or use of this file is strictly 

8# prohibited without prior written permission from Flipdare Pty Ltd. 

9# 

10# This software includes third-party components licensed under MIT, 

11# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details. 

12# 

13# NOTE: THIS FILE IS AUTO GENERATED. DO NOT EDIT. 

14# 

15# Generated by codegen_models.py 

16# 

17# Modify 'codegen_models.py' 

18# and re-run the script above to update. 

19# 

20# pragma: no cover 

21from __future__ import annotations 

22from datetime import datetime 

23from google.cloud.firestore_v1.transforms import Sentinel 

24from flipdare.core.firestore_field import FirestoreField 

25from flipdare.util.time_util import FirestoreTime 

26from typing import Any, TypedDict, cast, Unpack 

27from enum import StrEnum 

28from pydantic import Field, ConfigDict, TypeAdapter 

29from flipdare.firestore.core.app_base_model import AppBaseModel 

30from flipdare.generated.shared.model.issue.issue_progress import IssueProgress 

31from flipdare.generated.shared.model.issue.flag_type import FlagType 

32from flipdare.generated.shared.model.restriction.restriction_category import RestrictionCategory 

33from flipdare.generated.shared.model.core.stopwatch_duration import StopwatchDuration 

34from flipdare.generated.shared.model.restriction.restriction_action import RestrictionAction 

35from flipdare.generated.shared.model.restriction.restriction_status import RestrictionStatus 

36from flipdare.generated.model.internal.stopwatch_model import StopwatchModel, StopwatchDict 

37 

38 

39class RestrictionKeys(StrEnum): 

40 ID = "id" 

41 CREATED_AT = "created_at" 

42 UPDATED_AT = "updated_at" 

43 SLUG_CODE = "slug_code" 

44 FLAG_ID = "flag_id" 

45 FLAG_PROGRESS = "flag_progress" 

46 FLAG_TYPE = "flag_type" 

47 UID = "uid" 

48 IN_DANGER = "in_danger" 

49 CATEGORY = "category" 

50 DURATION = "duration" 

51 ACTION = "action" 

52 REASON = "reason" 

53 STATUS = "status" 

54 STOPWATCH = "stopwatch" 

55 VERSION = "version" 

56 PROCESSED = "processed" 

57 ERROR_COUNT = "error_count" 

58 

59 

60# !! IMPORTANT !! 

61# !! 

62# !! this should only be used in the database to query. 

63# !! 

64class RestrictionInternalKeys(StrEnum): 

65 CREATED_AT = "created_at" 

66 UPDATED_AT = "updated_at" 

67 VERSION = "VERSION" 

68 PROCESSED = "INT_P" 

69 ERROR_COUNT = "INT_E" 

70 

71 

72class RestrictionModel(AppBaseModel): 

73 """Represents a restriction applied to a user""" 

74 

75 model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True) 

76 

77 id: str | None = Field(None, alias="id") 

78 created_at: FirestoreField = Field( 

79 default_factory=cast("Any", lambda: FirestoreTime.server_timestamp()) 

80 ) 

81 updated_at: FirestoreField = Field( 

82 default_factory=cast("Any", lambda: FirestoreTime.server_timestamp()) 

83 ) 

84 slug_code: str 

85 flag_id: str 

86 # If disputed, otherwise use status to determine the flag status. 

87 flag_progress: IssueProgress | None = None 

88 flag_type: FlagType 

89 uid: str 

90 in_danger: bool 

91 category: RestrictionCategory 

92 duration: StopwatchDuration 

93 action: RestrictionAction 

94 reason: str 

95 status: RestrictionStatus = Field(default=RestrictionStatus.PENDING) 

96 stopwatch: StopwatchModel | None = None 

97 # Version (base internal field) 

98 version: int = Field(default=1, alias="VERSION") 

99 # Processed (base internal field) 

100 processed: bool = Field(default=False, alias="INT_P") 

101 # Error Count (base internal field) 

102 error_count: int = Field(default=0, alias="INT_E") 

103 

104 @classmethod 

105 def validate_partial(cls, **data: Unpack[RestrictionDict]) -> dict[str, Any]: 

106 """ 

107 Uses Unpack to give you autocomplete and static warnings 

108 if you pass an invalid key or type in your code. 

109 

110 Returns a dict with Firestore field names (aliases) for use with batch.update(). 

111 """ 

112 result: dict[str, Any] = {} 

113 for k, v in data.items(): 

114 if k in cls.__pydantic_fields__: 

115 field_info = cls.__pydantic_fields__[k] 

116 validated_value = cast( 

117 "Any", 

118 TypeAdapter(field_info.annotation).validate_python(v), 

119 ) 

120 # Use alias if defined, otherwise use field name 

121 output_key = field_info.alias or k 

122 result[output_key] = validated_value 

123 return result 

124 

125 # ---- Convenience predicates ----------------------------------------- 

126 def start_stopwatch(self) -> None: 

127 stopwatch = StopwatchModel.from_now(self.duration) 

128 self.stopwatch = stopwatch 

129 

130 def equivalent_restriction(self, other: RestrictionModel) -> bool: 

131 return ( 

132 self.category == other.category 

133 and self.action == other.action 

134 and self.duration == other.duration 

135 ) 

136 

137 @property 

138 def user_label(self) -> str: 

139 return ( 

140 f"Restricted for {self.duration.label} - " 

141 f"{self.action.label} - {self.category.label}" 

142 ) 

143 

144 

145RESTRICTION_FIELD_NAMES: list[str] = list(RestrictionModel.model_fields.keys()) 

146 

147 

148class RestrictionDict(TypedDict, total=False): 

149 id: str | None 

150 created_at: Sentinel | datetime | str 

151 updated_at: Sentinel | datetime | str 

152 slug_code: str 

153 flag_id: str 

154 flag_progress: IssueProgress | None 

155 flag_type: FlagType 

156 uid: str 

157 in_danger: bool 

158 category: RestrictionCategory 

159 duration: StopwatchDuration 

160 action: RestrictionAction 

161 reason: str 

162 status: RestrictionStatus | None 

163 stopwatch: StopwatchDict | None 

164 VERSION: int | None 

165 INT_P: bool | None 

166 INT_E: int | None