Пример #1
0
def insert_codebooks(context, request):
    """
    Insert appropriate records to the database
    """
    check_csrf_token(request)
    db_session = request.db_session

    study_name = request.POST['study']

    study = (
        db_session.query(studies.Study)
        .filter(studies.Study.title == study_name)
        .one())

    dry = request.POST['mode'] == u'dry'
    codebook = request.POST['codebook'].file
    codebook_format = request.matchdict['format']
    delimiter = request.POST.get('delimiter', ',')
    delimiter = convert_delimiter(delimiter)

    if codebook_format != u'iform':
        delimiter_mismatch, errors = validate_delimiter(delimiter, codebook)
    else:
        delimiter_mismatch = None
        errors = []

    # if delimiter isn't correct, don't process the file
    if delimiter_mismatch:
        records = []
        fields_inserted = 0
        forms = []

    else:
        records = parse.parse_dispatch(codebook, codebook_format, delimiter)
        records = parse.remove_system_entries(records)

        errors, imports, forms = validate_populate_imports(request, records)
        errors = is_duplicate_schema(forms, errors, db_session)

        fields_inserted = 0
        if not dry and not errors:
            fields_inserted = group_imports_by_schema(imports, study, db_session)

    return {
        'fields_evaluated': len(records),
        'errors': errors,
        'error_count': len(errors),
        'fields_inserted': fields_inserted,
        'forms_inserted': len(forms) if not dry and not errors else 0,
        'forms': forms
    }
Пример #2
0
def test_remove_system_entries():
    records = [{'is_system': True}, {'is_system': False}]

    assert parse.remove_system_entries(records) == [{'is_system': False}]
Пример #3
0
def test_remove_system_entries():
    records = [{'is_system': True}, {'is_system': False}]

    assert parse.remove_system_entries(records) == [{'is_system': False}]