import shutil


def task_clean_up(*args, **kwargs):
    """
    Cleans up after tasks. By default, this involves removing a temporary directory,
    but more can be done on a per-app basis based on the `task_model` kwarg.
    Note that some parameters are kwarg-only due to Celery mechanics rather than convenience.

    Parameters
    ----------
    task_id: UUID or str
        The ID of the Django task.
    task_model: str (kwarg-only)
        The name of the `django.db.models.Model` representing the query task
        for Django (e.g. `apps.custom_mosaic_tool.models.CustomMosaicToolTask`).
    """
    # Note that these imports will not work for Celery workers when outside this function.

    task_id = str(kwargs.get('task_id', None))
    assert task_id is not None, "The `task_id` argument must be specified as a string " \
                                "keyword argument."
    task_model = kwargs['task_model']
    task = eval("{}.objects.get(pk='{}')".format(task_model, task_id))
    shutil.rmtree(task.get_temp_path())
    return True