Skip to content
Snippets Groups Projects
Commit 32718825 authored by Remco Tukker's avatar Remco Tukker
Browse files

make an endpoint for publishing for preview and use it from the survey management page

parent 3bd0ff2c
Branches
Tags
1 merge request!71Create a preview mode for publishing survey data
...@@ -2,12 +2,12 @@ import logging ...@@ -2,12 +2,12 @@ import logging
from typing import Any, TypedDict, List, Dict from typing import Any, TypedDict, List, Dict
from flask import Blueprint from flask import Blueprint
from sqlalchemy import select from sqlalchemy import delete, select
from sqlalchemy.orm import joinedload, load_only from sqlalchemy.orm import joinedload, load_only
from compendium_v2.db import db from compendium_v2.db import db
from compendium_v2.db.model import NREN from compendium_v2.db.model import NREN, PreviewYear
from compendium_v2.db.survey_model import Survey, SurveyResponse, SurveyStatus, ResponseStatus, RESPONSE_NOT_STARTED from compendium_v2.db.survey_model import Survey, SurveyResponse, SurveyStatus, RESPONSE_NOT_STARTED
from compendium_v2.routes import common from compendium_v2.routes import common
from compendium_v2.auth.session_management import admin_required from compendium_v2.auth.session_management import admin_required
...@@ -179,6 +179,34 @@ def close_survey(year) -> Any: ...@@ -179,6 +179,34 @@ def close_survey(year) -> Any:
return {'success': True} return {'success': True}
@routes.route('/preview/<int:year>', methods=['POST'])
@common.require_accepts_json
@admin_required
def preview_survey(year) -> Any:
"""
endpoint to preview a survey on the compendium website
:returns: ``{'success': True}`` or a 400 or 404 status with a descriptive message
"""
survey = db.session.scalar(select(Survey).where(Survey.year == year))
if not survey:
return {'message': 'Survey not found'}, 404
if survey.status not in [SurveyStatus.closed, SurveyStatus.preview]:
return {'message': 'Survey is not closed or in preview and can therefore not be published for preview'}, 400
# TODO call new survey_publisher with all completed responses and the year
preview = db.session.scalar(select(PreviewYear).where(PreviewYear.year == year))
if not preview:
db.session.add(PreviewYear(year=year))
survey.status = SurveyStatus.preview
db.session.commit()
return {'success': True}
@routes.route('/publish/<int:year>', methods=['POST']) @routes.route('/publish/<int:year>', methods=['POST'])
@common.require_accepts_json @common.require_accepts_json
@admin_required @admin_required
...@@ -192,14 +220,12 @@ def publish_survey(year) -> Any: ...@@ -192,14 +220,12 @@ def publish_survey(year) -> Any:
if not survey: if not survey:
return {'message': 'Survey not found'}, 404 return {'message': 'Survey not found'}, 404
if survey.status not in [SurveyStatus.closed, SurveyStatus.published]: if survey.status not in [SurveyStatus.preview, SurveyStatus.published]:
return {'message': 'Survey is not closed or published and can therefore not be published'}, 400 return {'message': 'Survey is not in preview or published and can therefore not be published'}, 400
# TODO probably replace checked state with something else # TODO call new survey_publisher with all completed responses and the year
if any([response.status != ResponseStatus.checked for response in survey.responses]):
return {'message': 'There are responses that arent checked yet'}, 400
# TODO call new survey_publisher with all responses and the year db.session.execute(delete(PreviewYear).where(PreviewYear.year == year))
survey.status = SurveyStatus.published survey.status = SurveyStatus.published
db.session.commit() db.session.commit()
......
This diff is collapsed.
This diff is collapsed.
...@@ -26,12 +26,12 @@ export enum VerificationStatus { ...@@ -26,12 +26,12 @@ export enum VerificationStatus {
export enum ResponseStatus { export enum ResponseStatus {
not_started = "not started", not_started = "not started",
started = "started", started = "started",
completed = "completed", completed = "completed"
checked = "checked"
} }
export enum SurveyStatus { export enum SurveyStatus {
closed = "closed", closed = "closed",
open = "open", open = "open",
preview = "preview",
published = "published" published = "published"
} }
...@@ -63,6 +63,8 @@ function SurveyManagementComponent() { ...@@ -63,6 +63,8 @@ function SurveyManagementComponent() {
const navigate = useNavigate(); const navigate = useNavigate();
const previewLink = window.location.origin + "/data?preview";
return ( return (
<div> <div>
<Button onClick={newSurvey} disabled={!newSurveyAllowed} style={{ pointerEvents: 'auto' }} <Button onClick={newSurvey} disabled={!newSurveyAllowed} style={{ pointerEvents: 'auto' }}
...@@ -83,7 +85,7 @@ function SurveyManagementComponent() { ...@@ -83,7 +85,7 @@ function SurveyManagementComponent() {
title="Open the survey exactly as the nrens will see it, but without any nren data."> title="Open the survey exactly as the nrens will see it, but without any nren data.">
Try Survey Try Survey
</Button> </Button>
{survey.status !== 'published' && {survey.status !== SurveyStatus.published &&
<Button onClick={() => postSurveyStatus(survey.year, 'open')} disabled={survey.status != SurveyStatus.closed} style={{ pointerEvents: 'auto', marginLeft: '.5rem' }} <Button onClick={() => postSurveyStatus(survey.year, 'open')} disabled={survey.status != SurveyStatus.closed} style={{ pointerEvents: 'auto', marginLeft: '.5rem' }}
title="Allow the NRENs to respond to this survey. Only 1 survey may be open at a time, and published surveys cannot be opened anymore."> title="Allow the NRENs to respond to this survey. Only 1 survey may be open at a time, and published surveys cannot be opened anymore.">
Mark as Open Mark as Open
...@@ -92,12 +94,15 @@ function SurveyManagementComponent() { ...@@ -92,12 +94,15 @@ function SurveyManagementComponent() {
title="Do not allow the NRENs to respond to this survey anymore. Only surveys with status open can be closed."> title="Do not allow the NRENs to respond to this survey anymore. Only surveys with status open can be closed.">
Mark as closed Mark as closed
</Button> </Button>
<Button onClick={() => postSurveyStatus(survey.year, 'publish')} disabled={(survey.status != SurveyStatus.closed && survey.status != SurveyStatus.published) || !survey.responses.every(r => r.status == ResponseStatus.checked)} style={{ pointerEvents: 'auto', marginLeft: '.5rem' }} <Button onClick={() => postSurveyStatus(survey.year, 'preview')} disabled={survey.status != SurveyStatus.closed && survey.status != SurveyStatus.preview} style={{ pointerEvents: 'auto', marginLeft: '.5rem' }}
title="Publish or re-publish all survey responses to the compendium website. This is only possible if the survey is closed or published already, and all responses are checked."> title="Publish all completed survey responses to the compendium website for preview. This is only possible if the survey is closed or previewed already.">
Preview results
</Button>
<Button onClick={() => postSurveyStatus(survey.year, 'publish')} disabled={survey.status != SurveyStatus.preview && survey.status != SurveyStatus.published} style={{ pointerEvents: 'auto', marginLeft: '.5rem' }}
title="Publish or re-publish all completed survey responses to the compendium website. This is only possible if the survey is in preview or published already.">
Publish results Publish results
</Button> </Button>
{/* maybe something like this: <Button disabled={survey.status!='published'}>Show in compendium</Button> {survey.status == SurveyStatus.preview && <span>&nbsp; Preview link: <a href={previewLink}>{previewLink}</a></span>}
<Button disabled={survey.status!='published'}>Hide from compendium</Button> */}
</div> </div>
<Table> <Table>
<tbody> <tbody>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment