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 test_data_payload_team(self):
     team = Team.get_by_id('frc1')
     notification = AllianceSelectionNotification(self.event, team)
     payload = notification.data_payload
     self.assertEqual(len(payload), 2)
     self.assertEqual(payload['event_key'], self.event.key_name)
     self.assertEqual(payload['team_key'], team.key_name)
 def test_webhook_message_data_team(self):
     team = Team.get_by_id('frc1')
     notification = AllianceSelectionNotification(self.event, team)
     payload = notification.webhook_message_data
     self.assertEqual(len(payload), 4)
     self.assertEqual(payload['event_key'], self.event.key_name)
     self.assertEqual(payload['team_key'], team.key_name)
     self.assertEqual(payload['event_name'], 'Present Test Event')
     self.assertIsNotNone(payload['event'])
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()
        ndb.get_context().clear_cache()

        self.testbed.init_taskqueue_stub(root_path=".")

        for team_number in range(7):
            Team(id="frc%s" % team_number, team_number=team_number).put()

        self.event = EventTestCreator.createPresentEvent()
        self.notification = AllianceSelectionNotification(self.event)
    def test_fcm_notification_team_four(self):
        team = Team.get_by_id('frc1')
        # Setup alliance selection information
        EventDetails(id=self.event.key_name,
                     alliance_selections=[{
                         "declines": [],
                         "picks": ["frc2", "frc1", "frc3", "frc4"]
                     }]).put()

        notification = AllianceSelectionNotification(self.event, team)
        self.assertIsNotNone(notification.fcm_notification)
        self.assertEqual(notification.fcm_notification.title,
                         'TESTPRESENT Alliances Updated')
        self.assertEqual(
            notification.fcm_notification.body,
            'Present Test Event alliances have been updated. Team 1 is on Alliance 1 with Team 2, Team 3 and Team 4.'
        )
 def test_type(self):
     self.assertEqual(AllianceSelectionNotification._type(),
                      NotificationType.ALLIANCE_SELECTION)