Skip to content

Commit

Permalink
Merge pull request #1 from tum-gis/2.9-support
Browse files Browse the repository at this point in the history
ckan 2.9 support
  • Loading branch information
BWibo authored Apr 28, 2023
2 parents 6707ae0 + c7feedb commit 44502bf
Show file tree
Hide file tree
Showing 63 changed files with 13,649 additions and 416 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
*.swp
*egg-info
.DS_Store
8 changes: 5 additions & 3 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
recursive-include ckanext/hierarchy_g/public *
recursive-include ckanext/hierarchy_g/templates *
recursive-include ckanext/hierarchy_g/fanstatic *
recursive-include ckanext/grouphierarchy/fanstatic *
recursive-include ckanext/grouphierarchy/logic *
recursive-include ckanext/grouphierarchy/public *
recursive-include ckanext/grouphierarchy/templates *
recursive-include ckanext/grouphierarchy init_data.json
71 changes: 71 additions & 0 deletions ckanext/grouphierarchy/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import json
import click

import ckan.lib.navl.dictization_functions as dict_fns
import ckan.plugins.toolkit as tk
import ckan.model as model

from ckan.cli import error_shout


NotFound = tk.ObjectNotFound
NotAuthorized = tk.NotAuthorized
ValidationError = tk.ValidationError

HERE = os.path.dirname(__file__)
_site_url = tk.config.get('ckan.site_url')


@click.group("grouphierarchy", short_help="Grouphierarchy commands")
def grouphierarchy():
pass


@grouphierarchy.command("init_data")
def init_data():

data = []
# ckanext.grouphierarchy.init_data = example.json
# make sure the .json file is inside grouphierarchy directory,
# otherwise it won't work
filepath = tk.config.get("ckanext.grouphierarchy.init_data", None)
# if the .json file is not set in the .ini it would fall to the default one
if not filepath:
filepath = "init_data.json"

with open(os.path.join(HERE, filepath), encoding='utf-8') as f:
data = json.load(f)

user = tk.get_action('get_site_user')({'ignore_auth': True})

for group in data:
context = {
"model": model,
"session": model.Session,
"user": user['name'],
"return_id_only": True
}
if group.get('image_url'):
group['image_url'] = _site_url + group.get('image_url')

try:
tk.get_action('group_create')(context, group)
except (NotFound, NotAuthorized) as e:
error_shout(e)
raise click.Abort()
except dict_fns.DataError:
error_shout('Integrity Error')
raise click.Abort()
except ValidationError as e:
error_shout(e)
raise click.Abort()
click.secho(
"Successfully created the initial Groups",
fg="green",
bold=True
)


def get_commands():
return [grouphierarchy]
13 changes: 13 additions & 0 deletions ckanext/grouphierarchy/fanstatic/webassets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ghierarchy_js:
filter: rjsmin
output: ckanext-hierarchy/%(version)s_hierarchy.js
extra:
preload:
- base/main
contents:
- jquery.hierarchy.js
ghierarchy_css:
filters: cssrewrite
output: ckanext-hierarchy/%(version)s_hierarchy.css
contents:
- hierarchy_theme.css
197 changes: 88 additions & 109 deletions ckanext/grouphierarchy/helpers.py
Original file line number Diff line number Diff line change
@@ -1,152 +1,131 @@
from ckan.plugins import toolkit as tk
import ckan.model as model

def groups():
query = model.Group.all(group_type='group')
from ckan.plugins import toolkit as tk

from ckanext.hierarchy import helpers


def convert_to_dict(user):
out = {}
for k in ['id', 'name', 'title']:
out[k] = getattr(user, k)
return out
# def groups():
# query = model.Group.all(group_type='group')

out = map(convert_to_dict, query.all())
return out

# def convert_to_dict(user):
# out = {}
# for k in ['id', 'name', 'title']:
# out[k] = getattr(user, k)
# return out

# out = map(convert_to_dict, query.all())
# return out


def group_tree_g(organizations=[], type_="groups"):
full_tree_list = tk.get_action("group_tree_g")({}, {"type": type_})

def group_tree_g(organizations=[], type_='groups'):
full_tree_list = tk.get_action('group_tree_g')({}, {'type': type_})

if not organizations:
return full_tree_list
else:
filtered_tree_list = group_tree_filter_g(organizations, full_tree_list)
filtered_tree_list = helpers.group_tree_filter(organizations, full_tree_list)
return filtered_tree_list


def group_tree_section_g(id_, type_='groups', include_parents=True):
return tk.get_action('group_tree_section_g')(
{}, {'id': id_, 'type': type_, 'include_parents': include_parents})

def group_tree_crumbs_g(id_):
''' Returns list of dicts with
def group_tree_section_g(id_, type_="groups", include_parents=True):
return tk.get_action("group_tree_section")(
{}, {"id": id_, "type": type_, "include_parents": include_parents}
)


def group_tree_crumbs(id_):
"""Returns list of dicts with
+ either shortname (if available) or title (alternatively) and
+ id and
+ url
for <id_> and all parents.
'''
tree_node = tk.get_action('group_show')({},{'id':id_})
crumbs = [{'crumbname': tree_node.get('shortname') or tree_node.get('title'),
'id': id_,
'url': tk.url_for(controller='group',
action='read', id=id_)}]
if (tree_node['groups']):
id_parent = tree_node['groups'][0]['name']
return group_tree_crumbs_g(id_parent) + crumbs
"""
tree_node = tk.get_action("group_show")({}, {"id": id_})
crumbs = [
{
"crumbname": tree_node.get("shortname") or tree_node.get("title"),
"id": id_,
"url": tk.url_for(controller="group", action="read", id=id_),
}
]
if tree_node["groups"]:
id_parent = tree_node["groups"][0]["name"]
return group_tree_crumbs(id_parent) + crumbs
else:
return(crumbs)


def get_allowable_parent_groups_g(group_id):
if group_id:
group = model.Group.get(group_id)
allowable_parent_groups = group.groups_allowed_to_be_its_parent(type='group')
else:
allowable_parent_groups = model.Group.all(
group_type='group')
return allowable_parent_groups

return crumbs


def get_allowable_children_groups_g(group_id):
def get_allowable_children_groups(group_id):
if group_id:
group = model.Group.get(group_id)
if group == None:
return []
allowable_parent_groups = group.get_children_group_hierarchy_g(type='group')
allowable_parent_groups = group.get_children_group_hierarchy(type="group")
else:
allowable_parent_groups = model.Group.all(
group_type='group')
allowable_parent_groups = model.Group.all(group_type="group")
print(allowable_parent_groups)

return allowable_parent_groups


def get_selected_group(groups, parent_group):
"""Return a list of groups selected for a datase."""

# Helper function from
# https://github.com/datagovuk/ckanext-dgu/blob/5fb78b354517c2198245bdc9c98fb5d6c82c6bcc/ckanext/dgu/lib/helpers.py
# for speedier rendering of organization-tree
group_list = get_allowable_children_groups(parent_group)



def group_tree_filter_g(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
# organizations in the list are included
def traverse_select_highlighted_g(group_tree, selection=[], highlight=False):
# add highlighted branches to the filtered tree
if group_tree['highlighted']:
# add to the selection and remove highlighting if necessary
if highlight:
selection += [group_tree]
else:
selection += group_tree_highlight([], [group_tree])
else:
# check if there is any highlighted child tree
for child in group_tree.get('children', []):
traverse_select_highlighted_g(child, selection)

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_g(group, filtered_tree, highlight)

return filtered_tree


def get_selected_group (groups, parent_group):
'''Return a list of groups selected for a datase.'''

group_list = get_allowable_children_groups_g(parent_group)

group_name = []
for gr in group_list:
group_name.append(gr[1])
category_gr = []

category_gr = []
for name in group_name:
for selected_gr in groups:
print(selected_gr)
if selected_gr['name'] == name:
#category_gr.append(selected_gr['title'])
category_gr.append(selected_gr)
if selected_gr["name"] == name:
# category_gr.append(selected_gr['title'])
category_gr.append(selected_gr)

return category_gr


def group_tree_get_longname_g(id_, default="", type_='groups'):
tree_node = tk.get_action('group_show')({},{'id':id_})
longname = tree_node.get("longname", default)
if not longname:
return default
return longname


def group_tree_highlight_g(organizations, group_tree_list):

def traverse_highlight_g(group_tree, name_list):
if group_tree.get('name', "") in name_list:
group_tree['highlighted'] = True
else:
group_tree['highlighted'] = False
for child in group_tree.get('children', []):
traverse_highlight_g(child, name_list)

selected_names = [ o.get('name',None) for o in organizations]

for group in group_tree_list:
traverse_highlight_g(group, selected_names)
return group_tree_list
def group_tree_get_longname_g(id_, default="", type_="groups"):
tree_node = tk.get_action("group_show")({}, {"id": id_})
longname = tree_node.get("longname", default)
if not longname:
return default
return longname


def get_group_image(group_id):
if group_id:
group = model.Group.get(group_id)
return group.image_url
return


def get_recently_modified_group(_type):
num = int(tk.config.get("ckanext.grouphierachy.homepage.group_show", 4))

action = f'{_type}_list'
groups = tk.get_action(action)(
{},
{
"type": _type,
"sort": "package_count",
},
)

sorted_groups = []
_groups = []
if groups:
for group in groups:
groupobj = model.Session.query(model.Group).filter_by(name=group).first()
_groups.append(groupobj)
sorted_groups = sorted(
_groups,
key=lambda grp: [pkg.metadata_modified for pkg in grp.packages() if pkg],
reverse=True,
)
return sorted_groups[:num]
30 changes: 30 additions & 0 deletions ckanext/grouphierarchy/init_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{"title": "Hauptkategorien", "name": "main-categories"},
{"title": "Datensatz und Dokumente", "name": "dataset", "image_url": "/base/images/group_icons/dataset.jpg", "groups":[{"capacity": "public", "name": "main-categories"}]},
{"title": "Online-Dienst", "name": "online-service", "image_url": "/base/images/group_icons/online_service.png", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Projekt", "name": "project", "image_url": "/base/images/group_icons/project.png", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Software", "name": "software", "image_url": "/base/images/group_icons/software.png", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Online-Anwendung", "name": "online-application", "image_url": "/base/images/group_icons/online_application.svg", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Methode", "name": "method", "image_url": "/base/images/group_icons/method.png", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Gerät / Ding", "name": "device", "image_url": "/base/images/group_icons/device.svg", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Geoobjekt", "name": "geoobject", "image_url": "/base/images/group_icons/geoobject.svg", "groups": [{"capacity": "public", "name": "main-categories"}]},
{"title": "Digitaler Zwilling", "name": "digitaler-zwilling", "image_url": "/base/images/group_icons/dz-logo.jpg", "groups": [{"capacity": "public", "name": "main-categories"}]},

{"title": "Themen", "name": "topics"},
{"title": "Verwaltung", "name": "administration", "image_url": "/base/images/group_icons/administration.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Stadtplanung", "name": "urban-planning", "image_url": "/base/images/group_icons/urban_planning.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Umwelt", "name": "environment", "image_url": "/base/images/group_icons/environment.png", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Gesundheit", "name": "health", "image_url": "/base/images/group_icons/health.png", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Energie", "name": "energy", "image_url": "/base/images/group_icons/energy.png", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Informations-Technologie", "name": "information-technology", "image_url": "/base/images/group_icons/it-technology.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Tourismus & Freizeit", "name": "tourism", "image_url": "/base/images/group_icons/tourism.png", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Wohnen", "name": "living", "image_url": "/base/images/group_icons/living.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Bildung", "name": "education", "image_url": "/base/images/group_icons/education.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Handel", "name": "trade", "image_url": "/base/images/group_icons/trade.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Bauen", "name": "construction", "image_url": "/base/images/group_icons/construction.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Kultur", "name": "culture", "image_url": "/base/images/group_icons/culture.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Mobilität", "name": "mobility", "image_url": "/base/images/group_icons/mobility.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Landwirtschaft", "name": "agriculture", "image_url": "/base/images/group_icons/agriculture.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Gewerbe / Handwerk", "name": "craft", "image_url": "/base/images/group_icons/craft.svg", "groups": [{"capacity": "public", "name": "topics"}]},
{"title": "Arbeiten", "name": "work", "image_url": "/base/images/group_icons/work.svg", "groups": [{"capacity": "public", "name": "topics"}]}
]
Loading

0 comments on commit 44502bf

Please sign in to comment.