Commit 6d344df4 authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

add option to addlocalfiles

parent b3e8e10d
Loading
Loading
Loading
Loading
+18 −4
Original line number Diff line number Diff line
import sys

from django.core.management.base import BaseCommand, CommandError
import os
from django.contrib.auth.models import User
@@ -8,6 +10,7 @@ from resources.helpers import handle_local_file
from resources.models import Project, FileExtension
from concurrent.futures import ThreadPoolExecutor
import multiprocessing
from pathlib import Path


@lru_cache(maxsize=None)
@@ -31,8 +34,13 @@ def scan_dir(start_path: str, extension: str = None) -> Iterator[str]:
                yield os.path.join(root, file)


def _add_file(file_path: str, project: Project, current_user: User, start_dir: str):

def _add_file(
    file_path: str,
    project: Project,
    current_user: User,
    start_dir: str,
    delete_source: bool = False,
):
    resource_id = handle_local_file(file_path, project)
    collection_path = os.path.dirname(file_path)[len(start_dir) :]
    if collection_path:
@@ -40,7 +48,9 @@ def _add_file(file_path: str, project: Project, current_user: User, start_dir: s
            current_user, collection_path, project.pk
        )
        collection_id = hierarchy_of_collections[-1]["id"]
        add_resource_to_collection(current_user, resource_id, collection_id)
        if add_resource_to_collection(current_user, resource_id, collection_id):
            if delete_source:
                Path(file_path).unlink(missing_ok=True)


class Command(BaseCommand):
@@ -49,12 +59,14 @@ class Command(BaseCommand):
        parser.add_argument("user", type=str)
        parser.add_argument("project", type=int)
        parser.add_argument("--extensions", nargs="+", type=str)
        parser.add_argument("--delete", action="store_true")

    def handle(self, *args, **options):
        if options.get("extensions"):
            allowed_extensions = options.get("extensions")
        else:
            allowed_extensions = ALLOWED_EXTENSIONS
        delete_source = options.get("delete", False)
        start_dir = options.get("start_dir")
        if not os.path.isdir(start_dir):
            raise CommandError("{} is not a directory".format(start_dir))
@@ -75,4 +87,6 @@ class Command(BaseCommand):
            _, extension = os.path.splitext(file_path)
            if extension.lower() not in allowed_extensions:
                continue
            executor.submit(_add_file, file_path, project, current_user, start_dir)
            executor.submit(
                _add_file, file_path, project, current_user, start_dir, delete_source
            )