Skip to content

Commit

Permalink
Merge pull request #4977 from DataShades/stats-blueprint
Browse files Browse the repository at this point in the history
Stats Controller -> Blueprint
  • Loading branch information
amercader committed Sep 13, 2019
2 parents 0b2a6c7 + 95c4800 commit f23a6cb
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 171 deletions.
1 change: 0 additions & 1 deletion ckan/tests/legacy/test_coding_standards.py
Expand Up @@ -468,7 +468,6 @@ class TestPep8(object):
'ckanext/multilingual/plugin.py',
'ckanext/resourceproxy/plugin.py',
'ckanext/stats/controller.py',
'ckanext/stats/plugin.py',
'ckanext/stats/stats.py',
'ckanext/stats/tests/test_stats_lib.py',
'ckanext/stats/tests/test_stats_plugin.py',
Expand Down
66 changes: 66 additions & 0 deletions ckanext/stats/blueprint.py
@@ -0,0 +1,66 @@
# encoding: utf-8

from flask import Blueprint

from ckan.plugins.toolkit import c, render
from ckan.lib.base import BaseController
import ckanext.stats.stats as stats_lib
import ckan.lib.helpers as h

stats = Blueprint(u'stats', __name__)


def index():
stats = stats_lib.Stats()
rev_stats = stats_lib.RevisionStats()
c.top_rated_packages = stats.top_rated_packages()
c.most_edited_packages = stats.most_edited_packages()
c.largest_groups = stats.largest_groups()
c.top_tags = stats.top_tags()
c.top_package_creators = stats.top_package_creators()
c.new_packages_by_week = rev_stats.get_by_week(u'new_packages')
c.deleted_packages_by_week = rev_stats.get_by_week(u'deleted_packages')
c.num_packages_by_week = rev_stats.get_num_packages_by_week()
c.package_revisions_by_week = rev_stats.get_by_week(u'package_revisions')

c.raw_packages_by_week = []
for (
week_date, num_packages, cumulative_num_packages
) in c.num_packages_by_week:
c.raw_packages_by_week.append({
u'date': h.date_str_to_datetime(week_date),
u'total_packages': cumulative_num_packages
})

c.all_package_revisions = []
c.raw_all_package_revisions = []
for (
week_date, revs, num_revisions, cumulative_num_revisions
) in c.package_revisions_by_week:
c.all_package_revisions.append(
u'[new Date(%s), %s]' %
(week_date.replace(u'-', u','), num_revisions)
)
c.raw_all_package_revisions.append({
u'date': h.date_str_to_datetime(week_date),
u'total_revisions': num_revisions
})

c.new_datasets = []
c.raw_new_datasets = []
for (
week_date, pkgs, num_packages, cumulative_num_packages
) in c.new_packages_by_week:
c.new_datasets.append(
u'[new Date(%s), %s]' %
(week_date.replace(u'-', u','), num_packages)
)
c.raw_new_datasets.append({
u'date': h.date_str_to_datetime(week_date),
u'new_packages': num_packages
})

return render(u'ckanext/stats/index.html')


stats.add_url_rule(u'/stats', view_func=index)
41 changes: 0 additions & 41 deletions ckanext/stats/controller.py

This file was deleted.

24 changes: 11 additions & 13 deletions ckanext/stats/plugin.py
Expand Up @@ -3,27 +3,25 @@
from logging import getLogger

import ckan.plugins as p
from ckanext.stats import blueprint

log = getLogger(__name__)


class StatsPlugin(p.SingletonPlugin):
'''Stats plugin.'''
u'''Stats plugin.'''

p.implements(p.IRoutes, inherit=True)
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IBlueprint)

def after_map(self, map):
map.connect('stats', '/stats',
controller='ckanext.stats.controller:StatsController',
action='index')
map.connect('stats_action', '/stats/{action}',
controller='ckanext.stats.controller:StatsController')
return map
def get_blueprint(self):
return blueprint.stats

def update_config(self, config):
templates = 'templates'
if p.toolkit.asbool(config.get('ckan.legacy_templates', False)):
templates = 'templates_legacy'
templates = u'templates'
if p.toolkit.asbool(config.get(u'ckan.legacy_templates', False)):
templates = u'templates_legacy'
p.toolkit.add_template_directory(config, templates)
p.toolkit.add_public_directory(config, 'public')
p.toolkit.add_resource('public/ckanext/stats', 'ckanext_stats')
p.toolkit.add_public_directory(config, u'public')
p.toolkit.add_resource(u'public/ckanext/stats', u'ckanext_stats')

0 comments on commit f23a6cb

Please sign in to comment.