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

correctly handle opening a survey that the user isnt allowed to edit

parent 9f5b9a45
No related branches found
No related tags found
1 merge request!63Feature/locking bugfixes
...@@ -30,6 +30,7 @@ SURVEY_RESPONSE_SCHEMA = { ...@@ -30,6 +30,7 @@ SURVEY_RESPONSE_SCHEMA = {
'verification_status': {'type': 'object'}, 'verification_status': {'type': 'object'},
'mode': {'type': 'string'}, 'mode': {'type': 'string'},
'status': {'type': 'string'}, 'status': {'type': 'string'},
'edit_allowed': {'type': 'boolean'},
}, },
'required': ['model', 'locked_by', 'data', 'page', 'verification_status', 'mode', 'status'], 'required': ['model', 'locked_by', 'data', 'page', 'verification_status', 'mode', 'status'],
'additionalProperties': False 'additionalProperties': False
...@@ -111,7 +112,8 @@ def try_survey(year) -> Any: ...@@ -111,7 +112,8 @@ def try_survey(year) -> Any:
"page": 0, "page": 0,
"verification_status": {}, "verification_status": {},
"mode": SurveyMode.Edit, "mode": SurveyMode.Edit,
"status": RESPONSE_NOT_STARTED "status": RESPONSE_NOT_STARTED,
"edit_allowed": True
} }
...@@ -152,7 +154,8 @@ def inspect_survey(year) -> Any: ...@@ -152,7 +154,8 @@ def inspect_survey(year) -> Any:
"page": 0, "page": 0,
"verification_status": {}, "verification_status": {},
"mode": SurveyMode.Edit, "mode": SurveyMode.Edit,
"status": RESPONSE_NOT_STARTED "status": RESPONSE_NOT_STARTED,
"edit_allowed": True
} }
...@@ -219,7 +222,8 @@ def load_survey(year, nren_name) -> Any: ...@@ -219,7 +222,8 @@ def load_survey(year, nren_name) -> Any:
"page": page, "page": page,
"verification_status": verification_status, "verification_status": verification_status,
"mode": SurveyMode.Display, "mode": SurveyMode.Display,
"status": response.status.value if response else RESPONSE_NOT_STARTED "status": response.status.value if response else RESPONSE_NOT_STARTED,
"edit_allowed": current_user.is_admin or survey.status == SurveyStatus.open
} }
...@@ -248,6 +252,9 @@ def lock_survey(year, nren_name) -> Any: ...@@ -248,6 +252,9 @@ def lock_survey(year, nren_name) -> Any:
if not check_access_nren(current_user, nren): if not check_access_nren(current_user, nren):
return {'message': 'You do not have permissions to access this survey.'}, 403 return {'message': 'You do not have permissions to access this survey.'}, 403
if survey.status != SurveyStatus.open and not current_user.is_admin:
return {'message': 'Survey is closed'}, 400
response = db.session.scalar( response = db.session.scalar(
select(SurveyResponse).where(SurveyResponse.survey_year == year) select(SurveyResponse).where(SurveyResponse.survey_year == year)
.where(SurveyResponse.nren_id == nren.id) .where(SurveyResponse.nren_id == nren.id)
... ...
......
...@@ -66,6 +66,7 @@ function SurveyContainerComponent({ loadFrom }) { ...@@ -66,6 +66,7 @@ function SurveyContainerComponent({ loadFrom }) {
survey.mode = json['mode']; survey.mode = json['mode'];
survey.lockedBy = json['locked_by']; survey.lockedBy = json['locked_by'];
survey.status = json['status']; survey.status = json['status'];
survey.editAllowed = json['edit_allowed'];
setSurveyModel(survey); setSurveyModel(survey);
} }
... ...
......
...@@ -49,7 +49,7 @@ function SurveyNavigationComponent({ surveyModel, surveyActions, year, nren, chi ...@@ -49,7 +49,7 @@ function SurveyNavigationComponent({ surveyModel, surveyActions, year, nren, chi
const renderExternalNavigation = () => { const renderExternalNavigation = () => {
return ( return (
<div className="survey-edit-buttons-block"> <div className="survey-edit-buttons-block">
{!editing && !lockedBy && renderButton('Start editing', 'startEdit')} {!editing && !lockedBy && surveyModel.editAllowed && renderButton('Start editing', 'startEdit')}
{!editing && lockedBy && lockedBy == loggedInUser.name && renderButton('Discard any unsaved changes and release your lock', 'releaseLock')} {!editing && lockedBy && lockedBy == loggedInUser.name && renderButton('Discard any unsaved changes and release your lock', 'releaseLock')}
{editing && (pageNo === surveyModel.visiblePages.length - 1) && renderButton('Complete Survey', 'complete')} {editing && (pageNo === surveyModel.visiblePages.length - 1) && renderButton('Complete Survey', 'complete')}
{editing && renderButton(saveAndStopEdit, 'saveAndStopEdit')} {editing && renderButton(saveAndStopEdit, 'saveAndStopEdit')}
...@@ -83,7 +83,8 @@ function SurveyNavigationComponent({ surveyModel, surveyActions, year, nren, chi ...@@ -83,7 +83,8 @@ function SurveyNavigationComponent({ surveyModel, surveyActions, year, nren, chi
<Row className="survey-content"> <Row className="survey-content">
{!editing && ( {!editing && (
<div className="survey-edit-explainer"> <div className="survey-edit-explainer">
{!lockedBy && <span>The survey is in read-only mode; click the “Start editing“ button to start editing the answers.</span>} {!lockedBy && surveyModel.editAllowed && <span>The survey is in read-only mode; click the “Start editing“ button to start editing the answers.</span>}
{!lockedBy && !surveyModel.editAllowed && <span>The survey is in read-only mode and can not be edited by you.</span>}
{lockedBy && lockedBy != loggedInUser.name && 'The survey is in READONLY mode and currently being edited by: ' + lockedBy + '. To start editing the survey, ask them to complete their edits.'} {lockedBy && lockedBy != loggedInUser.name && 'The survey is in READONLY mode and currently being edited by: ' + lockedBy + '. To start editing the survey, ask them to complete their edits.'}
{lockedBy && lockedBy == loggedInUser.name && 'The survey is in READONLY mode because you started editing in another tab, browser or device. To start editing the survey, either complete those edits or click the "Discard any unsaved changes" button.'} {lockedBy && lockedBy == loggedInUser.name && 'The survey is in READONLY mode because you started editing in another tab, browser or device. To start editing the survey, either complete those edits or click the "Discard any unsaved changes" button.'}
</div> </div>
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment