Search code examples
pythonjinja2ckan

How can additional visibility options be added to the CKAN form for data records?


I'm wondering if there is a straightforward way to change the CKAN dataset form. I would be interested in adding extra visibility options than public and private: that is to say;

  1. Public
  2. Private
  3. Sub Project
  4. Project
  5. Organisation/Company/Institute

CKAN uses package_basic_fields.html

...

 {% if show_visibility_selector %}
    {% block package_metadata_fields_visibility %}
    <div class="form-group control-medium">
      <label for="field-private" class="form-label">{{ _('Visibility') }}</label>
      <div class="controls">
        <select id="field-private" name="private" class="form-control">
          {% for option in [('True', _('Private')), ('False', _('Public'))] %}
          <option value="{{ option[0] }}" {% if option[0] == data.private|trim %}selected="selected"{% endif %}>{{ option[1] }}</option>

          ### I want to add extra options here

          {% endfor %}
        </select>
      </div>
    </div>
    {% endblock %}
    {% endif %}

    {% if show_organizations_selector and show_visibility_selector %}

...

ckanext-scheming doesn't talk about this either.

Any recommendation for me?


Solution

  • I believe you might be able to achieve this with a combination of ckanext-scheming and the interfaces provided by CKAN: IPackageController and possibly IPermissionLabels.

    The idea is the following:

    1. Find the place where Visibility options are rendered in scheming. I believe it is at templates/scheming/form_snippets/organization.html
    2. Make Visibility hidden using css
    3. Add a new field with scheming with your desired options for visibility.
    4. In IPackageController, examine the value of this field and manually set ckan's visibility (you most likely want Private here for all options except Public). You have now set ckan's visibility, but also have information about your custom visibility field.
    5. Use IPermissionLabels to achieve the visibility level you want between users and packages.

    CKAN is not designed for something like this, but I am positive that this is a workaround that could work given some tweaking here and there.

    EDIT: I confirm that I have managed to achieve a similar functionality following what I wrote above. In my case, I wanted to add the option of having public metadata for datasets, but not allow non authorized users to read resources nor download them. I also had to use IResourceController for this, manually checking if the user is allowed to have access to a resource and at what level.