def load_appdata(): """ Utility function to load the application data file :return: Return the parsed application data file """ return read_json_file(Settings.appdata_file)
def get_l10n(session, tid, lang): """ Transaction for retrieving the custom texts configured for a specific language :param session: An ORM session :param tid: The tenant ID of the tenant on which perform the lookup :param lang: A requested language :return: A dictionary containing the custom texts configured for a specific language """ if tid != 1: config = ConfigFactory(session, tid) if config.get_val('mode') != 'default': tid = 1 path = langfile_path(lang) directory_traversal_check(Settings.client_path, path) custom_texts = session.query(models.CustomTexts).filter(models.CustomTexts.lang == lang, models.CustomTexts.tid == tid).one_or_none() custom_texts = custom_texts.texts if custom_texts is not None else {} texts = read_json_file(path) texts.update(custom_texts) return texts
def db_fix_fields_attrs(session): """ Ensures that the current store and the field_attrs.json file correspond. The content of the field_attrs dict is used to add and remove all of the excepted forms of field_attrs for FieldAttrs in the db. :param session: An ORM session """ field_attrs = read_json_file(Settings.field_attrs_file) std_lst = ['inputbox', 'textarea', 'checkbox', 'tos', 'date'] for field_type, attrs_dict in field_attrs.items(): attrs_to_keep_for_type = attrs_dict.keys() if field_type in std_lst: # Ensure that the standard field attrs do not have extra attr rows _filter = not_(models.FieldAttr.name.in_(attrs_to_keep_for_type)), \ models.FieldAttr.field_id == models.Field.id, \ models.Field.type == field_type, \ models.Field.template_id.is_(None) else: # Look for dropped attrs in non-standard field_groups like whistleblower_identity _filter = not_(models.FieldAttr.name.in_(attrs_to_keep_for_type)), \ models.FieldAttr.field_id == models.Field.id, \ models.Field.template_id == field_type for res in session.query(models.FieldAttr).filter(*_filter): session.delete(res) # Add keys to the db that have been added to field_attrs for field in session.query(models.Field): type = field.type if field.template_id is None else field.template_id attrs = field_attrs.get(type, {}) db_update_fieldattrs(session, field.id, attrs, None)
def db_load_default_fields(session): """ Transaction for loading default questions :param session: An ORM session """ ffiles = [ os.path.join(Settings.questions_path, path) for path in os.listdir(Settings.questions_path) ] questions = [] qids = [] for ffile in ffiles: questions.append(read_json_file(ffile)) qids.append(questions[-1]['id']) session.query(models.Field).filter( models.Field.id.in_(qids)).delete(synchronize_session=False) session.query(models.Field).filter( models.Field.fieldgroup_id.in_(qids)).delete(synchronize_session=False) session.query(models.FieldAttr).filter( models.FieldAttr.field_id.in_(qids)).delete(synchronize_session=False) session.query(models.FieldOption).filter( models.FieldOption.field_id.in_(qids)).delete( synchronize_session=False) for question in questions: db_create_field(session, 1, question, None)
def test_post_valid_json(self): self.test_data_dir = os.path.join(helpers.DATA_DIR, 'questionnaires') new_q = read_json_file(os.path.join(self.test_data_dir, 'valid.json')) handler = self.request(new_q, role='admin') handler.request.language = None yield handler.post()
def load_profile(session, tid, name): """ Transaction for loading a configuration profile :param session: An ORM session :param tid: The tenant id of the tenant to be configured :param name: The name of the profile to be used """ path = os.path.join(Settings.client_path, 'data/profiles', '{}.json'.format(name)) prof = read_json_file(path) ConfigFactory(session, tid).update('node', prof['node'])
def test_post_invalid_json(self): self.test_data_dir = os.path.join(helpers.DATA_DIR, 'questionnaires') invalid_test_cases = [('cyclic_groupid.json', errors.InputValidationError), ('duplicate_ids.json', IntegrityError)] for fname, err in invalid_test_cases: new_q = read_json_file(os.path.join(self.test_data_dir, fname)) handler = self.request(new_q, role='admin') handler.request.language = None yield self.assertFailure(handler.post(), err)
def db_load_default_questionnaires(session): """ Transaction for loading default questionnaires :param session: An ORM session """ qfiles = [ os.path.join(Settings.questionnaires_path, path) for path in os.listdir(Settings.questionnaires_path) ] questionnaires = [] qids = [] for qfile in qfiles: questionnaires.append(read_json_file(qfile)) qids.append(questionnaires[-1]['id']) db_del(session, models.Questionnaire, models.Questionnaire.id.in_(qids)) db_del(session, models.Step, models.Step.questionnaire_id.in_(qids)) for questionnaire in questionnaires: db_create_questionnaire(session, 1, questionnaire, None)
def db_fix_fields_attrs(session): """ Ensures that questions loaded into the database have the same structure of field_attrs.json :param session: An ORM session """ field_attrs = read_json_file(Settings.field_attrs_file) std_lst = [ 'inputbox', 'textarea', 'checkbox', 'selectbox', 'fieldgroup', 'tos', 'date', 'daterange', 'map' ] for field_type, attrs_dict in field_attrs.items(): attrs_to_keep_for_type = attrs_dict.keys() if field_type in std_lst: # Ensure that the standard field attrs do not have extra attr rows _filter = not_(models.FieldAttr.name.in_(attrs_to_keep_for_type)), \ models.FieldAttr.field_id == models.Field.id, \ models.Field.type == field_type, \ models.Field.template_id.is_(None) else: # Look for dropped attrs in non-standard field_groups like whistleblower_identity _filter = not_(models.FieldAttr.name.in_(attrs_to_keep_for_type)), \ models.FieldAttr.field_id == models.Field.id, \ models.Field.template_id == field_type subquery = session.query( models.FieldAttr.id).filter(*_filter).subquery() session.query(models.FieldAttr).filter( models.FieldAttr.id.in_(subquery)).delete( synchronize_session=False) # Add keys to the db that have been added to field_attrs for field in session.query(models.Field): type = field.type if field.template_id is None else field.template_id attrs = field_attrs.get(type, {}) db_update_fieldattrs(session, field.id, attrs, None)
def db_create_field(session, tid, request, language): """ Transaction for creating a field :param session: An ORM session :param tid: The tenant ID :param request: The request data :param language: The language of the request :return: The created field """ request['tid'] = tid fill_localized_keys(request, models.Field.localized_keys, language) check_field_association(session, tid, request) if request.get('template_id', '') != '': if request['template_id'] == 'whistleblower_identity': if request.get('step_id', '') == '': raise errors.InputValidationError( "Cannot associate whistleblower identity field to a fieldgroup" ) q_id = session.query(models.Questionnaire.id) \ .filter(models.Questionnaire.id == models.Step.questionnaire_id, models.Step.id == request['step_id']) field = session.query(models.Field) \ .filter(models.Field.template_id == 'whistleblower_identity', models.Field.step_id == models.Step.id, models.Step.questionnaire_id.in_(q_id.subquery())).one_or_none() if field is not None: raise errors.InputValidationError( "Whistleblower identity field already present") field = models.db_forge_obj(session, models.Field, request) template = session.query(models.Field).filter( models.Field.id == request['template_id']).one() field.label = template.label field.description = template.description field.hint = template.hint field.placeholder = template.placeholder attrs = request.get('attrs') if not attrs: field_attrs = read_json_file(Settings.field_attrs_file) attrs = field_attrs.get(field.template_id, {}) db_update_fieldattrs(session, field.id, attrs, None) else: field = models.db_forge_obj(session, models.Field, request) attrs = request.get('attrs') options = request.get('options') db_update_fieldattrs(session, field.id, attrs, language) db_update_fieldoptions(session, field.id, options, language) for trigger in request.get('triggered_by_options', []): db_create_option_trigger(session, trigger['option'], 'field', field.id, trigger.get('sufficient', True)) if field.instance != 'reference': for c in request.get('children', []): c['tid'] = field.tid c['fieldgroup_id'] = field.id db_create_field(session, tid, c, language) return field