Changeset View
Changeset View
Standalone View
Standalone View
swh/web/common/identifiers.py
Show All 18 Lines | from swh.model.identifiers import ( | ||||
RELEASE, | RELEASE, | ||||
REVISION, | REVISION, | ||||
SNAPSHOT, | SNAPSHOT, | ||||
PersistentId, | PersistentId, | ||||
) | ) | ||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
from swh.web.common.typing import QueryParameters | from swh.web.common.typing import QueryParameters | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import swh_object_icons, reverse | ||||
def get_swh_persistent_id( | def get_swh_persistent_id( | ||||
object_type: str, object_id: str, scheme_version: int = 1 | object_type: str, object_id: str, scheme_version: int = 1 | ||||
) -> str: | ) -> str: | ||||
""" | """ | ||||
Returns the persistent identifier for a swh object based on: | Returns the persistent identifier for a swh object based on: | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | ) -> Dict[str, List[bytes]]: | ||||
} | } | ||||
for pid in persistent_ids: | for pid in persistent_ids: | ||||
obj_id = pid.object_id | obj_id = pid.object_id | ||||
obj_type = pid.object_type | obj_type = pid.object_type | ||||
pids_by_type[obj_type].append(hash_to_bytes(obj_id)) | pids_by_type[obj_type].append(hash_to_bytes(obj_id)) | ||||
return pids_by_type | return pids_by_type | ||||
ardumont: types are known, maybe make those official at the same time ;) | |||||
Done Inline ActionsIt's planned of course ;-) anlambert: It's planned of course ;-) | |||||
def get_swh_persistent_ids(swh_objects, snapshot_context=None): | |||||
""" | |||||
Returns a list of dict containing info related to persistent | |||||
identifiers of swh objects. | |||||
Args: | |||||
swh_objects (list): a list of dict with the following keys: | |||||
* type: swh object type | |||||
(content/directory/release/revision/snapshot) | |||||
* id: swh object id | |||||
snapshot_context (dict): optional parameter describing the snapshot in | |||||
which the object has been found | |||||
Returns: | |||||
list: a list of dict with the following keys: | |||||
* object_type: the swh object type | |||||
(content/directory/release/revision/snapshot) | |||||
* object_icon: the swh object icon to use in HTML views | |||||
* swh_id: the computed swh object persistent identifier | |||||
* swh_id_url: the url resolving the persistent identifier | |||||
* show_options: boolean indicating if the persistent id options | |||||
must be displayed in persistent ids HTML view | |||||
""" | |||||
swh_ids = [] | |||||
for swh_object in swh_objects: | |||||
if not swh_object["id"]: | |||||
continue | |||||
swh_id = get_swh_persistent_id(swh_object["type"], swh_object["id"]) | |||||
show_options = swh_object["type"] == "content" or ( | |||||
snapshot_context and snapshot_context["origin_info"] is not None | |||||
) | |||||
object_icon = swh_object_icons[swh_object["type"]] | |||||
swh_ids.append( | |||||
{ | |||||
"object_type": swh_object["type"], | |||||
"object_id": swh_object["id"], | |||||
"object_icon": object_icon, | |||||
"swh_id": swh_id, | |||||
"swh_id_url": reverse("browse-swh-id", url_args={"swh_id": swh_id}), | |||||
"show_options": show_options, | |||||
} | |||||
) | |||||
return swh_ids |
types are known, maybe make those official at the same time ;)