Skip to content

Commit

Permalink
compatibility with ckan-2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-iv committed Jul 19, 2024
1 parent 056ef39 commit a8b1df8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 28 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Tests](https://github.com/DataShades/ckanext-relationship/workflows/Tests/badge.svg)](https://github.com/DataShades/ckanext-relationship/actions/workflows/test.yml)
[![Tests](https://github.com/DataShades/ckanext-relationship/actions/workflows/test.yml/badge.svg)](https://github.com/DataShades/ckanext-relationship/actions/workflows/test.yml)

# ckanext-relationship

Expand Down Expand Up @@ -44,12 +44,10 @@ If your extension works across different versions you can add the following tabl
Compatibility with core CKAN versions:
| CKAN version | Compatible? |
| --------------- | ------------- |
| 2.6 and earlier | not tested |
| 2.7 | not tested |
| 2.8 | not tested |
| 2.9 | not tested |
| CKAN version | Compatible? |
|-----------------|-------------|
| 2.9 | yes |
| 2.10 | yes |
Suggested values:
Expand Down
17 changes: 17 additions & 0 deletions ckanext/relationship/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

import ckan.plugins.toolkit as tk

CONFIG_VIEWS_WITHOUT_RELATIONSHIPS = (
"ckanext.relationship.views_without_relationships_in_package_show"
)
DEFAULT_VIEWS_WITHOUT_RELATIONSHIPS = ["search", "read"]


def views_without_relationships_in_package_show() -> list[str]:
return tk.aslist(
tk.config.get(
CONFIG_VIEWS_WITHOUT_RELATIONSHIPS,
DEFAULT_VIEWS_WITHOUT_RELATIONSHIPS,
),
)
31 changes: 11 additions & 20 deletions ckanext/relationship/logic/action.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

from typing import Any

from flask import jsonify
from sqlalchemy import or_

import ckan.plugins.toolkit as tk
from ckan import authz, logic
from ckan.logic import validate
from ckan.types import Action, Context, DataDict

from ckanext.relationship import utils
from ckanext.relationship.config import views_without_relationships_in_package_show
from ckanext.relationship.logic import schema
from ckanext.relationship.model.relationship import Relationship
from ckanext.relationship.utils import entity_name_by_id
Expand All @@ -29,10 +31,7 @@ def get_actions():


@validate(schema.relation_create)
def relationship_relation_create(
context: Context,
data_dict: DataDict,
) -> list[dict[str, str]]:
def relationship_relation_create(context, data_dict) -> list[dict[str, str]]:
"""Create relation with specified type (relation_type) between two entities
specified by ids (subject_id, object_id). Also create reverse relation."""
tk.check_access("relationship_relation_create", context, data_dict)
Expand Down Expand Up @@ -64,10 +63,7 @@ def relationship_relation_create(


@validate(schema.relation_delete)
def relationship_relation_delete(
context: Context,
data_dict: DataDict,
) -> list[dict[str, str]]:
def relationship_relation_delete(context, data_dict) -> list[dict[str, str]]:
"""Delete relation with specified type (relation_type) between two entities
specified by ids (subject_id, object_id). Also delete reverse relation."""
tk.check_access("relationship_relation_delete", context, data_dict)
Expand Down Expand Up @@ -128,10 +124,7 @@ def relationship_relation_delete(


@validate(schema.relations_list)
def relationship_relations_list(
context: Context,
data_dict: DataDict,
) -> list[dict[str, str]]:
def relationship_relations_list(context, data_dict) -> list[dict[str, str]]:
"""Return a list of dictionaries representing the relations of a specified entity
(object_entity, object_type) related to the specified type of relation
(relation_type) with an entity specified by its id (subject_id).
Expand All @@ -158,7 +151,7 @@ def relationship_relations_list(


@validate(schema.relations_ids_list)
def relationship_relations_ids_list(context: Context, data_dict: DataDict) -> list[str]:
def relationship_relations_ids_list(context, data_dict) -> list[str]:
"""Return ids list of specified entity (object_entity, object_type) related
with specified type of relation (relation_type) with entity specified
by id (subject_id).
Expand All @@ -171,7 +164,7 @@ def relationship_relations_ids_list(context: Context, data_dict: DataDict) -> li


@validate(schema.get_entity_list)
def relationship_get_entity_list(context: Context, data_dict: DataDict) -> list[str]:
def relationship_get_entity_list(context, data_dict) -> list[str]:
"""Return ids list of specified entity (entity, entity_type)"""
tk.check_access("relationship_get_entity_list", context, data_dict)

Expand All @@ -191,7 +184,7 @@ def relationship_get_entity_list(context: Context, data_dict: DataDict) -> list[


@validate(schema.autocomplete)
def relationship_autocomplete(context: Context, data_dict: DataDict) -> DataDict:
def relationship_autocomplete(context, data_dict) -> dict[str, Any]:
fq = f'type:{data_dict["entity_type"]} -id:{data_dict["current_entity_id"]}'

if data_dict.get("owned_only") and not (
Expand Down Expand Up @@ -229,15 +222,13 @@ def relationship_autocomplete(context: Context, data_dict: DataDict) -> DataDict

@tk.chained_action
@tk.side_effect_free
def package_show(next_: Action, context: Context, data_dict: DataDict) -> DataDict:
def package_show(next_, context, data_dict) -> dict[str, Any]:
result = next_(context, data_dict)

pkg_id = result["id"]
pkg_type = result["type"]

views_without_relationships = tk.config[
"ckanext.relationship.views_without_relationships_in_package_show"
]
views_without_relationships = views_without_relationships_in_package_show()

if (
tk.get_endpoint()[1] in views_without_relationships
Expand Down
13 changes: 13 additions & 0 deletions ckanext/relationship/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ def before_dataset_index(self, pkg_dict):

return pkg_dict

# CKAN < 2.10 hooks
def after_create(self, context, data_dict):
return self.after_dataset_create(context, data_dict)

def after_update(self, context, data_dict):
return self.after_dataset_update(context, data_dict)

def after_delete(self, context, data_dict):
return self.after_dataset_delete(context, data_dict)

def before_index(self, pkg_dict):
return self.before_dataset_index(pkg_dict)


if tk.check_ckan_version("2.10"):
tk.blanket.config_declarations(RelationshipPlugin)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# http://packaging.python.org/en/latest/tutorial.html#version
version="0.2.8",
version="0.2.9",
description="""""",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit a8b1df8

Please sign in to comment.