forked from datopian/ckanext-iaea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a049f0
commit e1fc92e
Showing
4 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |