from collections import OrderedDict
from onegov.core.security import Private
from onegov.form.collection import SurveySubmissionCollection
from onegov.form.models.definition import SurveyDefinition
from onegov.form.models.submission import SurveySubmission
from onegov.form.models.survey_window import SurveySubmissionWindow
from onegov.org import _
from onegov.org import OrgApp
from onegov.org.forms.survey_export import SurveySubmissionsExport
from onegov.org.layout import SurveySubmissionLayout
from onegov.core.elements import Link
from onegov.org.utils import keywords_first
from typing import Any, NamedTuple, TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable, Collection, Sequence
from datetime import date
from onegov.core.orm.types import UUID
from onegov.core.types import RenderData
from onegov.org.request import OrgRequest
from sqlalchemy.orm import Query
from webob import Response
[docs]
class SurveySubmissionRow(NamedTuple):
[docs]
submission_window_start: date
[docs]
submission_window_end: date
@OrgApp.form(
model=SurveyDefinition,
name='export',
permission=Private,
form=SurveySubmissionsExport,
template='export.pt'
)
[docs]
def subset_by_window(
submissions: SurveySubmissionCollection,
window_ids: 'Collection[UUID]'
) -> 'Query[SurveySubmission]':
return (
submissions.query()
.filter(SurveySubmission.submission_window_id.in_(window_ids))
)
[docs]
def run_export(
subset: 'Query[SurveySubmissionRow]',
nested: bool,
formatter: 'Callable[[object], Any]'
) -> tuple['Callable[[str], tuple[int, str]]', 'Sequence[dict[str, Any]]']:
keywords = (
'submission_window_start',
'submission_window_end'
)
def transform(submission: 'SurveySubmissionRow') -> dict[str, Any]:
r = OrderedDict()
for keyword in keywords:
r[keyword] = formatter(getattr(submission, keyword))
if nested:
r['survey'] = {
k: formatter(v)
for k, v in submission.data.items()
}
else:
for k, v in submission.data.items():
r[f'{k}'] = formatter(v)
return r
return keywords_first(keywords), tuple(transform(s) for s in subset)