diff --git a/debian/control b/debian/control --- a/debian/control +++ b/debian/control @@ -23,7 +23,7 @@ python3-sphinxcontrib.httpdomain, python3-yaml, python3-swh.core (>= 0.0.40~), - python3-swh.model (>= 0.0.23~), + python3-swh.model (>= 0.0.24~), python3-swh.storage (>= 0.0.101~), python3-swh.indexer.storage (>= 0.0.52~), python3-swh.vault (>= 0.0.20~) @@ -33,7 +33,7 @@ Package: python3-swh.web Architecture: all Depends: python3-swh.core (>= 0.0.40~), - python3-swh.model (>= 0.0.23~), + python3-swh.model (>= 0.0.24~), python3-swh.storage (>= 0.0.101~), python3-swh.indexer.storage (>= 0.0.52~), python3-swh.vault (>= 0.0.20~), diff --git a/requirements-swh.txt b/requirements-swh.txt --- a/requirements-swh.txt +++ b/requirements-swh.txt @@ -1,5 +1,5 @@ swh.core >= 0.0.40 -swh.model >= 0.0.23 +swh.model >= 0.0.24 swh.storage >= 0.0.101 swh.vault >= 0.0.20 swh.indexer.storage >= 0.0.52 diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -13,7 +13,8 @@ from django.core import urlresolvers from django.http import QueryDict -from swh.model.fields.hashes import validate_sha1_git +from swh.model.exceptions import ValidationError +from swh.model.identifiers import persistent_identifier from swh.web.common import service from swh.web.common.exc import BadInputExc @@ -264,33 +265,20 @@ (content/directory/release/revision/snapshot) object_id (str): the swh object id (hexadecimal representation of its hash value) - schem_version (int): the scheme version of the swh + scheme_version (int): the scheme version of the swh persistent identifiers - Returns: str: the swh object persistent identifier + Returns: + str: the swh object persistent identifier Raises: BadInputExc if the provided parameters do not enable to generate a valid identifier """ - swh_id = 'swh:%s:' % scheme_version - if object_type == 'content': - swh_id += 'cnt:' - elif object_type == 'directory': - swh_id += 'dir:' - elif object_type == 'release': - swh_id += 'rel:' - elif object_type == 'revision': - swh_id += 'rev:' - elif object_type == 'snapshot': - swh_id += 'snp:' - else: - raise BadInputExc('Invalid object type (%s) for swh persistent id' % - object_type) try: - validate_sha1_git(object_id) - swh_id += object_id - except Exception: - raise BadInputExc('Invalid object id (%s) for swh persistent id' % - object_id) - return swh_id + swh_id = persistent_identifier(object_type, object_id, scheme_version) + except ValidationError as e: + raise BadInputExc('Invalid object (%s) for swh persistent id. %s' % + (object_id, e)) + else: + return swh_id diff --git a/swh/web/tests/common/test_utils.py b/swh/web/tests/common/test_utils.py --- a/swh/web/tests/common/test_utils.py +++ b/swh/web/tests/common/test_utils.py @@ -152,8 +152,8 @@ with self.assertRaises(BadInputExc) as cm: utils.get_swh_persistent_id('foo', sha1_git) - self.assertIn('Invalid object type', cm.exception.args[0]) + self.assertIn('Invalid object', cm.exception.args[0]) with self.assertRaises(BadInputExc) as cm: utils.get_swh_persistent_id(swh_object_type, 'not a valid id') - self.assertIn('Invalid object id', cm.exception.args[0]) + self.assertIn('Invalid object', cm.exception.args[0])