def _add_word_and_translations_to_db(self, id_user, id_user_lang, word, id_word_type, transcription=None, translations=None): db_word = self._add_word_to_db(id_user, word, id_word_type, transcription) for tr in translations: tr = tr.strip().lower() db_translation = db.session.query( DbTranslation ).filter( DbTranslation.id_word == db_word.id_word, DbTranslation.id_language == id_user_lang, DbTranslation.translation == tr, DbTranslation.is_in_use == True ).first() if db_translation is not None: continue db_translation = DbTranslation(db_word.id_word, id_user_lang, tr) db.session.add(db_translation) save_db_changes() return db_word
def _create_user_session_and_access_token(self, db_user): if db_user.id_session is None: db_user.id_session = uuid.uuid4().hex save_db_changes() access_token = Jwt.generate(db_user.id_user, db_user.id_session) return access_token
def _update_db_word_or_raise_exception(self, id_user, id_word, word, id_word_type, transcription=None): db_word = self._get_db_word_or_raise_exception(id_user, id_word) db_word.word = word db_word.transcription = transcription db_word.id_word_type = id_word_type save_db_changes()
def _delete_cascade_db_word_or_raise_exception(self, id_user, id_word): db_word = self._get_db_word_or_raise_exception(id_user, id_word) db_word.is_in_use = False db.session.query( DbTranslation ).filter( DbTranslation.id_word == id_word ).update({ DbTranslation.is_in_use: False }) save_db_changes()
def setUp(self): self.app = self.create_test_app() self.client = self.app.test_client() self.test_user_id = None with self.app.app_context(): db_manager.delete_db() db_manager.create_db() db_manager.init_db_with_default_values() test_user = DbUser('test', DbLanguage.RU, 'test') db.session.add(test_user) db_manager.save_db_changes() self.test_user_id = test_user.id_user db.session.remove()
def _add_word_to_db(self, id_user, word, id_word_type, transcription=None): db_word = db.session.query( DbWord ).filter( DbWord.id_user == id_user, DbWord.word == word, DbWord.id_word_type == id_word_type, DbWord.is_in_use == True ).first() if db_word: return db_word db_word = DbWord(id_user, word, id_word_type, transcription) db.session.add(db_word) db.session.flush() save_db_changes() return db_word
def _remove_user_session_and_access_token(self, db_user): if db_user is not None: db_user.id_session = None save_db_changes()
def post(self): request = get_current_request() current_user = get_db_user_by_id(get_current_user_id()) direction = request.get_string('direction') if direction is None: return bad_response('direction is required') direction = direction.strip().lower() if direction not in [ TranslationDirection.ORIGINAL, TranslationDirection.USER_LANGUAGE ]: return bad_response( 'unknown direction type. Supported types are "original", "user_language"' ) user_answers = request.get_obj_list('answers') result_mistakes = [] result_ok_count = 0 for answer_obj in user_answers: word_id = types.to_int(answer_obj.get('id_word')) answer = answer_obj.get('answer') if answer is not None: answer = answer.strip().lower() if word_id is None: continue db_word = get_db_user_word_by_id(current_user.id_user, word_id) if db_word is None: continue db_word.last_learn_db_dts = datetime.datetime.utcnow() try: if direction == TranslationDirection.ORIGINAL: self._process_original_direction_answer_or_raise_exception( db_word, current_user.id_language, answer) elif direction == TranslationDirection.USER_LANGUAGE: self._process_user_language_direction_answer_or_raise_exception( db_word, current_user.id_language, answer) result_ok_count += 1 except LearnException as e: result_mistakes.append({ 'answer': answer, 'translation': e.translation, 'correct': e.connect_value }) db_word.is_learnt = False if db_word.score > 0: db_word.score -= 1 db.session.query(DbUserWordRepeat).filter( DbUserWordRepeat.id_word == db_word.id_word).delete( synchronize_session='fetch') db.session.flush() save_db_changes() return ok_response({ 'result': { 'ok_count': result_ok_count, 'mistakes_count': len(result_mistakes), 'mistakes': result_mistakes } })