|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +"""Typed return model for relationship metadata.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Any, Dict, Optional |
| 10 | + |
| 11 | +__all__ = ["RelationshipInfo"] |
| 12 | + |
| 13 | +_OTM_ODATA_TYPE = "Microsoft.Dynamics.CRM.OneToManyRelationshipMetadata" |
| 14 | +_MTM_ODATA_TYPE = "Microsoft.Dynamics.CRM.ManyToManyRelationshipMetadata" |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class RelationshipInfo: |
| 19 | + """Typed return model for relationship metadata. |
| 20 | +
|
| 21 | + Returned by :meth:`~PowerPlatform.Dataverse.operations.tables.TableOperations.create_one_to_many_relationship`, |
| 22 | + :meth:`~PowerPlatform.Dataverse.operations.tables.TableOperations.create_many_to_many_relationship`, |
| 23 | + :meth:`~PowerPlatform.Dataverse.operations.tables.TableOperations.get_relationship`, and |
| 24 | + :meth:`~PowerPlatform.Dataverse.operations.tables.TableOperations.create_lookup_field`. |
| 25 | +
|
| 26 | + :param relationship_id: Relationship metadata GUID. |
| 27 | + :type relationship_id: :class:`str` or None |
| 28 | + :param relationship_schema_name: Relationship schema name. |
| 29 | + :type relationship_schema_name: :class:`str` |
| 30 | + :param relationship_type: Either ``"one_to_many"`` or ``"many_to_many"``. |
| 31 | + :type relationship_type: :class:`str` |
| 32 | + :param lookup_schema_name: Lookup field schema name (one-to-many only). |
| 33 | + :type lookup_schema_name: :class:`str` or None |
| 34 | + :param referenced_entity: Parent entity logical name (one-to-many only). |
| 35 | + :type referenced_entity: :class:`str` or None |
| 36 | + :param referencing_entity: Child entity logical name (one-to-many only). |
| 37 | + :type referencing_entity: :class:`str` or None |
| 38 | + :param entity1_logical_name: First entity logical name (many-to-many only). |
| 39 | + :type entity1_logical_name: :class:`str` or None |
| 40 | + :param entity2_logical_name: Second entity logical name (many-to-many only). |
| 41 | + :type entity2_logical_name: :class:`str` or None |
| 42 | +
|
| 43 | + Example:: |
| 44 | +
|
| 45 | + result = client.tables.create_one_to_many_relationship(lookup, relationship) |
| 46 | + print(result.relationship_schema_name) |
| 47 | + print(result.lookup_schema_name) |
| 48 | + """ |
| 49 | + |
| 50 | + relationship_id: Optional[str] = None |
| 51 | + relationship_schema_name: str = "" |
| 52 | + relationship_type: str = "" |
| 53 | + |
| 54 | + # One-to-many specific |
| 55 | + lookup_schema_name: Optional[str] = None |
| 56 | + referenced_entity: Optional[str] = None |
| 57 | + referencing_entity: Optional[str] = None |
| 58 | + |
| 59 | + # Many-to-many specific |
| 60 | + entity1_logical_name: Optional[str] = None |
| 61 | + entity2_logical_name: Optional[str] = None |
| 62 | + |
| 63 | + @classmethod |
| 64 | + def from_one_to_many( |
| 65 | + cls, |
| 66 | + *, |
| 67 | + relationship_id: Optional[str], |
| 68 | + relationship_schema_name: str, |
| 69 | + lookup_schema_name: str, |
| 70 | + referenced_entity: str, |
| 71 | + referencing_entity: str, |
| 72 | + ) -> RelationshipInfo: |
| 73 | + """Create from a one-to-many relationship result. |
| 74 | +
|
| 75 | + :param relationship_id: Relationship metadata GUID. |
| 76 | + :type relationship_id: :class:`str` or None |
| 77 | + :param relationship_schema_name: Relationship schema name. |
| 78 | + :type relationship_schema_name: :class:`str` |
| 79 | + :param lookup_schema_name: Lookup field schema name. |
| 80 | + :type lookup_schema_name: :class:`str` |
| 81 | + :param referenced_entity: Parent entity logical name. |
| 82 | + :type referenced_entity: :class:`str` |
| 83 | + :param referencing_entity: Child entity logical name. |
| 84 | + :type referencing_entity: :class:`str` |
| 85 | + :rtype: :class:`RelationshipInfo` |
| 86 | + """ |
| 87 | + return cls( |
| 88 | + relationship_id=relationship_id, |
| 89 | + relationship_schema_name=relationship_schema_name, |
| 90 | + relationship_type="one_to_many", |
| 91 | + lookup_schema_name=lookup_schema_name, |
| 92 | + referenced_entity=referenced_entity, |
| 93 | + referencing_entity=referencing_entity, |
| 94 | + ) |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def from_many_to_many( |
| 98 | + cls, |
| 99 | + *, |
| 100 | + relationship_id: Optional[str], |
| 101 | + relationship_schema_name: str, |
| 102 | + entity1_logical_name: str, |
| 103 | + entity2_logical_name: str, |
| 104 | + ) -> RelationshipInfo: |
| 105 | + """Create from a many-to-many relationship result. |
| 106 | +
|
| 107 | + :param relationship_id: Relationship metadata GUID. |
| 108 | + :type relationship_id: :class:`str` or None |
| 109 | + :param relationship_schema_name: Relationship schema name. |
| 110 | + :type relationship_schema_name: :class:`str` |
| 111 | + :param entity1_logical_name: First entity logical name. |
| 112 | + :type entity1_logical_name: :class:`str` |
| 113 | + :param entity2_logical_name: Second entity logical name. |
| 114 | + :type entity2_logical_name: :class:`str` |
| 115 | + :rtype: :class:`RelationshipInfo` |
| 116 | + """ |
| 117 | + return cls( |
| 118 | + relationship_id=relationship_id, |
| 119 | + relationship_schema_name=relationship_schema_name, |
| 120 | + relationship_type="many_to_many", |
| 121 | + entity1_logical_name=entity1_logical_name, |
| 122 | + entity2_logical_name=entity2_logical_name, |
| 123 | + ) |
| 124 | + |
| 125 | + @classmethod |
| 126 | + def from_api_response(cls, response_data: Dict[str, Any]) -> RelationshipInfo: |
| 127 | + """Create from a raw Dataverse Web API response. |
| 128 | +
|
| 129 | + Detects one-to-many vs many-to-many from the ``@odata.type`` field |
| 130 | + in the response and maps PascalCase keys to snake_case attributes. |
| 131 | +
|
| 132 | + :param response_data: Raw relationship metadata from the Web API. |
| 133 | + :type response_data: :class:`dict` |
| 134 | + :rtype: :class:`RelationshipInfo` |
| 135 | + """ |
| 136 | + odata_type = response_data.get("@odata.type", "") |
| 137 | + rel_id = response_data.get("MetadataId") |
| 138 | + schema_name = response_data.get("SchemaName", "") |
| 139 | + |
| 140 | + if _OTM_ODATA_TYPE in odata_type: |
| 141 | + return cls( |
| 142 | + relationship_id=rel_id, |
| 143 | + relationship_schema_name=schema_name, |
| 144 | + relationship_type="one_to_many", |
| 145 | + referenced_entity=response_data.get("ReferencedEntity"), |
| 146 | + referencing_entity=response_data.get("ReferencingEntity"), |
| 147 | + lookup_schema_name=response_data.get("ReferencingEntityNavigationPropertyName"), |
| 148 | + ) |
| 149 | + |
| 150 | + if _MTM_ODATA_TYPE in odata_type: |
| 151 | + return cls( |
| 152 | + relationship_id=rel_id, |
| 153 | + relationship_schema_name=schema_name, |
| 154 | + relationship_type="many_to_many", |
| 155 | + entity1_logical_name=response_data.get("Entity1LogicalName"), |
| 156 | + entity2_logical_name=response_data.get("Entity2LogicalName"), |
| 157 | + ) |
| 158 | + |
| 159 | + # Fallback: unknown type, populate what we can |
| 160 | + return cls( |
| 161 | + relationship_id=rel_id, |
| 162 | + relationship_schema_name=schema_name, |
| 163 | + ) |
0 commit comments