Source code for tdw_catalog.metadata_template
from typing import List, Optional
from tdw_catalog import organization_member
from tdw_catalog.entity import Entity, Property, EntityBase
from tdw_catalog.errors import _convert_error
from datetime import datetime
from tdw_catalog import Catalog
import tdw_catalog.metadata_template.field as field
import tdw_catalog.metadata_template.update_builder as update_builder
import tdw_catalog.organization as organization
[docs]@Entity([
    Property("id", str, serialize=True),
    Property("organization_id",
             str,
             relation="tdw_catalog.organization.Organization",
             serialize=True),
    Property("created_by",
             str,
             relation="tdw_catalog.organization.OrganizationMember",
             relation_key="created_by_member",
             serialize=True),
    Property("title", str, writable=True),
    Property("description", Optional[str], writable=True),
    Property("fields",
             List[field.MetadataTemplateField],
             deserialize=field._deserialize_metadata_template_fields,
             writable=True),
    Property("created_at", datetime),
    Property("updated_at", datetime)
])
class MetadataTemplate(EntityBase):
    """
    :class:`.MetadataTemplate`\\ s encompass a standard set of :class:`.MetadataTemplateField`\\ s which can be applied to :class:`.Dataset`\\ s within the :class:`.Catalog`
    Attributes
    ----------
    id : str
        The unique ID of the :class:`.MetadataTemplate`
    organization : Organization
        The :class:`.Organization` to which this :class:`.MetadataTemplate` belongs
    organization_id :  str
        The unique ID of the :class:`.Organization` to which this :class:`.MetadataTemplate` belongs
    created_by : str
        The unique user ID of the :class:`.OrganizationMember` who created this :class:`.MetadataTemplate`
    created_by_member : OrganizationMember
        The :class:`.OrganizationMember` who created this :class:`.MetadataTemplate`
    title : str
        A title for this :class:`.MetadataTemplate`
    description : Optional[str]
        An optional description of this :class:`.MetadataTemplate`
    fields: List[MetadataTemplateField]
        This list of :class:`.MetadataTemplateField`\\ s constitutes the fields which would be attached to a :class:`.Dataset` if this :class:`.MetadataTemplate` was assigned to the :class:`.Dataset`
    created_at : datetime
        The datetime at which this :class:`.MetadataTemplate` was created
    updated_at :  datetime
        The datetime at which this :class:`.MetadataTemplate` was last updated
    """
    _client: 'Catalog'
    id: str
    organization: 'organization.Organization'
    organization_id: str
    created_by: str
    title: str
    description: Optional[str]
    fields: List['field.MetadataTemplateField']
    created_at: datetime
    updated_at: datetime
    def __str__(self) -> str:
        return f"<{self.__class__.__name__} id={self.id} title={self.title}>"
    def _get_organization(self):
        return organization.Organization.get(self._client,
                                             self.organization_id)
    def _get_created_by_member(self):
        return organization_member.OrganizationMember.get(
            self._client, self.organization_id, self.created_by)
[docs]    @classmethod
    def get(cls, client: 'Catalog', organization_id: str, id: str):
        """
        Retrieve a :class:`.MetadataTemplate` belonging to this :class:`.Organization`
        Parameters
        ----------
        client : Catalog
            The :class:`.Catalog`  client of the :class:`.Organization` containing the :class:`.MetadataTemplate`
        organization_id : str
            The unique ID of the :class:`.Organization`
        id : str
            The unique ID of the :class:`.MetadataTemplate`
        Returns
        -------
        MetadataTemplate
            The :class:`.MetadataTemplate` associated with the given ID
        Raises
        ------
        CatalogInternalException
            If call to the :class:`.Catalog` server fails
        CatalogNotFoundException
            If no MetadataTemplate is found matching the provided ID
        CatalogPermissionDeniedException
            If the caller is not allowed to retrieve this :class:`.MetadataTemplate`
        """
        try:
            res = client._get_field_template(organization_id=organization_id,
                                             id=id)
            return MetadataTemplate(client, **res)
        except Exception as e:
            raise _convert_error(e)
[docs]    def delete(self) -> None:
        """
        Delete this :class:`.MetadataTemplate` from the :class:`.Organization`. :class:`.Dataset`\\ s which employ this template will have their templated metadata shifted to custom metadata.
        Parameters
        ----------
        None
        Returns
        -------
        None
        Raises
        ------
        CatalogInternalException
            If call to the :class:`.Catalog` server fails
        CatalogNotFoundException
            If no MetadataTemplate is found matching the given ID
        CatalogPermissionDeniedException
            if the caller is not allowed to delete this :class:`.MetadataTemplate`
        """
        try:
            self._client._delete_field_template(template=self.serialize())
        except Exception as e:
            raise _convert_error(e)
[docs]    def save(self) -> 'MetadataTemplate':
        """
        Save and update this :class:`.MetadataTemplate` inside the :class:`.Organization`
        Parameters
        ----------
        None
        Returns
        -------
        MetadataTemplate
            The updated :class:`.MetadataTemplate` after it has been saved
        Raises
        ------
        CatalogInternalException
            If call to the :class:`.Catalog` server fails
        CatalogNotFoundException
            If no :class:`.MetadataTemplate` is found inside the :class:`.Organization` matching the ID of the updated :class:`.MetadataTemplate`
        CatalogPermissionDeniedException
            If the caller is not allowed to update this :class:`.MetadataTemplate`
        """
        try:
            res = self._client._update_field_template(
                template=self.serialize())
            return MetadataTemplate(client=self._client, **res)
        except Exception as e:
            raise _convert_error(e)
[docs]    def modify_fields(self) -> 'update_builder.MetadataTemplateUpdateBuilder':
        """
        Returns an object that can be used to manage and modify fields within this template. Changes to the template are saved to the :class:`.Catalog` when `.save()` is called on the returned object.
        Parameters
        ----------
        None
        Returns
        -------
        MetadataTemplateUpdateBuilder
            A MetadataTemplateUpdateBuilder which allows for the addition, removal, updating, and reordering of fields for the :class:`.MetadataTemplate`.
        """
        return update_builder.MetadataTemplateUpdateBuilder(
            client=self._client, template=self)