def METHOD_NAME(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