2023-10-30 15:24:26 -05:00
|
|
|
from pathlib import Path
|
2015-10-07 15:26:19 -05:00
|
|
|
|
|
|
|
from django.core.files import File
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2018-05-18 14:40:38 -05:00
|
|
|
from umap.models import Pictogram
|
2015-10-07 15:26:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2023-10-30 15:24:26 -05:00
|
|
|
help = "Import pictograms from a folder"
|
2015-10-07 15:26:19 -05:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2023-10-30 15:24:26 -05:00
|
|
|
parser.add_argument("path")
|
|
|
|
parser.add_argument(
|
|
|
|
"--attribution",
|
|
|
|
required=True,
|
|
|
|
help="Attribution of the imported pictograms",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--extensions",
|
|
|
|
help="Optional list of extensins to process",
|
|
|
|
nargs="+",
|
|
|
|
default=[".svg"],
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--exclude",
|
|
|
|
help="Optional list of files or dirs to exclude",
|
|
|
|
nargs="+",
|
|
|
|
default=["font"],
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--force", action="store_true", help="Update picto if it already exists."
|
|
|
|
)
|
2015-10-07 15:26:19 -05:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2023-10-30 15:24:26 -05:00
|
|
|
self.path = Path(options["path"])
|
|
|
|
self.attribution = options["attribution"]
|
|
|
|
self.extensions = options["extensions"]
|
|
|
|
self.force = options["force"]
|
|
|
|
self.exclude = options["exclude"]
|
|
|
|
self.handle_directory(self.path)
|
|
|
|
|
|
|
|
def handle_directory(self, path):
|
|
|
|
for filename in path.iterdir():
|
|
|
|
if filename.name in self.exclude:
|
|
|
|
continue
|
|
|
|
if filename.is_dir():
|
|
|
|
self.handle_directory(filename)
|
|
|
|
continue
|
|
|
|
if filename.suffix in self.extensions:
|
|
|
|
name = filename.stem
|
2015-10-07 15:26:19 -05:00
|
|
|
picto = Pictogram.objects.filter(name=name).last()
|
|
|
|
if picto:
|
2023-10-30 15:24:26 -05:00
|
|
|
if not self.force:
|
|
|
|
self.stdout.write(
|
|
|
|
f"⚠ Pictogram with name '{name}' already exists. Skipping."
|
|
|
|
)
|
2015-10-07 15:26:19 -05:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
picto = Pictogram()
|
|
|
|
picto.name = name
|
2023-11-23 11:04:23 -06:00
|
|
|
if path.name != self.path.name: # Subfolders only
|
2023-10-30 15:24:26 -05:00
|
|
|
picto.category = path.name
|
|
|
|
picto.attribution = self.attribution
|
2024-02-25 03:50:13 -06:00
|
|
|
with (filename).open("rb") as f:
|
2023-10-30 15:24:26 -05:00
|
|
|
picto.pictogram.save(filename.name, File(f), save=True)
|
|
|
|
self.stdout.write(f"✔ Imported pictogram {filename}.")
|