def get(self, name): name = ExportService.camel_case_it(name) if self.request_wants_json(): schema = ExportService.get_schema(name, many=True) return schema.dump(ExportService().get_data(name)) else: return ExportXlsService.export_xls(name=name, app=app)
def post(self, flow, questionnaire_name): flow = Flows.get_flow_by_name(flow) if flow is None: raise RestException(RestException.NOT_FOUND) if not flow.has_step(questionnaire_name): raise RestException(RestException.NOT_IN_THE_FLOW) request_data = request.get_json() request_data["user_id"] = g.user.id if "_links" in request_data: request_data.pop("_links") schema = ExportService.get_schema( ExportService.camel_case_it(questionnaire_name)) new_quest, errors = schema.load(request_data, session=db.session) if errors: raise RestException(RestException.INVALID_OBJECT, details=errors) if new_quest.participant_id is None: raise RestException(RestException.INVALID_OBJECT, details="You must supply a participant id.") if not g.user.related_to_participant(new_quest.participant_id): raise RestException(RestException.UNRELATED_PARTICIPANT) db.session.add(new_quest) db.session.commit() self.log_progress(flow, questionnaire_name, new_quest) return schema.dump(new_quest)
def add_step(self, questionnaireName): if not self.has_step(questionnaireName): class_name = ExportService.camel_case_it(questionnaireName) cls = ExportService.get_class(class_name) q = cls() step = Step(questionnaireName, q.__question_type__, q.__label__) self.steps.append(step)
def get(self, name): if name == "admin": return self.get_admin() name = ExportService.camel_case_it(name) schema = ExportService.get_schema(name, many=True) return schema.dump(ExportService().get_data(name, last_updated=get_date_arg()))
def put(self, name, id): """ Modifies an existing questionnaire record. Parameters: name (str): Snake-cased name of the questionnaire class (should also match the table name), found in app.model.questionnaires. E.g., clinical_diagnoses_questionnaire -> ClinicalDiagnosesQuestionnaire id (int): ID of the questionnaire record to retrieve Returns: The updated questionnaire record. """ name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) instance = db.session.query(class_ref).filter( class_ref.id == id).first() schema = ExportService.get_schema(name, session=db.session) request_data = request.get_json() if "_links" in request_data: request_data.pop("_links") try: updated = schema.load(request_data, instance=instance) except Exception as errors: raise RestException(RestException.INVALID_OBJECT, details=errors) updated.last_updated = datetime.datetime.utcnow() db.session.add(updated) db.session.commit() return schema.dump(updated)
def get(self, name): """ Retrieves metadata about the given questionnaire name. Includes JSON Formly field definition. Used for data export to get meta without specifying flow and relationship. Returns: A dict object containing the metadata about the questionnaire. Example: { table: { question_type: "sensitive", label: "Clinical Diagnosis" }, fields: [ { name: "id", key: "id", display_order: 0 }, ... ] } """ name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) questionnaire = class_ref() meta = {"table": {}} try: meta["table"]['question_type'] = questionnaire.__question_type__ meta["table"]["label"] = questionnaire.__label__ except: pass # If these fields don't exist, just keep going. meta["fields"] = [] # This will move fields referenced by the field groups into the group, but will otherwise add them # the base meta object if they are not contained within a group. for c in questionnaire.__table__.columns: if c.info: c.info['name'] = c.name c.info['key'] = c.name meta['fields'].append(c.info) elif c.type.python_type == datetime.datetime: meta['fields'].append({ 'name': c.name, 'key': c.name, 'display_order': 0, 'type': 'DATETIME' }) else: meta['fields'].append({ 'name': c.name, 'key': c.name, 'display_order': 0 }) # Sort the fields meta['fields'] = sorted(meta['fields'], key=lambda field: field['display_order']) return meta
def get(self, flow, questionnaire_name): questionnaire_name = ExportService.camel_case_it(questionnaire_name) flow = Flows.get_flow_by_name(flow) if flow is None: raise RestException(RestException.NOT_FOUND) class_ref = ExportService.get_class(questionnaire_name) questionnaire = class_ref() return ExportService.get_meta(questionnaire, flow.relationship)
def get(self, name, id): name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) instance = db.session.query(class_ref).filter( class_ref.id == id).first() if instance is None: raise RestException(RestException.NOT_FOUND) schema = ExportService.get_schema(name) return schema.dump(instance)
def get_questionnaire_names(app): all_file_names = os.listdir( os.path.dirname(app.instance_path) + '/app/model/questionnaires') non_questionnaires = ['mixin', '__'] questionnaire_file_names = [] for index, file_name in enumerate(all_file_names): if any(string in file_name for string in non_questionnaires): pass else: f = file_name.replace(".py", "") questionnaire_file_names.append(ExportService.camel_case_it(f)) return sorted(questionnaire_file_names)
def delete(self, name, id): try: name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) instance = db.session.query(class_ref).filter( class_ref.id == id).first() db.session.delete(instance) # db.session.query(class_ref).filter(class_ref.id == id).delete() db.session.commit() except IntegrityError as error: raise RestException(RestException.CAN_NOT_DELETE) return
def put(self, name, id): name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) instance = db.session.query(class_ref).filter( class_ref.id == id).first() schema = ExportService.get_schema(name, session=db.session) request_data = request.get_json() if "_links" in request_data: request_data.pop("_links") updated, errors = schema.load(request_data, instance=instance) if errors: raise RestException(RestException.INVALID_OBJECT, details=errors) updated.last_updated = datetime.datetime.utcnow() db.session.add(updated) db.session.commit() return schema.dump(updated)
def get(self, name, id): """ Returns a single questionnaire record. Parameters: name (str): Snake-cased name of the questionnaire class (should also match the table name), found in app.model.questionnaires. E.g., clinical_diagnoses_questionnaire -> ClinicalDiagnosesQuestionnaire id (int): ID of the questionnaire record to retrieve Returns: A single questionnaire record. """ name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) instance = db.session.query(class_ref).filter( class_ref.id == id).first() if instance is None: raise RestException(RestException.NOT_FOUND) schema = ExportService.get_schema(name) return schema.dump(instance)
def get(self, name): name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) questionnaire = class_ref() meta = {"table": {}} try: meta["table"]['question_type'] = questionnaire.__question_type__ meta["table"]["label"] = questionnaire.__label__ except: pass # If these fields don't exist, just keep going. meta["fields"] = [] # This will move fields referenced by the field groups into the group, but will otherwise add them # the base meta object if they are not contained within a group. for c in questionnaire.__table__.columns: if c.info: c.info['name'] = c.name c.info['key'] = c.name meta['fields'].append(c.info) elif c.type.python_type == datetime.datetime: meta['fields'].append({ 'name': c.name, 'key': c.name, 'display_order': 0, 'type': 'DATETIME' }) else: meta['fields'].append({ 'name': c.name, 'key': c.name, 'display_order': 0 }) # Sort the fields meta['fields'] = sorted(meta['fields'], key=lambda field: field['display_order']) return meta
def delete(self, name, id): """ Deletes a single questionnaire record. Parameters: name (str): Snake-cased name of the questionnaire class (should also match the table name), found in app.model.questionnaires. E.g., clinical_diagnoses_questionnaire -> ClinicalDiagnosesQuestionnaire id (int): ID of the questionnaire record to delete """ try: name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) instance = db.session.query(class_ref).filter( class_ref.id == id).first() db.session.delete(instance) # db.session.query(class_ref).filter(class_ref.id == id).delete() db.session.commit() except IntegrityError as error: raise RestException(RestException.CAN_NOT_DELETE) return
def get(self, name): name = ExportService.camel_case_it(name) class_ref = ExportService.get_class(name) schema = ExportService.get_schema(name, many=True) questionnaires = db.session.query(class_ref).all() return schema.dump(questionnaires)