<commit_before>import json
import logging
import traceback

from google.appengine.api import taskqueue
from google.appengine.ext import ndb

from helpers.cache_clearer import CacheClearer
from helpers.firebase.firebase_pusher import FirebasePusher
from helpers.notification_helper import NotificationHelper
from helpers.manipulator_base import ManipulatorBase


class MatchManipulator(ManipulatorBase):
    """
    Handle Match database writes.
    """
    @classmethod
    def getCacheKeysAndControllers(cls, affected_refs):
        return CacheClearer.get_match_cache_keys_and_controllers(affected_refs)

    @classmethod
    def postDeleteHook(cls, matches):
        '''
        To run after the match has been deleted.
        '''
        for match in matches:
            try:
                FirebasePusher.delete_match(match)
            except Exception:
                logging.warning("Enqueuing Firebase delete failed!")

    @classmethod
    def postUpdateHook(cls, matches, updated_attr_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
        '''
        # Note, updated_attr_list will always be empty, for now
        # Still needs to be implemented in updateMerge
        # See helpers.EventManipulator
        unplayed_match_events = []
        for (match, updated_attrs) in zip(matches, updated_attr_list):
            event = match.event.get()
            if event.within_a_day:
                if match.has_been_played:
                    # 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())
                elif not match.has_been_played and event not in unplayed_match_events:
                    # The match has not been played, so we're changing a different property
                    # This means the event's schedule has been updated (e.g. we're adding elim matches)
                    # So send a schedule update notification for the parent event<commit_msg>Check for updated score json props for notifs<commit_after>import json
import logging
import traceback

from google.appengine.api import taskqueue
from google.appengine.ext import ndb

from helpers.cache_clearer import CacheClearer
from helpers.firebase.firebase_pusher import FirebasePusher
from helpers.notification_helper import NotificationHelper
from helpers.manipulator_base import ManipulatorBase


class MatchManipulator(ManipulatorBase):
    """
    Handle Match database writes.
    """
    @classmethod
    def getCacheKeysAndControllers(cls, affected_refs):
        return CacheClearer.get_match_cache_keys_and_controllers(affected_refs)

    @classmethod
    def postDeleteHook(cls, matches):
        '''
        To run after the match has been deleted.
        '''
        for match in matches:
            try:
                FirebasePusher.delete_match(match)
            except Exception:
                logging.warning("Enqueuing Firebase delete failed!")

    @classmethod
    def postUpdateHook(cls, matches, updated_attr_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) in zip(matches, updated_attr_list):
            event = match.event.get()
            # Only continue if the event is currently happening
            # And we're updating a property that affects scores
            if ("alliances_json" in updated_attrs or "score_breakdown_json" in updated_attrs) and event.within_a_day:
                if match.has_been_played:
                    # 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())
                elif not match.has_been_played and event not in unplayed_match_events:
                    # The match has not been played, so we're changing a different property
                    # This means the event's schedule has been updated (e.g. we're adding elim matches)
                    # So send a schedule update notification for the parent event
{'fraction_non_alphanumeric': 0.057962172056131786, 'fraction_numerical': 0.0, 'mean_word_length': 2.3524199045671437, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2201383', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1175, 'n_words': 403}