Add rating data to datasets in search and show

This commit is contained in:
Jari Voutilainen 2018-05-29 16:13:48 +03:00
parent ddc88ab830
commit 1c6f91b63f
4 changed files with 32 additions and 6 deletions

View File

@ -9,6 +9,12 @@ from ckanext.rating import helpers
import ckanext.rating.logic.auth as rating_auth
from ckanext.rating.model import Rating
from ckan.lib.plugins import DefaultTranslation
from ckan.plugins.toolkit import get_action
from helpers import show_rating_in_type
import logging
log = logging.getLogger(__name__)
def sort_by_rating(sort):
@ -97,6 +103,24 @@ class RatingPlugin(plugins.SingletonPlugin, DefaultTranslation):
data_dict['rating'] = rating_dict.get('rating')
return data_dict
def after_show(self, context, pkg_dict):
if show_rating_in_type(pkg_dict.get('type')):
rating_dict = get_action('rating_package_get')(context, {'package_id': pkg_dict.get('id')})
pkg_dict['rating'] = rating_dict.get('rating', 0)
pkg_dict['ratings_count'] = rating_dict.get('ratings_count', 0)
return pkg_dict
def after_search(self, search_results, search_params):
for pkg in search_results['results']:
if show_rating_in_type(pkg.get('type')):
rating_dict = get_action('rating_package_get')({}, {'package_id': pkg.get('id')})
pkg['rating'] = rating_dict.get('rating', 0)
pkg['ratings_count'] = rating_dict.get('ratings_count', 0)
return search_results
# IRoutes
def before_map(self, map):
@ -116,3 +140,5 @@ class RatingPlugin(plugins.SingletonPlugin, DefaultTranslation):
)
return map

View File

@ -18,7 +18,7 @@
{% set action = 'submit_showcase_rating' %}
{% endif %}
{% set stars = h.package_rating(None, {'package_id' : package.id} ).rating %}
{% set stars = package.rating %}
<div class="rating-wrapper">
<span class="rating-value">{{stars}}</span>
<div class="rating-stars-wrapper {{ 'input-enabled' if enable_input }}">
@ -42,7 +42,7 @@
</span>
</div>
<span class="rating-count">
{% set ratings_count = h.package_rating(None, {'package_id' : package.id} ).ratings_count%}
{% set ratings_count = package.ratings_count%}
({{ ratings_count }})
</span>

View File

@ -20,7 +20,7 @@ package - The package for which the rating is displayed
{% set action = 'submit_showcase_rating' %}
{% endif %}
{% set stars = h.package_rating(None, {'package_id' : package.id} ).rating %}
{% set stars = package.rating %}
<div class="rating-wrapper">
<div class="rating-stars-wrapper rating-stars-wrapper-flat {{ 'input-enabled' if enable_input }}">
<span class="star-rating{% if stars == 0 %} no-stars{% endif %}">
@ -43,7 +43,7 @@ package - The package for which the rating is displayed
</span>
</div>
<span class="rating-count">
{% set ratings_count = h.package_rating(None, {'package_id' : package.id} ).ratings_count%}
{% set ratings_count = package.ratings_count%}
({{ ratings_count }})
</span>

View File

@ -7,8 +7,8 @@ stars - The number of stars to be displayed.
#}
{% set ratings_count = h.package_rating(None, {'package_id' : package.id} ).ratings_count%}
{% set stars = h.package_rating(None, {'package_id' : package.id} ).rating %}
{% set ratings_count = package.ratings_count%}
{% set stars = package.rating %}
{% if stars|int < stars %}
{% set half_star = 1 %}
{% else %}