Coverage for functions \ flipdare \ generated \ model \ internal \ image_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.model.internal.stored_file_model import StoredFileModel, StoredFileDict 

31from flipdare.util.scaled_image import ScaledImage, ScaledImageType 

32 

33 

34class ImageKeys(StrEnum): 

35 UPDATED_AT = "updated_at" 

36 CREATED_AT = "created_at" 

37 SOURCE = "source" 

38 BLUR_HASH = "blur_hash" 

39 W = "w" 

40 H = "h" 

41 

42 

43# !! IMPORTANT !! 

44# !! 

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

46# !! 

47class ImageInternalKeys(StrEnum): 

48 UPDATED_AT = "updated_at" 

49 CREATED_AT = "created_at" 

50 

51 

52class ImageModel(AppBaseModel): 

53 """Represent an image with dimensions and a source file.""" 

54 

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

56 

57 updated_at: FirestoreField = Field( 

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

59 ) 

60 created_at: FirestoreField = Field( 

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

62 ) 

63 source: StoredFileModel 

64 blur_hash: str | None = None 

65 w: int 

66 h: int 

67 

68 @classmethod 

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

70 """ 

71 Uses Unpack to give you autocomplete and static warnings 

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

73 

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

75 """ 

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

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

78 if k in cls.__pydantic_fields__: 

79 field_info = cls.__pydantic_fields__[k] 

80 validated_value = cast( 

81 "Any", 

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

83 ) 

84 # Use alias if defined, otherwise use field name 

85 output_key = field_info.alias or k 

86 result[output_key] = validated_value 

87 return result 

88 

89 # ---- Convenience predicates ----------------------------------------- 

90 # stored internally, therefore no wrapper .. 

91 

92 @property 

93 def hash_(self) -> str | None: 

94 # NOTE: need to be careful with 'hash' since it's a built-in function name 

95 if self.hash is not None: 

96 return self.hash 

97 else: 

98 return None 

99 

100 @hash_.setter 

101 def hash_(self, value: str | None) -> None: 

102 # NOTE: need to be careful with 'hash' since it's a built-in function name 

103 self.hash = value 

104 

105 @property 

106 def aspect_ratio(self) -> float: 

107 if self.h == 0: 

108 return 0.0 

109 

110 # round to two decimal places 

111 return round(self.w / self.h, 2) 

112 

113 def scaled(self, scale_type: ScaledImageType) -> tuple[int, int]: 

114 return ScaledImage(self).scale(scale_type) 

115 

116 

117IMAGE_FIELD_NAMES: list[str] = list(ImageModel.model_fields.keys()) 

118 

119 

120class ImageDict(TypedDict, total=False): 

121 updated_at: Sentinel | datetime | str 

122 created_at: Sentinel | datetime | str 

123 source: StoredFileDict 

124 blur_hash: str | None 

125 w: int 

126 h: int