okfn / ckanext-lacounts

CKAN extension for the LA Counts project
GNU Affero General Public License v3.0
8 stars 5 forks source link

Investigate usage of `package_show` in cycles #219

Open roll opened 5 years ago

roll commented 5 years ago

Overview

Code like this was sometimes failing with Exception: Action function package_show did not call its auth function:

def some_helper():
    context = {'model': model}

    # Get datasets
    datasets = []
    ids = normalize_list(value)
    for id in ids:
        try:
            dataset = tk.get_action('package_show')(context, {'id': id})
            href = tk.url_for('ckanext_showcase_read', id=dataset['name'], qualified=False)
            datasets.append({'text': dataset['title'], 'href': href})
        except tk.ObjectNotFound:
            pass

    return datasets

Moving context definition inside the cycle solved this problem:

def some_helper():

    # Get datasets
    datasets = []
    ids = normalize_list(value)
    for id in ids:
        try:
            dataset = tk.get_action('package_show')({'model': model}, {'id': id})
            href = tk.url_for('ckanext_showcase_read', id=dataset['name'], qualified=False)
            datasets.append({'text': dataset['title'], 'href': href})
        except tk.ObjectNotFound:
            pass

    return datasets

We have more functions like this but it seems it doesn't create any problems, for now. But anyway it makes sense to double check.