def test_find_or_create(self): random_word = self.faker.word() random_language = LanguageRule().random user_word_not_in_db = UserWord(random_word, random_language) user_word_created = UserWord.find_or_create(self.db.session, random_word, random_language) assert user_word_created == user_word_not_in_db
def process_bookmarked_sentences(user_article, start_time=LONG_TIME_IN_THE_PAST, end_time=datetime.now()): """ Process all bookmarks for the user_article within the specified times Parameters: user_article = user_article class object start_time = datetime from which to take bookmarks end_time = datetime of final bookmark to process returns: list of processed bookmarks """ user = user_article.user # Get all the bookmarks for the specified article and dates query = Bookmark.query.join(Text).filter(Bookmark.user == user).filter(Text.url == user_article.article.url) query = query.filter(Bookmark.time >= start_time).filter(Bookmark.time <= end_time) bookmarks = query.order_by(Bookmark.time).all() for bookmark in bookmarks: # Get text in the sentence of the bookmark sentence = Text.query.filter(Text.id == bookmark.text_id).one().content # Get unique words in sentence unique_words_in_sentence = extract_words_from_text(sentence, user_article.article.language) for word in unique_words_in_sentence: # label the word as clicked or not clicked if word in bookmark.origin.word.lower(): event_type = WIH_READ_CLICKED else: event_type = WIH_READ_NOT_CLICKED_IN_SENTENCE # Get or create word object user_word = UserWord.find_or_create(session=session, _word=word, language=user_article.article.language) # Find a WordInteractionHistory word_interaction_history = WordInteractionHistory.find_or_create(user=user, word=user_word) # Add event word_interaction_history.insert_event(event_type, bookmark.time) word_interaction_history.save_to_db(session) return bookmarks
previous_fully_read_date = fully_read_date # Extract words from bookmarked sentences for bookmark in processed_bookmarks: # Get text in the sentece of the bookmark sentence = Text.query.filter(Text.id == bookmark.text_id).one().content # Get unique words in sentence unique_words_in_sentence = extract_words_from_text(sentence, ua.article.language) # Remove already processed words unique_words_in_article = unique_words_in_article.difference(unique_words_in_sentence) # Insert remaining words as not clicked out of sentence for word in unique_words_in_article: user_word = UserWord.find_or_create(session=session, _word=word, language=ua.article.language) word_interaction_history = WordInteractionHistory.find_or_create(user=user, word=user_word) word_interaction_history.insert_event(WIH_READ_NOT_CLICKED_OUT_SENTENCE, fully_read_date) word_interaction_history.save_to_db(session) # ==============================EXERCISES================================ # words encountered in exercises bmex_mapping = data = session.query(bookmark_exercise_mapping).all() for bm_id, ex_id in bmex_mapping: try: bm = Bookmark.query.filter(Bookmark.id == bm_id).one() ex = Exercise.query.filter(Exercise.id == ex_id).one()