diff --git a/ckan/controllers/home.py b/ckan/controllers/home.py index ad34bd702ca..da3d4b3901b 100644 --- a/ckan/controllers/home.py +++ b/ckan/controllers/home.py @@ -179,8 +179,7 @@ def db_to_form_schema(group_type=None): # END OF DIRTYNESS - template = 'home/layout{0}.html'.format(g.homepage_style) - return base.render(template, cache_force=True) + return base.render('home/index.html', cache_force=True) def license(self): return base.render('home/license.html') diff --git a/ckan/lib/app_globals.py b/ckan/lib/app_globals.py index 074deb4e274..dde98f0a1ed 100644 --- a/ckan/lib/app_globals.py +++ b/ckan/lib/app_globals.py @@ -29,6 +29,7 @@ 'ckan.site_about', 'ckan.site_intro_text', 'ckan.site_custom_css', + 'ckan.homepage_style', ] config_details = { diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index c70d74d7dbc..94e7c97b0e6 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -1663,6 +1663,74 @@ def new_activities(): action = logic.get_action('dashboard_new_activities_count') return action({}, {}) +def get_featured_organizations(count=1): + '''Returns a list of favourite organization in the form + of organization_list action function + ''' + config_orgs = config.get('ckan.featured_orgs', '').split() + orgs = featured_group_org(get_action='organization_show', + list_action='organization_list', + count=count, + items=config_orgs) + return orgs + +def get_featured_groups(count=1): + '''Returns a list of favourite group the form + of organization_list action function + ''' + config_groups = config.get('ckan.featured_groups', '').split() + groups = featured_group_org(get_action='group_show', + list_action='group_list', + count=count, + items=config_groups) + return groups + +def featured_group_org(items, get_action, list_action, count): + def get_group(id): + context = {'ignore_auth': True, + 'limits': {'packages': 2}, + 'for_view': True} + data_dict = {'id': id} + + try: + out = logic.get_action(get_action)(context, data_dict) + except logic.ObjectNotFound: + return None + return out + + groups_data = [] + + extras = logic.get_action(list_action)({}, {}) + + # list of found ids to prevent duplicates + found = [] + for group_name in items + extras: + group = get_group(group_name) + if not group: + continue + # ckeck if duplicate + if group['id'] in found: + continue + found.append(group['id']) + groups_data.append(group) + if len(groups_data) == count: + break + + return groups_data + +def get_site_statistics(): + stats = {} + stats['dataset_count'] = logic.get_action('package_search')({}, {"rows": 1})['count'] + stats['group_count'] = len(logic.get_action('group_list')({}, {})) + stats['organization_count'] = len(logic.get_action('organization_list')({}, {})) + result =model.Session.execute( + '''select count(*) from related r + left join related_dataset rd on r.id = rd.related_id + where rd.status = 'active' or rd.id is null''').first()[0] + stats['related_count'] = result + + return stats + # these are the functions that will end up in `h` template helpers __allowed_functions__ = [ @@ -1761,4 +1829,7 @@ def new_activities(): 'radio', 'submit', 'asbool', + 'get_featured_organizations', + 'get_featured_groups', + 'get_site_statistics', ] diff --git a/ckan/templates/home/index.html b/ckan/templates/home/index.html index d9e4abfb17b..8684c551732 100644 --- a/ckan/templates/home/index.html +++ b/ckan/templates/home/index.html @@ -1,15 +1 @@ -{% extends "page.html" %} - -{% block subtitle %}{{ _("Welcome") }}{% endblock %} - -{% block maintag %}{% endblock %} -{% block toolbar %}{% endblock %} - -{% block content %} -
-
- {{ self.flash() }} -
- {% block primary_content %}{% endblock %} -
-{% endblock %} +{% include "home/layout" + (g.homepage_style or '1') + ".html" %} diff --git a/ckan/templates/home/index_base.html b/ckan/templates/home/index_base.html new file mode 100644 index 00000000000..d9e4abfb17b --- /dev/null +++ b/ckan/templates/home/index_base.html @@ -0,0 +1,15 @@ +{% extends "page.html" %} + +{% block subtitle %}{{ _("Welcome") }}{% endblock %} + +{% block maintag %}{% endblock %} +{% block toolbar %}{% endblock %} + +{% block content %} +
+
+ {{ self.flash() }} +
+ {% block primary_content %}{% endblock %} +
+{% endblock %} diff --git a/ckanext/homepage/theme/templates/home/layout1.html b/ckan/templates/home/layout1.html similarity index 95% rename from ckanext/homepage/theme/templates/home/layout1.html rename to ckan/templates/home/layout1.html index e40b5fe5e1b..50621df3a1b 100644 --- a/ckanext/homepage/theme/templates/home/layout1.html +++ b/ckan/templates/home/layout1.html @@ -1,4 +1,4 @@ -{% extends 'home/index.html' %} +{% extends 'home/index_base.html' %} {% block homepage_layout_class %} layout-1{% endblock %} diff --git a/ckanext/homepage/theme/templates/home/layout2.html b/ckan/templates/home/layout2.html similarity index 95% rename from ckanext/homepage/theme/templates/home/layout2.html rename to ckan/templates/home/layout2.html index 8ccf432533a..41e625092d2 100644 --- a/ckanext/homepage/theme/templates/home/layout2.html +++ b/ckan/templates/home/layout2.html @@ -1,4 +1,4 @@ -{% extends 'home/index.html' %} +{% extends 'home/index_base.html' %} {% block homepage_layout_class %} layout-2{% endblock %} diff --git a/ckanext/homepage/theme/templates/home/layout3.html b/ckan/templates/home/layout3.html similarity index 93% rename from ckanext/homepage/theme/templates/home/layout3.html rename to ckan/templates/home/layout3.html index 4e805845b21..08e56f45ff3 100644 --- a/ckanext/homepage/theme/templates/home/layout3.html +++ b/ckan/templates/home/layout3.html @@ -1,4 +1,4 @@ -{% extends 'home/index.html' %} +{% extends 'home/index_base.html' %} {% block homepage_layout_class %} layout-3{% endblock %} diff --git a/ckanext/homepage/theme/templates/home/snippets/featured_group.html b/ckan/templates/home/snippets/featured_group.html similarity index 100% rename from ckanext/homepage/theme/templates/home/snippets/featured_group.html rename to ckan/templates/home/snippets/featured_group.html diff --git a/ckanext/homepage/theme/templates/home/snippets/featured_organization.html b/ckan/templates/home/snippets/featured_organization.html similarity index 100% rename from ckanext/homepage/theme/templates/home/snippets/featured_organization.html rename to ckan/templates/home/snippets/featured_organization.html diff --git a/ckanext/homepage/theme/templates/home/snippets/promoted.html b/ckan/templates/home/snippets/promoted.html similarity index 100% rename from ckanext/homepage/theme/templates/home/snippets/promoted.html rename to ckan/templates/home/snippets/promoted.html diff --git a/ckanext/homepage/theme/templates/home/snippets/search.html b/ckan/templates/home/snippets/search.html similarity index 100% rename from ckanext/homepage/theme/templates/home/snippets/search.html rename to ckan/templates/home/snippets/search.html diff --git a/ckanext/homepage/theme/templates/home/snippets/stats.html b/ckan/templates/home/snippets/stats.html similarity index 100% rename from ckanext/homepage/theme/templates/home/snippets/stats.html rename to ckan/templates/home/snippets/stats.html diff --git a/ckanext/homepage/__init__.py b/ckanext/homepage/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/ckanext/homepage/plugin.py b/ckanext/homepage/plugin.py deleted file mode 100644 index d9a79b48a2c..00000000000 --- a/ckanext/homepage/plugin.py +++ /dev/null @@ -1,89 +0,0 @@ -import logging - -import ckan.plugins as p -import ckan.model as model - -log = logging.getLogger(__name__) - -class HomepagePlugin(p.SingletonPlugin): - p.implements(p.IConfigurer, inherit=True) - p.implements(p.ITemplateHelpers, inherit=True) - p.implements(p.IConfigurable, inherit=True) - - def configure(self, config): - groups = config.get('ckan.featured_groups', '') - if groups: - log.warning('Config setting `ckan.featured_groups` is deprecated ' - 'please use `ckanext.homepage.groups`') - self.groups = config.get('ckanext.homepage.groups', groups).split() - self.orgs = config.get('ckanext.homepage.orgs', '').split() - - def update_config(self, config): - p.toolkit.add_template_directory(config, 'theme/templates') - - def get_featured_organizations(self, count=1): - orgs = self.featured_group_org(get_action='organization_show', - list_action='organization_list', - count=count, - items=self.orgs) - return orgs - - def get_featured_groups(self, count=1): - groups = self.featured_group_org(get_action='group_show', - list_action='group_list', - count=count, - items=self.groups) - return groups - - def featured_group_org(self, items, get_action, list_action, count): - def get_group(id): - context = {'ignore_auth': True, - 'limits': {'packages': 2}, - 'for_view': True} - data_dict = {'id': id} - - try: - out = p.toolkit.get_action(get_action)(context, data_dict) - except p.toolkit.ObjectNotFound: - return None - return out - - groups_data = [] - - extras = p.toolkit.get_action(list_action)({}, {}) - - # list of found ids to prevent duplicates - found = [] - for group_name in items + extras: - group = get_group(group_name) - if not group: - continue - # ckeck if duplicate - if group['id'] in found: - continue - found.append(group['id']) - groups_data.append(group) - if len(groups_data) == count: - break - - return groups_data - - def get_site_statistics(self): - stats = {} - stats['dataset_count'] = p.toolkit.get_action('package_search')({}, {"rows": 1})['count'] - stats['group_count'] = len(p.toolkit.get_action('group_list')({}, {})) - stats['organization_count'] = len(p.toolkit.get_action('organization_list')({}, {})) - result =model.Session.execute( - '''select count(*) from related r - left join related_dataset rd on r.id = rd.related_id - where rd.status = 'active' or rd.id is null''').first()[0] - stats['related_count'] = len(p.toolkit.get_action('organization_list')({}, {})) - - return stats - - def get_helpers(self): - return { - 'get_featured_organizations': self.get_featured_organizations, - 'get_featured_groups': self.get_featured_groups, - 'get_site_statistics': self.get_site_statistics, - } diff --git a/doc/configuration.rst b/doc/configuration.rst index 089693d3fbd..3b2b5956153 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -810,6 +810,20 @@ Defines a list of group names or group ids. This setting is used to display groups and datasets from each group on the home page in the default templates (2 groups and 2 datasets for each group are displayed). +.. _ckan.featured_organizations: + +ckan.featured_orgs +^^^^^^^^^^^^^^^^^^^^ + +Example:: + + ckan.featured_orgs = org_one org_two + +Default Value: (empty) + +Defines a list of organization names or ids. This setting is used to display +organization and datasets from each group on the home page in the default +templates (2 groups and 2 datasets for each group are displayed). .. _ckan.gravatar_default: diff --git a/setup.py b/setup.py index 5245b96223d..3f5115bfa8f 100644 --- a/setup.py +++ b/setup.py @@ -129,7 +129,6 @@ [ckan.system_plugins] domain_object_mods = ckan.model.modification:DomainObjectModificationExtension - homepage = ckanext.homepage.plugin:HomepagePlugin [ckan.test_plugins] routes_plugin=tests.ckantestplugins:RoutesPlugin