Exemplo n.º 1
0
def update(word_id):
    # Get the word
    word = WordsService.get_instance().get(word_id)

    # Verify the word creation inputs
    if word:

        # Get the input validator
        inputs = UpdateInputs(get_inputs())
        combined_inputs = dict(inputs.serialized().items() + {'id': word_id}.items())

        if inputs.validate_on_submit():
            # If we're only marking the word as active or inactive, pass through to the update
            if inputs.is_active.data and \
                    not any([inputs.lexeme_form.data, inputs.lexical_class.data]):
                try:
                    word.update(**get_mixed_dict_from_multidict(get_inputs(), inputs))
                    return render_view('words/show', 200, word=word.serialized)
                except Exception as e:
                    return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs)

            else:
                word.update(**{'is_active': False})

                lexeme_form = inputs.lexeme_form.data if inputs.lexeme_form.data else word.get_lexeme_form()
                lexical_class = inputs.lexical_class.data if inputs.lexical_class.data else word.get_lexical_class()
                is_active = inputs.is_active.data if inputs.is_active.data else word.get_is_active()

                word = Word(lexeme_form, lexical_class)
                word.set_is_active(is_active)

                try:
                    word.save()
                    return render_view(
                        'words/show', 200, word=word.serialized
                    )
                except Exception as e:
                    return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs)

        return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs)

    return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})