def user_article_history(user_id): user = User.find_by_id(user_id) sessions = UserReadingSession.find_by_user(user.id) dates = {} for each in sessions[:-650:-1]: if each.article and each.duration > 1000: if not dates.get(each.human_readable_date()): dates[each.human_readable_date()] = [] # user_article = UserArticle.find(user, each.article) events_in_this_session = each.events_in_this_session() has_like = False feedback = "" difficulty = "" for event in events_in_this_session: if event.is_like(): has_like = True if event.is_feedback(): feedback = event.value difficulty = "fk: " + str(each.article.fk_difficulty) dates[each.human_readable_date()].append({ 'date': each.human_readable_date(), 'duration': each.human_readable_duration(), 'start': each.start_time.strftime("%H:%M:%S"), 'article': each.article.title, 'liked': has_like, 'feedback': feedback, 'difficulty': difficulty }) text_result = f"<title>{user.name}</title>" text_result += f"<h1>{user.name} ({user.id})</h1><br/>" previous_title = "" for date in dates: text_result += date + "<br/>" for session in dates[date]: if previous_title != session['article']: text_result += f"<br/> <b> {session['article']} </b><br/>" text_result += f" {session['duration']} ({session['start']})" if session['liked']: text_result += ' (LIKED) ' text_result += session['difficulty'] + " " + session[ 'feedback'] + " <br/>" previous_title = session['article'] text_result += "<br/><br/>" return text_result
def _link_teacher_cohort(user_id, cohort_id): ''' Takes user_id and cohort_id and links them together in teacher_cohort_map table. ''' from zeeguu_core.model import TeacherCohortMap user = User.find_by_id(user_id) cohort = Cohort.find(cohort_id) db.session.add(TeacherCohortMap(user, cohort)) db.session.commit() return 'added teacher_cohort relationship'
def _link_teacher_cohort(user_id, cohort_id): ''' Takes user_id and cohort_id and links them together in teacher_cohort_map table. ''' from zeeguu_core.model import TeacherCohortMap user = User.find_by_id(user_id) cohort = Cohort.find(cohort_id) db.session.add(TeacherCohortMap(user, cohort)) db.session.commit() return 'added teacher_cohort relationship'
def fix_bookmark_priorities(USER_ID): print(f"fixing for user {USER_ID}") user = User.find_by_id(USER_ID) all_users_bookmarks = user.all_bookmarks() for each in all_users_bookmarks: each.update_fit_for_study() db.session.commit() BookmarkPriorityUpdater.update_bookmark_priority(db, user) print(f"... OK for {len(all_users_bookmarks)} bookmarks")
def user_article_history(user_id): user = User.find_by_id(user_id) sessions = UserReadingSession.find_by_user(user.id) dates = {} for each in sessions[:-650:-1]: if each.article and each.duration > 1000: if not dates.get(each.human_readable_date()): dates[each.human_readable_date()] = [] # user_article = UserArticle.find(user, each.article) events_in_this_session = each.events_in_this_session() has_like = False feedback = "" difficulty = "" for event in events_in_this_session: if event.is_like(): has_like = True if event.is_feedback(): feedback = event.value difficulty = "fk: " + str(each.article.fk_difficulty) dates[each.human_readable_date()].append( { 'date': each.human_readable_date(), 'duration': each.human_readable_duration(), 'start': each.start_time.strftime("%H:%M:%S"), 'article': each.article.title, 'liked': has_like, 'feedback': feedback, 'difficulty': difficulty } ) text_result = f"<title>{user.name}</title>" text_result += f"<h1>{user.name} ({user.id})</h1><br/>" previous_title = "" for date in dates: text_result += date + "<br/>" for session in dates[date]: if previous_title != session['article']: text_result += f"<br/> <b> {session['article']} </b><br/>" text_result += f" {session['duration']} ({session['start']})" if session['liked']: text_result += ' (LIKED) ' text_result += session['difficulty'] + " " + session['feedback'] + " <br/>" previous_title = session['article'] text_result += "<br/><br/>" return text_result
def recompute_for_users(): """ recomputes only those caches that are already in the table and belong to a user. if multiple users have the same preferences the computation is donne only for the first because this is how recompute_recommender_cache_if_needed does. To think about: - what happens when this script is triggered simultaneously with triggering recompute_recommender_cache_if_needed from the UI? will there end up be duplicated recommendations? should we add a uninque constraint on (hash x article)? Note: in theory, the recomputing should be doable independent of users in practice, the recompute_recommender_cache takes the user as input. for that function to become independent of the user we need to be able to recover the ids of the languages, topics, searchers, etc. from the content_hash to do this their ids would need to be comma separated OTOH, in the future we might still want to have a per-user cache because the recommendations might be different for each user since every user has different language levels!!! :param existing_hashes: :return: """ already_done = [] for user_id in User.all_recent_user_ids(): try: user = User.find_by_id(user_id) reading_pref_hash = reading_preferences_hash(user) if reading_pref_hash not in already_done: recompute_recommender_cache_if_needed(user, session) zeeguu_core.log_n_print( f"Success for {reading_pref_hash} and {user}") already_done.append(reading_pref_hash) else: zeeguu_core.log_n_print( f"nno need to do for {user}. hash {reading_pref_hash} already done" ) except Exception as e: zeeguu_core.log_n_print(f"Failed for user {user}")
def bookmarks_for_article(article_id, user_id): """ Returns the bookmarks of this user organized by date. Based on the POST arguments, it can return also the context of the bookmark as well as it can return only the bookmarks after a given date. :param (POST) with_context: If this parameter is "true", the endpoint also returns the text where the bookmark was found. :param (POST) after_date: the date after which to start retrieving the bookmarks. if no date is specified, all the bookmarks are returned. The date format is: %Y-%m-%dT%H:%M:%S. E.g. 2001-01-01T01:55:00 """ user = User.find_by_id(user_id) article = Article.query.filter_by(id=article_id).one() bookmarks = user.bookmarks_for_article(article_id, with_context=True, with_title=True) return json_result(dict(bookmarks=bookmarks, article_title=article.title))
def bookmarks_for_article(article_id, user_id): """ Returns the bookmarks of this user organized by date. Based on the POST arguments, it can return also the context of the bookmark as well as it can return only the bookmarks after a given date. :param (POST) with_context: If this parameter is "true", the endpoint also returns the text where the bookmark was found. :param (POST) after_date: the date after which to start retrieving the bookmarks. if no date is specified, all the bookmarks are returned. The date format is: %Y-%m-%dT%H:%M:%S. E.g. 2001-01-01T01:55:00 """ user = User.find_by_id(user_id) article = Article.query.filter_by(id=article_id).one() bookmarks = user.bookmarks_for_article(article_id, with_context=True, with_title=True) return json_result(dict( bookmarks = bookmarks, article_title = article.title ))
#!/usr/bin/env python """ Script that lists recent users To be called from a cron job. """ from zeeguu_core.model import User for user_id in User.all_recent_user_ids(): user = User.find_by_id(user_id) print(user.name) print(user.email)
def past_exercises_for(user_id): user = User.find_by_id(USER_ID) q = (db.session.query(Exercise).join(bookmark_exercise_mapping).join( Bookmark).join(User).filter(User.id == USER_ID).order_by( Exercise.time)) for ex in q.all(): bookmark = ex.get_bookmark() past = "" sorted_log = sorted( bookmark.exercise_log, key=lambda x: datetime.datetime.strftime(x.time, "%Y-%m-%d"), reverse=True) corrects_in_a_row = 0 for each in sorted_log: if each.time < ex.time: if each.outcome.outcome == "Correct": corrects_in_a_row += 1 else: corrects_in_a_row = 0 past += f"{each.time.day}/{each.time.month} {each.outcome.outcome} < " if ex.outcome.outcome == "Correct": corrects_in_a_row += 1 else: corrects_in_a_row = 0 if corrects_in_a_row: print( f"{ex.time.day}/{ex.time.month} {bookmark.origin.word}({bookmark.id}) {ex.outcome.outcome}:{corrects_in_a_row} < ({past})" ) else: print( f"{ex.time.day}/{ex.time.month} {bookmark.origin.word}({bookmark.id}) {ex.outcome.outcome} < ({past})" ) if bookmark.learned and ex.time == bookmark.learned_time: print("Learned!") print(" ") print("All Bookmarks") for bookmark in user.all_bookmarks(): btime = datetime.datetime.strftime(bookmark.time, "%Y-%m-%d") print(f"{btime} " + ("[fit_for_study] " if bookmark.fit_for_study else "") + ("[Learned] " if bookmark.learned else "") + f"Ctx: {bookmark.context_word_count()} " + f"{bookmark.id} " + f"{bookmark.origin.word} / {bookmark.translation.word}") print("") print("Bookmarks to Study") for bookmark in user.bookmarks_to_study(): btime = datetime.datetime.strftime(bookmark.time, "%Y-%m-%d") print(f"{btime} " + ("[Quality] " if bookmark.quality_bookmark() else "") + ("[fit_for_study] " if bookmark.fit_for_study else "") + ("[Learned] " if bookmark.learned else "") + f"Ctx: {bookmark.context_word_count()} " + f"{bookmark.id} " + f"{bookmark.origin.word} / {bookmark.translation.word}")
def __get_bookmarks_for_user(self, user_id): user = User.find_by_id(user_id) print('Using user ' + user.name + ' with id ' + str(user.id)) return user.all_bookmarks()
import datetime from zeeguu_core.model import User, Exercise, Bookmark from zeeguu_core.model.bookmark import bookmark_exercise_mapping from zeeguu_core import db # USER_ID = 2162 # USER_ID = 2145 # Fe # USER_ID = 2134 #Victor USER_ID = 534 user = User.find_by_id(USER_ID) q = (db.session.query(Exercise).join(bookmark_exercise_mapping).join( Bookmark).join(User).filter(User.id == USER_ID).order_by(Exercise.time)) for ex in q.all(): bookmark = ex.get_bookmark() past = "" sorted_log = sorted( bookmark.exercise_log, key=lambda x: datetime.datetime.strftime(x.time, "%Y-%m-%d"), reverse=True) corrects_in_a_row = 0 for each in sorted_log: if each.time < ex.time: if each.outcome.outcome == "Correct": corrects_in_a_row += 1 else:
#!/usr/bin/env python """ Script that lists recent users To be called from a cron job. """ from sortedcontainers import SortedList from zeeguu_core.model import User, Bookmark from wordstats import Word user = User.find_by_id(1890) language = 'nl' months_dict = dict() for bookmark in Bookmark.query.filter_by(user=user): if not bookmark.origin.language.code == language: continue # if not bookmark.quality_bookmark(): # continue if len(bookmark.origin.word) < 4: continue date_key = bookmark.time.strftime("%y-%m")
def __init__(self, user_id): self.user_id = user_id self.user = User.find_by_id(user_id)
from zeeguu_core.model import User, UserActivityData, Bookmark, UserArticle, UserReadingSession, UserExerciseSession from sys import argv if len(argv) < 3: print("CALL: consolidate_accounts <primary_id> <secondary_id>") exit(-1) PRIMARY_ID = argv[1] SECONDARY_ID = argv[2] tables_to_modify = [ Bookmark, UserActivityData, UserArticle, UserReadingSession, UserExerciseSession ] primary_user = User.find_by_id(PRIMARY_ID) secondary_user = User.find_by_id(SECONDARY_ID) for each_table in tables_to_modify: primary_user_items = each_table.query.filter_by( user_id=primary_user.id).all() secondary_user_items = each_table.query.filter_by( user_id=secondary_user.id).all() print(each_table.__tablename__) print(f"= Primary User Before:{len(primary_user_items)}") print(f"= Secondary User Before:{len(secondary_user_items)}") for each in secondary_user_items: each.user = primary_user