Exemple #1
0
def db_load_default_fields(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='fetch')
    session.query(models.Field).filter(
        models.Field.fieldgroup_id.in_(qids)).delete(
            synchronize_session='fetch')
    session.query(models.FieldAttr).filter(
        models.FieldAttr.field_id.in_(qids)).delete(
            synchronize_session='fetch')
    session.query(models.FieldOption).filter(
        models.FieldOption.field_id.in_(qids)).delete(
            synchronize_session='fetch')

    for question in questions:
        db_create_field(session, 1, question, None)
Exemple #2
0
    def add_whistleblower_identity_field_to_step(self, store, step_id):
        wbf = store.find(models.Field, models.Field.id == u'whistleblower_identity').one()

        reference_field = get_dummy_field()
        reference_field['instance'] = 'reference'
        reference_field['template_id'] = wbf.id
        reference_field['step_id'] = step_id
        db_create_field(store, reference_field, 'en')
Exemple #3
0
    def add_whistleblower_identity_field_to_step(self, session, step_id):
        wbf = session.query(models.Field).filter(models.Field.id == u'whistleblower_identity', models.Field.tid == 1).one()

        reference_field = get_dummy_field()
        reference_field['instance'] = 'reference'
        reference_field['template_id'] = wbf.id
        reference_field['step_id'] = step_id
        db_create_field(session, 1, reference_field, 'en')
Exemple #4
0
    def add_whistleblower_identity_field_to_step(self, store, step_id):
        wbf = store.find(models.Field, models.Field.key == u'whistleblower_identity').one()

        reference_field = get_dummy_field()
        reference_field['instance'] = 'reference'
        reference_field['template_id'] = wbf.id
        reference_field['step_id'] = step_id
        db_create_field(store, reference_field, 'en')
Exemple #5
0
    def add_whistleblower_identity_field_to_step(self, session, step_id):
        wbf = session.query(models.Field).filter(models.Field.id == u'whistleblower_identity', models.Field.tid == 1).one()

        reference_field = get_dummy_field()
        reference_field['instance'] = 'reference'
        reference_field['template_id'] = wbf.id
        reference_field['step_id'] = step_id
        db_create_field(session, 1, reference_field, 'en')
Exemple #6
0
def load_default_fields(store):
    ffiles = [
        os.path.join(Settings.questions_path, path)
        for path in os.listdir(Settings.questions_path)
    ]
    for ffile in ffiles:
        question = read_json_file(ffile)
        store.find(models.Field, id=question['id']).remove()
        db_create_field(store, question, None)
Exemple #7
0
def load_default_fields(store):
    if os.path.exists(GLSettings.fields_path):
        for fname in os.listdir(GLSettings.fields_path):
            fpath = os.path.join(GLSettings.fields_path, fname)
            with file(fpath, 'r') as f:
                json_string = f.read()
                field_dict = json.loads(json_string)
                db_create_field(store, field_dict, None)
                return

    raise errors.InternalServerError("Unable to load default fields")
Exemple #8
0
def load_default_fields(session):
    ffiles = [
        os.path.join(Settings.questions_path, path)
        for path in os.listdir(Settings.questions_path)
    ]
    for ffile in ffiles:
        question = read_json_file(ffile)
        question['tid'] = 1
        session.query(
            models.Field).filter(models.Field.id == question['id']).delete(
                synchronize_session='fetch')
        db_create_field(session, 1, question, None)
Exemple #9
0
def load_default_fields(store):
    for fname in os.listdir(GLSettings.fields_path):
        fpath = os.path.join(GLSettings.fields_path, fname)
        with file(fpath, 'r') as f:
            json_string = f.read()
            field_dict = json.loads(json_string)
            old_field = store.find(models.Field, models.Field.key == field_dict['key']).one()

            if old_field is not None:
                field_dict['id'] = old_field.id
                store.remove(old_field)

            db_create_field(store, field_dict, None)
Exemple #10
0
def load_default_fields(store):
    for fname in os.listdir(GLSettings.fields_path):
        fpath = os.path.join(GLSettings.fields_path, fname)
        with file(fpath, 'r') as f:
            json_string = f.read()
            field_dict = json.loads(json_string)
            old_field = store.find(models.Field, models.Field.key == field_dict['key']).one()

            if old_field is not None:
                field_dict['id'] = old_field.id
                store.remove(old_field)

            db_create_field(store, field_dict, None)
Exemple #11
0
def db_create_step(store, step, language):
    """
    Create the specified step

    :param store: the store on which perform queries.
    :param language: the language of the specified steps.
    """
    fill_localized_keys(step, models.Step.localized_keys, language)

    s = models.Step.new(store, step)

    for c in step['children']:
        c['step_id'] = s.id
        db_create_field(store, c, language)

    return s
Exemple #12
0
def db_load_default_fields(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='fetch')
    session.query(models.Field).filter(models.Field.fieldgroup_id.in_(qids)).delete(synchronize_session='fetch')
    session.query(models.FieldAttr).filter(models.FieldAttr.field_id.in_(qids)).delete(synchronize_session='fetch')
    session.query(models.FieldOption).filter(models.FieldOption.field_id.in_(qids)).delete(synchronize_session='fetch')

    for question in questions:
        db_create_field(session, 1, question, None)
Exemple #13
0
def db_create_step(store, step, language):
    """
    Create the specified step

    :param store: the store on which perform queries.
    :param language: the language of the specified steps.
    """
    fill_localized_keys(step, models.Step.localized_keys, language)

    s = models.Step.new(store, step)

    for c in step['children']:
        c['step_id'] = s.id
        db_create_field(store, c, language)

    return s
Exemple #14
0
def db_create_step(session, tid, step_dict, language):
    """
    Create the specified step

    :param session: the session on which perform queries.
    :param language: the language of the specified steps.
    """
    fill_localized_keys(step_dict, models.Step.localized_keys, language)

    step = models.db_forge_obj(session, models.Step, step_dict)

    for c in step_dict['children']:
        c['tid'] = tid
        c['step_id'] = step.id
        db_create_field(session, tid, c, language)

    return step
Exemple #15
0
    def prologue(self):
        default_questionnaire = read_json_file(os.path.join(GLSettings.questionnaires_path, 'default.json'))

        steps = default_questionnaire.pop('steps')

        questionnaire = db_forge_obj(self.store_new, self.model_to['Questionnaire'], default_questionnaire)
        questionnaire.key = u'default'

        for step in steps:
            f_children = step.pop('children')
            s = db_forge_obj(self.store_new, self.model_to['Step'], step)
            for child in f_children:
                child['step_id'] = s.id
                db_create_field(self.store_new, child, None)
            s.questionnaire_id = questionnaire.id

        self.store_new.commit()
Exemple #16
0
def db_create_step(session, tid, step_dict, language):
    """
    Create the specified step

    :param session: the session on which perform queries.
    :param language: the language of the specified steps.
    """
    fill_localized_keys(step_dict, models.Step.localized_keys, language)

    step = models.db_forge_obj(session, models.Step, step_dict)

    for trigger in step_dict.get('triggered_by_options', []):
        db_create_trigger(session, tid, trigger['option'], 'step', step.id, trigger.get('sufficient', True))

    for c in step_dict['children']:
        c['tid'] = tid
        c['step_id'] = step.id
        db_create_field(session, tid, c, language)

    return step
Exemple #17
0
def db_create_step(session, tid, step_dict, language):
    """
    Create the specified step

    :param session: the session on which perform queries.
    :param language: the language of the specified steps.
    """
    fill_localized_keys(step_dict, models.Step.localized_keys, language)

    step = models.db_forge_obj(session, models.Step, step_dict)

    for trigger in step_dict.get('triggered_by_options', []):
        db_create_trigger(session, tid, trigger['option'], 'step', step.id,
                          trigger.get('sufficient', True))

    for c in step_dict['children']:
        c['tid'] = tid
        c['step_id'] = step.id
        db_create_field(session, tid, c, language)

    return step
Exemple #18
0
    def test_check_field_constraints(self):
        # This test case asserts that a migration from db ver 32 up to the latest
        # db with fields that fail the constraints still functions.
        self._initStartDB(32)

        field_dict = helpers.get_dummy_field()
        field_dict['instance'] = 'reference'
        field_dict['step_id'] = None
        field_dict['field_id'] = None

        db_create_field(self.store, field_dict, u'en')

        field_dict = helpers.get_dummy_field()
        field_dict['instance'] = 'instance'

        db_create_field(self.store, field_dict, u'en')

        field_dict = helpers.get_dummy_field()
        field_dict['instance'] = 'template'
        field_dict['step_id'] = None
        fld_grp_id = self.store.find(Field, Field.fieldgroup_id
                                     is not None)[0].fieldgroup_id
        field_dict['field_id'] = fld_grp_id

        db_create_field(self.store, field_dict, u'en')
        self.store.commit()

        ret = update_db()
        shutil.rmtree(GLSettings.db_path)
        self.assertNotEqual(ret, -1)
Exemple #19
0
    def test_check_field_constraints(self):
        # This test case asserts that a migration from db ver 32 up to 34 with
        # fields that fail the constraints still functions.
        self._initStartDB(32)

        field_dict = helpers.get_dummy_field()
        field_dict['instance'] = 'reference'
        field_dict['step_id'] = None
        field_dict['field_id'] = None

        db_create_field(self.store, field_dict, u'en')

        field_dict = helpers.get_dummy_field()
        field_dict['instance'] = 'instance'

        db_create_field(self.store, field_dict, u'en')

        field_dict = helpers.get_dummy_field()
        field_dict['instance'] = 'template'
        field_dict['step_id'] = None
        fld_grp_id = self.store.find(Field, Field.fieldgroup_id is not None)[0].fieldgroup_id
        field_dict['field_id'] = fld_grp_id

        db_create_field(self.store, field_dict, u'en')
        self.store.commit()

        ret = perform_system_update()
        shutil.rmtree(GLSettings.db_path)
        self.assertNotEqual(ret, -1)
Exemple #20
0
def db_create_step(session, tid, request, language):
    """
    Transaction for creating a step

    :param session: An ORM session
    :param tid: A tenant ID
    :param request: The request data
    :param session: the session on which perform queries.
    :param language: the language of the specified steps.
    """
    fill_localized_keys(request, models.Step.localized_keys, language)

    step = db_add(session, models.Step, request)

    for trigger in request.get('triggered_by_options', []):
        db_create_option_trigger(session, trigger['option'], 'step', step.id, trigger.get('sufficient', True))

    for c in request['children']:
        c['tid'] = tid
        c['step_id'] = step.id
        db_create_field(session, tid, c, language)

    return serialize_step(session, tid, step, language)
Exemple #21
0
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'])

    db_del(session, models.Field, models.Field.id.in_(qids))
    db_del(session, models.Field, models.Field.fieldgroup_id.in_(qids))
    db_del(session, models.FieldAttr, models.FieldAttr.field_id.in_(qids))
    db_del(session, models.FieldOption, models.FieldOption.field_id.in_(qids))

    for question in questions:
        db_create_field(session, 1, question, None)