def post(self): schema = QuestionnaireSchema() data, errors = schema.load(request.json) for err in errors: if err in data: del data[err] if not data: return { 'message': 'Questionnaire not created. Some errors occurred.', 'errors': errors }, 400 questionnaire = ConcreteQuestionnaire(data['name'], data['description']) for k, v in data.items(): if k == 'template' and not current_has_minimum_role( Role.Contributor): errors[ k] = 'You need to be a contributor to publish templates.' continue db.session.add(questionnaire) db.session.commit() response = { 'message': 'Questionnaire created.', 'questionnaire': schema.dump(questionnaire).data } if errors: response['message'] += ' Some errors occurred.' response['errors'] = errors return response, 201
def patch(self): dataclient = current_user() schema = DataClientSchema(partial=True) data, errors = schema.load(request.json) for err in errors: if err in data: del data[err] if not data: return { 'message': 'DataClient not updated. Some errors occurred', 'errors': errors }, 400 for k, v in data.items(): if k == 'roles': if not current_has_minimum_role(Role.Admin): errors[k] = [ 'You are note allowed to change user\'s ' 'roles' ] continue dataclient.update_roles(data['roles']) setattr(dataclient, k, v) db.session.commit() response = { 'message': 'DataClient updated.', 'data_client': schema.dump(dataclient).data } if errors: response['message'] += ' Some errors occurred.' response['errors'] = errors return response
def get(self, dataclient_id=None): schema = DataClientSchema() if dataclient_id != current_user().id: if not current_has_minimum_role(Role.Admin): abort(403, message='You are not allowed to access other user\'s' ' data.') dataclient = DataClient.query.get_or_404(dataclient_id) return schema.dump(dataclient).data
def get(self, dataclient_id=None): if dataclient_id is None: dataclient = current_user() else: if not current_has_minimum_role(Role.Admin): abort(403) dataclient = DataClient.query.get(dataclient_id) query = query_owned(DataClient, dataclient.id, Questionnaire) questionnaires = query.all() schema = QuestionnaireSchema(many=True) return schema.dump(questionnaires).data
def patch(self, questionnaire_id=None, dimension_id=None, question_id=None): question = self.get_question_or_404(questionnaire_id, dimension_id, question_id) if not question.accessible_by(current_user()): abort(403) if not question.modifiable_by(current_user()): abort(403) schema = QuestionSchema() data, errors = schema.load(request.json, partial=True) for err in errors: if err in data: del data[err] if not data: return { 'message': 'Question not updated. Some errors occurred.', 'errors': errors }, 400 shadow_attributes = ['position'] for k, v in data.items(): if isinstance(question, ShadowQuestion): if k not in shadow_attributes: errors[k] = [ 'Can\'t update {} of a ShadowQuestion. The contributor owning ' 'the template is responsible for the Dimension\'s content.' .format(k) ] continue else: if 'k' == 'template': if not current_has_minimum_role(Role.Contributor): errors[k] = [ 'You need to be a contributor to publish ' 'templates.' ] continue setattr(question, k, v) db.session.commit() response = { 'message': 'Question updated.', 'question': schema.dump(question).data } if errors: response['message'] += ' Some errors occurred.' response['errors'] = errors return response
def delete(self, dataclient_id=None): if dataclient_id != current_user().id: if not current_has_minimum_role(Role.Admin): abort(403, message='You are not allowed to remove other user\'s' ' accounts.') dataclient = DataClient.query.get_or_404(dataclient_id) dataclient_data = DataClientSchema().dump(dataclient).data db.session.delete(dataclient) db.session.commit() return { 'message': 'DataClient removed', 'data_client': dataclient_data }
def post(self, questionnaire_id: int = None, dimension_id: int = None): schema = QuestionSchema() data, errors = schema.load(request.json) for err in errors: if err in data: del data[err] if not data: return { 'message': 'Question not created. Some errors occurred.', 'errors': errors }, 400 dimension = Dimension.query.get_or_404(dimension_id) if questionnaire_id is not None: if dimension.questionnaire_id != questionnaire_id: abort(404) if not dimension.accessible_by(current_user()): abort(403) if not dimension.modifiable_by(current_user()): abort(403) if dimension.shadow: abort(403) question = dimension.new_question(data['text'], data['range_start_label'], data['range_end_label']) for k, v in data.items(): if 'k' == 'template': if not current_has_minimum_role(Role.Contributor): errors[k] = [ 'You need to be a contributor to publish ' 'templates.' ] continue setattr(question, k, v) db.session.commit() data = QuestionSchema().dump(question).data response = {'message': 'Question created.', 'question': data} if errors: response['message'] += ' Some errors occurred.' response['errors'] = errors return response, 201
def post(self, questionnaire_id=None): schema = DimensionSchema() data, errors = schema.load(request.json) for err in errors: if err in data: del data[err] if not data: return { 'message': 'Dimension not created. Some errors occurred.', 'errors': errors }, 400 questionnaire = Questionnaire.query.get_or_404(questionnaire_id) if not questionnaire.accessible_by(current_user()): abort(403) if not questionnaire.modifiable_by(current_user()): abort(403) if questionnaire.shadow: abort(403) dimension = questionnaire.new_dimension(data['name']) for k, v in data.items(): if k == 'template': if not current_has_minimum_role(Role.Contributor): errors[k] = ['You need to be a contributor to publish ' 'templates.'] continue setattr(dimension, k, v) db.session.commit() data = DimensionSchema().dump(dimension).data response = { 'message': 'Dimension created.', 'dimension': data } if errors: response['message'] += ' Some errors occurred.' response['errors'] = errors return response, 201
def patch(self, questionnaire_id=None): questionnaire = Questionnaire.query.get_or_404(questionnaire_id) if not questionnaire.modifiable_by(current_user()): abort(403) schema = QuestionnaireSchema(partial=True) data, errors = schema.load(request.json) for err in errors: if err in data: del data[err] if not data: return { 'message': 'Questionnaire not updated. Some errors occurred.', 'errors': errors }, 400 previously_published = copy(questionnaire.published) shadow_attributes = [ 'published', 'accepts_submissions', 'allow_embedded', 'xapi_target' ] for k, v in data.items(): if isinstance(questionnaire, ShadowQuestionnaire): if k not in shadow_attributes: errors[k] = [ 'Can\'t update {} of a ShadowQuestionnaire. The ' 'contributor owning the template is responsible ' 'for the Questionnaire\'s content.'.format(k) ] continue else: if k == 'template' and not current_has_minimum_role( Role.Contributor): errors[k] = [ 'You need to be a contributor to publish templates.' ] continue elif k == 'accepts_submissions': if not questionnaire.published: errors[k] = [ 'You can\'t set an unpublished Questionnaire to accept submissions. ' 'Please publish it first.' ] continue elif k == 'published': if questionnaire.accepts_submissions: errors[k] = [ 'The Questionnaire still accepts submissions. Please conclude it first.' ] continue setattr(questionnaire, k, v) db.session.commit() if not previously_published and questionnaire.published: SIG_QUESTIONNAIRE_PUBLISHED.send(questionnaire) if previously_published and not questionnaire.published: SIG_QUESTIONNAIRE_UNPUBLISHED.send(questionnaire) response = { 'message': 'Questionnaire updated.', 'questionnaire': schema.dump(questionnaire).data } if errors: response['message'] += ' Some errors occurred.' response['errors'] = errors return response