Skip to content

Commit

Permalink
Update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Feb 9, 2022
1 parent 53ae67c commit 81804b8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 96 deletions.
11 changes: 5 additions & 6 deletions ckanext/files/logic/action.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import annotations

from pathlib import Path
import os
from typing import Optional
import ckan.plugins.toolkit as tk
from ckan.logic import validate
from ckan.lib.uploader import get_uploader
from ckan.lib.uploader import get_uploader, get_storage_path

from ckanext.toolbelt.decorators import Collector

Expand All @@ -27,13 +26,13 @@ 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, "url", "upload", None)
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)

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

file = File(**data_dict)
context["session"].add(file)
Expand Down
4 changes: 2 additions & 2 deletions ckanext/files/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

@validator_args
def file_create(
ignore_empty, dict_only, not_empty, unicode_safe, default, ignore
ignore_empty, dict_only, not_empty, unicode_safe, default, ignore, not_missing
):
default_kind = tk.config.get(CONFIG_KIND, DEFAULT_KIND)
return {
"name": [not_empty, unicode_safe],
"upload": [not_empty],
"upload": [not_missing],
"kind": [default(default_kind), unicode_safe],
"extras": [ignore_empty, dict_only],
"__extras": [ignore],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def upgrade():
sa.UnicodeText,
primary_key=True,
),
sa.Column("name", sa.UnicodeText, nullable=False, unique=True),
sa.Column("url", sa.UnicodeText, nullable=False),
sa.Column("name", sa.UnicodeText, nullable=False),
sa.Column("path", sa.UnicodeText, nullable=False),
sa.Column("kind", sa.UnicodeText, nullable=False),
sa.Column(
"uploaded_at",
Expand Down
6 changes: 3 additions & 3 deletions ckanext/files/model/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
class File(Base):
__tablename__ = "files_file"
id = Column(UnicodeText, primary_key=True, default=make_uuid)
name = Column(UnicodeText, nullable=False, unique=True)
url = Column(UnicodeText, nullable=False)
name = Column(UnicodeText, nullable=False)
path = Column(UnicodeText, nullable=False)
kind = Column(UnicodeText, nullable=False)
uploaded_at = Column(
DateTime, nullable=False, default=datetime.datetime.utcnow
Expand All @@ -25,5 +25,5 @@ class File(Base):

def dictize(self, context):
result = table_dictize(self, context)
result["url"] = tk.h.url_for_static(result["url"])
result["url"] = tk.h.url_for_static(result["path"], qualified=True)
return result
11 changes: 4 additions & 7 deletions ckanext/files/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from __future__ import annotations

import pytest

import ckan.model as model
from ckan.cli.db import _resolve_alembic_config


@pytest.fixture
def clean_db(reset_db, monkeypatch):
def clean_db(reset_db, migrate_db_for):
reset_db()
monkeypatch.setattr(
model.repo, "_alembic_ini", _resolve_alembic_config("files")
)
model.repo.upgrade_db()
migrate_db_for("files")
35 changes: 35 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
[metadata]
name = ckanext-files
version = 0.0.2
description =
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/DataShades/ckanext-files
author = Sergey Motornyuk
author_email = sergey.motornyuk@linkdigital.com.au
license = AGPL
classifiers =
Development Status :: 4 - Beta
License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
keywords =
CKAN

[options]
python_requires = >= 3.7
install_requires =
ckanext-toolbelt
typing_extensions
packages = find:
namespace_packages = ckanext
include_package_data = True

[options.entry_points]
ckan.plugins =
files = ckanext.files.plugin:FilesPlugin
babel.extractors =
ckan = ckan.lib.extract:extract_ckan
[extract_messages]
keywords = translate isPlural
add_comments = TRANSLATORS:
Expand Down Expand Up @@ -25,3 +59,4 @@ filterwarnings =
ignore::sqlalchemy.exc.SADeprecationWarning
ignore::sqlalchemy.exc.SAWarning
ignore::DeprecationWarning
addopts = --ckan-ini test.ini
77 changes: 1 addition & 76 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,83 +1,8 @@
# -*- coding: utf-8 -*-
# Always prefer setuptools over distutils
from setuptools import setup, find_packages
from codecs import open # To use a consistent encoding
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the relevant file
with open(path.join(here, "README.md"), encoding="utf-8") as f:
long_description = f.read()
from setuptools import setup

setup(
name="""ckanext-files""",
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# http://packaging.python.org/en/latest/tutorial.html#version
version="0.0.1",
description="""""",
long_description=long_description,
long_description_content_type="text/markdown",
# The project's main homepage.
url="https://github.com/DataShades/ckanext-files",
# Author details
author="""Sergey Motornyuk""",
author_email="""sergey.motornyuk@linkdigital.com.au""",
# Choose your license
license="AGPL",
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=[
# How mature is this project? Common values are
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
"Development Status :: 4 - Beta",
# Pick your license as you wish (should match "license" above)
"License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)",
# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
"Programming Language :: Python :: 2.7",
],
# What does your project relate to?
keywords="""CKAN""",
# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
packages=find_packages(exclude=["contrib", "docs", "tests*"]),
namespace_packages=["ckanext"],
install_requires=[
# CKAN extensions should not list dependencies here, but in a separate
# ``requirements.txt`` file.
#
# http://docs.ckan.org/en/latest/extensions/best-practices.html
# add-third-party-libraries-to-requirements-txt
"ckanext-toolbelt",
],
# If there are data files included in your packages that need to be
# installed, specify them here. If using Python 2.6 or less, then these
# have to be included in MANIFEST.in as well.
include_package_data=True,
package_data={},
# Although 'package_data' is the preferred approach, in some case you may
# need to place data files outside of your packages.
# see http://docs.python.org/3.4/distutils/setupscript.html
# installing-additional-files
# In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
data_files=[],
# To provide executable scripts, use entry points in preference to the
# "scripts" keyword. Entry points provide cross-platform support and allow
# pip to create the appropriate form of executable for the target platform.
entry_points="""
[ckan.plugins]
files=ckanext.files.plugin:FilesPlugin
[babel.extractors]
ckan = ckan.lib.extract:extract_ckan
""",
# If you are changing from the default layout of your extension, you may
# have to change the message extractors, you can read more about babel
# message extraction at
# http://babel.pocoo.org/docs/messages/#extraction-method-mapping-and-configuration
message_extractors={
"ckanext": [
("**.py", "python", None),
Expand Down

0 comments on commit 81804b8

Please sign in to comment.