Exemplo n.º 1
0
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_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)