from flask import g, Response from datetime import datetime from auth.auth_view import AuthorizedView from projects.services.projects_exporter import ProjectsExporter from utils.handlers import get_request_query_model from projects.schemas import ProjectListQueryParams from dateutil.tz import gettz class ExportProjectsView(AuthorizedView): """Export projects to CSV.""" def get(self): """Export projects to CSV. Generated CSV file has the same set of fields as the table on projects dashboard. This endpoint also accepts the same set of filters as the projects dashboard endpoint plus filter for exporting selected projects by ID. Returns: HTTP 200: CSV file with project list. Raises: HTTP 401: If user is not authenticated. """ params = get_request_query_model(ProjectListQueryParams) service = ProjectsExporter() csv_content = service.export_projects_csv(g.user_id, params) response = Response(csv_content, mimetype="text/csv") response.headers["Content-Disposition"] = "attachment; filename={} All Projects.csv".format( datetime.now(gettz(params.timeZone)).strftime("%Y-%m-%d %H-%M-%S") ) return response