Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use type mapping objects #1135

Merged
merged 11 commits into from
Dec 9, 2024
4 changes: 2 additions & 2 deletions home/forms/search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from copy import deepcopy
from urllib.parse import urlencode

from data_platform_catalogue.entities import FindMoJDataEntityType
murdo-moj marked this conversation as resolved.
Show resolved Hide resolved
from data_platform_catalogue.search_types import DomainOption
from data_platform_catalogue.entities import EntityTypes
from django import forms
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -38,7 +38,7 @@ def get_entity_types():
return sorted(
[
(entity.name, entity.value)
for entity in EntityTypes
for entity in FindMoJDataEntityType
if entity.name != "GLOSSARY_TERM"
]
)
Expand Down
15 changes: 9 additions & 6 deletions home/service/details.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
from urllib.parse import urlsplit

from data_platform_catalogue.entities import EntityRef, EntityTypes, RelationshipType
from data_platform_catalogue.entities import (
EntityRef, RelationshipType, DatabaseEntityMapper,
DashboardEntityMapper, PublicationCollectionEntityMapper, PublicationDatasetEntityMapper
)
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.validators import URLValidator
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -114,7 +117,7 @@ def _get_context(self):
"entity": self.table_metadata,
"entity_type": "Table",
"parent_entity": self.parent_entity,
"parent_type": EntityTypes.DATABASE.name.lower(),
"parent_type": DatabaseEntityMapper.datahub_type.lower(),
"h1_value": self.table_metadata.name,
"has_lineage": self.has_lineage(),
"lineage_url": f"{split_datahub_url.scheme}://{split_datahub_url.netloc}/dataset/{self.table_metadata.urn}/Lineage?is_lineage_mode=true&", # noqa: E501
Expand Down Expand Up @@ -161,7 +164,7 @@ def _get_context(self):
self.chart_metadata.platform.display_name
),
"parent_entity": self.parent_entity,
"parent_type": EntityTypes.DASHBOARD.name.lower(),
"parent_type": DashboardEntityMapper.datahub_type.lower(),
"h1_value": self.chart_metadata.name,
"is_access_requirements_a_url": is_access_requirements_a_url(
self.chart_metadata.custom_properties.access_information.dc_access_requirements
Expand Down Expand Up @@ -219,7 +222,7 @@ def __init__(self, urn: str):
def _get_context(self):
context = {
"entity": self.publication_collection_metadata,
"entity_type": EntityTypes.PUBLICATION_COLLECTION.value,
"entity_type": PublicationCollectionEntityMapper.find_moj_data_type,
"platform_name": friendly_platform_name(
self.publication_collection_metadata.platform.display_name
),
Expand Down Expand Up @@ -258,12 +261,12 @@ def _get_context(self):

return {
"entity": self.publication_dataset_metadata,
"entity_type": EntityTypes.PUBLICATION_DATASET.value,
"entity_type": PublicationDatasetEntityMapper.find_moj_data_type,
"parent_entity": self.parent_entity,
"platform_name": friendly_platform_name(
self.publication_dataset_metadata.platform.display_name
),
"parent_type": EntityTypes.PUBLICATION_COLLECTION.name.lower(),
"parent_type": DatabaseEntityMapper.datahub_type.lower(),
murdo-moj marked this conversation as resolved.
Show resolved Hide resolved
"h1_value": self.publication_dataset_metadata.name,
# noqa: E501
"is_access_requirements_a_url": is_access_requirements_a_url(
Expand Down
8 changes: 4 additions & 4 deletions home/service/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from copy import deepcopy
from typing import Any

from data_platform_catalogue.entities import EntityTypes
from data_platform_catalogue.entities import FindMoJDataEntityType
from data_platform_catalogue.search_types import (
DomainOption,
MultiSelectFilter,
Expand Down Expand Up @@ -45,15 +45,15 @@ def _build_custom_property_filter(
) -> list[str]:
return [f"{filter_param}{filter_value}" for filter_value in filter_value_list]

def _build_entity_types(self, entity_types: list[str]) -> tuple[EntityTypes, ...]:
def _build_entity_types(self, entity_types: list[str]) -> tuple[FindMoJDataEntityType, ...]:
default_entities = tuple(
entity
for entity in EntityTypes
for entity in FindMoJDataEntityType
if entity.name != "GLOSSARY_TERM"
)
chosen_entities = (
tuple(
EntityTypes[entity]
FindMoJDataEntityType[entity]
for entity in entity_types
)
if entity_types
Expand Down
27 changes: 17 additions & 10 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
from urllib.parse import urlparse

from data_platform_catalogue.client.exceptions import EntityDoesNotExist
from data_platform_catalogue.entities import (
ChartEntityMapper,
DashboardEntityMapper,
DatabaseEntityMapper,
PublicationCollectionEntityMapper,
PublicationDatasetEntityMapper,
TableEntityMapper,
)
from data_platform_catalogue.search_types import DomainOption
from django.conf import settings
from django.http import Http404, HttpResponse, HttpResponseBadRequest
from django.shortcuts import render
from django.utils.translation import gettext as _
from django.views.decorators.cache import cache_control

from data_platform_catalogue.entities import EntityTypes
from home.forms.search import SearchForm
from home.service.details import (
ChartDetailsService,
Expand All @@ -31,12 +38,12 @@
from home.service.search import SearchService

type_details_map = {
EntityTypes.TABLE.url_formatted: DatasetDetailsService,
EntityTypes.DATABASE.url_formatted: DatabaseDetailsService,
EntityTypes.CHART.url_formatted: ChartDetailsService,
EntityTypes.DASHBOARD.url_formatted: DashboardDetailsService,
EntityTypes.PUBLICATION_COLLECTION.url_formatted: PublicationCollectionDetailsService,
EntityTypes.PUBLICATION_DATASET.url_formatted: PublicationDatasetDetailsService
TableEntityMapper.url_formatted: DatasetDetailsService,
DatabaseEntityMapper.url_formatted: DatabaseDetailsService,
ChartEntityMapper.url_formatted: ChartDetailsService,
DashboardEntityMapper.url_formatted: DashboardDetailsService,
PublicationCollectionEntityMapper.url_formatted: PublicationCollectionDetailsService,
PublicationDatasetEntityMapper.url_formatted: PublicationDatasetDetailsService
}


Expand Down Expand Up @@ -66,11 +73,11 @@ def details_view(request, result_type, urn):
@cache_control(max_age=300, private=True)
def details_view_csv(request, result_type, urn) -> HttpResponse:
match result_type:
case EntityTypes.TABLE.url_formatted:
case TableEntityMapper.url_formatted:
csv_formatter = DatasetDetailsCsvFormatter(DatasetDetailsService(urn))
case EntityTypes.DATABASE.url_formatted:
case DatabaseEntityMapper.url_formatted:
csv_formatter = DatabaseDetailsCsvFormatter(DatabaseDetailsService(urn))
case EntityTypes.DASHBOARD.url_formatted:
case DashboardEntityMapper.url_formatted:
csv_formatter = DashboardDetailsCsvFormatter(DashboardDetailsService(urn))
case _:
logging.error("Invalid result type for csv details view %s", result_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
Database,
EntityRef,
EntitySummary,
EntityTypes,
FindMoJDataEntityMapper,
TableEntityMapper,
ChartEntityMapper,
DatabaseEntityMapper,
Governance,
PublicationCollection,
PublicationDataset,
Expand Down Expand Up @@ -203,10 +206,10 @@ def search(
query: str = "*",
count: int = 20,
page: str | None = None,
result_types: Sequence[EntityTypes] = (
EntityTypes.TABLE,
EntityTypes.CHART,
EntityTypes.DATABASE,
result_types: Sequence[FindMoJDataEntityMapper] = (
TableEntityMapper,
ChartEntityMapper,
DatabaseEntityMapper,
),
filters: Sequence[MultiSelectFilter] | None = None,
sort: SortOption | None = None,
Expand Down Expand Up @@ -395,7 +398,7 @@ def get_publication_collection_details(self, urn: str) -> PublicationCollection:
child_relations = parse_relations(
relationship_type=RelationshipType.CHILD,
relations_list=[response["relationships"]],
entity_type_of_relations=EntityTypes.PUBLICATION_DATASET.url_formatted,
entity_type_of_relations=PublicationDatasetEntityMapper.url_formatted,
)
relations_to_display = self.list_relations_to_display(child_relations)

Expand Down
Loading