Commit 600e2960 authored by David Read's avatar David Read
Browse files

Merge branch 'apply_flake8' of git://github.com/vrk-kpa/ckanext-hierarchy into vrk-kpa-apply_flake8

parents 4cb5c78d 05ea22b2
...@@ -4,6 +4,7 @@ import ckan.plugins as p ...@@ -4,6 +4,7 @@ import ckan.plugins as p
import ckan.model as model import ckan.model as model
from ckan.common import request from ckan.common import request
def group_tree(organizations=[], type_='organization'): def group_tree(organizations=[], type_='organization'):
full_tree_list = p.toolkit.get_action('group_tree')({}, {'type': type_}) full_tree_list = p.toolkit.get_action('group_tree')({}, {'type': type_})
...@@ -31,7 +32,7 @@ def group_tree_filter(organizations, group_tree_list, highlight=False): ...@@ -31,7 +32,7 @@ def group_tree_filter(organizations, group_tree_list, highlight=False):
for child in group_tree.get('children', []): for child in group_tree.get('children', []):
traverse_select_highlighted(child, selection) traverse_select_highlighted(child, selection)
filtered_tree=[] filtered_tree = []
# first highlights all the organizations from the list in the three # first highlights all the organizations from the list in the three
for group in group_tree_highlight(organizations, group_tree_list): for group in group_tree_highlight(organizations, group_tree_list):
traverse_select_highlighted(group, filtered_tree, highlight) traverse_select_highlighted(group, filtered_tree, highlight)
...@@ -41,26 +42,28 @@ def group_tree_filter(organizations, group_tree_list, highlight=False): ...@@ -41,26 +42,28 @@ def group_tree_filter(organizations, group_tree_list, highlight=False):
def group_tree_section(id_, type_='organization', include_parents=True, include_siblings=True): def group_tree_section(id_, type_='organization', include_parents=True, include_siblings=True):
return p.toolkit.get_action('group_tree_section')( 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'): def group_tree_parents(id_, type_='organization'):
tree_node = p.toolkit.get_action('organization_show')({},{'id':id_}) tree_node = p.toolkit.get_action('organization_show')({}, {'id': id_})
if (tree_node['groups']): if (tree_node['groups']):
parent_id = tree_node['groups'][0]['name'] parent_id = tree_node['groups'][0]['name']
parent_node = p.toolkit.get_action('organization_show')({},{'id':parent_id}) parent_node = p.toolkit.get_action('organization_show')({}, {'id': parent_id})
return group_tree_parents(parent_id) + [parent_node] return group_tree_parents(parent_id) + [parent_node]
else: else:
return [] return []
def group_tree_get_longname(id_, default="", type_='organization'): def group_tree_get_longname(id_, default="", type_='organization'):
tree_node = p.toolkit.get_action('organization_show')({},{'id':id_}) tree_node = p.toolkit.get_action('organization_show')({}, {'id': id_})
longname = tree_node.get("longname", default) longname = tree_node.get("longname", default)
if not longname: if not longname:
return default return default
return longname 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): def traverse_highlight(group_tree, name_list):
if group_tree.get('name', "") in name_list: if group_tree.get('name', "") in name_list:
group_tree['highlighted'] = True group_tree['highlighted'] = True
...@@ -69,13 +72,14 @@ def group_tree_highlight(organizations, group_tree_list): ...@@ -69,13 +72,14 @@ def group_tree_highlight(organizations, group_tree_list):
for child in group_tree.get('children', []): for child in group_tree.get('children', []):
traverse_highlight(child, name_list) 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) print(selected_names)
for group in group_tree_list: for group in group_tree_list:
traverse_highlight(group, selected_names) traverse_highlight(group, selected_names)
return group_tree_list return group_tree_list
def get_allowable_parent_groups(group_id): def get_allowable_parent_groups(group_id):
if group_id: if group_id:
group = model.Group.get(group_id) group = model.Group.get(group_id)
...@@ -86,6 +90,7 @@ def get_allowable_parent_groups(group_id): ...@@ -86,6 +90,7 @@ def get_allowable_parent_groups(group_id):
group_type='organization') group_type='organization')
return allowable_parent_groups return allowable_parent_groups
def is_include_children_selected(fields): def is_include_children_selected(fields):
include_children_selected = False include_children_selected = False
if request.params.get('include_children'): if request.params.get('include_children'):
......
...@@ -38,7 +38,7 @@ def group_tree_section(context, data_dict): ...@@ -38,7 +38,7 @@ def group_tree_section(context, data_dict):
group_type = data_dict.get('type', 'group') group_type = data_dict.get('type', 'group')
if group.type != group_type: if group.type != group_type:
how_type_was_set = 'was specified' if data_dict.get('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( raise p.toolkit.ValidationError(
'Group type is "%s" not "%s" that %s' % 'Group type is "%s" not "%s" that %s' %
(group.type, group_type, how_type_was_set)) (group.type, group_type, how_type_was_set))
...@@ -48,15 +48,16 @@ def group_tree_section(context, data_dict): ...@@ -48,15 +48,16 @@ def group_tree_section(context, data_dict):
root_group = (group.get_parent_group_hierarchy(type=group_type) or [group])[0] root_group = (group.get_parent_group_hierarchy(type=group_type) or [group])[0]
else: else:
root_group = group 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, return _group_tree_branch(root_group, highlight_group_name=group.name,
type=group_type) type=group_type)
else: else:
section_subtree = _group_tree_branch(group, highlight_group_name=group.name, section_subtree = _group_tree_branch(group, highlight_group_name=group.name,
type=group_type) 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) section_subtree)
def _nest_group_tree_list(group_tree_list, group_tree_leaf): def _nest_group_tree_list(group_tree_list, group_tree_leaf):
'''Returns a tree branch composed by nesting the groups in the list. '''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): ...@@ -69,9 +70,9 @@ def _nest_group_tree_list(group_tree_list, group_tree_leaf):
for group in group_tree_list: for group in group_tree_list:
log.debug(group) log.debug(group)
node = GroupTreeNode( node = GroupTreeNode(
{'id': group.id, {'id': group.id,
'name': group.name, 'name': group.name,
'title': group.title}) 'title': group.title})
if not root_node: if not root_node:
root_node = last_node = node root_node = last_node = node
else: else:
......
...@@ -16,8 +16,8 @@ log = logging.getLogger(__name__) ...@@ -16,8 +16,8 @@ log = logging.getLogger(__name__)
# This plugin is designed to work only these versions of CKAN # This plugin is designed to work only these versions of CKAN
p.toolkit.check_ckan_version(min_version='2.0') 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.''' '''Converts values from extras, tailored for groups.'''
# Set to empty string to remove Missing objects # Set to empty string to remove Missing objects
...@@ -27,18 +27,18 @@ def custom_convert_from_extras(key, data, errors, context): ...@@ -27,18 +27,18 @@ def custom_convert_from_extras(key, data, errors, context):
for data_key in data.keys(): for data_key in data.keys():
if (data_key[0] == 'extras'): if (data_key[0] == 'extras'):
data_value = data[data_key] data_value = data[data_key]
if( 'key' in data_value and data_value['key'] == key[-1]): if ('key' in data_value and data_value['key'] == key[-1]):
data[key] = data_value['value'] data[key] = data_value['value']
to_remove.append(data_key) to_remove.append(data_key)
break break
else: else:
return return
for remove_key in to_remove: for remove_key in to_remove:
del data[remove_key] del data[remove_key]
class HierarchyDisplay(p.SingletonPlugin):
class HierarchyDisplay(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True) p.implements(p.IConfigurer, inherit=True)
p.implements(p.IActions, inherit=True) p.implements(p.IActions, inherit=True)
p.implements(p.ITemplateHelpers, inherit=True) p.implements(p.ITemplateHelpers, inherit=True)
...@@ -70,7 +70,6 @@ class HierarchyDisplay(p.SingletonPlugin): ...@@ -70,7 +70,6 @@ class HierarchyDisplay(p.SingletonPlugin):
'is_include_children_selected': helpers.is_include_children_selected, 'is_include_children_selected': helpers.is_include_children_selected,
} }
# IPackageController # IPackageController
def before_search(self, search_params): def before_search(self, search_params):
...@@ -95,9 +94,9 @@ class HierarchyDisplay(p.SingletonPlugin): ...@@ -95,9 +94,9 @@ class HierarchyDisplay(p.SingletonPlugin):
# Remove the param from the fields - NB no longer works # Remove the param from the fields - NB no longer works
# e.g. [('include_children', 'True')] # e.g. [('include_children', 'True')]
new_fields = set() new_fields = set()
for field,value in c.fields: for field, value in c.fields:
if (field != 'include_children'): if (field != 'include_children'):
new_fields.add((field,value)) new_fields.add((field, value))
c.fields = list(new_fields) c.fields = list(new_fields)
# parse the query string to check if children are requested # parse the query string to check if children are requested
...@@ -140,10 +139,8 @@ class HierarchyDisplay(p.SingletonPlugin): ...@@ -140,10 +139,8 @@ class HierarchyDisplay(p.SingletonPlugin):
class HierarchyForm(p.SingletonPlugin, DefaultOrganizationForm): class HierarchyForm(p.SingletonPlugin, DefaultOrganizationForm):
p.implements(p.IGroupForm, inherit=True) p.implements(p.IGroupForm, inherit=True)
# IGroupForm # IGroupForm
def group_types(self): def group_types(self):
......
from setuptools import setup, find_packages from setuptools import setup, find_packages
import sys, os
version = '0.1' version = '0.1'
......
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