def send_due_reminders(): """ Sends all email reminders that are due :return: None """ app.logger.info("Checking for new email reminders") reminders = ReminderSettings.query.join(ReminderSettings.user).all() all_users = User.query.filter_by(confirmed=True).all() for reminder_type in ReminderType: reminder_users = [ reminder.user_id for reminder in reminders if reminder.reminder_type == reminder_type ] for user in all_users: if user.id not in reminder_users and user.confirmed: reminder = ReminderSettings( user=user, reminder_type=reminder_type ) db.session.add(reminder) reminders.append(reminder) db.session.commit() for reminder in reminders: reminder.send_reminder()
def test_missing_column_data(self): """ Tests that missing column data is handled correctly :return: None """ self._test_missing_column_data([ ReminderSettings(user=self.user_two), ReminderSettings(reminder_type=ReminderType.EMAIL) ])
def set_reminder(): """ Allows the user to set an email reminder :return: The response """ hours = int(request.form["hours"]) reminder_states = { reminder_type: request.form.get(reminder_type.value) in ["on", True] for reminder_type in ReminderType } if not 0 < hours < 49: flash("Ungültige Anzahl Stunden eingegeben", "danger") else: for reminder_type, reminder_state in reminder_states.items(): setting = ReminderSettings(user_id=current_user.id, reminder_type=reminder_type, active=reminder_state, reminder_time=hours) db.session.merge(setting) db.session.commit() flash("Erinnerungseinstellungen gespeichert", "success") return redirect(url_for("user_management.profile"))
def test_due_when_bets_placed(self): """ Tests if the is_due method works correctly when bets are placed :return: None """ now = datetime.utcnow() reminder = ReminderSettings(user=self.user_two, reminder_time=600, reminder_type=ReminderType.EMAIL) self.db.session.delete(self.match) self.db.session.commit() match_one = Match( home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff=(now + timedelta(minutes=5)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) match_two = Match( home_team=self.team_two, away_team=self.team_one, matchday=1, kickoff=(now + timedelta(minutes=7)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.db.session.add(match_one) self.db.session.add(match_two) self.db.session.commit() self.assertEqual(len(reminder.get_due_matches()), 2) self.generate_sample_bet(self.user_two, match_one) self.assertEqual(len(reminder.get_due_matches()), 1) self.generate_sample_bet(self.user_two, match_two) self.assertEqual(len(reminder.get_due_matches()), 0)
def test_uniqueness(self): """ Tests that unique attributes are correctly checked :return: None """ self._test_uniqueness([ ReminderSettings(user=self.user_one, reminder_type=ReminderType.EMAIL) ])
def setUp(self): """ Sets up some sample data :return: None """ super().setUp() self.model_cls = None # type: db.Model self.team_one, self.team_two, self.player, self.match, self.goal = \ self.generate_sample_match_data() self.user_one = self.generate_sample_user(True)[0] self.user_two = self.generate_sample_user(True)[0] self.api_key = self.generate_api_key(self.user_one)[0] self.bet = self.generate_sample_bet(self.user_one, self.match) self.reminder = ReminderSettings(user=self.user_one, reminder_type=ReminderType.EMAIL, reminder_time=1) self.db.session.add(self.reminder) self.db.session.commit()
def setUp(self): """ Sets up a user for testing :return: None """ super().setUp() self.user = self.generate_sample_user(True)[0] now = datetime.utcnow() now_str = now.strftime("%Y-%m-%d:%H-%M-%S") then = now + timedelta(hours=24) then_str = then.strftime("%Y-%m-%d:%H-%M-%S") self.reminder = ReminderSettings(user_id=self.user.id, reminder_type=ReminderType.EMAIL, last_reminder=now_str, reminder_time=48 * 60 * 60) _, _, _, self.match, _ = self.generate_sample_match_data() self.match.kickoff = then_str self.db.session.add(self.reminder) self.db.session.commit()
def test_due(self): """ Tests if due matches can be found correctly Dates: Now ----- Reminder 1 ----- Match 1 ----- Reminder 2 ----- Match 2 -0----------10min-----------30min-----------60min---------120min- :return: None """ now = datetime.utcnow() new_user = User(username="******", email="Z", confirmed=True, password_hash="Z", confirmation_hash="Z") reminder_one = ReminderSettings(user=self.user_two, reminder_time=600, reminder_type=ReminderType.EMAIL) reminder_two = ReminderSettings(user=new_user, reminder_time=3600, reminder_type=ReminderType.EMAIL) self.db.session.delete(self.match) self.db.session.commit() match_one = Match( home_team=self.team_one, away_team=self.team_two, matchday=1, kickoff=(now + timedelta(minutes=30)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) match_two = Match( home_team=self.team_two, away_team=self.team_one, matchday=1, kickoff=(now + timedelta(minutes=120)).strftime("%Y-%m-%d:%H-%M-%S"), started=False, finished=False, home_current_score=0, away_current_score=0, season=self.config.season(), league=self.config.OPENLIGADB_LEAGUE) self.db.session.add(match_one) self.db.session.add(match_two) self.db.session.add(new_user) self.db.session.commit() self.assertEqual(reminder_one.get_due_matches(), []) self.assertEqual(reminder_two.get_due_matches(), [match_one]) with self.context: with mock.patch("bundesliga_tippspiel.db.settings." "ReminderSettings.send_email") as mocked: reminder_one.send_reminder() self.assertEqual(0, mocked.call_count) reminder_two.send_reminder() self.assertEqual(1, mocked.call_count) reminder_one.send_reminder() reminder_two.send_reminder() self.assertEqual(1, mocked.call_count) self.assertEqual(reminder_one.get_due_matches(), []) self.assertEqual(reminder_two.get_due_matches(), []) reminder_one.set_reminder_time(10000) reminder_two.set_reminder_time(10000) self.assertEqual(reminder_two.get_due_matches(), [match_one, match_two]) self.assertEqual(reminder_two.get_due_matches(), [match_one, match_two]) with self.context: with mock.patch("bundesliga_tippspiel.db.settings." "ReminderSettings.send_email") as mocked: reminder_one.send_reminder() self.assertEqual(1, mocked.call_count) reminder_two.send_reminder() self.assertEqual(2, mocked.call_count) reminder_one.send_reminder() reminder_two.send_reminder() self.assertEqual(2, mocked.call_count)