Skip to content
Snippets Groups Projects
Commit 94c5163e authored by Benjamin Becker's avatar Benjamin Becker
Browse files

refactoring: extracts method get_collection_info from get_collection

preparation for use in controller.py
parent d7318f56
No related branches found
No related tags found
No related merge requests found
......@@ -10,14 +10,18 @@ from profilehooks import timecall, profile
def get_collection(dataset_dict):
collection_id = get_collection_id(dataset_dict)
if collection_id:
return get_collection_info(collection_id, dataset_dict)
return None
def get_collection_info(collection_id, dataset_dict=None):
collection_dict = get_package_dict(collection_id)
dataset_names = get_dataset_names(collection_dict)
datasets_in_collection = get_datasets_from_solr(dataset_names)
collection_info = gather_collection_info(collection_dict, dataset_dict, datasets_in_collection)
collection_info = gather_collection_info(collection_dict, datasets_in_collection, dataset_dict)
return collection_info
return None
@timecall(immediate=True)
def get_collection_id(dataset_dict):
......@@ -67,23 +71,9 @@ def get_datasets_from_solr(dataset_names):
return datasets_found
def gather_collection_info(collection_dict, dataset_dict, datasets_in_collection):
def gather_collection_info(collection_dict, datasets_in_collection, dataset_dict=None):
url_from_id = lambda id: helpers.url_for(controller='package', action='read', id=id)
name_current_dataset = dataset_dict.get('name')
dataset_names = [d.get('name') for d in datasets_in_collection]
def get_predecessor():
id_current = dataset_names.index(name_current_dataset)
if id_current > 0:
return dataset_names[id_current - 1]
return None
def get_successor():
id_current = dataset_names.index(name_current_dataset)
if id_current < len(dataset_names) - 1:
return dataset_names[id_current + 1]
return None
name_first_dataset = datasets_in_collection[0].get('name')
url_first_dataset = url_from_id(name_first_dataset)
......@@ -98,11 +88,29 @@ def gather_collection_info(collection_dict, dataset_dict, datasets_in_collection
id=name_collection
)
if dataset_dict:
name_current_dataset = dataset_dict.get('name')
dataset_names = [d.get('name') for d in datasets_in_collection]
def get_predecessor():
id_current = dataset_names.index(name_current_dataset)
if id_current and id_current > 0:
return dataset_names[id_current - 1]
return None
def get_successor():
id_current = dataset_names.index(name_current_dataset)
if id_current and id_current < len(dataset_names) - 1:
return dataset_names[id_current + 1]
return None
name_predecessor = get_predecessor()
url_predecessor = url_from_id(name_predecessor) if name_predecessor else None
name_successor = get_successor()
url_successor = url_from_id(name_successor) if name_successor else None
else:
url_predecessor = url_successor = None
return {
'title': collection_dict.get('title'),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment