def postUpdateHook(cls, events, updated_attr_list, is_new_list):
        """
        To run after models have been updated
        """
        for (event, updated_attrs) in zip(events, updated_attr_list):
            # Disabled due to unreliability. 2017-01-24 -fangeugene
            # try:
            #     LocationHelper.update_event_location(event)
            # except Exception, e:
            #     logging.error("update_event_location for {} errored!".format(event.key.id()))
            #     logging.exception(e)

            try:
                if event.normalized_location and event.normalized_location.lat_lng:
                    timezone_id = LocationHelper.get_timezone_id(
                        None, lat_lng=event.normalized_location.lat_lng)
                    if not timezone_id:
                        logging.warning("Timezone update for event {} failed!".format(event.key_name))
                    else:
                        event.timezone_id = timezone_id
                else:
                    logging.warning("No Lat/Lng to update timezone_id for event {}!".format(event.key_name))
            except Exception, e:
                logging.error("Timezone update for {} errored!".format(event.key.id()))
                logging.exception(e)

            try:
                SearchHelper.update_event_location_index(event)
            except Exception, e:
                logging.error("update_event_location_index for {} errored!".format(event.key.id()))
                logging.exception(e)
    def postUpdateHook(cls, events, updated_attr_list, is_new_list):
        """
        To run after models have been updated
        """
        for (event, updated_attrs) in zip(events, updated_attr_list):
            lat_lon = event.get_lat_lon()
            if not lat_lon:
                logging.warning("Lat/Lon update for event {} failed with location!".format(event.key_name))
            else:
                timezone_id = LocationHelper.get_timezone_id(None, lat_lon=lat_lon)
                if not timezone_id:
                    logging.warning("Timezone update for event {} failed!".format(event.key_name))
                else:
                    event.timezone_id = timezone_id
                    cls.createOrUpdate(event, run_post_update_hook=False)

            # Add event to lat/lon info to search index
            if lat_lon:
                fields = [
                    search.NumberField(name='year', value=event.year),
                    search.GeoField(name='location', value=search.GeoPoint(lat_lon[0], lat_lon[1]))
                ]
                search.Index(name="eventLocation").put(search.Document(doc_id=event.key.id(), fields=fields))

        # Enqueue task to calculate district points
        for event in events:
            taskqueue.add(
                url='/tasks/math/do/district_points_calc/{}'.format(event.key.id()),
                method='GET')
    def postUpdateHook(cls, events, updated_attr_list, is_new_list):
        """
        To run after models have been updated
        """
        for (event, updated_attrs) in zip(events, updated_attr_list):
            try:
                LocationHelper.update_event_location(event)
            except Exception, e:
                logging.error("update_event_location for {} errored!".format(
                    event.key.id()))
                logging.exception(e)

            try:
                if event.normalized_location and event.normalized_location.lat_lng:
                    timezone_id = LocationHelper.get_timezone_id(
                        None, lat_lng=event.normalized_location.lat_lng)
                    if not timezone_id:
                        logging.warning(
                            "Timezone update for event {} failed!".format(
                                event.key_name))
                    else:
                        event.timezone_id = timezone_id
                else:
                    logging.warning(
                        "No Lat/Lng to update timezone_id for event {}!".
                        format(event.key_name))
            except Exception, e:
                logging.error("Timezone update for {} errored!".format(
                    event.key.id()))
                logging.exception(e)
Exemple #4
0
    def test_2012ct_bad_location(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        event['location'] = "somewhere on mars"
        self.assertEqual(LocationHelper.get_lat_lng(event['location']), None)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), None)
    def postUpdateHook(cls, events, updated_attr_list, is_new_list):
        """
        To run after models have been updated
        """
        for (event, updated_attrs) in zip(events, updated_attr_list):
            # Disabled due to unreliability. 2017-01-24 -fangeugene
            # try:
            #     LocationHelper.update_event_location(event)
            # except Exception, e:
            #     logging.error("update_event_location for {} errored!".format(event.key.id()))
            #     logging.exception(e)

            try:
                if event.normalized_location and event.normalized_location.lat_lng:
                    timezone_id = LocationHelper.get_timezone_id(
                        None, lat_lng=event.normalized_location.lat_lng)
                    if not timezone_id:
                        logging.warning("Timezone update for event {} failed!".format(event.key_name))
                    else:
                        event.timezone_id = timezone_id
                else:
                    logging.warning("No Lat/Lng to update timezone_id for event {}!".format(event.key_name))
            except Exception, e:
                logging.error("Timezone update for {} errored!".format(event.key.id()))
                logging.exception(e)

            try:
                SearchHelper.update_event_location_index(event)
            except Exception, e:
                logging.error("update_event_location_index for {} errored!".format(event.key.id()))
                logging.exception(e)
    def test_2012ct(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (41.76371109999999, -72.6850932))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/New_York')
    def test_2005is(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005is.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (32.7940463, 34.989571))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'Asia/Jerusalem')
    def test_2005or(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005or.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (45.5230622, -122.6764816))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Los_Angeles')
    def test_1997il(self):
        with open('test_data/usfirst_html/usfirst_event_details_1997il.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (41.8781136, -87.6297982))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Chicago')
    def test_2013flbr(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013flbr.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (26.1224386, -80.13731740000001))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/New_York')
    def test_2001sj(self):
        with open('test_data/usfirst_html/usfirst_event_details_2001ca2.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lng(event['location']),
                         (37.3382082, -121.8863286))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Los_Angeles')
    def test_2013casj(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013casj.html',
                  'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = LocationHelper.get_lat_lng(event['location'])
        self.assertAlmostEqual(location[0], 37.3382082, places=6)
        self.assertAlmostEqual(location[1], -121.8863286, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']),
                         'America/Los_Angeles')
Exemple #13
0
    def test_2005is(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005is.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 32.7940463, places=6)
        self.assertAlmostEqual(location[1], 34.989571, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'Asia/Jerusalem')
Exemple #14
0
    def test_2005or(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005or.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 45.5230622, places=6)
        self.assertAlmostEqual(location[1], -122.6764816, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Los_Angeles')
Exemple #15
0
    def test_2012ct(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 41.76371109999999, places=6)
        self.assertAlmostEqual(location[1], -72.6850932, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
Exemple #16
0
    def test_2013flbr(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013flbr.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 26.1224386, places=6)
        self.assertAlmostEqual(location[1], -80.13731740000001, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
Exemple #17
0
    def test_1997il(self):
        with open('test_data/usfirst_html/usfirst_event_details_1997il.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        location = None
        for _ in xrange(5):
            location = LocationHelper.get_lat_lng(event['location'])
            if location:
                break
        self.assertAlmostEqual(location[0], 41.8781136, places=6)
        self.assertAlmostEqual(location[1], -87.6297982, places=6)
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Chicago')
Exemple #18
0
    def postUpdateHook(cls, events, updated_attr_list, is_new_list):
        """
        To run after models have been updated
        """
        for (event, updated_attrs) in zip(events, updated_attr_list):
            lat_lon = event.get_lat_lon()
            if not lat_lon:
                logging.warning(
                    "Lat/Lon update for event {} failed with location!".format(
                        event.key_name))
            else:
                timezone_id = LocationHelper.get_timezone_id(None,
                                                             lat_lon=lat_lon)
                if not timezone_id:
                    logging.warning(
                        "Timezone update for event {} failed!".format(
                            event.key_name))
                else:
                    event.timezone_id = timezone_id
                    cls.createOrUpdate(event, run_post_update_hook=False)

            # Add event to lat/lon info to search index
            if lat_lon:
                fields = [
                    search.NumberField(name='year', value=event.year),
                    search.GeoField(name='location',
                                    value=search.GeoPoint(
                                        lat_lon[0], lat_lon[1]))
                ]
                search.Index(name="eventLocation").put(
                    search.Document(doc_id=event.key.id(), fields=fields))

        # Enqueue task to calculate district points
        for event in events:
            taskqueue.add(url='/tasks/math/do/district_points_calc/{}'.format(
                event.key.id()),
                          method='GET')
    def postUpdateHook(cls, events, updated_attr_list, is_new_list):
        """
        To run after models have been updated
        """
        for (event, updated_attrs) in zip(events, updated_attr_list):
            try:
                LocationHelper.update_event_location(event)
            except Exception, e:
                logging.error("update_event_location for {} errored!".format(event.key.id()))
                logging.exception(e)

            try:
                if event.normalized_location and event.normalized_location.lat_lng:
                    timezone_id = LocationHelper.get_timezone_id(
                        None, lat_lng=event.normalized_location.lat_lng)
                    if not timezone_id:
                        logging.warning("Timezone update for event {} failed!".format(event.key_name))
                    else:
                        event.timezone_id = timezone_id
                else:
                    logging.warning("No Lat/Lng to update timezone_id for event {}!".format(event.key_name))
            except Exception, e:
                logging.error("Timezone update for {} errored!".format(event.key.id()))
                logging.exception(e)
    def test_2012ct(self):
        with open('test_data/usfirst_html/usfirst_event_details_2012ct.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lon(event['location'])[0], (41.76371109999999, -72.6850932))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
    def test_2013flbr(self):
        with open('test_data/usfirst_html/usfirst_event_details_2013flbr.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lon(event['location'])[0], (26.1224386, -80.13731740000001))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/New_York')
    def test_2005is(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005is.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lon(event['location'])[0], (32.7940463, 34.989571))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'Asia/Jerusalem')
    def test_2005or(self):
        with open('test_data/usfirst_html/usfirst_event_details_2005or.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lon(event['location'])[0], (45.5230622, -122.6764816))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Los_Angeles')
    def test_1997il(self):
        with open('test_data/usfirst_html/usfirst_event_details_1997il.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lon(event['location'])[0], (41.8781136, -87.6297982))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Chicago')
    def test_2002sj(self):
        with open('test_data/usfirst_html/usfirst_event_details_2002sj.html', 'r') as f:
            event, _ = UsfirstEventDetailsParser.parse(f.read())

        self.assertEqual(LocationHelper.get_lat_lon(event['location'])[0], (37.3382082, -121.8863286))
        self.assertEqual(LocationHelper.get_timezone_id(event['location']), 'America/Los_Angeles')