diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3e77ad0..caf11db 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,30 +9,24 @@ repos: hooks: # - id: check-yaml - id: end-of-file-fixer - stages: [commit] - - id: trailing-whitespace - stages: [commit] - - id: debug-statements - stages: [push] - -## Isort -- repo: https://github.com/pycqa/isort - rev: 5.12.0 - hooks: - - id: isort - name: isort stages: [pre-commit] - -## Black -- repo: https://github.com/psf/black - rev: 23.3.0 - hooks: - - id: black + - id: trailing-whitespace stages: [pre-commit] + - id: debug-statements + stages: [pre-push] ## Ruff - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: v0.0.260 + rev: v0.5.0 hooks: - id: ruff + args: [--fix] + stages: [pre-commit] + - id: ruff-format stages: [pre-commit] + +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: check-merge-conflict + args: [--assume-in-merge] diff --git a/ckanext/relationship/helpers.py b/ckanext/relationship/helpers.py index bda4452..eae4935 100644 --- a/ckanext/relationship/helpers.py +++ b/ckanext/relationship/helpers.py @@ -19,7 +19,7 @@ def get_helpers(): def relationship_get_entity_list(entity, entity_type, include_private=True): - """Return ids list of specified entity (entity, entity_type)""" + """Return ids list of specified entity (entity, entity_type).""" context = {} if entity == "package": entity_list = tk.get_action("package_search")( @@ -87,7 +87,7 @@ def relationship_get_selected_json(selected_ids: list | None = None) -> str: try: pkg_dict = tk.get_action("package_show")({}, {"id": pkg_id}) selected_pkgs.append({"name": pkg_dict["id"], "title": pkg_dict["title"]}) - except Exception: + except (tk.ObjectNotFound, tk.NotAuthorized): continue return json.dumps(selected_pkgs) diff --git a/ckanext/relationship/logic/action.py b/ckanext/relationship/logic/action.py index aeb22d3..cb8a161 100644 --- a/ckanext/relationship/logic/action.py +++ b/ckanext/relationship/logic/action.py @@ -33,7 +33,8 @@ def get_actions(): @validate(schema.relation_create) 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.""" + specified by ids (subject_id, object_id). Also create reverse relation. + """ tk.check_access("relationship_relation_create", context, data_dict) subject_id = data_dict["subject_id"] @@ -65,7 +66,8 @@ def relationship_relation_create(context, data_dict) -> list[dict[str, str]]: @validate(schema.relation_delete) 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.""" + specified by ids (subject_id, object_id). Also delete reverse relation. + """ tk.check_access("relationship_relation_delete", context, data_dict) subject_id = data_dict["subject_id"] @@ -165,7 +167,7 @@ def relationship_relations_ids_list(context, data_dict) -> list[str]: @validate(schema.get_entity_list) def relationship_get_entity_list(context, data_dict) -> list[str]: - """Return ids list of specified entity (entity, entity_type)""" + """Return ids list of specified entity (entity, entity_type).""" tk.check_access("relationship_get_entity_list", context, data_dict) model = context["model"] diff --git a/ckanext/relationship/logic/auth.py b/ckanext/relationship/logic/auth.py index 76cda8a..44a79f2 100644 --- a/ckanext/relationship/logic/auth.py +++ b/ckanext/relationship/logic/auth.py @@ -1,5 +1,8 @@ from __future__ import annotations +from typing import Any + +from ckan import types from ckan.plugins import toolkit as tk @@ -15,29 +18,32 @@ def get_auth_functions(): return {f.__name__: f for f in auth_functions} -def relationship_relation_create(context, data_dict): +def relationship_relation_create(context: types.Context, data_dict: dict[str, Any]): return {"success": True} -def relationship_relation_delete(context, data_dict): +def relationship_relation_delete(context: types.Context, data_dict: dict[str, Any]): return {"success": True} @tk.auth_allow_anonymous_access -def relationship_relations_list(context, data_dict): +def relationship_relations_list(context: types.Context, data_dict: dict[str, Any]): return {"success": True} @tk.auth_allow_anonymous_access -def relationship_relations_ids_list(context, data_dict): +def relationship_relations_ids_list(context: types.Context, data_dict: dict[str, Any]): return {"success": True} @tk.auth_allow_anonymous_access -def relationship_get_entity_list(context, data_dict): +def relationship_get_entity_list(context: types.Context, data_dict: dict[str, Any]): return {"success": True} @tk.auth_allow_anonymous_access -def relationship_relationship_autocomplete(context, data_dict): +def relationship_relationship_autocomplete( + context: types.Context, + data_dict: dict[str, Any], +): return {"success": True} diff --git a/ckanext/relationship/logic/validators.py b/ckanext/relationship/logic/validators.py index 24f3b3d..cf12065 100644 --- a/ckanext/relationship/logic/validators.py +++ b/ckanext/relationship/logic/validators.py @@ -39,8 +39,7 @@ def validator(key, data, errors, context): ) selected_relations = get_selected_relations(data[key]) - - data[key] = json.dumps([value for value in selected_relations]) + data[key] = json.dumps(list(selected_relations)) add_relations = selected_relations - current_relations del_relations = current_relations - selected_relations @@ -76,7 +75,7 @@ def get_current_relations( return set(current_relations) -def get_selected_relations(selected_relations): +def get_selected_relations(selected_relations: list | str | None) -> set[str]: if isinstance(selected_relations, string_types) and "," in selected_relations: selected_relations = selected_relations.split(",") diff --git a/ckanext/relationship/migration/relationship/env.py b/ckanext/relationship/migration/relationship/env.py index 2d1f539..25ba762 100644 --- a/ckanext/relationship/migration/relationship/env.py +++ b/ckanext/relationship/migration/relationship/env.py @@ -38,7 +38,6 @@ def run_migrations_offline(): script output. """ - url = config.get_main_option("sqlalchemy.url") context.configure( url=url, diff --git a/ckanext/relationship/migration/relationship/versions/520e6ea9f57c_create_relationship_table.py b/ckanext/relationship/migration/relationship/versions/520e6ea9f57c_create_relationship_table.py index 103f286..770a23f 100644 --- a/ckanext/relationship/migration/relationship/versions/520e6ea9f57c_create_relationship_table.py +++ b/ckanext/relationship/migration/relationship/versions/520e6ea9f57c_create_relationship_table.py @@ -1,4 +1,4 @@ -"""Create relationship table +"""Create relationship table. Revision ID: aca2ff1d3ce4 Revises: diff --git a/ckanext/relationship/model/relationship.py b/ckanext/relationship/model/relationship.py index fa3ca50..a938cb6 100644 --- a/ckanext/relationship/model/relationship.py +++ b/ckanext/relationship/model/relationship.py @@ -92,10 +92,7 @@ def by_subject_id( def _entity_name_by_id(entity_id): - """ - Returns entity (package or organization or group) name by its id - """ - + """Returns entity (package or organization or group) name by its id.""" pkg = ( model.Session.query(model.Package) .filter(model.Package.id == entity_id) diff --git a/ckanext/relationship/plugin.py b/ckanext/relationship/plugin.py index 09d3403..120787b 100644 --- a/ckanext/relationship/plugin.py +++ b/ckanext/relationship/plugin.py @@ -1,10 +1,10 @@ import ckan.plugins.toolkit as tk from ckan import plugins +from ckan.common import CKANConfig from ckan.lib.search import rebuild from ckan.logic import NotFound import ckanext.scheming.helpers as sch - from ckanext.relationship import helpers, utils, views from ckanext.relationship.logic import action, auth, validators @@ -19,7 +19,7 @@ class RelationshipPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IPackageController, inherit=True) # IConfigurer - def update_config(self, config_): + def update_config(self, config_: CKANConfig): tk.add_template_directory(config_, "templates") tk.add_public_directory(config_, "public") tk.add_resource("assets", "relationship") diff --git a/ckanext/relationship/tests/test_plugin.py b/ckanext/relationship/tests/test_plugin.py index 05bd059..017dcd3 100644 --- a/ckanext/relationship/tests/test_plugin.py +++ b/ckanext/relationship/tests/test_plugin.py @@ -1,5 +1,4 @@ -""" -Tests for plugin.py. +"""Tests for plugin.py. Tests are written using the pytest library (https://docs.pytest.org), and you should read the testing guidelines in the CKAN docs: diff --git a/requirements.txt b/requirements.txt index 10ef97e..e69de29 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +0,0 @@ -ckanext-scheming \ No newline at end of file diff --git a/setup.py b/setup.py index cae381c..7ffbc01 100644 --- a/setup.py +++ b/setup.py @@ -1,81 +1,6 @@ -# Always prefer setuptools over distutils -from codecs import open # To use a consistent encoding -from os import path - -from setuptools import find_packages, setup - -here = path.abspath(path.dirname(__file__)) - -# Get the long description from the relevant file -with open(path.join(here, "README.md"), encoding="utf-8") as f: - long_description = f.read() +from setuptools import setup setup( - name="""ckanext-relationship""", - # 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.9", - description="""""", - long_description=long_description, - long_description_content_type="text/markdown", - # The project's main homepage. - url="https://github.com/DataShades/ckanext-relationship", - # Author details - author="""Oleksandr Ivaniuk""", - author_email="""aleks.ivaniuk@gmail.com""", - # Choose your license - license="AGPL", - # See https://pypi.python.org/pypi?%3Aaction=list_classifiers - classifiers=[ - # How mature is this project? Common values are - # 3 - Alpha - # 4 - Beta - # 5 - Production/Stable - "Development Status :: 4 - Beta", - # Pick your license as you wish (should match "license" above) - ( - "License :: OSI Approved :: GNU Affero General Public License v3" - " or later (AGPLv3+)" - ), - # Specify the Python versions you support here. In particular, ensure - # that you indicate whether you support Python 2, Python 3 or both. - "Programming Language :: Python :: 2.7", - ], - # What does your project relate to? - keywords="""CKAN""", - # You can just specify the packages manually here if your project is - # simple. Or you can use find_packages(). - packages=find_packages(exclude=["contrib", "docs", "tests*"]), - namespace_packages=["ckanext"], - install_requires=[ - # CKAN extensions should not list dependencies here, but in a separate - # ``requirements.txt`` file. - # - # http://docs.ckan.org/en/latest/extensions/best-practices.html - # add-third-party-libraries-to-requirements-txt - ], - # If there are data files included in your packages that need to be - # installed, specify them here. If using Python 2.6 or less, then these - # have to be included in MANIFEST.in as well. - include_package_data=True, - package_data={}, - # Although 'package_data' is the preferred approach, in some case you may - # need to place data files outside of your packages. - # see http://docs.python.org/3.4/distutils/setupscript.html - # installing-additional-files - # In this case, 'data_file' will be installed into '/my_data' - data_files=[], - # To provide executable scripts, use entry points in preference to the - # "scripts" keyword. Entry points provide cross-platform support and allow - # pip to create the appropriate form of executable for the target platform. - entry_points=""" - [ckan.plugins] - relationship=ckanext.relationship.plugin:RelationshipPlugin - - [babel.extractors] - ckan = ckan.lib.extract:extract_ckan - """, # If you are changing from the default layout of your extension, you may # have to change the message extractors, you can read more about babel # message extraction at