Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
1.1.0
- BREAKING: remove unused `paths.get_nginx_config_path`,
`paths.get_supervisord_worker_config_path`, `paths.get_uwsgi_config_path`,
and `VALID_FORMATS`
- BREAKING: remove the `testing.create_with_upload_no_temp` fixture
(resources cannot be uploaded via CKAN anymore)
- fix: use the actual site user instead of the user "default" in
`get_resource_info` and `wait_for_resource`
- enh: new `admin_context` returning the context of the site user
1.0.4
- enh: implement "artifact" kwarg for `s3cc.compute_checksum`
- enh: introduce `enable_basins` in `s3cc.get_s3_dc_handle`
Expand Down
6 changes: 3 additions & 3 deletions dcor_shared/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# flake8: noqa: F401
from .ckan import (
get_ckan_config_option, get_ckan_storage_path, get_resource_dc_config,
get_resource_info, is_resource_private
admin_context, get_ckan_config_option, get_ckan_storage_path,
get_resource_dc_config, get_resource_info, is_resource_private
)
from .data import wait_for_resource
from .dcinst import get_dc_instance
from .mime import DC_MIME_TYPES, VALID_FORMATS
from .mime import DC_MIME_TYPES
from . import paths
from .rqjob import RQJob, rqjob_register
from .util import sha256sum
Expand Down
15 changes: 13 additions & 2 deletions dcor_shared/ckan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
from .parse import get_ini_config_option


def admin_context():
"""Context of the CKAN site user (sysadmin)

The site user is named after `ckan.site_id`, which is "default"
unless specified otherwise.
"""
from ckan import logic
site_user = logic.get_action("get_site_user")({"ignore_auth": True}, {})
return {'ignore_auth': True, 'user': site_user["name"]}


def get_ckan_config_option(option):
"""Return the CKAN configuration option

Expand Down Expand Up @@ -64,10 +75,10 @@ def get_resource_info(resource_id):
"""
from ckan import logic
res_dict = logic.get_action("resource_show")(
context={'ignore_auth': True, 'user': 'default'},
context=admin_context(),
data_dict={"id": resource_id})
ds_dict = logic.get_action("package_show")(
context={'ignore_auth': True, 'user': 'default'},
context=admin_context(),
data_dict={"id": res_dict["package_id"]})
return ds_dict, res_dict

Expand Down
4 changes: 2 additions & 2 deletions dcor_shared/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time

from . import s3cc
from .ckan import admin_context
from .util import sha256sum # noqa: F401


Expand Down Expand Up @@ -30,8 +31,7 @@ def wait_for_resource(resource_id: str,
t0 = time.time()
while True:
try:
res_dict = resource_show(context={'ignore_auth': True,
'user': 'default'},
res_dict = resource_show(context=admin_context(),
data_dict={"id": resource_id})
except logic.NotFound:
# Other processes are still working on getting the resource
Expand Down
3 changes: 0 additions & 3 deletions dcor_shared/mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
"RT-DC": ".rtdc",
"DC": ".dc", # more general
}

#: Valid values for resource "format"
VALID_FORMATS = sorted(DC_MIME_TYPES.keys()) + ["RT-FDC"]
14 changes: 0 additions & 14 deletions dcor_shared/paths.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
import pathlib

from .ckan import get_ckan_storage_path, get_ckan_webassets_path # noqa: F401
from .ckan import get_ckan_config_path # noqa: F401


def get_nginx_config_path():
return pathlib.Path("/etc/nginx/sites-enabled/ckan")


def get_supervisord_worker_config_path():
return pathlib.Path("/etc/supervisor/conf.d/ckan-worker.conf")


def get_uwsgi_config_path():
return pathlib.Path("/etc/ckan/default/ckan-uwsgi.ini")
30 changes: 0 additions & 30 deletions dcor_shared/testing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

from io import BytesIO
import numbers
import pathlib
from typing import Dict, List
Expand All @@ -10,41 +9,12 @@
import ckan.model
import ckan.tests.factories as factories
import ckan.tests.helpers as helpers
from ckan.tests.pytest_ckan.fixtures import FakeFileStorage
import requests

import pytest

from .ckan import get_ckan_config_option
from . import s3


@pytest.fixture
def create_with_upload_no_temp(clean_db, ckan_config, monkeypatch):
"""
Create upload without tempdir
"""

def factory(data, filename, context=None, **kwargs):
if context is None:
context = {}
action = kwargs.pop("action", "resource_create")
field = kwargs.pop("upload_field_name", "upload")
test_file = BytesIO()
if type(data) is not bytes:
data = bytes(data, encoding="utf-8")
test_file.write(data)
test_file.seek(0)
test_resource = FakeFileStorage(test_file, filename)

params = {
field: test_resource,
}
params.update(kwargs)
return helpers.call_action(action, context, **params)
return factory


def activate_dataset(ds_id, create_context=None):
"""Activate a dataset by setting its "state" to "active"."""

Expand Down
6 changes: 6 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from ckan import logic

import dcor_shared.data

from dcor_shared import (
get_dc_instance, s3, sha256sum, wait_for_resource
)
Expand Down Expand Up @@ -98,6 +100,8 @@ def test_sha256sum(tmp_path):

def test_wait_for_resource_s3(monkeypatch):
res_id = str(uuid.uuid4())
monkeypatch.setattr(dcor_shared.data, "admin_context",
lambda: {"ignore_auth": True})
monkeypatch.setattr(logic, "get_action",
lambda x: lambda context, data_dict: {
"id": res_id,
Expand All @@ -108,6 +112,8 @@ def test_wait_for_resource_s3(monkeypatch):

def test_wait_resource_not_available(monkeypatch):
res_id = str(uuid.uuid4())
monkeypatch.setattr(dcor_shared.data, "admin_context",
lambda: {"ignore_auth": True})
monkeypatch.setattr(logic, "get_action",
lambda x: lambda context, data_dict: {
"id": res_id})
Expand Down
Loading