Skip to content

Commit

Permalink
Add a built-in method to Metadata model to get the edit URL (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Aug 27, 2019
1 parent 6e06d49 commit 78c01f8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 86 deletions.
6 changes: 4 additions & 2 deletions isogeo_pysdk/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,13 @@ def check_edit_tab(self, tab: str, md_type: str):
"""
# check parameters types
if not isinstance(tab, str):
raise TypeError("'tab' expected a str value.")
raise TypeError("'tab' expected a str value, not {}".format(type(tab)))
else:
pass
if not isinstance(md_type, str):
raise TypeError("'md_type' expected a str value.")
raise TypeError(
"'md_type' expected a str value, not {}".format(type(md_type))
)
else:
pass
# check parameters values
Expand Down
2 changes: 1 addition & 1 deletion isogeo_pysdk/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def url(self, url: str):
def admin_url(self, url_base: str = "https://manage.isogeo.com") -> str:
"""Returns the administration URL (https://manage.isogeo.com) for this application.
:param str url_base: base URL of admin site. Defaults to: https://app.isogeo.com
:param str url_base: base URL of admin site. Defaults to: https://manage.isogeo.com
:rtype: str
"""
Expand Down
16 changes: 16 additions & 0 deletions isogeo_pysdk/models/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# ##################################

# standard library
import logging
import pprint
import re
import unicodedata
Expand All @@ -24,6 +25,8 @@
# ########## Globals ###############
# ##################################

logger = logging.getLogger(__name__)

# for slugified title
_regex_slugify_strip = re.compile(r"[^\w\s-]")
_regex_slugify_hyphenate = re.compile(r"[-\s]+")
Expand Down Expand Up @@ -1249,6 +1252,19 @@ def validityComment(self, validityComment: str):
self._validityComment = validityComment

# -- METHODS -----------------------------------------------------------------------
def admin_url(self, url_base: str = "https://app.isogeo.com") -> str:
"""Returns the administration URL (https://app.isogeo.com) for this metadata.
:param str url_base: base URL of admin site. Defaults to: https://app.isogeo.com
:rtype: str
"""
if self._creator is None:
logger.warning("Creator is required to build admin URL")
return False

creator_id = self._creator.get("_id")
return "{}/groups/{}/resources/{}/".format(url_base, creator_id, self._id)

def title_or_name(self, slugged: bool = False) -> str:
"""Gets the title of this Metadata or the name if there is no title.
Expand Down
43 changes: 20 additions & 23 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

# modules
from isogeo_pysdk.checker import IsogeoChecker
from isogeo_pysdk.models import Metadata

# ##############################################################################
# ########## Globals ###############
Expand Down Expand Up @@ -359,37 +360,33 @@ def get_request_base_url(self, route: str, prot: str = "https") -> str:
prot, self.api_url, route, self.lang
)

def get_edit_url(
self,
md_id: str = None,
md_type: str = None,
owner_id: str = None,
tab: str = "identification",
) -> str:
"""Constructs the edition URL of a metadata.
def get_edit_url(self, metadata: Metadata, tab: str = "identification") -> str:
"""Returns the edition URL of a metadata.
:param str md_id: metadata/resource UUID
:param str owner_id: owner UUID
:param str tab: target tab in the web form
:param Metadata metadata: metadata
:param str tab: target tab in the web form. Optionnal. Defaults to 'identification'.
"""
# checks inputs
if not checker.check_is_uuid(md_id) or not checker.check_is_uuid(owner_id):
raise ValueError("One of md_id or owner_id is not a correct UUID.")
else:
pass
if not checker.check_edit_tab(tab, md_type=md_type):
if not isinstance(metadata, Metadata):
raise TypeError(
"Method expects a metadata object, no {}".format(type(metadata))
)
if not checker.check_is_uuid(metadata._id):
raise ValueError(
"Metadata _id is not a correct UUID: {}".format(metadata._id)
)

# base edit URL
edit_url = metadata.admin_url(self.app_url)
# add tab
if not checker.check_edit_tab(tab, md_type=metadata.type):
logger.warning(
"Tab '{}' is not a valid tab for the is type '{}' of metadata. "
"It'll be replaced by default value.".format(tab, md_type)
"It'll be replaced by default value.".format(tab, metadata.type)
)
tab = "identification"
# construct URL
return (
"{}"
"/groups/{}"
"/resources/{}"
"/{}".format(self.APP_URLS.get(self.platform), owner_id, md_id, tab)
)
return edit_url + tab

def get_view_url(self, webapp: str = "oc", **kwargs):
"""Constructs the view URL of a metadata.
Expand Down
3 changes: 3 additions & 0 deletions tests/test_metadatas.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ def test_metadatas_in_search_results(self):
ds_date_creation = utils.hlpr_datetimes(metadata.created)
self.assertEqual(int(metadata.created[:4]), ds_date_creation.year)

# admin url
self.assertIsInstance(metadata.admin_url(self.isogeo.app_url), str)

# def test_search_specific_mds_bad(self):
# """Searches filtering on specific metadata."""
# # get random metadata within a small search
Expand Down
77 changes: 17 additions & 60 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

# 3rd party
from dotenv import load_dotenv
import urllib3

# module target
from isogeo_pysdk import IsogeoUtils
from isogeo_pysdk import IsogeoUtils, Metadata

# #############################################################################
# ######## Globals #################
Expand All @@ -36,6 +37,10 @@
if Path("dev.env").exists():
load_dotenv("dev.env", override=True)

# API access
METADATA_TEST_FIXTURE_UUID = environ.get("ISOGEO_FIXTURES_METADATA_COMPLETE")
WORKGROUP_TEST_FIXTURE_UUID = environ.get("ISOGEO_WORKGROUP_TEST_UUID")

# #############################################################################
# ########## Classes ###############
# ##################################
Expand All @@ -50,6 +55,10 @@ def setUpClass(cls):
"""Executed when module is loaded before any test."""
cls.utils = IsogeoUtils()

# ignore warnings related to the QA self-signed cert
if environ.get("ISOGEO_PLATFORM").lower() == "qa":
urllib3.disable_warnings()

# standard methods
def setUp(self):
""" Fixtures prepared before each test."""
Expand Down Expand Up @@ -138,75 +147,23 @@ def test_set_base_url_bad_parameter(self):
"""Raise error if platform parameter is bad."""
with self.assertRaises(ValueError):
self.utils.set_base_url(platform="skynet")
self.utils.set_base_url(platform=environ.get("ISOGEO_PLATFORM", "qa"))

# -- URLs Builders - edit (app) ------------------------------------------
def test_get_edit_url_ok(self):
"""Test URL builder for edition link on APP"""
self.utils.set_base_url(platform=environ.get("ISOGEO_PLATFORM", "prod"))
url = self.utils.get_edit_url(
md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="vector-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="identification",

metadata = Metadata(
_id=METADATA_TEST_FIXTURE_UUID,
_creator={"_id": WORKGROUP_TEST_FIXTURE_UUID},
type="vectorDataset",
)
url = self.utils.get_edit_url(metadata=metadata, tab="history")
self.assertIsInstance(url, str)
self.assertIn("app", url)
self.assertIn("groups", url)
urlparse(url)
# again with type extracted from metadata model
url = self.utils.get_edit_url(
md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="vectorDataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="identification",
)

def test_get_edit_url_bad_md_uuid(self):
"""Must raise an error if metadata/resource UUID check fails."""
with self.assertRaises(ValueError):
self.utils.get_edit_url(
md_id="oh_my_bad_i_m_not_a_correct_uuid",
md_type="vector-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="identification",
)

def test_get_edit_url_bad_md_type(self):
"""Must raise an error if metadata/resource type check fails."""
with self.assertRaises(ValueError):
self.utils.get_edit_url(
md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="bad_md_type",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="identification",
)

def test_get_edit_url_bad_wg_uuid(self):
"""Must raise an error if workgroup UUID check fails."""
with self.assertRaises(ValueError):
self.utils.get_edit_url(
md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="vector-dataset",
owner_id="oh_my_bad_i_m_not_a_correct_uuid",
tab="identification",
)

def test_get_edit_url_bad_tab(self):
"""Must raise an error if tab check fails."""
with self.assertRaises(ValueError):
self.utils.get_edit_url(
md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="raster-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="attributes",
)
with self.assertRaises(ValueError):
self.utils.get_edit_url(
md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="raster-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="what_a_tab_name",
)

# -- URLs Builders - request -------------------------------------
def test_get_request_base_url(self):
Expand Down

0 comments on commit 78c01f8

Please sign in to comment.