def post(self):
        self._require_admin()
        event_key = self.request.get('event_key')
        matches_csv = self.request.get('matches_csv')
        matches, _ = OffseasonMatchesParser.parse(matches_csv)

        event = Event.get_by_id(event_key)
        matches = [Match(
            id=Match.renderKeyName(
                event.key.id(),
                match.get("comp_level", None),
                match.get("set_number", 0),
                match.get("match_number", 0)),
            event=event.key,
            year=event.year,
            set_number=match.get("set_number", 0),
            match_number=match.get("match_number", 0),
            comp_level=match.get("comp_level", None),
            team_key_names=match.get("team_key_names", None),
            alliances_json=match.get("alliances_json", None)
            )
            for match in matches]
        MatchManipulator.createOrUpdate(matches)

        self.redirect('/admin/event/{}'.format(event_key))
    def post(self):
        self._require_admin()
        event_key = self.request.get('event_key')
        matches_csv = self.request.get('matches_csv')
        matches = OffseasonMatchesParser.parse(matches_csv)

        event = Event.get_by_id(event_key)
        matches = [
            Match(id=Match.renderKeyName(event.key.id(),
                                         match.get("comp_level", None),
                                         match.get("set_number", 0),
                                         match.get("match_number", 0)),
                  event=event.key,
                  game=Match.FRC_GAMES_BY_YEAR.get(event.year, "frc_unknown"),
                  set_number=match.get("set_number", 0),
                  match_number=match.get("match_number", 0),
                  comp_level=match.get("comp_level", None),
                  team_key_names=match.get("team_key_names", None),
                  alliances_json=match.get("alliances_json", None))
            for match in matches
        ]

        try:
            FirebasePusher.updated_event(event.key_name)
        except:
            logging.warning("Enqueuing Firebase push failed!")

        self.redirect('/admin/event/{}'.format(event_key))
    def post(self):
        self._require_admin()
        event_key = self.request.get('event_key')
        matches_csv = self.request.get('matches_csv')
        matches = OffseasonMatchesParser.parse(matches_csv)

        event = Event.get_by_id(event_key)
        matches = [Match(
            id=Match.renderKeyName(
                event.key.id(),
                match.get("comp_level", None),
                match.get("set_number", 0),
                match.get("match_number", 0)),
            event=event.key,
            game=Match.FRC_GAMES_BY_YEAR.get(event.year, "frc_unknown"),
            set_number=match.get("set_number", 0),
            match_number=match.get("match_number", 0),
            comp_level=match.get("comp_level", None),
            team_key_names=match.get("team_key_names", None),
            alliances_json=match.get("alliances_json", None)
            )
            for match in matches]

        try:
            FirebasePusher.updated_event(event.key_name)
        except:
            logging.warning("Enqueuing Firebase push failed!")

        self.redirect('/admin/event/{}'.format(event_key))
def setupMatches(csv):
    with open(csv, 'r') as f:
        event = Event(
          id = "2013test",
          event_short = "test",
          year = 2013
        )
        
        parsed_matches = OffseasonMatchesParser.parse(f.read())
        matches = [Match(
            id = Match.renderKeyName(
                event, 
                match.get("comp_level", None), 
                match.get("set_number", 0), 
                match.get("match_number", 0)),
            event = event.key,
            game = Match.FRC_GAMES_BY_YEAR.get(event.year, "frc_unknown"),
            set_number = match.get("set_number", 0),
            match_number = match.get("match_number", 0),
            comp_level = match.get("comp_level", None),
            team_key_names = match.get("team_key_names", None),
            alliances_json = match.get("alliances_json", None)
            )
            for match in parsed_matches]
        return matches
Esempio n. 5
0
    def post(self):
        self._require_admin()
        event_key = self.request.get('event_key')
        matches_csv = self.request.get('matches_csv')
        matches, _ = OffseasonMatchesParser.parse(matches_csv)

        event = Event.get_by_id(event_key)
        matches = [Match(
            id=Match.renderKeyName(
                event.key.id(),
                match.get("comp_level", None),
                match.get("set_number", 0),
                match.get("match_number", 0)),
            event=event.key,
            year=event.year,
            set_number=match.get("set_number", 0),
            match_number=match.get("match_number", 0),
            comp_level=match.get("comp_level", None),
            team_key_names=match.get("team_key_names", None),
            alliances_json=match.get("alliances_json", None)
            )
            for match in matches]
        MatchManipulator.createOrUpdate(matches)

        self.redirect('/admin/event/{}'.format(event_key))
    def post(self):
        self._require_admin()
        event_key = self.request.get('event_key')
        matches_csv = self.request.get('matches_csv')
        matches = OffseasonMatchesParser.parse(matches_csv)

        event = Event.get_by_id(event_key)
        matches = [Match(
            id=Match.renderKeyName(
                event.key.id(),
                match.get("comp_level", None),
                match.get("set_number", 0),
                match.get("match_number", 0)),
            event=event.key,
            game=Match.FRC_GAMES_BY_YEAR.get(event.year, "frc_unknown"),
            set_number=match.get("set_number", 0),
            match_number=match.get("match_number", 0),
            comp_level=match.get("comp_level", None),
            team_key_names=match.get("team_key_names", None),
            alliances_json=match.get("alliances_json", None)
            )
            for match in matches]

        new_matches = MatchManipulator.createOrUpdate(matches)
        try:
            last_matches = MatchHelper.recentMatches(new_matches, 1)
            upcoming_matches = MatchHelper.upcomingMatches(new_matches, 8)
        except:
            logging.warning("Computing last/upcoming matches for Firebase failed!")
        try:
            FirebasePusher.updateEvent(event, last_matches, upcoming_matches)
        except:
            logging.warning("Enqueuing Firebase push failed!")

        self.redirect('/admin/event/{}'.format(event_key))
    def test_parse(self):
        with open('test_data/offseason_matches.csv', 'r') as f:
            matches = OffseasonMatchesParser.parse(f.read())

        match = matches[0]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 1)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[1]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 22)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": -1, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": -1, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[2]
        self.assertEqual(match["comp_level"], "qf")
        self.assertEqual(match["set_number"], 2)
        self.assertEqual(match["match_number"], 1)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")


        match = matches[3]
        self.assertEqual(match["comp_level"], "sf")
        self.assertEqual(match["set_number"], 2)
        self.assertEqual(match["match_number"], 3)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[4]
        self.assertEqual(match["comp_level"], "f")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 2)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[5]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 99)
        self.assertEqual(match["team_key_names"], ['frc3', 'frc4', 'frc5'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6A"]}, "red": {"score": 7, "teams": ["frc1A", "frc2B", "frc3"]}}""")

        match = matches[6]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 12)
        self.assertEqual(match["team_key_names"], ['frc3', 'frc1', 'frc2', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc1", "frc2", "frc6"]}, "red": {"score": 7, "teams": ["frc1B", "frc2B", "frc3"]}}""")
    def test_parse(self):
        with open('test_data/offseason_matches.csv', 'r') as f:
            matches, _ = OffseasonMatchesParser.parse(f.read())

        match = matches[0]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 1)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[1]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 22)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": -1, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": -1, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[2]
        self.assertEqual(match["comp_level"], "qf")
        self.assertEqual(match["set_number"], 2)
        self.assertEqual(match["match_number"], 1)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[3]
        self.assertEqual(match["comp_level"], "sf")
        self.assertEqual(match["set_number"], 2)
        self.assertEqual(match["match_number"], 3)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[4]
        self.assertEqual(match["comp_level"], "f")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 2)
        self.assertEqual(match["team_key_names"], ['frc1', 'frc2', 'frc3', 'frc4', 'frc5', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6"]}, "red": {"score": 7, "teams": ["frc1", "frc2", "frc3"]}}""")

        match = matches[5]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 99)
        self.assertEqual(match["team_key_names"], ['frc3', 'frc4', 'frc5'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc4", "frc5", "frc6A"]}, "red": {"score": 7, "teams": ["frc1A", "frc2B", "frc3"]}}""")

        match = matches[6]
        self.assertEqual(match["comp_level"], "qm")
        self.assertEqual(match["set_number"], 1)
        self.assertEqual(match["match_number"], 12)
        self.assertEqual(match["team_key_names"], ['frc3', 'frc1', 'frc2', 'frc6'])
        self.assertEqual(match["alliances_json"], """{"blue": {"score": 8, "teams": ["frc1", "frc2", "frc6"]}, "red": {"score": 7, "teams": ["frc1B", "frc2B", "frc3"]}}""")
 def setupMatches(self, csv):
     with open(csv, 'r') as f:
         parsed_matches, _ = OffseasonMatchesParser.parse(f.read())
         matches = [Match(id=Match.renderKeyName(self.event.key.id(),
                                                 match.get("comp_level", None),
                                                 match.get("set_number", 0),
                                                 match.get("match_number", 0)),
                          event=self.event.key,
                          year=self.event.year,
                          set_number=match.get("set_number", 0),
                          match_number=match.get("match_number", 0),
                          comp_level=match.get("comp_level", None),
                          team_key_names=match.get("team_key_names", None),
                          alliances_json=match.get("alliances_json", None))
                    for match in parsed_matches]
         return matches
 def setupMatches(self, csv):
     with open(csv, "r") as f:
         parsed_matches = OffseasonMatchesParser.parse(f.read())
         matches = [
             Match(
                 id=Match.renderKeyName(
                     self.event.key.id(),
                     match.get("comp_level", None),
                     match.get("set_number", 0),
                     match.get("match_number", 0),
                 ),
                 event=self.event.key,
                 game=Match.FRC_GAMES_BY_YEAR.get(self.event.year, "frc_unknown"),
                 set_number=match.get("set_number", 0),
                 match_number=match.get("match_number", 0),
                 comp_level=match.get("comp_level", None),
                 team_key_names=match.get("team_key_names", None),
                 alliances_json=match.get("alliances_json", None),
             )
             for match in parsed_matches
         ]
         return matches
Esempio n. 11
0
    def get(self, event_key):
        if tba_config.CONFIG["env"] == "prod":  # disable in prod for now
            logging.error("Tried to restore {} from CSV in prod! No can do.".format(event_key))
            return

        event = Event.get_by_id(event_key)

        # alliances
        result = urlfetch.fetch(self.ALLIANCES_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.ALLIANCES_URL.format(event.year, event_key, event_key)))
        else:
            data = result.content.replace('frc', '')
            alliance_selections = CSVAllianceSelectionsParser.parse(data)

            event_details = EventDetails(
                id=event_key,
                alliance_selections=alliance_selections
            )
            EventDetailsManipulator.createOrUpdate(event_details)

        # awards
        result = urlfetch.fetch(self.AWARDS_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.AWARDS_URL.format(event.year, event_key, event_key)))
        else:
            # convert into expected input format
            data = StringIO.StringIO()
            writer = csv.writer(data, delimiter=',')
            for row in csv.reader(StringIO.StringIO(result.content), delimiter=','):
                writer.writerow([event.year, event.event_short, row[1], row[2].replace('frc', ''), row[3]])

            awards = []
            for award in CSVAwardsParser.parse(data.getvalue()):
                awards.append(Award(
                    id=Award.render_key_name(event.key_name, award['award_type_enum']),
                    name_str=award['name_str'],
                    award_type_enum=award['award_type_enum'],
                    year=event.year,
                    event=event.key,
                    event_type_enum=event.event_type_enum,
                    team_list=[ndb.Key(Team, 'frc{}'.format(team_number)) for team_number in award['team_number_list']],
                    recipient_json_list=award['recipient_json_list']
                ))
            AwardManipulator.createOrUpdate(awards)

        # matches
        result = urlfetch.fetch(self.MATCHES_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.MATCHES_URL.format(event.year, event_key, event_key)))
        else:
            data = result.content.replace('frc', '').replace('{}_'.format(event_key), '')
            match_dicts, _ = OffseasonMatchesParser.parse(data)
            matches = [
                Match(
                    id=Match.renderKeyName(
                        event.key.id(),
                        match.get("comp_level", None),
                        match.get("set_number", 0),
                        match.get("match_number", 0)),
                    event=event.key,
                    year=event.year,
                    set_number=match.get("set_number", 0),
                    match_number=match.get("match_number", 0),
                    comp_level=match.get("comp_level", None),
                    team_key_names=match.get("team_key_names", None),
                    alliances_json=match.get("alliances_json", None)
                )
            for match in match_dicts]
            MatchManipulator.createOrUpdate(matches)

        # rankings
        result = urlfetch.fetch(self.RANKINGS_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.RANKINGS_URL.format(event.year, event_key, event_key)))
        else:
            # convert into expected input format
            rankings = list(csv.reader(StringIO.StringIO(result.content), delimiter=','))

            event_details = EventDetails(
                id=event_key,
                rankings=rankings
            )
            EventDetailsManipulator.createOrUpdate(event_details)

        self.response.out.write("Done restoring {}!".format(event_key))
    def get(self, event_key):
        if tba_config.CONFIG["env"] == "prod":  # disable in prod for now
            logging.error("Tried to restore {} from CSV in prod! No can do.".format(event_key))
            return

        event = Event.get_by_id(event_key)

        # alliances
        result = urlfetch.fetch(self.ALLIANCES_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.ALLIANCES_URL.format(event.year, event_key, event_key)))
        else:
            data = result.content.replace('frc', '')
            alliance_selections = CSVAllianceSelectionsParser.parse(data)

            event_details = EventDetails(
                id=event_key,
                alliance_selections=alliance_selections
            )
            EventDetailsManipulator.createOrUpdate(event_details)

        # awards
        result = urlfetch.fetch(self.AWARDS_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.AWARDS_URL.format(event.year, event_key, event_key)))
        else:
            # convert into expected input format
            data = StringIO.StringIO()
            writer = csv.writer(data, delimiter=',')
            for row in csv.reader(StringIO.StringIO(result.content), delimiter=','):
                writer.writerow([event.year, event.event_short, row[1], row[2].replace('frc', ''), row[3]])

            awards = []
            for award in CSVAwardsParser.parse(data.getvalue()):
                awards.append(Award(
                    id=Award.render_key_name(event.key_name, award['award_type_enum']),
                    name_str=award['name_str'],
                    award_type_enum=award['award_type_enum'],
                    year=event.year,
                    event=event.key,
                    event_type_enum=event.event_type_enum,
                    team_list=[ndb.Key(Team, 'frc{}'.format(team_number)) for team_number in award['team_number_list']],
                    recipient_json_list=award['recipient_json_list']
                ))
            AwardManipulator.createOrUpdate(awards)

        # matches
        result = urlfetch.fetch(self.MATCHES_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.MATCHES_URL.format(event.year, event_key, event_key)))
        else:
            data = result.content.replace('frc', '').replace('{}_'.format(event_key), '')
            match_dicts, _ = OffseasonMatchesParser.parse(data)
            matches = [
                Match(
                    id=Match.renderKeyName(
                        event.key.id(),
                        match.get("comp_level", None),
                        match.get("set_number", 0),
                        match.get("match_number", 0)),
                    event=event.key,
                    year=event.year,
                    set_number=match.get("set_number", 0),
                    match_number=match.get("match_number", 0),
                    comp_level=match.get("comp_level", None),
                    team_key_names=match.get("team_key_names", None),
                    alliances_json=match.get("alliances_json", None)
                )
            for match in match_dicts]
            MatchManipulator.createOrUpdate(matches)

        # rankings
        result = urlfetch.fetch(self.RANKINGS_URL.format(event.year, event_key, event_key))
        if result.status_code != 200:
            logging.warning('Unable to retreive url: ' + (self.RANKINGS_URL.format(event.year, event_key, event_key)))
        else:
            # convert into expected input format
            rankings = list(csv.reader(StringIO.StringIO(result.content), delimiter=','))

            event_details = EventDetails(
                id=event_key,
                rankings=rankings
            )
            EventDetailsManipulator.createOrUpdate(event_details)

        self.response.out.write("Done restoring {}!".format(event_key))