Commit 05ea22b2 authored by Jari Voutilainen's avatar Jari Voutilainen
Browse files

Apply flake8

parent 72b6dfdd
......@@ -2,6 +2,7 @@ import ckan.plugins as p
import ckan.model as model
from ckan.common import request
def group_tree(organizations=[], type_='organization'):
full_tree_list = p.toolkit.get_action('group_tree')({}, {'type': type_})
......@@ -14,7 +15,7 @@ def group_tree(organizations=[], type_='organization'):
def group_tree_filter(organizations, group_tree_list, highlight=False):
# this method leaves only the sections of the tree corresponding to the list
# since it was developed for the users, all children organizations from the
# since it was developed for the users, all children organizations from the
# organizations in the list are included
def traverse_select_highlighted(group_tree, selection=[], highlight=False):
# add highlighted branches to the filtered tree
......@@ -29,7 +30,7 @@ def group_tree_filter(organizations, group_tree_list, highlight=False):
for child in group_tree.get('children', []):
traverse_select_highlighted(child, selection)
filtered_tree=[]
filtered_tree = []
# first highlights all the organizations from the list in the three
for group in group_tree_highlight(organizations, group_tree_list):
traverse_select_highlighted(group, filtered_tree, highlight)
......@@ -39,26 +40,28 @@ def group_tree_filter(organizations, group_tree_list, highlight=False):
def group_tree_section(id_, type_='organization', include_parents=True, include_siblings=True):
return p.toolkit.get_action('group_tree_section')(
{'include_parents':include_parents, 'include_siblings':include_siblings}, {'id': id_, 'type': type_,})
{'include_parents': include_parents, 'include_siblings': include_siblings}, {'id': id_, 'type': type_, })
def group_tree_parents(id_, type_='organization'):
tree_node = p.toolkit.get_action('organization_show')({},{'id':id_})
if (tree_node['groups']):
parent_id = tree_node['groups'][0]['name']
parent_node = p.toolkit.get_action('organization_show')({},{'id':parent_id})
return group_tree_parents(parent_id) + [parent_node]
else:
return []
tree_node = p.toolkit.get_action('organization_show')({}, {'id': id_})
if (tree_node['groups']):
parent_id = tree_node['groups'][0]['name']
parent_node = p.toolkit.get_action('organization_show')({}, {'id': parent_id})
return group_tree_parents(parent_id) + [parent_node]
else:
return []
def group_tree_get_longname(id_, default="", type_='organization'):
tree_node = p.toolkit.get_action('organization_show')({},{'id':id_})
longname = tree_node.get("longname", default)
if not longname:
return default
return longname
tree_node = p.toolkit.get_action('organization_show')({}, {'id': id_})
longname = tree_node.get("longname", default)
if not longname:
return default
return longname
def group_tree_highlight(organizations, group_tree_list):
def group_tree_highlight(organizations, group_tree_list):
def traverse_highlight(group_tree, name_list):
if group_tree.get('name', "") in name_list:
group_tree['highlighted'] = True
......@@ -67,13 +70,14 @@ def group_tree_highlight(organizations, group_tree_list):
for child in group_tree.get('children', []):
traverse_highlight(child, name_list)
selected_names = [ o.get('name',None) for o in organizations]
selected_names = [o.get('name', None) for o in organizations]
print(selected_names)
for group in group_tree_list:
traverse_highlight(group, selected_names)
return group_tree_list
def get_allowable_parent_groups(group_id):
if group_id:
group = model.Group.get(group_id)
......@@ -84,9 +88,9 @@ def get_allowable_parent_groups(group_id):
group_type='organization')
return allowable_parent_groups
def is_include_children_selected(fields):
include_children_selected = False
if request.params.get('include_children'):
include_children_selected = True
return include_children_selected
......@@ -38,7 +38,7 @@ def group_tree_section(context, data_dict):
group_type = data_dict.get('type', 'group')
if group.type != group_type:
how_type_was_set = 'was specified' if data_dict.get('type') \
else 'is filtered by default'
else 'is filtered by default'
raise p.toolkit.ValidationError(
'Group type is "%s" not "%s" that %s' %
(group.type, group_type, how_type_was_set))
......@@ -48,15 +48,16 @@ def group_tree_section(context, data_dict):
root_group = (group.get_parent_group_hierarchy(type=group_type) or [group])[0]
else:
root_group = group
if include_siblings or root_group==group:
if include_siblings or root_group == group:
return _group_tree_branch(root_group, highlight_group_name=group.name,
type=group_type)
else:
section_subtree = _group_tree_branch(group, highlight_group_name=group.name,
type=group_type)
return _nest_group_tree_list(group.get_parent_group_hierarchy(type=group_type),
return _nest_group_tree_list(group.get_parent_group_hierarchy(type=group_type),
section_subtree)
def _nest_group_tree_list(group_tree_list, group_tree_leaf):
'''Returns a tree branch composed by nesting the groups in the list.
......@@ -69,9 +70,9 @@ def _nest_group_tree_list(group_tree_list, group_tree_leaf):
for group in group_tree_list:
log.debug(group)
node = GroupTreeNode(
{'id': group.id,
'name': group.name,
'title': group.title})
{'id': group.id,
'name': group.name,
'title': group.title})
if not root_node:
root_node = last_node = node
else:
......
......@@ -2,9 +2,7 @@ import ckan.plugins as p
from ckanext.hierarchy.logic import action
from ckanext.hierarchy import helpers
from ckan.lib.plugins import DefaultOrganizationForm
from ckan.lib.plugins import DefaultGroupForm
import ckan.logic.schema as s
from ckan.common import c, request
from ckan.common import c
import logging
import re
......@@ -13,8 +11,8 @@ log = logging.getLogger(__name__)
# This plugin is designed to work only these versions of CKAN
p.toolkit.check_ckan_version(min_version='2.0')
def custom_convert_from_extras(key, data, errors, context):
def custom_convert_from_extras(key, data, errors, context):
'''Converts values from extras, tailored for groups.'''
# Set to empty string to remove Missing objects
......@@ -24,18 +22,18 @@ def custom_convert_from_extras(key, data, errors, context):
for data_key in data.keys():
if (data_key[0] == 'extras'):
data_value = data[data_key]
if( 'key' in data_value and data_value['key'] == key[-1]):
data[key] = data_value['value']
to_remove.append(data_key)
break
if ('key' in data_value and data_value['key'] == key[-1]):
data[key] = data_value['value']
to_remove.append(data_key)
break
else:
return
for remove_key in to_remove:
del data[remove_key]
class HierarchyDisplay(p.SingletonPlugin):
class HierarchyDisplay(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IActions, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True)
......@@ -67,7 +65,6 @@ class HierarchyDisplay(p.SingletonPlugin):
'is_include_children_selected': helpers.is_include_children_selected,
}
# IPackageController
# Modify the search query to include the datasets from
# the children organizations in the result list
......@@ -88,22 +85,21 @@ class HierarchyDisplay(p.SingletonPlugin):
# fix the issues with multiple times repeated fields
# remove the param from the fields
new_fields = set()
for field,value in c.fields:
for field, value in c.fields:
if (field != 'include_children'):
new_fields.add((field,value))
new_fields.add((field, value))
c.fields = list(new_fields)
# parse the query string to check if children are requested
if query:
base_query = []
# remove whitespaces between fields and values
query = re.sub(': +', ':', query)
query = re.sub(': +', ':', query)
for item in query.split(' '):
field = item.split(':')[0]
value = item.split(':')[-1]
# skip organization
# skip organization
if (field == 'owner_org'):
org_id = value
continue
# skip include children andset option value
if (field == 'include_children'):
......@@ -113,27 +109,27 @@ class HierarchyDisplay(p.SingletonPlugin):
base_query += [item]
if c.include_children_selected:
# add all the children organizations in an 'or' join
children = _children_name_list(helpers.group_tree_section(c.group_dict.get('id'), include_parents=False, include_siblings=False).get('children',[]))
if(children):
children = _children_name_list(
helpers.group_tree_section(c.group_dict.get('id'), include_parents=False, include_siblings=False).get(
'children', []))
if (children):
search_params['q'] = " ".join(base_query)
if (len(search_params['q'].strip())>0):
if (len(search_params['q'].strip()) > 0):
search_params['q'] += ' AND '
search_params['q'] += '(organization:%s' % c.group_dict.get('name')
for name in children:
if name:
search_params['q'] += ' OR organization:%s' % name
search_params['q'] += ' OR organization:%s' % name
search_params['q'] += ")"
# add it back to fields
c.fields += [('include_children','True')]
c.fields += [('include_children', 'True')]
return search_params
class HierarchyForm(p.SingletonPlugin, DefaultOrganizationForm):
p.implements(p.IGroupForm, inherit=True)
# IGroupForm
def group_types(self):
......
from setuptools import setup, find_packages
import sys, os
version = '0.1'
setup(
name='ckanext-hierarchy',
version=version,
description="CKAN Organization hierarchy - templates and configuration",
long_description="""\
""",
classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords='',
author='David Read',
author_email='david.read@hackneyworkshop.com',
url='',
license='Affero General Public License (AGPL)',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
namespace_packages=['ckanext', 'ckanext.hierarchy'],
include_package_data=True,
zip_safe=False,
install_requires=[
# -*- Extra requirements: -*-
],
entry_points=\
"""
[ckan.plugins]
hierarchy_display=ckanext.hierarchy.plugin:HierarchyDisplay
hierarchy_form=ckanext.hierarchy.plugin:HierarchyForm
""",
name='ckanext-hierarchy',
version=version,
description="CKAN Organization hierarchy - templates and configuration",
long_description="""\
""",
classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords='',
author='David Read',
author_email='david.read@hackneyworkshop.com',
url='',
license='Affero General Public License (AGPL)',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
namespace_packages=['ckanext', 'ckanext.hierarchy'],
include_package_data=True,
zip_safe=False,
install_requires=[
# -*- Extra requirements: -*-
],
entry_points="""
[ckan.plugins]
hierarchy_display=ckanext.hierarchy.plugin:HierarchyDisplay
hierarchy_form=ckanext.hierarchy.plugin:HierarchyForm
""",
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment