Skip to content

Commit

Permalink
chore: migration for ckan v2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Mar 11, 2024
1 parent 9cdab46 commit c77c7aa
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Compatibility with core CKAN versions:
| 2.9 | yes |
| master | yes |


alembic==1.4

## Installation

To install ckanext-files:
Expand Down
75 changes: 75 additions & 0 deletions ckanext/files/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from __future__ import print_function

import logging

import paste.script

from ckan import model
from ckan.lib.cli import CkanCommand

from ckanext.files.model.base import metadata

log = logging.getLogger(__name__)


def drop_tables():
"""Drop tables defined in model."""
metadata.drop_all(model.meta.engine)


def create_tables():
"""Create tables defined in model."""
metadata.create_all(model.meta.engine)


class FilesCommand(CkanCommand):
"""
ckanext-files management commands.
Usage::
paster --plugin=ckanext-files files -c ckan.ini initdb
paster --plugin=ckanext-files files -c ckan.ini dropdb
"""

summary = __doc__.split("\n")[0]
usage = __doc__

parser = paste.script.command.Command.standard_parser(verbose=True)
parser.add_option(
"-c",
"--config",
dest="config",
default="ckan.ini",
help="Config file to use.",
)

def command(self):
"""Command itself."""
self._load_config()

if not len(self.args):
print(self.usage) # noqa

elif self.args[0] == "initdb":
self._init()
elif self.args[0] == "dropdb":
self._drop()
elif self.args[0] == "createdb":
self._create()

def _init(self):
self._drop()
self._create()
log.info("DB tables are reinitialized")

def _drop(self):
model.Session.rollback()

drop_tables()
log.info("DB tables are removed")

def _create(self):
model.Session.rollback()

create_tables()
log.info("DB tables are setup")
4 changes: 3 additions & 1 deletion ckanext/files/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def files_file_search_by_user(context, data_dict):

total = sess.scalar(sa.select(sa.func.count()).select_from(stmt))

sort, *sort_path = data_dict["sort"].split(".")
parts = data_dict["sort"].split(".")
sort = parts[0]
sort_path = parts[1:]

inspector = sa.inspect(File) # type: types.Any
columns = inspector.columns
Expand Down
8 changes: 4 additions & 4 deletions ckanext/files/model/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class File(Base): # type: ignore
name = Column(UnicodeText, nullable=False)
storage = Column(UnicodeText, nullable=False)

ctime = Column(DateTime, nullable=False, default=now)
ctime = Column(DateTime, nullable=False, default=now, server_default=sa.func.now())
mtime = Column(DateTime)
atime = Column(DateTime)

storage_data = Column(JSONB, default=dict)
plugin_data = Column(JSONB, default=dict)
completed = Column(sa.Boolean, default=False)
storage_data = Column(JSONB, default=dict, server_default="{}")
plugin_data = Column(JSONB, default=dict, server_default="{}")
completed = Column(sa.Boolean, default=False, server_default="false")

def __init__(self, **kwargs):
# type: (**types.Any) -> None
Expand Down
2 changes: 2 additions & 0 deletions ckanext/files/model/owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Owner(Base): # type: ignore
owner_id = sa.Column(sa.Text, nullable=False)
owner_type = sa.Column(sa.Text, nullable=False)

sa.UniqueConstraint(item_id, item_type, owner_id, owner_type)

def dictize(self, context):
# type: (Any) -> dict[str, Any]
return table_dictize(self, context)
Expand Down
8 changes: 5 additions & 3 deletions ckanext/files/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
bp = Blueprint("files", __name__)


def not_found_handler(error: tk.ObjectNotFound):
def not_found_handler(error):
# type: (tk.ObjectNotFound) -> tuple[str, int]
"""Generic handler for ObjectNotFound exception"""
return (
tk.render(
"error_document_template.html",
{
"code": 404,
"content": f"Object not found: {error.message}",
"content": "Object not found: {}".format(error.message),
"name": "Not found",
},
),
404,
)


def not_authorized_handler(error: tk.NotAuthorized):
def not_authorized_handler(error):
# type: (tk.NotAuthorized) -> tuple[str, int]
"""Generic handler for NotAuthorized exception"""
return (
tk.render(
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

[tool.ruff]
target-version = "py38"
[tool.ruff.lint]
select = [
"B", # likely bugs and design problems
# "BLE", # do not catch blind exception
Expand All @@ -29,15 +30,17 @@ select = [
# "TRY", # better exceptions
# "UP", # upgrade syntax for newer versions of the language
]

ignore = [
"E712", # comparison to bool: violated by SQLAlchemy filters
"PT004", # fixture does not return anything, add leading underscore: violated by clean_db
"PLC1901", # simplify comparison to empty string: violated by SQLAlchemy filters
]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"ckanext/files/tests*" = ["S", "PL"]


[tool.isort]
known_ckan = "ckan"
known_ckanext = "ckanext"
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ keywords =
install_requires =
six
typing_extensions; python_version >= "3.6"

packages = find:
namespace_packages = ckanext
include_package_data = True
Expand All @@ -48,6 +49,10 @@ ckan.plugins =
file_manager = ckanext.file_manager.plugin:FileManagerPlugin
babel.extractors =
ckan = ckan.lib.extract:extract_ckan

paste.paster_command =
files = ckanext.files.command:FilesCommand

[extract_messages]
keywords = translate isPlural
add_comments = TRANSLATORS:
Expand Down

0 comments on commit c77c7aa

Please sign in to comment.