def update_last_action(db_user: User) -> bool: """ Updates the last action field of the user-row in database. Returns boolean if the users session is older than one hour or True, when she wants to keep the login :param db_user: User in refactored fns, else nickname :return: Boolean """ if not db_user or db_user.nickname == nick_of_anonymous_user: return False timeout_in_sec = 60 * 60 * 24 * 7 # check difference of diff_action = get_now() - db_user.last_action diff_login = get_now() - db_user.last_login diff_action = diff_action.seconds + diff_action.days * 24 * 60 * 60 diff_login = diff_login.seconds + diff_login.days * 24 * 60 * 60 diff = diff_action if diff_action < diff_login else diff_login should_log_out = diff > timeout_in_sec and not db_user.settings.keep_logged_in db_user.update_last_action() transaction.commit() return should_log_out
def _get_special_infos(lang: str) -> Dict[str, Any]: return { 'public_nick': 'Son Goku', 'last_action': sql_timestamp_pretty_print(get_now(), lang), 'last_login': sql_timestamp_pretty_print(get_now(), lang), 'registered': sql_timestamp_pretty_print(get_now(), lang), 'group': 'Saiyajin', 'is_male': True, 'is_female': False, 'is_neutral': False, 'statements_posted': '>9000', 'edits_done': '>9000', 'reviews_proposed': '>9000', 'discussion_arg_votes': '>9000', 'discussion_stat_votes': '>9000', 'avatar_url': '/static/images/goku.jpg', 'discussion_stat_rep': '>9000', }
def tidy_up_optimization_locks(): """ Tidy up all expired locks :return: None """ logger('ReviewQueues', 'main') db_locks = DBDiscussionSession.query(OptimizationReviewLocks).all() for lock in db_locks: if (get_now() - lock.locked_since).seconds >= max_lock_time_in_sec: DBDiscussionSession.query(OptimizationReviewLocks).filter_by( review_optimization_uid=lock.review_optimization_uid).delete()
def should_log_out(timeout: int, user: User) -> bool: """ Returns a boolean stating that the user should be logged out when its True, and False otherwise. :param timeout: A timeout in seconds. :param user: The User that should be checked for a possible log-out :return: Boolean """ if not user or user.nickname == nick_of_anonymous_user: return False # check difference of diff_action: timedelta = get_now() - user.last_action diff_login: timedelta = get_now() - user.last_login diff_action: int = diff_action.seconds + diff_action.days * 24 * 60 * 60 diff_login: int = diff_login.seconds + diff_login.days * 24 * 60 * 60 diff = diff_action if diff_action < diff_login else diff_login log_out = diff > timeout and not user.settings.keep_logged_in return log_out
def is_review_locked(review_uid): """ Is the OptimizationReviewLocks set? :param review_uid: OptimizationReviewLocks.uid :return: Boolean """ tidy_up_optimization_locks() logger('ReviewQueues', 'main') db_lock = DBDiscussionSession.query(OptimizationReviewLocks).filter_by( review_optimization_uid=review_uid).first() if not db_lock: return False return (get_now() - db_lock.locked_since).seconds < max_lock_time_in_sec
def is_review_locked(self, review_uid): """ Is the OptimizationReviewLocks set? :param review_uid: OptimizationReviewLocks.uid :return: Boolean """ self.tidy_up_optimization_locks() LOG.debug("Check whether review %s is locked.", review_uid) db_lock = DBDiscussionSession.query(OptimizationReviewLocks).filter_by( review_optimization_uid=review_uid).first() if not db_lock: return False return (get_now() - db_lock.locked_since).seconds < max_lock_time_in_sec
def setUp(self): self.config = testing.setUp() self.config.include('pyramid_chameleon') clear_clicks_of('Tobias') clear_seen_by_of('Tobias') self.db_tobias = DBDiscussionSession.query(User).filter_by( nickname='Tobias').first() self.last_login_of_tobias = self.db_tobias.last_login self.db_tobias.last_login = get_now() DBDiscussionSession.add(self.db_tobias) DBDiscussionSession.flush() transaction.commit() clear_clicks_of('Tobias') clear_seen_by_of('Tobias') self.db_tobias.last_login = self.last_login_of_tobias DBDiscussionSession.add(self.db_tobias) DBDiscussionSession.flush() transaction.commit()
def generate_short_url(url) -> dict: """ Shortens the url via external service and uses our database as cache (7 days) :param url: Url as string, which should be shortened :rtype: dict :return: dictionary with the url, services name and the url of the service or an error """ service = Shorteners.TINYURL service_url = 'http://tinyurl.com/' db_url = DBDiscussionSession.query(ShortLinks).filter_by( long_url=url).first() rdict = { 'url': '', 'service': service, 'service_url': service_url, 'service_text': service, } if db_url and (get_now() - db_url.timestamp).days < 7: rdict['url'] = db_url.short_url return rdict # no or old url, so fetch and set it short_url, service_text = __fetch_url(service, url) if len(short_url) > 0: if db_url: db_url.update_short_url(short_url) else: db_url = ShortLinks(service, url, short_url) DBDiscussionSession.add(db_url) DBDiscussionSession.flush() rdict['url'] = short_url rdict['service_text'] = service_text return rdict