示例#1
0
 def postUpdateHook(cls, matches, updated_attr_list, is_new_list):
     '''
     To run after the match has been updated.
     Send push notifications to subscribed users
     Only if the match is part of an active event
     '''
     unplayed_match_events = []
     for (match, updated_attrs, is_new) in zip(matches, updated_attr_list,
                                               is_new_list):
         event = match.event.get()
         # Only continue if the event is currently happening
         if event.now:
             if match.has_been_played:
                 if is_new or 'alliances_json' in updated_attrs:
                     # There is a score update for this match, push a notification
                     logging.info(
                         "Sending push notifications for {}".format(
                             match.key_name))
                     try:
                         NotificationHelper.send_match_score_update(match)
                     except Exception, exception:
                         logging.error(
                             "Error sending match updates: {}".format(
                                 exception))
                         logging.error(traceback.format_exc())
                     try:
                         TBANSHelper.match_score(match)
                     except Exception, exception:
                         logging.error(
                             "Error sending match {} updates: {}".format(
                                 match.key_name, exception))
                         logging.error(traceback.format_exc())
             else:
                 if is_new or (set([
                         'alliances_json', 'time', 'time_string'
                 ]).intersection(set(updated_attrs)) != set()):
                     # The match has not been played and we're changing a property that affects the event's schedule
                     # So send a schedule update notification for the parent event
                     if event not in unplayed_match_events:
                         unplayed_match_events.append(event)
    def post(self):
        self._require_admin()

        user_id = self.user_bundle.account.key.id()

        notification_type = self.request.get('type')
        if notification_type == "alliance_selection":
            event_key = self.request.get('event_key')
            event = Event.get_by_id(event_key)
            if not event:
                self.template_values.update({
                    'error': 'No event for key {}'.format(event_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.alliance_selection(event, user_id)
        elif notification_type == "awards":
            event_key = self.request.get('event_key')
            event = Event.get_by_id(event_key)
            if not event:
                self.template_values.update({
                    'error': 'No event for key {}'.format(event_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.awards(event, user_id)
        elif notification_type == "event_level":
            match_key = self.request.get('match_key')
            match = Match.get_by_id(match_key)
            if not match:
                self.template_values.update({
                    'error': 'No match for key {}'.format(match_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.event_level(match, user_id)
        elif notification_type == "event_schedule":
            event_key = self.request.get('event_key')
            event = Event.get_by_id(event_key)
            if not event:
                self.template_values.update({
                    'error': 'No event for key {}'.format(event_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.event_schedule(event, user_id)
        elif notification_type == "match_score":
            match_key = self.request.get('match_key')
            match = Match.get_by_id(match_key)
            if not match:
                self.template_values.update({
                    'error': 'No match for key {}'.format(match_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.match_score(match, user_id)
        elif notification_type == "match_upcoming":
            match_key = self.request.get('match_key')
            match = Match.get_by_id(match_key)
            if not match:
                self.template_values.update({
                    'error': 'No match for key {}'.format(match_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.match_upcoming(match, user_id)
        elif notification_type == "match_video":
            match_key = self.request.get('match_key')
            match = Match.get_by_id(match_key)
            if not match:
                self.template_values.update({
                    'error': 'No match for key {}'.format(match_key)
                })
                return self.redirect('/admin/tbans')

            TBANSHelper.match_video(match, user_id)
        elif notification_type == "ping":
            clients = MobileClient.clients([user_id])
            for client in clients:
                TBANSHelper.ping(client)

        return self.redirect('/admin/tbans')