snapshot of https://github.com/makeplane/plane.git @ d868349a55de79979eb8b98701bf1702c67ad634 (2026-09-15)
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
This commit is contained in:
76
apps/api/plane/utils/exporters/exporter.py
Normal file
76
apps/api/plane/utils/exporters/exporter.py
Normal file
@@ -0,0 +1,76 @@
|
||||
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# See the LICENSE file for details.
|
||||
|
||||
from typing import Any, Dict, List, Type, Union
|
||||
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from .formatters import CSVFormatter, JSONFormatter, XLSXFormatter
|
||||
|
||||
|
||||
class Exporter:
|
||||
"""Generic exporter class that handles data exports using different formatters."""
|
||||
|
||||
# Available formatters
|
||||
FORMATTERS = {
|
||||
"csv": CSVFormatter,
|
||||
"json": JSONFormatter,
|
||||
"xlsx": XLSXFormatter,
|
||||
}
|
||||
|
||||
def __init__(self, format_type: str, schema_class: Type, options: Dict[str, Any] = None):
|
||||
"""Initialize exporter with specified format type and schema.
|
||||
|
||||
Args:
|
||||
format_type: The export format (csv, json, xlsx)
|
||||
schema_class: The schema class to use for field definitions
|
||||
options: Optional formatting options
|
||||
"""
|
||||
if format_type not in self.FORMATTERS:
|
||||
raise ValueError(f"Unsupported format: {format_type}. Available: {list(self.FORMATTERS.keys())}")
|
||||
|
||||
self.format_type = format_type
|
||||
self.schema_class = schema_class
|
||||
self.formatter = self.FORMATTERS[format_type]()
|
||||
self.options = options or {}
|
||||
|
||||
def export(
|
||||
self,
|
||||
filename: str,
|
||||
data: Union[QuerySet, List[dict]],
|
||||
fields: List[str] = None,
|
||||
) -> tuple[str, str | bytes]:
|
||||
"""Export data using the configured formatter and return (filename, content).
|
||||
|
||||
Args:
|
||||
filename: The filename for the export (without extension)
|
||||
data: Either a Django QuerySet or a list of already-serialized dicts
|
||||
fields: Optional list of field names to include in export
|
||||
|
||||
Returns:
|
||||
Tuple of (filename_with_extension, content)
|
||||
"""
|
||||
# Serialize the queryset if needed
|
||||
if isinstance(data, QuerySet):
|
||||
records = self.schema_class.serialize_queryset(data, fields=fields)
|
||||
else:
|
||||
# Already serialized data
|
||||
records = data
|
||||
|
||||
# Merge fields into options for the formatter
|
||||
format_options = {**self.options}
|
||||
if fields:
|
||||
format_options["fields"] = fields
|
||||
|
||||
return self.formatter.format(filename, records, self.schema_class, format_options)
|
||||
|
||||
@classmethod
|
||||
def get_available_formats(cls) -> List[str]:
|
||||
"""Get list of available export formats."""
|
||||
return list(cls.FORMATTERS.keys())
|
||||
|
||||
@classmethod
|
||||
def register_formatter(cls, format_type: str, formatter_class: type) -> None:
|
||||
"""Register a new formatter for a format type."""
|
||||
cls.FORMATTERS[format_type] = formatter_class
|
||||
Reference in New Issue
Block a user