Skip to content

Commit

Permalink
feat: add model data support to CsvSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Dec 5, 2023
1 parent 316af62 commit b774684
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ckanext/collection/utils/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ckan.plugins.toolkit as tk
import operator
from functools import reduce
from sqlalchemy.engine import Row
from typing import Any, Iterable
from ckanext.collection.types import TDataCollection
from .shared import AttachTrait, AttrSettingsTrait
Expand Down Expand Up @@ -46,6 +47,8 @@ def get_header_row(self, writer: csv.DictWriter[str]) -> dict[str, str]:
}

def prepare_row(self, row: Any, writer: csv.DictWriter[str]) -> dict[str, Any]:
if isinstance(row, Row):
return dict(zip(row.keys(), row))
return row

def stream(self) -> Iterable[str]:
Expand All @@ -58,6 +61,7 @@ def stream(self) -> Iterable[str]:
buff.seek(0)
buff.truncate()


for row in self._collection.data:
writer.writerow(self.prepare_row(row, writer))
yield buff.getvalue()
Expand Down

0 comments on commit b774684

Please sign in to comment.