def _contest_to_db(self, session, new_contest, contest_has_changed): """Add the new contest to the DB session (Session): session to use. new_contest (Contest): contest that has to end up in the DB. contest_has_changed (bool): whether the loader thinks new_contest has changed since the last time it was imported. return (Contest): the contest in the DB. raise (ImportDataError): if the contest already exists on the DB and the user did not ask to update any data. """ contest = session.query(Contest)\ .filter(Contest.name == new_contest.name).first() if contest is None: # Contest not present, we import it. logger.info("Creating contest on the database.") contest = new_contest # Creating a new main group contest.main_group = Group("main") contest.groups = [contest.main_group] session.add(contest) else: if not (self.update_contest or self.update_tasks): # Contest already present, but user did not ask to update any # data. We cannot import anything and this is most probably # not what the user wanted, so we let them know. raise ImportDataError( "Contest \"%s\" already exists in database. " "Use --update-contest to update it." % contest.name) if self.update_contest: # Contest already present, user asked us to update it; we do so # if it has changed. if contest_has_changed: logger.info("Contest data has changed, updating it.") # Don't update the groups groups = [] for g_old in contest.groups: g = Group(name=g_old.name) update_group(g, g_old) groups.append(g) new_contest.groups = groups update_contest(contest, new_contest) contest.main_group = [ g for g in contest.groups if g.name == contest.main_group.name ][0] else: logger.info("Contest data has not changed.") return contest
def _contest_to_db(self, session, new_contest, contest_has_changed): """Add the new contest to the DB session (Session): session to use. new_contest (Contest): contest that has to end up in the DB. contest_has_changed (bool): whether the loader thinks new_contest has changed since the last time it was imported. return (Contest): the contest in the DB. raise (ImportDataError): if the contest already exists on the DB and the user did not ask to update any data. """ contest = session.query(Contest)\ .filter(Contest.name == new_contest.name).first() if contest is None: # Contest not present, we import it. logger.info("Creating contest on the database.") contest = new_contest # Creating a new main group contest.main_group = Group("main") contest.groups = [contest.main_group] session.add(contest) else: if not (self.update_contest or self.update_tasks): # Contest already present, but user did not ask to update any # data. We cannot import anything and this is most probably # not what the user wanted, so we let them know. raise ImportDataError( "Contest \"%s\" already exists in database. " "Use --update-contest to update it." % contest.name) if self.update_contest: # Contest already present, user asked us to update it; we do so # if it has changed. if contest_has_changed: logger.info("Contest data has changed, updating it.") # Don't update the groups groups = [] for g_old in contest.groups: g = Group(name=g_old.name) update_group(g, g_old) groups.append(g) new_contest.groups = groups update_contest(contest, new_contest) contest.main_group = [ g for g in contest.groups if g.name == contest.main_group.name][0] else: logger.info("Contest data has not changed.") return contest