From 6fe009be3510bcaa06d6bb5b14fc888896a5d517 Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Tue, 28 Mar 2023 14:57:32 +0300 Subject: [PATCH] feat!: replace const module with config --- ckanext/comments/const.py | 23 ----- ckanext/comments/helpers.py | 12 +-- ckanext/comments/logic/action.py | 9 +- ckanext/comments/logic/auth.py | 31 ++----- ckanext/comments/tests/logic/test_action.py | 4 +- ckanext/comments/tests/logic/test_auth.py | 8 +- ckanext/comments/utils.py | 12 +-- pyproject.toml | 95 ++++++++++++++++++++- 8 files changed, 115 insertions(+), 79 deletions(-) delete mode 100644 ckanext/comments/const.py diff --git a/ckanext/comments/const.py b/ckanext/comments/const.py deleted file mode 100644 index cfe97a6..0000000 --- a/ckanext/comments/const.py +++ /dev/null @@ -1,23 +0,0 @@ -CONFIG_REQUIRE_APPROVAL = "ckanext.comments.require_approval" -DEFAULT_REQUIRE_APPROVAL = True - -CONFIG_DRAFT_EDITS = "ckanext.comments.draft_edits" -DEFAULT_DRAFT_EDITS = True - -CONFIG_DRAFT_EDITS_BY_AUTHOR = "ckanext.comments.draft_edits_by_author" -DEFAULT_DRAFT_EDITS_BY_AUTHOR = True - -CONFIG_APPROVED_EDITS = "ckanext.comments.approved_edits" -DEFAULT_APPROVED_EDITS = False - -CONFIG_APPROVED_EDITS_BY_AUTHOR = "ckanext.comments.approved_edits_by_author" -DEFAULT_APPROVED_EDITS_BY_AUTHOR = False - -CONFIG_MOBILE_THRESHOLD = "ckanext.comments.mobile_depth_threshold" -DEFAULT_MOBILE_THRESHOLD = 3 - -CONFIG_MODERATOR_CHECKER = "ckanext.comments.moderator_checker" -DEFAULT_MODERATOR_CHECKER = "ckanext.comments.utils:comments_is_moderator" - -CONFIG_ENABLE_DATASET = "ckanext.comments.enable_default_dataset_comments" -DEFAULT_ENABLE_DATASET = False diff --git a/ckanext/comments/helpers.py b/ckanext/comments/helpers.py index f8d3939..c611629 100644 --- a/ckanext/comments/helpers.py +++ b/ckanext/comments/helpers.py @@ -5,9 +5,9 @@ import ckan.plugins.toolkit as tk import ckan.model as model -import ckanext.comments.const as const from ckanext.comments.model.thread import Subject from .model import Comment +from . import config _helpers = {} @@ -45,12 +45,8 @@ def thread_for(id_: Optional[str], type_: str) -> dict[str, Any]: @helper -def mobile_depth_threshold(): - return tk.asint( - tk.config.get( - const.CONFIG_MOBILE_THRESHOLD, const.DEFAULT_MOBILE_THRESHOLD - ) - ) +def mobile_depth_threshold() -> int: + return config.mobile_depth_threshold() @helper @@ -75,4 +71,4 @@ def subject_of(id_: str) -> Optional[Subject]: @helper def enable_default_dataset_comments() -> bool: - return tk.asbool(tk.config.get(const.CONFIG_ENABLE_DATASET, const.DEFAULT_ENABLE_DATASET)) + return config.use_default_dataset_comments() diff --git a/ckanext/comments/logic/action.py b/ckanext/comments/logic/action.py index 5abf27b..b38ebe6 100644 --- a/ckanext/comments/logic/action.py +++ b/ckanext/comments/logic/action.py @@ -8,8 +8,7 @@ from ckanext.comments.model import Thread, Comment from ckanext.comments.model.dictize import get_dictizer -import ckanext.comments.const as const -from .. import signals +from .. import signals, config _actions = {} @@ -175,11 +174,7 @@ def comment_create(context, data_dict): # make sure we are not messing up with name_or_id comment.author_id = author.id - if not tk.asbool( - tk.config.get( - const.CONFIG_REQUIRE_APPROVAL, const.DEFAULT_REQUIRE_APPROVAL - ) - ): + if not config.approval_required(): comment.approve() context["session"].add(comment) context["session"].commit() diff --git a/ckanext/comments/logic/auth.py b/ckanext/comments/logic/auth.py index b488de2..53e89a1 100644 --- a/ckanext/comments/logic/auth.py +++ b/ckanext/comments/logic/auth.py @@ -4,8 +4,8 @@ import ckan.plugins.toolkit as tk from ckanext.comments.model import Comment -import ckanext.comments.const as const from ..utils import is_moderator +from .. import config log = logging.getLogger(__name__) _auth = {} @@ -14,32 +14,13 @@ def _can_edit(state: str, by_author: bool = False) -> bool: if state == Comment.State.draft: if by_author: - return tk.asbool( - tk.config.get( - const.CONFIG_DRAFT_EDITS_BY_AUTHOR, - const.DEFAULT_DRAFT_EDITS_BY_AUTHOR, - ) - ) - return tk.asbool( - tk.config.get( - const.CONFIG_DRAFT_EDITS, - const.DEFAULT_DRAFT_EDITS, - ) - ) + return config.allow_draft_edits_by_author() + return config.allow_draft_edits() elif state == Comment.State.approved: if by_author: - return tk.asbool( - tk.config.get( - const.CONFIG_APPROVED_EDITS_BY_AUTHOR, - const.DEFAULT_APPROVED_EDITS_BY_AUTHOR, - ) - ) - return tk.asbool( - tk.config.get( - const.CONFIG_APPROVED_EDITS, - const.DEFAULT_APPROVED_EDITS, - ) - ) + return config.allow_approved_edits_by_author() + return config.allow_approved_edits() + log.warning("Unexpected comment state: %s", state) return False diff --git a/ckanext/comments/tests/logic/test_action.py b/ckanext/comments/tests/logic/test_action.py index 09c8384..808a028 100644 --- a/ckanext/comments/tests/logic/test_action.py +++ b/ckanext/comments/tests/logic/test_action.py @@ -6,7 +6,7 @@ import ckan.tests.factories as factories from ckan.tests.helpers import call_action -import ckanext.comments.const as const +from ckanext.comments import config @pytest.mark.usefixtures("clean_db") @@ -215,7 +215,7 @@ def test_existing_reply(self, Thread, Comment): assert len(top["replies"]) == 1 assert top["replies"][0]["id"] == reply["id"] - @pytest.mark.ckan_config(const.CONFIG_REQUIRE_APPROVAL, False) + @pytest.mark.ckan_config(config.CONFIG_REQUIRE_APPROVAL, False) @pytest.mark.usefixtures("clean_db") def test_optional_approval(self, Comment): comment = Comment() diff --git a/ckanext/comments/tests/logic/test_auth.py b/ckanext/comments/tests/logic/test_auth.py index 5dfc177..888f459 100644 --- a/ckanext/comments/tests/logic/test_auth.py +++ b/ckanext/comments/tests/logic/test_auth.py @@ -4,7 +4,7 @@ import ckan.plugins.toolkit as tk import ckan.tests.factories as factories from ckan.tests.helpers import call_auth, call_action -import ckanext.comments.const as const +from ckanext.comments import config @pytest.mark.usefixtures("clean_db") @@ -54,7 +54,7 @@ def test_comment_show(self, Comment): "comments_comment_show", user_ctx.copy(), id=comment["id"] ) - @pytest.mark.ckan_config(const.CONFIG_DRAFT_EDITS_BY_AUTHOR, False) + @pytest.mark.ckan_config(config.CONFIG_DRAFT_EDITS_BY_AUTHOR, False) def test_comment_update(self, Comment, monkeypatch, ckan_config): user = factories.User() user_ctx = {"model": model, "user": user["name"]} @@ -67,7 +67,7 @@ def test_comment_update(self, Comment, monkeypatch, ckan_config): ) monkeypatch.setitem( - ckan_config, const.CONFIG_DRAFT_EDITS_BY_AUTHOR, True + ckan_config, config.CONFIG_DRAFT_EDITS_BY_AUTHOR, True ) with pytest.raises(tk.NotAuthorized): @@ -96,7 +96,7 @@ def test_comment_update(self, Comment, monkeypatch, ckan_config): ) monkeypatch.setitem( - ckan_config, const.CONFIG_APPROVED_EDITS_BY_AUTHOR, True + ckan_config, config.CONFIG_APPROVED_EDITS_BY_AUTHOR, True ) with pytest.raises(tk.NotAuthorized): call_auth( diff --git a/ckanext/comments/utils.py b/ckanext/comments/utils.py index 337e105..a16bf1b 100644 --- a/ckanext/comments/utils.py +++ b/ckanext/comments/utils.py @@ -1,17 +1,13 @@ -from werkzeug.utils import import_string +from __future__ import annotations -import ckan.model as model -import ckanext.comments.const as const -import ckan.plugins.toolkit as tk +import ckan.model as model +from . import config def comments_is_moderator(user: model.User, comment, thread) -> bool: return user.sysadmin def is_moderator(user: model.User, comment, thread) -> bool: - checker = tk.config.get( - const.CONFIG_MODERATOR_CHECKER, const.DEFAULT_MODERATOR_CHECKER - ) - func = import_string(checker, silent=True) or comments_is_moderator + func = config.moderator_checker() or comments_is_moderator return func(user, comment, thread) diff --git a/pyproject.toml b/pyproject.toml index ed4d7c1..5bf240d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,94 @@ [tool.black] -line-length = 79 -preview = true +# line-length = 88 +# preview = true + +[tool.ruff] +target-version = "py38" + +[tool.isort] +known_ckan = "ckan" +known_ckanext = "ckanext" +known_self = "ckanext.comments" +sections = "FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,CKAN,CKANEXT,SELF,LOCALFOLDER" + +[tool.pytest.ini_options] +addopts = "--ckan-ini test.ini" +filterwarnings = [ + "ignore::sqlalchemy.exc.SADeprecationWarning", + "ignore::sqlalchemy.exc.SAWarning", + "ignore::DeprecationWarning", +] + +[tool.pyright] +pythonVersion = "3.7" +include = ["ckanext"] +exclude = [ + "**/test*", + "**/migration", +] +strict = [] + +strictParameterNoneValue = true + +# Check the meaning of rules here +# https://github.com/microsoft/pyright/blob/main/docs/configuration.md +reportFunctionMemberAccess = true # non-standard member accesses for functions +reportMissingImports = true +reportMissingModuleSource = true +reportMissingTypeStubs = false +reportImportCycles = true +reportUnusedImport = true +reportUnusedClass = true +reportUnusedFunction = true +reportUnusedVariable = true +reportDuplicateImport = true +reportOptionalSubscript = true +reportOptionalMemberAccess = true +reportOptionalCall = true +reportOptionalIterable = true +reportOptionalContextManager = true +reportOptionalOperand = true +reportTypedDictNotRequiredAccess = false # Context won't work with this rule +reportConstantRedefinition = true +reportIncompatibleMethodOverride = true +reportIncompatibleVariableOverride = true +reportOverlappingOverload = true +reportUntypedFunctionDecorator = false +reportUnknownParameterType = true +reportUnknownArgumentType = false +reportUnknownLambdaType = false +reportUnknownMemberType = false +reportMissingTypeArgument = true +reportInvalidTypeVarUse = true +reportCallInDefaultInitializer = true +reportUnknownVariableType = true +reportUntypedBaseClass = true +reportUnnecessaryIsInstance = true +reportUnnecessaryCast = true +reportUnnecessaryComparison = true +reportAssertAlwaysTrue = true +reportSelfClsParameterName = true +reportUnusedCallResult = false # allow function calls for side-effect only +useLibraryCodeForTypes = true +reportGeneralTypeIssues = true +reportPropertyTypeMismatch = true +reportWildcardImportFromLibrary = true +reportUntypedClassDecorator = false +reportUntypedNamedTuple = true +reportPrivateUsage = true +reportPrivateImportUsage = true +reportInconsistentConstructor = true +reportMissingSuperCall = false +reportUninitializedInstanceVariable = true +reportInvalidStringEscapeSequence = true +reportMissingParameterType = true +reportImplicitStringConcatenation = false +reportUndefinedVariable = true +reportUnboundVariable = true +reportInvalidStubStatement = true +reportIncompleteStub = true +reportUnsupportedDunderAll = true +reportUnusedCoroutine = true +reportUnnecessaryTypeIgnoreComment = true +reportMatchNotExhaustive = true +