Skip to content

Commit

Permalink
feat!: replace const module with config
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Mar 28, 2023
1 parent b40a3d4 commit 6fe009b
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 79 deletions.
23 changes: 0 additions & 23 deletions ckanext/comments/const.py

This file was deleted.

12 changes: 4 additions & 8 deletions ckanext/comments/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand All @@ -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()
9 changes: 2 additions & 7 deletions ckanext/comments/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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()
Expand Down
31 changes: 6 additions & 25 deletions ckanext/comments/logic/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions ckanext/comments/tests/logic/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions ckanext/comments/tests/logic/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"]}
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 4 additions & 8 deletions ckanext/comments/utils.py
Original file line number Diff line number Diff line change
@@ -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)
95 changes: 93 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6fe009b

Please sign in to comment.