Пример #1
0
def test_parse_lexemes_WrongNumberOfFieldsError():
    tpsv = 'object|objects\nthing'
    with pytest.raises(parse_tpsv.WrongNumberOfFieldsError) as exc_info:
        parse_tpsv.parse_lexemes(tpsv, {'forms': [{}, {}]})
    assert exc_info.value.num_forms == 2
    assert exc_info.value.num_fields == 1
    assert exc_info.value.line_number == 2
Пример #2
0
def test_parse_lexemes_FirstFieldLexemeIdError():
    tpsv = 'object|objects\nL123|things'
    with pytest.raises(parse_tpsv.FirstFieldLexemeIdError) as exc_info:
        parse_tpsv.parse_lexemes(tpsv, {'forms': [{}, {}]})
    assert exc_info.value.num_forms == 2
    assert exc_info.value.num_fields == 2
    assert exc_info.value.first_field == 'L123'
    assert exc_info.value.line_number == 2
def test_parse_lexemes_skips_empty_lines():
    tpsv = '''

thing|things


entity|entities

'''
    assert parse_tpsv.parse_lexemes(tpsv) == [
        ['thing', 'things'],
        ['entity', 'entities'],
    ]
def test_parse_lexemes():
    tpsv = '''
thing|things
entity|entities
object|objects
test|tests
'''.strip()
    assert parse_tpsv.parse_lexemes(tpsv) == [
        ['thing', 'things'],
        ['entity', 'entities'],
        ['object', 'objects'],
        ['test', 'tests'],
    ]
Пример #5
0
def test_parse_lexemes():
    tpsv = '''
thing|things

\r

L123|entity|entities
'''.strip()
    assert parse_tpsv.parse_lexemes(tpsv, {'forms': [{}, {}]}) == [
        ImmutableMultiDict([
            ('form_representation', 'thing'),
            ('form_representation', 'things'),
        ]),
        ImmutableMultiDict([
            ('lexeme_id', 'L123'),
            ('form_representation', 'entity'),
            ('form_representation', 'entities'),
        ]),
    ]
Пример #6
0
def process_template_bulk(template_name):
    response = if_no_such_template_redirect(template_name)
    if response:
        return response

    response = if_needs_oauth_redirect()
    if response:
        return response

    template = templates[template_name]
    flask.g.language_code = template['language_code']

    if not can_use_bulk_mode():
        return flask.render_template('bulk-not-allowed.html', )

    if flask.request.method == 'POST' and flask.request.referrer == current_url(
    ):
        form_data = flask.request.form
        token = flask.session.pop('_csrf_token', None)
        if not token or token != form_data.get('_csrf_token'):
            return 'CSRF error', 400  # TODO better handling

        lexemes = parse_lexemes(form_data.get('lexemes'))
        results = []

        for lexeme in lexemes:
            lexeme_form_data = werkzeug.datastructures.ImmutableMultiDict([
                ('form_representation', form_representation)
                for form_representation in lexeme
            ])
            duplicates = find_duplicates(template, lexeme_form_data)
            if duplicates:
                results.append(('duplicates', duplicates))
                continue
            lexeme_data = build_lexeme(template, lexeme_form_data)
            summary = build_summary(template, form_data)

            if 'oauth' in app.config:
                lexeme_uri = submit_lexeme(template, lexeme_data, summary)
                results.append(('lexeme_uri', lexeme_uri))
            else:
                print(summary)
                results.append(('lexeme_data', lexeme_data))

        if 'oauth' in app.config:
            return flask.render_template(
                'bulk-result.html',
                template=template,
                results=results,
            )
        else:
            return flask.jsonify([result[1] for result in results])
    else:
        placeholder = ''
        for form in template['forms']:
            if placeholder:
                placeholder += '|'
            (prefix, form_placeholder, suffix) = split_example(form)
            placeholder += form_placeholder
        placeholder += '\n...'

        if flask.request.method == 'POST':
            form_data = flask.request.form
            value = '|'.join(form_data.getlist('form_representation'))
            value += '\n'  # for convenience when adding more
        else:
            value = None

        return flask.render_template(
            'bulk.html',
            template=template,
            placeholder=placeholder,
            value=value,
        )
Пример #7
0
def process_template_bulk(template_name):
    response = if_no_such_template_redirect(template_name)
    if response:
        return response

    response = if_needs_oauth_redirect()
    if response:
        return response

    template = templates[template_name]
    flask.g.language_code = template['language_code']

    if not can_use_bulk_mode():
        return flask.render_template(
            'bulk-not-allowed.html',
            template=template,
        )

    if (flask.request.method == 'POST'
            and flask.request.referrer == current_url()
            and csrf_token_matches(flask.request.form)):

        form_data = flask.request.form
        lexemes = parse_lexemes(form_data.get('lexemes'), template)
        results = []

        for lexeme in lexemes:
            if not lexeme.get('lexeme_id'):
                duplicates = find_duplicates(template, lexeme)
                if duplicates:
                    results.append(('duplicates', duplicates))
                    continue
            lexeme_data = build_lexeme(template, lexeme)
            summary = build_summary(template, form_data)

            if 'oauth' in app.config:
                lexeme_uri = submit_lexeme(template, lexeme_data, summary)
                results.append(('lexeme_uri', lexeme_uri))
            else:
                print(summary)
                results.append(('lexeme_data', lexeme_data))

        if 'oauth' in app.config:
            return flask.render_template(
                'bulk-result.html',
                template=template,
                results=results,
            )
        else:
            return flask.jsonify([result[1] for result in results])

    else:
        placeholder = ''
        for form in template['forms']:
            if placeholder:
                placeholder += '|'
            (prefix, form_placeholder, suffix) = split_example(form)
            placeholder += form_placeholder
        placeholder += '\n...'
        csrf_error = False

        if flask.request.method == 'POST':
            form_data = flask.request.form
            if 'form_representation' in form_data:
                # user came from non-bulk mode
                value = '|'.join(form_data.getlist('form_representation'))
                value += '\n'  # for convenience when adding more
            else:
                # user came from bulk mode with CSRF error
                value = form_data.get('lexemes')
                csrf_error = True
        else:
            value = None

        return flask.render_template(
            'bulk.html',
            template=template,
            placeholder=placeholder,
            value=value,
            csrf_error=csrf_error,
        )
def test_parse_lexemes_supports_crlf():
    tpsv = '''thing|things\r\nentity|entities'''
    assert parse_tpsv.parse_lexemes(tpsv) == [
        ['thing', 'things'],
        ['entity', 'entities'],
    ]
Пример #9
0
def process_template_bulk(template_name: str) -> RRV:
    response = if_no_such_template_redirect(template_name)
    if response:
        return response

    template = templates_without_redirects[template_name]
    flask.g.interface_language_code = lang_lex2int(template['language_code'])

    readonly = 'OAUTH' in app.config and 'oauth_access_token' not in flask.session

    if not can_use_bulk_mode() and not readonly:
        return flask.render_template(
            'bulk-not-allowed.html',
        )

    if (flask.request.method == 'POST' and
            '_bulk_mode' in flask.request.form and
            csrf_token_matches(flask.request.form) and
            not readonly):

        form_data = flask.request.form
        parse_error = None
        show_optional_forms_hint = False
        try:
            lexemes = parse_lexemes(form_data['lexemes'], template)
        except FirstFieldNotLexemeIdError as error:
            parse_error = message_with_kwargs(
                'bulk_first_field_not_lexeme_id',
                num_forms=error.num_forms,
                num_fields=error.num_fields,
                first_field=error.first_field,
                line_number=error.line_number,
            )
        except FirstFieldLexemeIdError as error:
            parse_error = message_with_kwargs(
                'bulk_first_field_lexeme_id',
                num_forms=error.num_forms,
                num_fields=error.num_fields,
                first_field=error.first_field,
                line_number=error.line_number,
            )
        except WrongNumberOfFieldsError as error:
            show_optional_forms_hint = error.num_fields < error.num_forms
            parse_error = message_with_kwargs(
                'bulk_wrong_number_of_fields',
                num_forms=error.num_forms,
                num_fields=error.num_fields,
                line_number=error.line_number,
            )
        except ValueError as error:
            parse_error = str(error)
        if parse_error is not None:
            return flask.render_template(
                'bulk.html',
                template=template,
                value=form_data['lexemes'],
                parse_error=parse_error,
                show_optional_forms_hint=show_optional_forms_hint,
            )

        results = []  # type: list[dict]

        for lexeme in lexemes:
            if not lexeme.get('lexeme_id'):
                duplicates = find_duplicates(template, lexeme)
                if duplicates:
                    results.append({
                        'duplicates': duplicates,
                        'form_representations': lexeme.getlist('form_representation'),
                    })
                    continue
            lexeme_data = build_lexeme(template, lexeme)
            summary = build_summary(template, form_data)

            if 'OAUTH' in app.config:
                lexeme_id, lexeme_uri = submit_lexeme(template, lexeme_data, summary)
                results.append({
                    'lexeme_data': lexeme_data,
                    'lexeme_id': lexeme_id,
                    'lexeme_uri': lexeme_uri,
                })
            else:
                print(summary)
                results.append({
                    'lexeme_data': lexeme_data,
                })

        if 'OAUTH' in app.config:
            return flask.render_template(
                'bulk-result.html',
                template=template,
                results=results,
            )
        else:
            return flask.jsonify(results)

    else:
        placeholder = ''
        for form in template['forms']:
            if placeholder:
                placeholder += '|'
            (prefix, form_placeholder, suffix) = split_example(form)
            placeholder += form_placeholder
        placeholder += '\n...'
        csrf_error = False

        if flask.request.method == 'POST':
            form_data = flask.request.form
            if 'form_representation' in form_data:
                # user came from non-bulk mode
                representations = form_data.getlist('form_representation')
                value = '|'.join(representations)
                if value == '|' * (len(representations) - 1):
                    # ...but had not typed anything into non-bulk mode yet,
                    # clear the value so that the placeholder is shown
                    value = ''
                else:
                    value += '\n'  # for convenience when adding more
            else:
                # user came from bulk mode with CSRF error
                value = form_data['lexemes']
                csrf_error = True
        else:
            value = None

        return flask.render_template(
            'bulk.html',
            template=template,
            placeholder=placeholder,
            value=value,
            csrf_error=csrf_error,
            show_optional_forms_hint=False,
            readonly=readonly,
        )