Skip to content

Commit

Permalink
feat: table serializer use record/collection variables instead of row…
Browse files Browse the repository at this point in the history
…/table
  • Loading branch information
smotornyuk committed Dec 30, 2023
1 parent 0be661c commit 8189eb0
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 35 deletions.
1 change: 0 additions & 1 deletion ckanext/collection/templates/collection/snippets/dump.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ul>
{%- for record in collection %}
{%- snippet record_template, record=record, collection=collection -%}
{% endfor -%}
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<li>{{ record | tojson }}</li>
16 changes: 0 additions & 16 deletions ckanext/collection/templates/collection/snippets/table.html

This file was deleted.

16 changes: 16 additions & 0 deletions ckanext/collection/templates/collection/snippets/table_main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table id="collection--{{ collection.name }}--table" class="collection-table">
<tr class="collection-table--row collection-table--header">
{% for col in collection.columns.names if col in collection.columns.visible %}

<td class="collection-table--cell{% if sortable %} collection-table--cell-sortable{% endif %}">
<span>{{ collection.columns.labels[col] or col }}</span>
</td>

{% endfor %}
</tr>

{%- for record in collection %}
{%- snippet record_template, record=record, collection=collection -%}
{% endfor -%}

</table>
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<tr class="collection-table--row">
{% for column in table.columns.names if column in table.columns.visible %}
{% for column in collection.columns.names if column in collection.columns.visible -%}
<td class="collection-table--cell{% if sortable %} collection-table--cell-sortable{% endif %}">
<div>
{{ row[column] }}
{{ record[column] }}
</div>
</td>
{% endfor %}

{%- endfor %}
</tr>
Empty file.
6 changes: 4 additions & 2 deletions ckanext/collection/tests/utils/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def test_output(self, collection: StaticCollection, ckan_config: Any):
serializer = serialize.HtmlSerializer(collection)
output = serializer.render().strip()

dump = json.dumps(list(collection.data), sort_keys=True)
expected_output = f"Dump: {dump}"
expected_output = (
"""<ul><li>{"age": 1, "name": "a"}</li>"""
+ """"<li>{"age": 2, "name": "b"}</li></ul>"""
)

assert output == expected_output
26 changes: 19 additions & 7 deletions ckanext/collection/utils/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from ckanext.collection import types

from . import data
from .columns import Columns
from .data import ApiData, Data, ModelData
from .filters import Filters
from .pager import ClassicPager, Pager
from .serialize import Serializer
Expand Down Expand Up @@ -73,7 +73,7 @@ class Collection(types.BaseCollection[types.TData]):

# keep these classes here to simplify overrides
ColumnsFactory: type[Columns[Self]] = Columns
DataFactory: type[Data[types.TData, Self]] = Data
DataFactory: type[data.Data[types.TData, Self]] = data.Data
FiltersFactory: type[Filters[Self]] = Filters
SerializerFactory: type[Serializer[Self]] = Serializer
PagerFactory: type[Pager[Self]] = ClassicPager
Expand Down Expand Up @@ -117,6 +117,9 @@ def _instantiate(self, name: str, kwargs: dict[str, Any]) -> Any:

return value

def __iter__(self) -> Iterator[types.TData]:
yield from self.data.range(self.pager.start, self.pager.end)

def make_serializer(self, **kwargs: Any) -> Serializer[Self]:
"""Return serializer."""
return self.SerializerFactory(self, **kwargs)
Expand All @@ -133,17 +136,26 @@ def make_filters(self, **kwargs: Any) -> Filters[Self]:
"""Return search filters."""
return self.FiltersFactory(self, **kwargs)

def make_data(self, **kwargs: Any) -> Data[types.TData, Self]:
def make_data(self, **kwargs: Any) -> data.Data[types.TData, Self]:
"""Return search filters."""
return self.DataFactory(self, **kwargs)

def __iter__(self) -> Iterator[types.TData]:
yield from self.data.range(self.pager.start, self.pager.end)

class StaticCollection(Collection[types.TData]):
DataFactory = data.StaticData


class ModelCollection(Collection[types.TData]):
DataFactory = ModelData
DataFactory = data.ModelData


class ApiCollection(Collection[types.TData]):
DataFactory = ApiData
DataFactory = data.ApiData


class ApiSearchCollection(ApiCollection[types.TData]):
DataFactory = data.ApiSearchData


class ApiListCollection(ApiCollection[types.TData]):
DataFactory = data.ApiListData
19 changes: 14 additions & 5 deletions ckanext/collection/utils/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,32 +161,41 @@ def stream(self):
class HtmlSerializer(Serializer[types.TDataCollection]):
"""Serialize collection into HTML document."""

template: str = shared.configurable_attribute("collection/snippets/dump.html")
main_template: str = shared.configurable_attribute(
"collection/snippets/html_main.html",
)
record_template: str = shared.configurable_attribute(
"collection/snippets/html_record.html",
)

def get_data(self) -> dict[str, Any]:
return {
"collection": self.attached,
"record_template": self.record_template,
}

def stream(self):
yield tk.render(
self.template,
self.main_template,
self.get_data(),
)


class TableSerializer(HtmlSerializer[types.TDataCollection]):
"""Serialize collection into HTML table."""

templatel: str = shared.configurable_attribute("collection/snippets/table.html")
row_snippet: str = shared.configurable_attribute("collection/snippets/row.html")
main_template: str = shared.configurable_attribute(
"collection/snippets/table_main.html",
)
record_template: str = shared.configurable_attribute(
"collection/snippets/table_record.html",
)

def get_data(self) -> dict[str, Any]:
data = super().get_data()
data.update(
{
"table": self.attached,
"row_snippet": self.row_snippet,
},
)
return data

0 comments on commit 8189eb0

Please sign in to comment.