Skip to content

Commit

Permalink
chore: ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Mar 5, 2024
1 parent cced1e8 commit 6271594
Show file tree
Hide file tree
Showing 22 changed files with 222 additions and 128 deletions.
7 changes: 5 additions & 2 deletions ckanext/file_manager/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FileManagerCollection(ApCollection):
id="bulk_check",
data_module="ap-bulk-check",
data_module_selector='input[name="entity_id"]',
)
),
),
"name": "Name",
"path": "Path",
Expand Down Expand Up @@ -132,7 +132,10 @@ class FileManagerCollection(ApCollection):
"label": "Clear",
"type": "button",
"attrs": {
"onclick": "$(this).closest('form').find('input,select').val('').prevObject[0].requestSubmit()"
"onclick": (
"$(this).closest('form').find('input,select')"
".val('').prevObject[0].requestSubmit()"
),
},
},
),
Expand Down
4 changes: 2 additions & 2 deletions ckanext/file_manager/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def update_config(self, config_):
def get_signal_subscriptions(self) -> types.SignalMapping:
return {
tk.signals.ckanext.signal("ap_main:collect_config_sections"): [
collect_config_sections_subs
collect_config_sections_subs,
],
}

Expand All @@ -44,6 +44,6 @@ def collect_config_sections_subs(sender: None):
"name": "File manager",
"blueprint": "file_manager.list",
"info": "Manage uploaded files",
}
},
],
}
12 changes: 7 additions & 5 deletions ckanext/file_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

log = logging.getLogger(__name__)
file_manager = Blueprint(
"file_manager", __name__, url_prefix="/admin-panel/file_manager"
"file_manager",
__name__,
url_prefix="/admin-panel/file_manager",
)
file_manager.before_request(ap_before_request)

Expand All @@ -25,7 +27,8 @@ def get(self) -> Union[str, Response]:
"file_manager/list.html",
extra_vars={
"collection": get_collection(
"file-manager", parse_params(tk.request.args)
"file-manager",
parse_params(tk.request.args),
),
},
)
Expand Down Expand Up @@ -96,12 +99,11 @@ def post(self, file_id: str):
return tk.redirect_to("file_manager.list")


file_manager.delete

file_manager.add_url_rule("/manage", view_func=FileManagerView.as_view("list"))
file_manager.add_url_rule("/upload", view_func=FileManagerUploadView.as_view("upload"))
file_manager.add_url_rule(
"/delete/<file_id>", view_func=FileManagerDeleteView.as_view("delete")
"/delete/<file_id>",
view_func=FileManagerDeleteView.as_view("delete"),
)

blueprints = [file_manager]
2 changes: 1 addition & 1 deletion ckanext/files/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ckan.plugins.toolkit as tk

if six.PY3: # pragma: no cover
from typing import Any # isort: skip
from typing import Any # isort: skip # noqa: F401


DEFAULT_STORAGE = "ckanext.files.default_storage"
Expand Down
26 changes: 21 additions & 5 deletions ckanext/files/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def __init__(self, operation, adapter):

def __str__(self):
return "Operation {} is not supported by storage adapter {}".format(
self.operation, self.adapter
self.operation,
self.adapter,
)


Expand All @@ -68,7 +69,8 @@ def __init__(self, adapter, problem):

def __str__(self):
return "Cannot initialize storage adapter {} due to following error: {}".format(
self.adapter.__name__, self.problem
self.adapter.__name__,
self.problem,
)


Expand All @@ -78,7 +80,8 @@ class MissingAdapterConfigurationError(InvalidAdapterConfigurationError):
def __init__(self, adapter, option):
# type: (type, str) -> None
return super(InvalidAdapterConfigurationError, self).__init__(
adapter, "{} option is required".format(option)
adapter,
"{} option is required".format(option),
)


Expand All @@ -92,7 +95,8 @@ def __init__(self, actual_size, max_size):

def __str__(self):
return "Upload size {} surpasses max allowed size {}".format(
localised_filesize(self.actual_size), localised_filesize(self.max_size)
localised_filesize(self.actual_size),
localised_filesize(self.max_size),
)


Expand All @@ -101,5 +105,17 @@ class UploadOutOfBoundError(LargeUploadError):

def __str__(self):
return "Upload size {} exceeds expected size {}".format(
localised_filesize(self.actual_size), localised_filesize(self.max_size)
localised_filesize(self.actual_size),
localised_filesize(self.max_size),
)


class NameStrategyError(UploadError):
"""Undefined name strategy."""

def __init__(self, strategy):
# type: (str) -> None
self.strategy = strategy

def __str__(self):
return "Unknown name strategy {}".format(self.strategy)
2 changes: 1 addition & 1 deletion ckanext/files/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import six

if six.PY3: # pragma: no cover
from typing import Any # isort: skip
from typing import Any # isort: skip # noqa: F401


from ckan.plugins import Interface
Expand Down
50 changes: 25 additions & 25 deletions ckanext/files/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from . import schema

if six.PY3:
from typing import Any # isort: skip
from typing import Any # isort: skip # noqa: F401

_actions, action = make_collector()

Expand All @@ -26,22 +26,23 @@ def get_actions():
def files_file_create(context, data_dict):
# type: (Any, dict[str, Any]) -> dict[str, Any]
tk.check_access("files_file_create", context, data_dict)
_ensure_name(data_dict)

extras = data_dict.get("__extras", {})
name = secure_filename(data_dict["name"])

try:
storage = shared.get_storage(data_dict["storage"])
except exceptions.UnknownStorageError as err:
raise tk.ValidationError({"storage": [str(err)]})
raise tk.ValidationError({"storage": [str(err)]}) # noqa: B904

if not storage.supports(Capability.CREATE):
raise tk.ValidationError({"storage": ["Operation is not supported"]})

try:
storage_data = storage.upload(name, data_dict["upload"], extras)
except exceptions.LargeUploadError as err:
raise tk.ValidationError({"upload": [str(err)]})
raise tk.ValidationError({"upload": [str(err)]}) # noqa: B904

fileobj = File(name=name, storage=data_dict["storage"], storage_data=storage_data)

Expand All @@ -52,6 +53,24 @@ def files_file_create(context, data_dict):
return fileobj.dictize(context)


def _ensure_name(data_dict, name_field="name", upload_field="upload"):
# type: (dict[str, Any], str, str) -> None
if name_field in data_dict:
return
name = data_dict[upload_field].filename
if not name:
raise tk.ValidationError(
{
name_field: [
"Name is missing and cannot be deduced from {}".format(
upload_field
),
],
},
)
data_dict[name_field] = name


@action
@validate(schema.file_delete)
def files_file_delete(context, data_dict):
Expand Down Expand Up @@ -94,46 +113,27 @@ def files_file_show(context, data_dict):
return fileobj.dictize(context)


@action
@validate(schema.file_show)
def files_file_show(context, data_dict):
# type: (Any, dict[str, Any]) -> dict[str, Any]
tk.check_access("files_file_show", context, data_dict)

data_dict["id"]
fileobj = context["session"].get(File, data_dict["id"])
if not fileobj:
raise tk.ObjectNotFound("file")

if context.get("update_access_time"):
fileobj.access()
if not context.get("defer_commit"):
context["session"].commit()

return fileobj.dictize(context)


@action
@validate(schema.upload_initialize)
def files_upload_initialize(context, data_dict):
# type: (Any, dict[str, Any]) -> dict[str, Any]
tk.check_access("files_upload_initialize", context, data_dict)

_ensure_name(data_dict)
extras = data_dict.get("__extras", {})
name = secure_filename(data_dict["name"])

try:
storage = shared.get_storage(data_dict["storage"])
except exceptions.UnknownStorageError as err:
raise tk.ValidationError({"storage": [str(err)]})
raise tk.ValidationError({"storage": [str(err)]}) # noqa: B904

if not storage.supports(Capability.MULTIPART_UPLOAD):
raise tk.ValidationError({"storage": ["Operation is not supported"]})

try:
upload_data = storage.initialize_multipart_upload(name, extras)
except exceptions.LargeUploadError as err:
raise tk.ValidationError({"upload": [str(err)]})
raise tk.ValidationError({"upload": [str(err)]}) # noqa: B904

upload = Upload(
name=name,
Expand Down
2 changes: 1 addition & 1 deletion ckanext/files/logic/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ckanext.files.utils import make_collector

if six.PY3:
from typing import Any # isort: skip
from typing import Any # isort: skip # noqa: F401


_auth_functions, auth = make_collector()
Expand Down
14 changes: 9 additions & 5 deletions ckanext/files/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from ckanext.files import config

if six.PY3:
from typing import Any # isort: skip
from typing import Any # isort: skip # noqa: F401


@validator_args
def file_create(not_empty, unicode_safe, default, files_into_upload, not_missing):
def file_create(ignore_empty, unicode_safe, default, files_into_upload, not_missing):
# type: (Any, Any, Any, Any, Any) -> Any

# name is checked inside action, using "upload" as source if empty
return {
"name": [not_empty, unicode_safe],
"name": [ignore_empty, unicode_safe],
"storage": [default(config.default_storage()), unicode_safe],
"upload": [not_missing, files_into_upload],
}
Expand All @@ -35,10 +37,12 @@ def file_show(not_empty, unicode_safe):


@validator_args
def upload_initialize(not_empty, unicode_safe, default, int_validator, not_missing):
def upload_initialize(ignore_empty, unicode_safe, default, int_validator, not_missing):
# type: (Any, Any, Any, Any, Any) -> Any

# name is checked inside action, using "upload" as source if empty
return {
"name": [not_empty, unicode_safe],
"name": [ignore_empty, unicode_safe],
"storage": [default(config.default_storage()), unicode_safe],
}

Expand Down
7 changes: 5 additions & 2 deletions ckanext/files/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ckanext.files.utils import make_collector

if six.PY3:
from typing import Any # isort: skip
from typing import Any # isort: skip # noqa: F401

_validators, validator = make_collector()

Expand Down Expand Up @@ -42,7 +42,10 @@ def files_into_upload(value):
value.file.seek(0)

return FileStorage(
value.file, value.filename, content_type=mime, content_length=size
value.file,
value.filename,
content_type=mime,
content_length=size,
)

if isinstance(value, str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def upgrade():
sa.Column("name", sa.Text, nullable=False),
sa.Column("storage", sa.Text, nullable=False),
sa.Column(
"initialized_at", sa.DateTime, nullable=False, server_default=sa.func.now()
"initialized_at",
sa.DateTime,
nullable=False,
server_default=sa.func.now(),
),
sa.Column("upload_data", JSONB, server_default="{}"),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ def upgrade():
op.execute(
sa.update(table)
.values(
atime=last_access, extras=dict(extras or {}, filename=os.path.basename(path))
atime=last_access,
extras=dict(extras or {}, filename=os.path.basename(path)),
)
.where(table.c.id == id)
.where(table.c.id == id),
)

op.alter_column(
"files_file", "extras", server_default="{}", new_column_name="storage_data"
"files_file",
"extras",
server_default="{}",
new_column_name="storage_data",
)
op.drop_column("files_file", "last_access")
op.drop_column("files_file", "path")
Expand All @@ -60,19 +64,28 @@ def downgrade():
op.add_column(
"files_file",
sa.Column(
"path", sa.UnicodeText, nullable=False, server_default="'/empty.data'"
"path",
sa.UnicodeText,
nullable=False,
server_default="'/empty.data'",
),
)
op.add_column(
"files_file",
sa.Column(
"last_access", sa.DateTime(), nullable=False, server_default=sa.func.now()
"last_access",
sa.DateTime(),
nullable=False,
server_default=sa.func.now(),
),
)
op.alter_column("files_file", "storage", new_column_name="kind")
op.alter_column("files_file", "ctime", new_column_name="uploaded_at")
op.alter_column(
"files_file", "storage_data", server_default=None, new_column_name="extras"
"files_file",
"storage_data",
server_default=None,
new_column_name="extras",
)

stmt = sa.select(table.c.id, table.c.atime, table.c.extras)
Expand All @@ -84,7 +97,7 @@ def downgrade():
op.execute(
sa.update(table)
.values(last_access=atime or datetime.now(), extras=extras, path=path)
.where(table.c.id == id)
.where(table.c.id == id),
)

op.drop_column("files_file", "atime")
Expand Down
Loading

0 comments on commit 6271594

Please sign in to comment.