def send_verification_email(user: models.User): """ Send a verification email. :param user: the user. """ verification_token = authentication.generate_token( user.id, authentication.TokenType.VERIFICATION) process_emails.set_language(user.language) return process_emails.send_verification_email(user.email, verification_token)
def process_alarms(session: sqlalchemy.orm.Session): """ Process the alarms that exist in the DB. :param session: the db session. """ alarms = db_calls.get_alarms(session) for a in alarms: user = db_calls.get_user_id(session, a.user_id) search_adult = user.show_adult if user is not None else False if a.alarm_type == response_models.AlarmType.LISTINGS.value: titles = [a.show_name] db_shows = [] else: titles = get_show_titles(session, a.trakt_id, a.is_movie) db_shows = search_sessions_db_with_tmdb_id( session, a.trakt_id, a.is_movie, only_new=True, show_season=a.show_season, show_episode=a.show_episode, use_excluded_channels=True, user_id=user.id) db_shows += search_sessions_db(session, titles, a.is_movie, complete_title=True, only_new=True, show_season=a.show_season, show_episode=a.show_episode, search_adult=search_adult, use_excluded_channels=True, user_id=user.id, ignore_with_tmdb_id=True) if len(db_shows) > 0: process_emails.set_language(user.language) process_emails.send_alarms_email(user.email, db_shows) # Update the datetime of the last processing of the alarms last_update = db_calls.get_last_update(session) last_update.alarms_datetime = datetime.datetime.utcnow() db_calls.commit(session)
def send_change_email_new(session, change_token_old: str, new_email: str) -> (bool, bool): """ Send a 'Change Email' email to the new email address. :param session: the db session. :param change_token_old: the change token from the old email address. :param new_email: the new email. :return: a pair of booleans: the first is the success of the operation and the second is if the motif of the failure is that the new email is already at use. """ # Validate the change token from the old email address valid, user_id = authentication.validate_token( change_token_old.encode(), authentication.TokenType.CHANGE_EMAIL_OLD) if not valid: return False, False # Get the user id from the token user_id = authentication.get_token_field(change_token_old.encode(), 'user') # Get user user = session.query(models.User).filter(models.User.id == user_id).first() if user is None: return False, False # Check if the new email is valid new_email_user = session.query( models.User).filter(models.User.email == new_email).first() if new_email_user is not None: return False, True changes = {ChangeType.NEW_EMAIL.value: new_email} change_email_new_token = authentication.generate_change_token( user.id, authentication.TokenType.CHANGE_EMAIL_NEW, changes).decode() process_emails.set_language(user.language) return process_emails.send_change_email_new(new_email, change_email_new_token, user.email), True
def send_deletion_email(session, user_id: str) -> bool: """ Send a verification email. :param session: the db session. :param user_id: the user id. """ # Get user user = session.query(models.User).filter(models.User.id == user_id).first() if user is None: return False deletion_token = authentication.generate_token( user.id, authentication.TokenType.DELETION, session) process_emails.set_language(user.language) return process_emails.send_deletion_email(user.email, deletion_token)
def send_password_recovery_email(session, user_id: str) -> bool: """ Send a recover password email. :param session: the db session. :param user_id: the user id. """ # Get user user = session.query(models.User).filter(models.User.id == user_id).first() if user is None: return False password_recovery_token = authentication.generate_token( user.id, authentication.TokenType.PASSWORD_RECOVERY, session) process_emails.set_language(user.language) return process_emails.send_password_recovery_email( user.email, password_recovery_token)
def send_change_email_old(session, user_id: str) -> bool: """ Send a change email email to the old email. :param session: the db session. :param user_id: the user id. """ # Get user user = session.query(models.User).filter(models.User.id == user_id).first() if user is None: return False change_email_old_token = authentication.generate_token( user.id, authentication.TokenType.CHANGE_EMAIL_OLD, session) process_emails.set_language(user.language) return process_emails.send_change_email_old(user.email, change_email_old_token)
def process_reminders(session: sqlalchemy.orm.Session) -> None: """ Process the reminders that exist in the DB, sending an email when the session is within the desired time frame. :param session: the db session. """ reminders_sessions = db_calls.get_sessions_reminders(session) for a_s in reminders_sessions: reminder: models.Reminder = a_s[0] show_session: models.ShowSession = a_s[1] anticipation_hours = int(reminder.anticipation_minutes / 60) # Replace the time minutes and seconds so that it can ensure the anticipation hours show_time = show_session.date_time.replace( minute=0, second=0) - datetime.timedelta(minutes=5) # If it is time to fire the reminder if datetime.datetime.utcnow() + datetime.timedelta( hours=anticipation_hours) > show_time: show_session_tuple = db_calls.get_show_session_complete( session, show_session.id) user = db_calls.get_user_id(session, reminder.user_id) local_show_result = response_models.LocalShowResult.create_from_show_session( show_session_tuple[0], show_session_tuple[1], show_session_tuple[2]) process_emails.set_language(user.language) process_emails.send_reminders_email(user.email, [local_show_result]) session.delete(reminder) session.commit()