Esempio n. 1
0
def add_bookmark(user, original_language, original_word, translation_language,
                 translation_word, date, the_context, the_url, the_url_title):

    url = Url.find(the_url, the_url_title)
    text = Text(the_context, translation_language, url)

    if RankedWord.exists(original_word.lower(), original_language):
        rank1 = UserWord.find_rank(original_word.lower(), original_language)
        w1 = UserWord(original_word, original_language, rank1)
    else:
        w1 = UserWord(original_word, original_language, None)
    if RankedWord.exists(translation_word.lower(), translation_language):
        rank2 = UserWord.find_rank(translation_word.lower(),
                                   translation_language)
        w2 = UserWord(translation_word, translation_language, rank2)
    else:
        w2 = UserWord(translation_word, translation_language, None)

    zeeguu.db.session.add(url)
    zeeguu.db.session.add(text)
    zeeguu.db.session.add(w1)
    zeeguu.db.session.add(w2)
    t1 = Bookmark(w1, w2, user, text, date)
    zeeguu.db.session.add(t1)

    zeeguu.db.session.commit()
    add_probability_to_existing_words_of_user(user, t1, original_language)
Esempio n. 2
0
def bookmark_with_context(from_lang_code, term, to_lang_code, translation):
    """
    The preferred way of a user saving a word/translation/context to his
    profile.
    :param from_lang_code:
    :param term:
    :param to_lang_code:
    :param translation:
    :return:
    """

    if 'title' in flask.request.form:
        bookmarked_url_title = flask.request.form['title']
    else:
        bookmarked_url_title = ''

    bookmarked_url = flask.request.form['url']
    context = flask.request.form['context']

    url = Url.find(bookmarked_url, bookmarked_url_title)

    from_lang = Language.find(from_lang_code)
    to_lang = Language.find(to_lang_code)

    word = (decode_word(term))
    translation_word = decode_word(translation)
    user_word = UserWord.find(word, from_lang)
    translation = UserWord.find(translation_word, to_lang)

    # search = Search.query.filter_by(
    #     user=flask.g.user, user_word=user_word, language=to_lang
    # ).order_by(Search.id.desc()).first()

    #create the text entity first
    new_text = Text(context, from_lang, url)
    bookmark = Bookmark(user_word, translation, flask.g.user, new_text,
                        datetime.datetime.now())
    zeeguu.db.session.add(bookmark)
    bookmark.calculate_probabilities_after_adding_a_bookmark(
        flask.g.user, bookmark.origin.language)
    return str(bookmark.id)