Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Feb 10, 2022
1 parent 81804b8 commit de395b5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
33 changes: 23 additions & 10 deletions ckanext/files/logic/action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
import os
from typing import Optional
from typing import Any, Optional
import ckan.plugins.toolkit as tk
from ckan.logic import validate
from ckan.lib.uploader import get_uploader, get_storage_path
Expand All @@ -25,26 +25,34 @@ def files_uploader(kind: str, old_filename: Optional[str] = None):
def file_create(context, data_dict):
tk.check_access("files_file_create", context, data_dict)

uploader = files_uploader(data_dict["kind"])
uploader.update_data_dict(data_dict, "path", "upload", None)

max_size = tk.asint(tk.config.get(CONFIG_SIZE.format(kind=data_dict["kind"]), DEFAULT_SIZE))
uploader.upload(max_size)

# TODO: try not to rely on hardcoded segments
data_dict["path"] = os.path.relpath(uploader.filepath, os.path.join(get_storage_path(), "storage"))
_upload(data_dict, data_dict["kind"])

file = File(**data_dict)
context["session"].add(file)
context["session"].commit()
return file.dictize(context)


def _upload(data_dict: dict[str, Any], kind: str):
uploader = files_uploader(kind)
uploader.update_data_dict(data_dict, "path", "upload", None)

max_size = tk.asint(
tk.config.get(CONFIG_SIZE.format(kind=kind), DEFAULT_SIZE)
)
uploader.upload(max_size)

# TODO: try not to rely on hardcoded segments
data_dict["path"] = os.path.relpath(
uploader.filepath, os.path.join(get_storage_path(), "storage")
)


@action
@validate(schema.file_update)
def file_update(context, data_dict):
tk.check_access("files_file_delete", context, data_dict)
file = (
file: File = (
context["session"]
.query(File)
.filter_by(id=data_dict["id"])
Expand All @@ -53,6 +61,10 @@ def file_update(context, data_dict):
if not file:
raise tk.ObjectNotFound("File not found")

# TODO: remove old file
if "upload" in data_dict:
_upload(data_dict, file.kind)

for attr, value in data_dict.items():
setattr(file, attr, value)
context["session"].commit()
Expand All @@ -67,6 +79,7 @@ def file_delete(context, data_dict):
if not file:
raise tk.ObjectNotFound("File not found")

# TODO: remove file
context["session"].delete(file)
context["session"].commit()
return True
Expand Down
9 changes: 7 additions & 2 deletions ckanext/files/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

@validator_args
def file_create(
ignore_empty, dict_only, not_empty, unicode_safe, default, ignore, not_missing
ignore_empty,
dict_only,
not_empty,
unicode_safe,
default,
ignore,
not_missing,
):
default_kind = tk.config.get(CONFIG_KIND, DEFAULT_KIND)
return {
Expand All @@ -26,7 +32,6 @@ def file_update(not_empty, ignore_missing, unicode_safe, dict_only, ignore):
"id": [not_empty],
"name": [ignore_missing, unicode_safe],
"upload": [ignore_missing],
"kind": [ignore_missing, unicode_safe],
"extras": [ignore_missing, dict_only],
"__extras": [ignore],
}
18 changes: 13 additions & 5 deletions ckanext/files/plugin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import ckan.plugins as plugins
import ckan.plugins as p
import ckan.plugins.toolkit as tk

from .logic import action, auth, schema

from .logic import action, auth

class FilesPlugin(p.SingletonPlugin):
p.implements(p.IActions)
p.implements(p.IAuthFunctions)

class FilesPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IActions)
plugins.implements(plugins.IAuthFunctions)
if tk.check_ckan_version("2.10"):
p.implements(p.IConfigDeclaration)

def declare_config_options(self, declaration, key):
kind = tk.config.get(schema.CONFIG_KIND, schema.DEFAULT_KIND)
declaration.declare_list(f"ckan.upload.{kind}.types", [])
declaration.declare_list(f"ckan.upload.{kind}.mimetypes", [])

# IActions
def get_actions(self):
Expand Down
1 change: 0 additions & 1 deletion ckanext/files/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest



@pytest.fixture
def clean_db(reset_db, migrate_db_for):
reset_db()
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ckanext-files
version = 0.0.2
version = 0.0.3
description =
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -59,4 +59,4 @@ filterwarnings =
ignore::sqlalchemy.exc.SADeprecationWarning
ignore::sqlalchemy.exc.SAWarning
ignore::DeprecationWarning
addopts = --ckan-ini test.ini
addopts = --ckan-ini test.ini

0 comments on commit de395b5

Please sign in to comment.