예제 #1
0
    def _task_to_db(self, session, contest, new_task, task_has_changed):
        """Add the task to the DB

        Return the task, or raise in case of one of these errors:
        - if the task is not in the DB and user did not ask to update it;
        - if the task is already in the DB and attached to another contest.

        """
        task = session.query(Task).filter(Task.name == new_task.name).first()
        if task is None:
            if contest is not None:
                logger.info("Attaching task to contest (id %s.)",
                            self.contest_id)
                new_task.num = len(contest.tasks)
                new_task.contest = contest
            session.add(new_task)
            return new_task

        if not self.update:
            raise ImportDataError("Task \"%s\" already exists in database. "
                                  "Use --update to update it." % new_task.name)

        if contest is not None and task.contest_id != contest.id:
            raise ImportDataError(
                "Task \"%s\" already tied to another contest." % task.name)

        if task_has_changed:
            logger.info("Task \"%s\" data has changed, updating it.",
                        task.name)
            update_task(task, new_task, get_statements=not self.no_statement)
        else:
            logger.info("Task \"%s\" data has not changed.", task.name)

        return task
예제 #2
0
파일: ImportTask.py 프로젝트: cms-dev/cms
    def _task_to_db(self, session, contest, new_task, task_has_changed):
        """Add the task to the DB

        Return the task, or raise in case of one of these errors:
        - if the task is not in the DB and user did not ask to update it;
        - if the task is already in the DB and attached to another contest.

        """
        task = session.query(Task).filter(Task.name == new_task.name).first()
        if task is None:
            if contest is not None:
                logger.info("Attaching task to contest (id %s.)",
                            self.contest_id)
                new_task.num = len(contest.tasks)
                new_task.contest = contest
            session.add(new_task)
            return new_task

        if not self.update:
            raise ImportDataError(
                "Task \"%s\" already exists in database. "
                "Use --update to update it." % new_task.name)

        if contest is not None and task.contest_id != contest.id:
            raise ImportDataError(
                "Task \"%s\" already tied to another contest." % task.name)

        if task_has_changed:
            logger.info(
                "Task \"%s\" data has changed, updating it.", task.name)
            update_task(task, new_task, get_statements=not self.no_statement)
        else:
            logger.info("Task \"%s\" data has not changed.", task.name)

        return task
예제 #3
0
    def _task_to_db(self, session, contest, tasknum, taskname):
        """Add the task to the DB and attach it to the contest

        session (Session): session to use.
        contest (Contest): the contest in the DB.
        tasknum (int): num the task should have in the contest.
        taskname (string): name of the task.

        return (Task): the task in the DB.
        raise (ImportDataError): in case of one of these errors:
            - if the task is not in the DB and user did not ask to import it;
            - if the loader cannot load the task;
            - if the task is already in the DB, attached to another contest.

        """
        task_loader = self.loader.get_task_loader(taskname)
        task = session.query(Task).filter(Task.name == taskname).first()

        if task is None:
            # Task is not in the DB; if the user asked us to import it, we do
            # so, otherwise we return an error.

            if not self.import_tasks:
                raise ImportDataError("Task \"%s\" not found in database. "
                                      "Use --import-task to import it." %
                                      taskname)

            task = task_loader.get_task(get_statement=not self.no_statements)
            if task is None:
                raise ImportDataError("Could not import task \"%s\"." %
                                      taskname)

            session.add(task)

        elif not task_loader.task_has_changed():
            # Task is in the DB and has not changed, nothing to do.
            logger.info("Task \"%s\" data has not changed.", taskname)

        elif self.update_tasks:
            # Task is in the DB, but has changed, and the user asked us to
            # update it. We do so.
            new_task = task_loader.get_task(
                get_statement=not self.no_statements)
            if new_task is None:
                raise ImportDataError("Could not reimport task \"%s\"." %
                                      taskname)
            logger.info("Task \"%s\" data has changed, updating it.", taskname)
            update_task(task, new_task, get_statements=not self.no_statements)

        else:
            # Task is in the DB, has changed, and the user didn't ask to update
            # it; we just show a warning.
            logger.warning(
                "Not updating task \"%s\", even if it has changed. "
                "Use --update-tasks to update it.", taskname)

        # Finally we tie the task to the contest, if it is not already used
        # elsewhere.
        if task.contest is not None and task.contest.name != contest.name:
            raise ImportDataError(
                "Task \"%s\" is already tied to contest \"%s\"." %
                (taskname, task.contest.name))

        task.num = tasknum
        task.contest = contest
        return task
예제 #4
0
    def _task_to_db(self, session, contest, tasknum, taskname):
        """Add the task to the DB and attach it to the contest

        session (Session): session to use.
        contest (Contest): the contest in the DB.
        tasknum (int): num the task should have in the contest.
        taskname (string): name of the task.

        return (Task): the task in the DB.

        raise (ImportDataError): in case of one of these errors:
            - if the task is not in the DB and user did not ask to import it;
            - if the loader cannot load the task;
            - if the task is already in the DB, attached to another contest.

        """
        task_loader = self.loader.get_task_loader(taskname)
        task = session.query(Task).filter(Task.name == taskname).first()

        if task is None:
            # Task is not in the DB; if the user asked us to import it, we do
            # so, otherwise we return an error.

            if not self.import_tasks:
                raise ImportDataError(
                    "Task \"%s\" not found in database. "
                    "Use --import-task to import it." % taskname)

            task = task_loader.get_task(get_statement=not self.no_statements)
            if task is None:
                raise ImportDataError(
                    "Could not import task \"%s\"." % taskname)

            session.add(task)

        elif not task_loader.task_has_changed():
            # Task is in the DB and has not changed, nothing to do.
            logger.info("Task \"%s\" data has not changed.", taskname)

        elif self.update_tasks:
            # Task is in the DB, but has changed, and the user asked us to
            # update it. We do so.
            new_task = task_loader.get_task(
                get_statement=not self.no_statements)
            if new_task is None:
                raise ImportDataError(
                    "Could not reimport task \"%s\"." % taskname)
            logger.info("Task \"%s\" data has changed, updating it.", taskname)
            update_task(task, new_task, get_statements=not self.no_statements)

        else:
            # Task is in the DB, has changed, and the user didn't ask to update
            # it; we just show a warning.
            logger.warning("Not updating task \"%s\", even if it has changed. "
                           "Use --update-tasks to update it.", taskname)

        # Finally we tie the task to the contest, if it is not already used
        # elsewhere.
        if task.contest is not None and task.contest.name != contest.name:
            raise ImportDataError(
                "Task \"%s\" is already tied to contest \"%s\"."
                % (taskname, task.contest.name))

        task.num = tasknum
        task.contest = contest
        return task