def alliance_selection(cls, event, user_id=None):
        from models.notifications.alliance_selection import AllianceSelectionNotification
        # Send to Event subscribers
        if NotificationType.ALLIANCE_SELECTION in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    event, NotificationType.ALLIANCE_SELECTION)
            if users:
                cls._send(users, AllianceSelectionNotification(event))

        # Send to Team subscribers
        if NotificationType.ALLIANCE_SELECTION in NotificationType.enabled_team_notifications:
            for team_key in event.alliance_teams:
                try:
                    team = Team.get_by_id(team_key)
                except:
                    continue

                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team, NotificationType.ALLIANCE_SELECTION)
                if users:
                    cls._send(users,
                              AllianceSelectionNotification(event, team))
    def match_video(cls, match, user_id=None):
        from models.notifications.match_video import MatchVideoNotification
        # Send to Event subscribers
        if NotificationType.MATCH_VIDEO in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    match.event.get(), NotificationType.MATCH_VIDEO)
            if users:
                cls._send(users, MatchVideoNotification(match))

        # Send to Team subscribers
        if NotificationType.MATCH_VIDEO in NotificationType.enabled_team_notifications:
            for team_key in match.team_keys:
                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team_key.get(), NotificationType.MATCH_VIDEO)
                if users:
                    cls._send(users,
                              MatchVideoNotification(match, team_key.get()))

        # Send to Match subscribers
        if NotificationType.MATCH_VIDEO in NotificationType.enabled_match_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_match(
                    match, NotificationType.MATCH_VIDEO)
            if users:
                cls._send(users, MatchVideoNotification(match))
    def match_upcoming(cls, match, user_id=None):
        from models.notifications.match_upcoming import MatchUpcomingNotification
        # Send to Event subscribers
        if NotificationType.UPCOMING_MATCH in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    match.event.get(), NotificationType.UPCOMING_MATCH)
            if users:
                cls._send(users, MatchUpcomingNotification(match))

        # Send to Team subscribers
        if NotificationType.UPCOMING_MATCH in NotificationType.enabled_team_notifications:
            for team_key in match.team_keys:
                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team_key.get(), NotificationType.UPCOMING_MATCH)
                if users:
                    cls._send(users,
                              MatchUpcomingNotification(match, team_key.get()))

        # Send to Match subscribers
        if NotificationType.UPCOMING_MATCH in NotificationType.enabled_match_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_match(
                    match, NotificationType.UPCOMING_MATCH)
            if users:
                cls._send(users, MatchUpcomingNotification(match))

        # Send LEVEL_STARTING for the first match of a new type
        if match.set_number == 1 and match.match_number == 1:
            cls.event_level(match, user_id)
    def match_score(cls, match, user_id=None):
        event = match.event.get()

        from models.notifications.match_score import MatchScoreNotification
        # Send to Event subscribers
        if NotificationType.MATCH_SCORE in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(
                    event, NotificationType.MATCH_SCORE)
            if users:
                cls._send(users, MatchScoreNotification(match))

        # Send to Team subscribers
        if NotificationType.MATCH_SCORE in NotificationType.enabled_team_notifications:
            for team_key in match.team_keys:
                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(
                        team_key.get(), NotificationType.MATCH_SCORE)
                if users:
                    cls._send(users,
                              MatchScoreNotification(match, team_key.get()))

        # Send to Match subscribers
        if NotificationType.MATCH_SCORE in NotificationType.enabled_match_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_match(
                    match, NotificationType.MATCH_SCORE)
            if users:
                cls._send(users, MatchScoreNotification(match))

        # Send UPCOMING_MATCH for the N + 2 match after this one
        if not event.matches:
            return
        from helpers.match_helper import MatchHelper
        next_matches = MatchHelper.upcomingMatches(event.matches, num=2)
        # TODO: Think about if we need special-case handling for replayed matches
        # (I don't think we do because if a match gets replayed at EoD, we'll cancel/reschedule
        # for that match notification. If a match gets replayed back-to-back (which doesn't happen?)
        # sending a second notification is probably fine.
        # If there are not 2 scheduled matches (end of Quals, end of Quarters, etc.) don't send
        if len(next_matches) < 2:
            return

        next_match = next_matches.pop()
        cls.schedule_upcoming_match(next_match, user_id)
 def test_users_subscribed_to_team_model_type(self):
     # Make sure we filter for model types
     Subscription(parent=ndb.Key(Account, 'user_id_1'),
                  user_id='user_id_1',
                  model_key='2020miket',
                  model_type=ModelType.EVENT,
                  notification_types=[NotificationType.UPCOMING_MATCH
                                      ]).put()
     Subscription(parent=ndb.Key(Account, 'user_id_2'),
                  user_id='user_id_2',
                  model_key='frc7332',
                  model_type=ModelType.TEAM,
                  notification_types=[NotificationType.UPCOMING_MATCH
                                      ]).put()
     users = Subscription.users_subscribed_to_team(
         self.team, NotificationType.UPCOMING_MATCH)
     self.assertItemsEqual(users, ['user_id_2'])
Beispiel #6
0
    def awards(cls, event, user_id=None):
        from models.notifications.awards import AwardsNotification
        # Send to Event subscribers
        if NotificationType.AWARDS in NotificationType.enabled_event_notifications:
            users = [user_id] if user_id else []
            if not users:
                users = Subscription.users_subscribed_to_event(event, NotificationType.AWARDS)
            if users:
                cls._send(users, AwardsNotification(event))

        # Send to Team subscribers
        if NotificationType.AWARDS in NotificationType.enabled_team_notifications:
            # Map all Teams to their Awards so we can populate our Awards notification with more specific info
            team_awards = event.team_awards()
            for team_key in team_awards.keys():
                team = team_key.get()

                users = [user_id] if user_id else []
                if not users:
                    users = Subscription.users_subscribed_to_team(team, NotificationType.AWARDS)
                if users:
                    cls._send(users, AwardsNotification(event, team))