示例#1
0
    def get(self, id_word):
        user_id = get_current_user_id()

        try:
            db_word = self._get_db_word_or_raise_exception(user_id, id_word)
        except ObjectDoesNotExists as e:
            return bad_response(str(e))

        word_type_name = db.session.query(
            DbWordType.name
        ).filter(
            DbWordType.id_type == db_word.id_word_type
        ).scalar()

        db_word_to_dict = {
            'id_word': db_word.id_word,
            'type_name': word_type_name,
            'word': db_word.word,
            'transcription': db_word.transcription,
            'score': db_word.score,
            'is_learnt': db_word.is_learnt,
            'last_learn_utc': dates.to_iso_datetime_string(db_word.last_learn_db_dts),
            'add_utc': dates.to_iso_datetime_string(db_word.add_db_dts)
        }

        return ok_response({
            'word': db_word_to_dict
        })
示例#2
0
    def post(self):
        request = get_current_request()

        current_user = get_db_user_by_id(get_current_user_id())

        word = request.get_string('word')
        id_word_type = request.get_int('id_type')
        transcription = request.get_string('transcription')

        translations = request.get_obj_list('translations')

        if not word:
            return bad_response('word is required')

        word = word.lower()

        if not id_word_type:
            return bad_response('id_type is required')

        if not translations:
            db_word = self._add_word_to_db(current_user.id_user, word, id_word_type, transcription)
        else:
            db_word = self._add_word_and_translations_to_db(
                current_user.id_user,
                current_user.id_language,
                word,
                id_word_type,
                transcription,
                translations
            )

        return ok_response({'id_word': db_word.id_word})
示例#3
0
    def get(self):
        current_user = get_db_user_by_id(get_current_user_id())

        words_to_learn = self._get_db_words_to_learn(current_user.id_user)
        words_to_repeat = self._get_db_words_to_repeat(current_user.id_user)

        random.shuffle(words_to_learn)
        random.shuffle(words_to_repeat)

        return ok_response({
            'repeat': [{
                'id_word':
                item[0],
                'word':
                item[1],
                'type_name':
                item[2],
                'transcription':
                item[3],
                'translations':
                self._get_db_translation(item[0], current_user.id_language)
            } for item in words_to_repeat],
            'learn': [{
                'id_word':
                item[0],
                'word':
                item[1],
                'type_name':
                item[2],
                'transcription':
                item[3],
                'translations':
                self._get_db_translation(item[0], current_user.id_language)
            } for item in words_to_learn]
        })
示例#4
0
    def delete(self, id_word):
        user_id = get_current_user_id()

        try:
            self._delete_cascade_db_word_or_raise_exception(user_id, id_word)
        except ObjectDoesNotExists as e:
            return bad_response(str(e))

        return ok_response()
示例#5
0
    def post(self):
        current_user_id = get_current_user_id()

        db_user = get_db_user_by_id(current_user_id)
        self._remove_user_session_and_access_token(db_user)

        request = get_current_request()
        if request.http_request.args.get('redirect', 'false') == 'true':
            response = redirect('/index')
        else:
            response = ok_response()

        response.set_cookie('access_token', '', expires=0)

        return response
示例#6
0
    def put(self, id_word):
        request = get_current_request()

        user_id = get_current_user_id()

        word = request.get_string('word')
        id_word_type = request.get_int('id_type')
        transcription = request.get_string('transcription')

        if not word:
            return bad_response('word is required')

        word = word.lower()

        if not id_word_type:
            return bad_response('id_type is required')

        try:
            self._update_db_word_or_raise_exception(user_id, id_word, word, id_word_type, transcription)
        except ObjectDoesNotExists as e:
            return bad_response(str(e))

        return ok_response()
示例#7
0
    def post(self):
        request = get_current_request()
        current_user = get_db_user_by_id(get_current_user_id())

        db_words_count = db.session.query(func.count(DbWord.id_word)).filter(
            DbWord.id_user == current_user.id_user,
            DbWord.is_in_use == True).scalar()

        db_words = db.session.query(
            DbWord, DbWordType,
            DbUserWordRepeat.repeat_after_db_dts).outerjoin(
                DbWordType,
                DbWordType.id_type == DbWord.id_word_type).outerjoin(
                    DbUserWordRepeat,
                    DbUserWordRepeat.id_word == DbWord.id_word).filter(
                        DbWord.id_user == current_user.id_user,
                        DbWord.is_in_use == True)

        if request.filter_by is not None:
            filter_param = '%' + request.filter_by + '%'
            db_words = db_words.filter(DbWord.word.ilike(filter_param), )

        if request.order_by is not None:
            if request.order_by == 'id_word':
                db_words = db_words.order_by('words.id_word ' +
                                             request.order_dir)
            elif request.order_by == 'word':
                db_words = db_words.order_by('words.word ' + request.order_dir)
            elif request.order_by == 'type_name':
                db_words = db_words.order_by('dc_word_types.name ' +
                                             request.order_dir)
            elif request.order_by == 'score':
                db_words = db_words.order_by('words.score ' +
                                             request.order_dir)
            elif request.order_by == 'add_db_dts':
                db_words = db_words.order_by('words.add_db_dts ' +
                                             request.order_dir)
            elif request.order_by == 'last_learn_db_dts':
                db_words = db_words.order_by('words.last_learn_db_dts ' +
                                             request.order_dir)
            elif request.order_by == 'repeat_db_dts':
                db_words = db_words.order_by(
                    'user_words_repeats.repeat_after_db_dts ' +
                    request.order_dir)
            elif request.order_by == 'transcription':
                db_words = db_words.order_by('words.transcription ' +
                                             request.order_dir)
            else:
                db_words = db_words.order_by(DbWord.add_db_dts.desc())

        if (request.limit is not None) and (request.limit > 0):
            db_words = db_words.limit(request.limit)

        if (request.skip is not None) and (request.skip > 0):
            db_words = db_words.offset(request.skip)

        db_words = db_words.all()

        def map_func(word_data):
            word, word_type, repeat_db_dts = word_data

            return {
                'id_word':
                word.id_word,
                'word':
                word.word,
                'transcription':
                word.transcription,
                'id_type':
                None if word_type is None else word_type.id_type,
                'type_name':
                None if word_type is None else word_type.name,
                'translations':
                list(
                    map(
                        lambda i: {
                            'id': i[0],
                            'translation': i[1]
                        },
                        self._get_db_translation(word.id_word,
                                                 current_user.id_language))),
                'learn_score':
                word.score,
                'add_db_dts':
                dates.to_iso_datetime_string(word.add_db_dts),
                'last_learn_db_dts':
                dates.to_iso_datetime_string(word.last_learn_db_dts),
                'repeat_db_dts':
                dates.to_iso_datetime_string(repeat_db_dts)
            }

        table = list(map(map_func, db_words))

        if request.filter_by is not None:
            db_words_filtered = len(table)
        else:
            db_words_filtered = db_words_count

        jq_table = {'table': table}

        return ok_jqdatatable_response(request.draw, db_words_filtered,
                                       db_words_count, jq_table)
示例#8
0
    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
            }
        })