Skip to content

Commit

Permalink
Add featured view
Browse files Browse the repository at this point in the history
  • Loading branch information
blagojabozinovski committed Mar 10, 2024
1 parent 4a049f0 commit e1fc92e
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ckanext/iaea/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from ckan.lib.plugins import DefaultTranslation
from ckanext.iaea.helpers import get_helpers
from ckanext.iaea.logic import action, validators
from ckanext.iaea import view
from flask import Blueprint
import ckan.logic as logic
import ckan.model as model

Expand All @@ -25,6 +27,7 @@ class IaeaPlugin(plugins.SingletonPlugin, DefaultTranslation):
plugins.implements(plugins.ITranslation)
plugins.implements(plugins.ITemplateHelpers, inherit=True)
plugins.implements(plugins.IValidators)
plugins.implements(plugins.IBlueprint)

# IConfigurer

Expand All @@ -50,3 +53,11 @@ def get_validators(self):
return {
'iaea_owner_org_validator': validators.package_organization_validator,
}

# IBlueprint
def get_blueprint(self):
blueprint = Blueprint(self.name, self.__module__)
blueprint.template_folder = u'templates'
# Add plugin url rules to Blueprint object
blueprint.add_url_rule(u'/dataset/<id>/view', view_func=view.FeatureView.as_view(str(u'feature_view')))
return blueprint
37 changes: 37 additions & 0 deletions ckanext/iaea/templates/package/features_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{% extends "package/read_base.html" %}

{% block primary_content_inner %}
{% if package_views %}

<form action="#" method="post">
<fieldset class="fields-feature-view form-group">
<p>Select a view that should be featured on the dataset page.</p>
{% for resource in package_views %}
<h3> {{ resource.resource_name }}</h3>
{% for view in resource.views %}
<div class="radio-groups">
{% set view_url = h.url_for(qualified=True, controller='package',
action='resource_view', id=pkg['name'],
resource_id=view.resource_id,
view_id=view.id) %}
{% if pkg['featured_view'] == view.id %}
<input id="featured_view" name="featured_view" type="radio" class="radio" value="{{view.id}}" checked />
<span><a href="{{view_url}}" target="_blank">{{view.title}}</a></span>
{% else %}
<input id="featured_view" name="featured_view" type="radio" class="radio" value="{{view.id}}" />
<span><a href="{{view_url}}" target="_blank">{{view.title}}</a></span>
{% endif %}
</div>
{% endfor %}
{% endfor %}
</fieldset>

<div class="form-actions">
<input type="submit" class="btn btn-primary" name="submit" value="submit" />
<input type="submit" class="btn btn-danger" name="submit" value="clear" />
</div>
</form>
{% else %}
<p>{{ _("There are no views created for this dataset yet.") }}</p>
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions ckanext/iaea/templates/package/read_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ <h4>Disclaimer</h4>
The IAEA makes no warranties, either expressed or implied, concerning the accuracy, completeness, reliability, or suitability of the information. The mention of names of specific companies or equipment does not imply any intention to infringe proprietary rights, nor should it be construed as an endorsement or recommendation on the part of the IAEA.
</p>
</div>
{% endblock %}

{% block content_primary_nav %}
{{ super() }}
{% if h.check_access('package_update', {'id':pkg.id }) %}
{{ h.build_nav_icon('iaea.feature_view', _('View'), id=pkg.name, icon='check-square-o') }}
{% endif %}
{% endblock %}
162 changes: 162 additions & 0 deletions ckanext/iaea/view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import logging
import ckan.logic as logic
import ckan.lib.base as base
import ckan.model as model
from flask.views import MethodView
import ckan.lib.dictization.model_dictize as model_dictize
from itertools import groupby
import ckan.lib.helpers as h

from ckan.common import _, c, request

log = logging.getLogger(__name__)

render = base.render
abort = base.abort

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
ValidationError = logic.ValidationError
check_access = logic.check_access
get_action = logic.get_action


class FeatureView(MethodView):
""" feature selected view on dataset page
"""

def _prepare(self, id):
context = {
u'model': model,
u'session': model.Session,
u'user': c.user,
u'auth_user_obj': c.userobj,
u'save': u'save' in request.form
}
try:
check_access(u'package_update', context, {u'id': id})

except NotFound:
return base.abort(404, _(u'Dataset not found'))
except NotAuthorized:
return base.abort(
403,
_(u'Unauthorized to edit %s') % u''
)

return context

def post(self, id):
context = self._prepare(id)

featured_view = request.form.get('featured_view', False)
data_dict = {u'id': id}
if request.form['submit'] == 'submit':
if not featured_view:
h.flash_error('Please select view from the list.')
data_dict['featured_view'] = ''
else:
data_dict['featured_view'] = featured_view
else:
data_dict['featured_view'] = ''

# update package with selected featured view
try:
pkg_dict = get_action(u'package_patch')(context, data_dict)
except NotFound:
return base.abort(404, _(u'Dataset not found'))
except NotAuthorized:
return base.abort(403, _(u'Unauthorized to read dataset %s') % id)

try:
pkg_dict = get_action(u'package_show')(context, data_dict)
pkg = context[u'package']
dataset_type = pkg_dict[u'type'] or u'dataset'
except NotFound:
return base.abort(404, _(u'Dataset not found'))
except NotAuthorized:
return base.abort(403, _(u'Unauthorized to read dataset %s') % id)

package_views = model.Session.query(
model.ResourceView
).join(model.Resource).filter(model.Resource.package_id == pkg_dict['id']).all()

package_views_list = model_dictize.resource_view_list_dictize(
package_views, context)

package_views_dict = []
package_views_list = sorted(package_views_list, key=lambda k: k['resource_id'])
for k, v in groupby(package_views_list, key=lambda x: x['resource_id']):
[resource_name, state] = model.Session.query(model.Resource.name.label('name'), model.Resource.state.label('state')).filter(
model.Resource.id == k).first()

if state == 'deleted':
continue
else:
view_dict = {
'resource_name': resource_name,
'resource_id': k,
'views': list(v)
}

package_views_dict.append(view_dict)

c.pkg_dict = pkg_dict
c.pkg = pkg

return base.render(
u'package/features_view.html', {
u'dataset_type': dataset_type,
u'pkg_dict': pkg_dict,
u'pkg': pkg,
u'id': id, # i.e. package's current name
u'package_views': package_views_dict
})

def get(self, id):
context = self._prepare(id)
data_dict = {u'id': id}
try:
pkg_dict = get_action(u'package_show')(context, data_dict)
pkg = context[u'package']
dataset_type = pkg_dict[u'type'] or u'dataset'
except NotFound:
return base.abort(404, _(u'Dataset not found'))
except NotAuthorized:
return base.abort(403, _(u'Unauthorized to read dataset %s') % id)

package_views = model.Session.query(
model.ResourceView
).join(model.Resource).filter(model.Resource.package_id == pkg_dict['id']).all()

package_views_list = model_dictize.resource_view_list_dictize(
package_views, context)

package_views_dict = []
package_views_list = sorted(package_views_list, key=lambda k: k['resource_id'])
for k, v in groupby(package_views_list, key=lambda x: x['resource_id']):
[resource_name, state] = model.Session.query(model.Resource.name.label('name'), model.Resource.state.label('state')).filter(
model.Resource.id == k).first()

if state == 'deleted':
continue
else:
view_dict = {
'resource_name': resource_name,
'resource_id': k,
'views': list(v)
}

package_views_dict.append(view_dict)

c.pkg_dict = pkg_dict
c.pkg = pkg

return base.render(
u'package/features_view.html', {
u'dataset_type': dataset_type,
u'pkg_dict': pkg_dict,
u'pkg': pkg,
u'id': id, # i.e. package's current name
u'package_views': package_views_dict
})

0 comments on commit e1fc92e

Please sign in to comment.