Skip to content

Commit

Permalink
feat: register collection-explorer collection in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Jan 26, 2024
1 parent feaa86c commit 42e547f
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 15 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

<!-- insertion marker -->
## [v0.1.1](https://github.com/DataShades/ckanext-collection/releases/tag/v0.1.1) - 2024-01-26

<small>[Compare with v0.1.1a0](https://github.com/DataShades/ckanext-collection/compare/v0.1.1a0...v0.1.1)</small>

## [v0.1.1a0](https://github.com/DataShades/ckanext-collection/releases/tag/v0.1.1a0) - 2024-01-25

<small>[Compare with v0.1.0](https://github.com/DataShades/ckanext-collection/compare/v0.1.0...v0.1.1a0)</small>

### Bug Fixes

- fix ModelData statement_with_filters method ([3c6cb1c](https://github.com/DataShades/ckanext-collection/commit/3c6cb1cfad74ac653b8e2e9a1a223016381e9d99) by mutantsan).

## [v0.1.0](https://github.com/DataShades/ckanext-collection/releases/tag/v0.1.0) - 2024-01-25

<small>[Compare with v0.1.0a10](https://github.com/DataShades/ckanext-collection/compare/v0.1.0a10...v0.1.0)</small>
Expand Down
13 changes: 9 additions & 4 deletions ckanext/collection/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@

from ckanext.collection import types

CONFIG_ANNONYMOUS = "ckanext.collection.auth.anonymous_collections"
CONFIG_AUTHENTICATED = "ckanext.collection.auth.authenticated_collections"
CONFIG_INCLUDE_ASSET = "ckanext.collection.include_htmx_asset"
CONFIG_INIT_MODULES = "ckanext.collection.htmx_init_modules"


def anonymous_collections() -> list[str]:
return tk.config["ckanext.collection.auth.anonymous_collections"]
return tk.config[CONFIG_ANNONYMOUS]


def authenticated_collections() -> list[str]:
return tk.config["ckanext.collection.auth.authenticated_collections"]
return tk.config[CONFIG_AUTHENTICATED]


def include_htmx_asset() -> bool:
return tk.config["ckanext.collection.include_htmx_asset"]
return tk.config[CONFIG_INCLUDE_ASSET]


def htmx_init_modules() -> bool:
return tk.config["ckanext.collection.htmx_init_modules"]
return tk.config[CONFIG_INIT_MODULES]


def serializer(format: str) -> type[types.BaseSerializer] | None:
Expand Down
13 changes: 12 additions & 1 deletion ckanext/collection/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ckan.common import CKANConfig

from . import shared
from .interfaces import ICollection
from .interfaces import CollectionFactory, ICollection


@tk.blanket.blueprints
Expand All @@ -16,6 +16,7 @@
class CollectionPlugin(p.SingletonPlugin):
p.implements(p.IConfigurer)
p.implements(p.IConfigurable)
p.implements(ICollection, inherit=True)

# IConfigurer

Expand All @@ -31,3 +32,13 @@ def configure(self, config_: CKANConfig):
for plugin in p.PluginImplementations(ICollection):
for name, factory in plugin.get_collection_factories().items():
shared.collection_registry.register(name, factory)

def get_collection_factories(self) -> dict[str, CollectionFactory]:
if tk.config["debug"]:
from .utils import CollectionExplorerCollection

return {
"collection-explorer": CollectionExplorerCollection,
}

return {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% if not request.headers["hx-request"] %}
{% extends "page.html" %}
{% asset "collection/htmx" %}
{% endif %}

{% block main_content %}
<ul hx-boost="true" hx-target="#collection-outlet" hx-push-url="false">
{%- for record in collection %}
<li>
<a href="{{ h.url_for('ckanext-collection.render', name=record) }}">
{{ record }}
</a>
</li>
{% endfor -%}
</ul>

<div id="collection-outlet">
</div>

{% endblock %}
11 changes: 11 additions & 0 deletions ckanext/collection/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

from ckanext.collection import shared


@pytest.fixture()
def collection_registry():
"""Collection registry cleaned after each test."""

yield shared.collection_registry
shared.collection_registry.reset()
28 changes: 27 additions & 1 deletion ckanext/collection/tests/utils/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from typing import Any

from ckanext.collection import utils
import pytest

from ckan.tests.helpers import call_action

from ckanext.collection import config, shared, utils


class TestCollection:
Expand Down Expand Up @@ -40,3 +44,25 @@ def test_replace_service(self):

assert collection.data is data
assert collection.columns is columns


@pytest.mark.usefixtures("with_plugins")
class TestCollectionExplorerCollection:
@pytest.mark.ckan_config(config.CONFIG_ANNONYMOUS, "hello")
def test_only_accessible_collections_are_visible(
self,
collection_registry: shared.Registry[Any],
):
collection_registry.register("hello", utils.Collection)
collection_registry.register("world", utils.Collection)

explorer = utils.CollectionExplorerCollection("", {})
assert set(explorer) == {"hello"}

user = call_action("get_site_user")
explorer = utils.CollectionExplorerCollection(
"",
{},
data_settings={"user": user["name"]},
)
assert set(explorer) == {"hello", "world"}
16 changes: 9 additions & 7 deletions ckanext/collection/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ApiListCollection,
ApiSearchCollection,
Collection,
CollectionExplorerCollection,
ModelCollection,
StaticCollection,
)
Expand Down Expand Up @@ -32,16 +33,17 @@
)

__all__ = [
"StaticCollection",
"ApiCollection",
"ApiListCollection",
"ApiSearchCollection",
"ApiData",
"ApiListCollection",
"ApiListData",
"ApiSearchCollection",
"ApiSearchData",
"BaseModelData",
"ChartJsSerializer",
"ClassicPager",
"Collection",
"CollectionExplorerCollection",
"Columns",
"CsvSerializer",
"Data",
Expand All @@ -52,11 +54,11 @@
"JsonlSerializer",
"ModelCollection",
"ModelData",
"UnionModelData",
"StatementModelData",
"BaseModelData",
"StaticData",
"Pager",
"Serializer",
"StatementModelData",
"StaticCollection",
"StaticData",
"TableSerializer",
"UnionModelData",
]
26 changes: 24 additions & 2 deletions ckanext/collection/utils/collection.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import annotations

from typing import Any, Iterator, overload
from typing import Any, Iterable, Iterator, overload

from typing_extensions import Self

from ckan.authz import is_authorized_boolean

from ckanext.collection import shared, types

from .columns import Columns
from .data import ApiData, ApiListData, ApiSearchData, Data, ModelData, StaticData
from .filters import Filters
from .pager import ClassicPager, Pager
from .serialize import Serializer
from .serialize import HtmlSerializer, Serializer


class Collection(types.BaseCollection[types.TData]):
Expand Down Expand Up @@ -196,3 +198,23 @@ class ApiSearchCollection(ApiCollection[types.TData]):

class ApiListCollection(ApiCollection[types.TData]):
DataFactory = ApiListData


class CollectionExplorerCollection(Collection[str]):
class DataFactory(Data[str, "CollectionExplorerCollection"], shared.UserTrait):
def compute_data(self) -> Iterable[str]:
return [
str(name)
for name in shared.collection_registry.members
if name != self.attached.name
and is_authorized_boolean(
"collection_view_render",
{"user": self.user},
{"name": name},
)
]

class SerializerFactory(HtmlSerializer["CollectionExplorerCollection"]):
main_template: str = shared.configurable_attribute(
"collection/serialize/collection_explorer/main.html",
)
File renamed without changes.
File renamed without changes.

0 comments on commit 42e547f

Please sign in to comment.