# Path: harnas/contest/helpers.py
# def copy_directory(src, dest):
# def save_task(task):
# def get_task_dir(task_id):
# def save_testcase_file(file, dest_dir):


import os
import shutil
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from harnas.contest import helpers

    @property
    def display_name(self):
        if self.contest is not None and self.contest.name.strip() != "":
            return "%s (%s)" % (self.name, self.contest.name)
        else:
            return self.name


class TaskFilesDirectoryError(Exception):
    pass


@receiver(post_save,
          sender=Task,
          dispatch_uid="make_directory_for_task_files")
def make_directory_for_task_files(sender, instance, **kwargs):
    task_dir = os.path.join(settings.TASK_STORAGE_PREFIX, str(instance.pk))
    if os.path.exists(task_dir):
        return
    if instance.parent:
        fetch_task_files(task_dir, instance.parent.pk)
    else:
        make_task_dir(task_dir)


def fetch_task_files(task_dir: str, source_task_id: int) -> None:
    parent_dir = os.path.join(settings.TASK_STORAGE_PREFIX,
                              str(source_task_id))
    try:

# Path: harnas/contest/helpers.py
# def copy_directory(src, dest):
# def save_task(task):
# def get_task_dir(task_id):
# def save_testcase_file(file, dest_dir):


# Path: harnas/contest/models.py
import os
import shutil
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from harnas.contest import helpers


    @property
    def display_name(self):
        if self.contest is not None and self.contest.name.strip() != "":
            return "%s (%s)" % (self.name, self.contest.name)
        else:
            return self.name


class TaskFilesDirectoryError(Exception):
    pass


@receiver(post_save,
          sender=Task,
          dispatch_uid="make_directory_for_task_files")
def make_directory_for_task_files(sender, instance, **kwargs):
    task_dir = os.path.join(settings.TASK_STORAGE_PREFIX, str(instance.pk))
    if os.path.exists(task_dir):
        return
    if instance.parent:
        fetch_task_files(task_dir, instance.parent.pk)
    else:
        make_task_dir(task_dir)


def fetch_task_files(task_dir: str, source_task_id: int) -> None:
    parent_dir = os.path.join(settings.TASK_STORAGE_PREFIX,
                              str(source_task_id))
    try:

        helpers.copy_directory(parent_dir, task_dir)