def get(self, year, first_eid):
        df = DatafeedUsfirst()
        df_legacy = DatafeedUsfirstLegacy()

        event = df.getEventDetails(first_eid)
        if not event:
            logging.warning("getEventDetails with DatafeedUsfirst for event id {} failed. Retrying with DatafeedUsfirstLegacy.".format(first_eid))
            event = df_legacy.getEventDetails(int(year), first_eid)

        if self.request.get('event_district_enum'):
            event.event_district_enum = int(self.request.get('event_district_enum'))
        event = EventManipulator.createOrUpdate(event)

        teams = df.getEventTeams(int(year), first_eid)
        if not teams:
            logging.warning("getEventTeams with DatafeedUsfirst for event id {} failed. Retrying with DatafeedUsfirstLegacy.".format(first_eid))
            teams = df_legacy.getEventTeams(int(year), first_eid)
            if not teams:
                logging.warning("getEventTeams with DatafeedUsfirstLegacy for event id {} failed.".format(first_eid))
                teams = []

        teams = TeamManipulator.createOrUpdate(teams)

        if teams:
            if type(teams) is not list:
                teams = [teams]

            event_teams = [EventTeam(
                id=event.key.id() + "_" + team.key.id(),
                event=event.key,
                team=team.key,
                year=event.year)
                for team in teams]

            # Delete eventteams of teams that unregister from an event
            if event.future:
                existing_event_team_keys = set(EventTeam.query(EventTeam.event == event.key).fetch(1000, keys_only=True))
                event_team_keys = set([et.key for et in event_teams])
                et_keys_to_delete = existing_event_team_keys.difference(event_team_keys)
                EventTeamManipulator.delete_keys(et_keys_to_delete)

            event_teams = EventTeamManipulator.createOrUpdate(event_teams)
            if type(event_teams) is not list:
                event_teams = [event_teams]
        else:
            event_teams = []

        template_values = {
            'event': event,
            'event_teams': event_teams,
        }

        path = os.path.join(os.path.dirname(__file__), '../templates/datafeeds/usfirst_event_details_get.html')
        self.response.out.write(template.render(path, template_values))
class TestDatafeedUsfirstLegacyEvents(unittest2.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_urlfetch_stub()
        self.testbed.init_memcache_stub()
        self.testbed.init_datastore_v3_stub()
        self.testbed.init_taskqueue_stub(root_path=".")
        self.datafeed = DatafeedUsfirstLegacy()

    def tearDown(self):
        self.testbed.deactivate()

    def test_getEvent(self):
        # test with 2011ct
        event = self.datafeed.getEventDetails(2011, "5561")

        self.assertEqual(event.key.id(), "2011ct")
        self.assertEqual(event.name, "Northeast Utilities FIRST Connecticut Regional")
        self.assertEqual(event.event_type_enum, EventType.REGIONAL)
        self.assertEqual(event.start_date, datetime.datetime(2011, 3, 31, 0, 0))
        self.assertEqual(event.end_date, datetime.datetime(2011, 4, 2, 0, 0))
        self.assertEqual(event.year, 2011)
        self.assertEqual(event.venue_address, "Connecticut Convention Center\r\n100 Columbus Blvd\r\nHartford, CT 06103\r\nUSA")
        self.assertEqual(event.website, "http://www.ctfirst.org/ctr")
        self.assertEqual(event.event_short, "ct")

    def test_getEventTeams(self):
        # test with 2011ct
        teams = self.datafeed.getEventTeams(2011, "5561")

        self.assertEqual(
            [(team.team_number, team.first_tpid) for team in teams],
            [(383, 41829), (1124, 42285), (155, 41609), (3634, 51637), (999, 42215), (1699, 42751), (173, 41625), (175, 41629), (716, 42049), (178, 41635), (2170, 43331), (3146, 44577), (2168, 43335), (2067, 43175), (181, 41641), (1991, 43133), (3125, 44539), (2785, 44073), (1740, 42765), (1784, 42895), (3654, 51609), (3718, 49891), (558, 41939), (3719, 52081), (230, 41681), (3464, 49827), (177, 41633), (2064, 43159), (195, 41651), (3104, 44463), (3555, 49069), (3141, 44487), (3461, 47483), (3525, 48801), (237, 41691), (3182, 44547), (571, 41947), (176, 41631), (1071, 42251), (2836, 43965), (126, 41585), (157, 41611), (69, 41519), (1027, 42235), (663, 42007), (3585, 50743), (1073, 42255), (501, 41899), (869, 42131), (714, 42047), (1923, 42947), (743, 42051), (20, 41475), (3204, 44731), (1601, 42659), (2791, 43935), (533, 41919), (694, 42027)]
        )

    def test_getEventList(self):
        events = self.datafeed.getEventList(2011)

        self.assertEqual(len(events), 58)  # 58 events expected

        self.assertEqual(events[0].first_eid, "5623")
        self.assertEqual(events[0].event_type_enum, EventType.REGIONAL)
        self.assertEqual(events[0].name, "Alamo Regional")

        self.assertEqual(events[1].first_eid, "5519")
        self.assertEqual(events[1].event_type_enum, EventType.REGIONAL)
        self.assertEqual(events[1].name, "BAE Systems/Granite State Regional")

        self.assertEqual(events[2].first_eid, "5523")
        self.assertEqual(events[2].event_type_enum, EventType.REGIONAL)
        self.assertEqual(events[2].name, "New Jersey Regional")
    def get(self, year, first_eid):
        df = DatafeedUsfirst()
        df_legacy = DatafeedUsfirstLegacy()

        event = df.getEventDetails(first_eid)
        if not event:
            logging.warning("getEventDetails with DatafeedUsfirst for event id {} failed. Retrying with DatafeedUsfirstLegacy.".format(first_eid))
            event = df_legacy.getEventDetails(int(year), first_eid)
        event = EventManipulator.createOrUpdate(event)

        teams = df.getEventTeams(int(year), first_eid)
        if not teams:
            logging.warning("getEventTeams with DatafeedUsfirst for event id {} failed. Retrying with DatafeedUsfirstLegacy.".format(first_eid))
            teams = df_legacy.getEventTeams(int(year), first_eid)
            if not teams:
                logging.warning("getEventTeams with DatafeedUsfirstLegacy for event id {} failed.".format(first_eid))
                teams = []

        teams = TeamManipulator.createOrUpdate(teams)

        if teams:
            if type(teams) is not list:
                teams = [teams]

            event_teams = [EventTeam(
                id=event.key.id() + "_" + team.key.id(),
                event=event.key,
                team=team.key,
                year=event.year)
                for team in teams]
            event_teams = EventTeamManipulator.createOrUpdate(event_teams)
            if type(event_teams) is not list:
                event_teams = [event_teams]
        else:
            event_teams = []

        template_values = {
            'event': event,
            'event_teams': event_teams,
        }

        path = os.path.join(os.path.dirname(__file__), '../templates/datafeeds/usfirst_event_details_get.html')
        self.response.out.write(template.render(path, template_values))
class TestDatafeedUsfirstLegacyEvents(unittest2.TestCase):
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_urlfetch_stub()
        self.testbed.init_memcache_stub()
        self.datafeed = DatafeedUsfirstLegacy()

    def tearDown(self):
        self.testbed.deactivate()

    def test_getEvent(self):
        # test with 2011ct
        event = self.datafeed.getEventDetails(2011, "5561")

        self.assertEqual(event.key.id(), "2011ct")
        self.assertEqual(event.name,
                         "Northeast Utilities FIRST Connecticut Regional")
        self.assertEqual(event.event_type_enum, EventType.REGIONAL)
        self.assertEqual(event.start_date,
                         datetime.datetime(2011, 3, 31, 0, 0))
        self.assertEqual(event.end_date, datetime.datetime(2011, 4, 2, 0, 0))
        self.assertEqual(event.year, 2011)
        self.assertEqual(
            event.venue_address,
            "Connecticut Convention Center\r\n100 Columbus Blvd\r\nHartford, CT 06103\r\nUSA"
        )
        self.assertEqual(event.website, "http://www.ctfirst.org/ctr")
        self.assertEqual(event.event_short, "ct")

    def test_getEventTeams(self):
        # test with 2011ct
        teams = self.datafeed.getEventTeams(2011, "5561")

        self.assertEqual([(team.team_number, team.first_tpid)
                          for team in teams], [(383, 41829), (1124, 42285),
                                               (155, 41609), (3634, 51637),
                                               (999, 42215), (1699, 42751),
                                               (173, 41625), (175, 41629),
                                               (716, 42049), (178, 41635),
                                               (2170, 43331), (3146, 44577),
                                               (2168, 43335), (2067, 43175),
                                               (181, 41641), (1991, 43133),
                                               (3125, 44539), (2785, 44073),
                                               (1740, 42765), (1784, 42895),
                                               (3654, 51609), (3718, 49891),
                                               (558, 41939), (3719, 52081),
                                               (230, 41681), (3464, 49827),
                                               (177, 41633), (2064, 43159),
                                               (195, 41651), (3104, 44463),
                                               (3555, 49069), (3141, 44487),
                                               (3461, 47483), (3525, 48801),
                                               (237, 41691), (3182, 44547),
                                               (571, 41947), (176, 41631),
                                               (1071, 42251), (2836, 43965),
                                               (126, 41585), (157, 41611),
                                               (69, 41519), (1027, 42235),
                                               (663, 42007), (3585, 50743),
                                               (1073, 42255), (501, 41899),
                                               (869, 42131), (714, 42047),
                                               (1923, 42947), (743, 42051),
                                               (20, 41475), (3204, 44731),
                                               (1601, 42659), (2791, 43935),
                                               (533, 41919), (694, 42027)])

    def test_getEventList(self):
        events = self.datafeed.getEventList(2011)

        self.assertEqual(len(events), 58)  # 58 events expected

        self.assertEqual(events[0].first_eid, "5519")
        self.assertEqual(events[0].event_type_enum, EventType.REGIONAL)
        self.assertEqual(events[0].name, "BAE Systems/Granite State Regional")

        self.assertEqual(events[1].first_eid, "5523")
        self.assertEqual(events[1].event_type_enum, EventType.REGIONAL)
        self.assertEqual(events[1].name, "New Jersey Regional")