|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +"""Label models for Dataverse metadata.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from typing import Any, Dict, List, Optional |
| 9 | +from dataclasses import dataclass |
| 10 | + |
| 11 | +from ..common.constants import ( |
| 12 | + ODATA_TYPE_LOCALIZED_LABEL, |
| 13 | + ODATA_TYPE_LABEL, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class LocalizedLabel: |
| 19 | + """ |
| 20 | + Represents a localized label with a language code. |
| 21 | +
|
| 22 | + :param label: The text of the label. |
| 23 | + :type label: str |
| 24 | + :param language_code: The language code (LCID), e.g., 1033 for English. |
| 25 | + :type language_code: int |
| 26 | + :param additional_properties: Optional dict of additional properties to include |
| 27 | + in the Web API payload. These are merged last and can override default values. |
| 28 | + :type additional_properties: Optional[Dict[str, Any]] |
| 29 | + """ |
| 30 | + |
| 31 | + label: str |
| 32 | + language_code: int |
| 33 | + additional_properties: Optional[Dict[str, Any]] = None |
| 34 | + |
| 35 | + def to_dict(self) -> Dict[str, Any]: |
| 36 | + """ |
| 37 | + Convert to Web API JSON format. |
| 38 | +
|
| 39 | + Example:: |
| 40 | +
|
| 41 | + >>> label = LocalizedLabel(label="Account", language_code=1033) |
| 42 | + >>> label.to_dict() |
| 43 | + { |
| 44 | + '@odata.type': 'Microsoft.Dynamics.CRM.LocalizedLabel', |
| 45 | + 'Label': 'Account', |
| 46 | + 'LanguageCode': 1033 |
| 47 | + } |
| 48 | + """ |
| 49 | + result = { |
| 50 | + "@odata.type": ODATA_TYPE_LOCALIZED_LABEL, |
| 51 | + "Label": self.label, |
| 52 | + "LanguageCode": self.language_code, |
| 53 | + } |
| 54 | + if self.additional_properties: |
| 55 | + result.update(self.additional_properties) |
| 56 | + return result |
| 57 | + |
| 58 | + |
| 59 | +@dataclass |
| 60 | +class Label: |
| 61 | + """ |
| 62 | + Represents a label that can have multiple localized versions. |
| 63 | +
|
| 64 | + :param localized_labels: List of LocalizedLabel instances. |
| 65 | + :type localized_labels: List[LocalizedLabel] |
| 66 | + :param user_localized_label: Optional user-specific localized label. |
| 67 | + :type user_localized_label: Optional[LocalizedLabel] |
| 68 | + :param additional_properties: Optional dict of additional properties to include |
| 69 | + in the Web API payload. These are merged last and can override default values. |
| 70 | + :type additional_properties: Optional[Dict[str, Any]] |
| 71 | + """ |
| 72 | + |
| 73 | + localized_labels: List[LocalizedLabel] |
| 74 | + user_localized_label: Optional[LocalizedLabel] = None |
| 75 | + additional_properties: Optional[Dict[str, Any]] = None |
| 76 | + |
| 77 | + def to_dict(self) -> Dict[str, Any]: |
| 78 | + """ |
| 79 | + Convert to Web API JSON format. |
| 80 | +
|
| 81 | + Example:: |
| 82 | +
|
| 83 | + >>> label = Label(localized_labels=[LocalizedLabel("Account", 1033)]) |
| 84 | + >>> label.to_dict() |
| 85 | + { |
| 86 | + '@odata.type': 'Microsoft.Dynamics.CRM.Label', |
| 87 | + 'LocalizedLabels': [ |
| 88 | + {'@odata.type': '...', 'Label': 'Account', 'LanguageCode': 1033} |
| 89 | + ], |
| 90 | + 'UserLocalizedLabel': {'@odata.type': '...', 'Label': 'Account', ...} |
| 91 | + } |
| 92 | + """ |
| 93 | + result = { |
| 94 | + "@odata.type": ODATA_TYPE_LABEL, |
| 95 | + "LocalizedLabels": [ll.to_dict() for ll in self.localized_labels], |
| 96 | + } |
| 97 | + # Use explicit user_localized_label, or default to first localized label |
| 98 | + if self.user_localized_label: |
| 99 | + result["UserLocalizedLabel"] = self.user_localized_label.to_dict() |
| 100 | + elif self.localized_labels: |
| 101 | + result["UserLocalizedLabel"] = self.localized_labels[0].to_dict() |
| 102 | + if self.additional_properties: |
| 103 | + result.update(self.additional_properties) |
| 104 | + return result |
| 105 | + |
| 106 | + |
| 107 | +__all__ = ["LocalizedLabel", "Label"] |
0 commit comments