def create(): # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the word creation inputs if inputs.validate_on_submit(): word = Word(inputs.lexeme_form.data, inputs.lexical_class.data) try: word.save() return render_view('words/show', 201, word=word.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
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})
def insert_dummy_data(self): word = None definition_template = None definition_filler = None for word_data in WORDS: word = Word(word_data.get('lexeme_form'), word_data.get('lexical_class')) word.save() self.NUM_WORDS += 1 for template_data in word_data.get('definition_templates', []): definition_template = DefinitionTemplate( word, template_data.get('definition'), template_data.get('filler_lexical_classes') ) definition_template.save() self.NUM_DEFINITION_TEMPLATES += 1 for filler_data in template_data.get('definition_fillers', []): definition_filler = DefinitionFiller( definition_template, filler_data.get('filler'), filler_data.get('is_dictionary') ) definition_filler.save() self.NUM_DEFINITION_FILLERS += 1 self.word = word self.definition_template = definition_template self.definition_filler = definition_filler