def update_stopstalk_rating(user_id, stopstalk_handle, custom): atable = db.auth_user cftable = db.custom_friend column_name = "custom_user_id" if custom else "user_id" query = """ SELECT time_stamp, problem_link, status, site, problem_id FROM submission WHERE %(column_name)s = %(user_id)d ORDER BY time_stamp """ % { "column_name": column_name, "user_id": user_id } all_submissions = db.executesql(query) user_submissions = [] for submission in all_submissions: user_submissions.append({ column_name: user_id, "time_stamp": submission[0], "problem_link": submission[1], "status": submission[2], "site": submission[3], "problem_id": submission[4] }) final_rating = utilities.get_stopstalk_user_stats( stopstalk_handle, custom, user_submissions)["rating_history"] final_rating = dict(final_rating) today = str(datetime.datetime.now().date()) current_rating = int(sum(final_rating[today])) update_params = dict(stopstalk_rating=current_rating) if custom: cftable(user_id).update_record(**update_params) else: current.REDIS_CLIENT.delete( utilities.get_user_record_cache_key(user_id)) atable(user_id).update_record(**update_params) db.commit() # Update global leaderboard cache current_value = current.REDIS_CLIENT.get("global_leaderboard_cache") if current_value is None: # Global leaderboard cache not present return current_rating import json current_value = json.loads(current_value) for row in current_value: if row[1] == stopstalk_handle: row[3] = current_rating current_value = reorder_leaderboard_data(current_value) current.REDIS_CLIENT.set("global_leaderboard_cache", json.dumps(current_value), ex=1 * 60 * 60) return current_rating
def update_details(): """ Update user details """ form_fields = [ "first_name", "last_name", "email", "institute", "country", "stopstalk_handle" ] for site in current.SITES: form_fields.append(site.lower() + "_handle") atable = db.auth_user stable = db.submission record = utilities.get_user_records([session.user_id], "id", "id", True) for field in form_fields: if record[field] is None: continue record[field] = record[field].encode("utf-8") # Do not allow to modify stopstalk_handle and email atable.stopstalk_handle.writable = False atable.stopstalk_handle.comment = T("StopStalk handle cannot be updated") atable.email.readable = True atable.email.writable = False atable.email.comment = T("Email cannot be updated") form = SQLFORM(db.auth_user, record, fields=form_fields, showid=False) if form.process(onvalidation=current.sanitize_fields).accepted: current.REDIS_CLIENT.delete( utilities.get_user_record_cache_key(session.user_id)) session.flash = T("User details updated") updated_sites = utilities.handles_updated(record, form) if updated_sites != []: utilities.clear_profile_page_cache(record.stopstalk_handle) site_lrs = {} nrtable = db.next_retrieval submission_query = (stable.user_id == session.user_id) nrtable_record = db( nrtable.user_id == session.user_id).select().first() if nrtable_record is None: nid = nrtable.insert(user_id=session.user_id) nrtable_record = nrtable(nid) for site in updated_sites: site_lrs[site.lower() + "_lr"] = current.INITIAL_DATE nrtable_record.update({site.lower() + "_delay": 0}) nrtable_record.update_record() pickle_file_path = "./applications/stopstalk/graph_data/" + \ str(session.user_id) + ".pickle" import os if os.path.exists(pickle_file_path): os.remove(pickle_file_path) # Reset the user only if any of the profile site handle is updated query = (atable.id == session.user_id) db(query).update(stopstalk_rating=0, stopstalk_prev_rating=0, per_day=0.0, per_day_change="0.0", authentic=False, graph_data_retrieved=False, **site_lrs) submission_query &= (stable.site.belongs(updated_sites)) # Only delete the submission of those particular sites # whose site handles are updated db(submission_query).delete() session.auth.user = db.auth_user(session.user_id) current.REDIS_CLIENT.delete( CARD_CACHE_REDIS_KEYS["more_accounts_prefix"] + str(session.user_id)) redirect(URL("default", "index")) elif form.errors: response.flash = T("Form has errors") return dict(form=form)
def update_stopstalk_rating(user_id, stopstalk_handle, custom): atable = db.auth_user cftable = db.custom_friend column_name = "custom_user_id" if custom else "user_id" query = """ SELECT time_stamp, problem_link, status, site, problem_id FROM submission WHERE %(column_name)s = %(user_id)d ORDER BY time_stamp """ % { "column_name": column_name, "user_id": user_id } all_submissions = db.executesql(query) user_submissions = [] for submission in all_submissions: user_submissions.append({ column_name: user_id, "time_stamp": submission[0], "problem_link": submission[1], "status": submission[2], "site": submission[3], "problem_id": submission[4] }) final_rating = utilities.get_stopstalk_user_stats( stopstalk_handle, custom, user_submissions)["rating_history"] final_rating = dict(final_rating) today = str(datetime.datetime.now().date()) current_rating = int(sum(final_rating[today])) update_params = dict(stopstalk_rating=current_rating) record = None if custom: record = cftable(user_id) else: record = atable(user_id) current.REDIS_CLIENT.delete( utilities.get_user_record_cache_key(user_id)) record.update_record(**update_params) db.commit() if custom == True: # Don't need to do anything on global_leaderboard_cache if custom is true return current_rating # Update global leaderboard cache current_value = current.REDIS_CLIENT.get(GLOBAL_LEADERBOARD_CACHE_KEY) if current_value is None: # Global leaderboard cache not present return current_rating import json current_value = json.loads(current_value) reorder_leaderboard = False for row in current_value: if row[1] == stopstalk_handle: row[3] = current_rating reorder_leaderboard = True break if not reorder_leaderboard: reorder_leaderboard = True cf_count = db(cftable.user_id == record.id).count() current_value.append( (record.first_name + " " + record.last_name, record.stopstalk_handle, record.institute, record.stopstalk_rating, float(record.per_day_change), utilities.get_country_details(record.country), cf_count, 0)) current_value = reorder_leaderboard_data(current_value) current.REDIS_CLIENT.set(GLOBAL_LEADERBOARD_CACHE_KEY, json.dumps(current_value), ex=ONE_HOUR) return current_rating