From 1683fb26cea0814d38da67bd6921a2870bb1105e Mon Sep 17 00:00:00 2001 From: Arthur Deierlein Date: Mon, 28 Aug 2023 14:27:42 +0200 Subject: [PATCH] feat(api/commands)!: added project base command --- api/outdated/commands.py | 41 ++++++++++++++++--- .../management/commands/syncproject.py | 24 ++++------- .../management/commands/syncprojects.py | 16 -------- 3 files changed, 43 insertions(+), 38 deletions(-) delete mode 100644 api/outdated/outdated/management/commands/syncprojects.py diff --git a/api/outdated/commands.py b/api/outdated/commands.py index 6c1efbb7..61d3de12 100644 --- a/api/outdated/commands.py +++ b/api/outdated/commands.py @@ -1,10 +1,41 @@ -from asyncio import run +from django.core.management.base import BaseCommand, CommandParser -from django.core.management.base import BaseCommand +from outdated.outdated.models import Project -class AsyncCommand(BaseCommand): - """Base command to run async code.""" +class ProjectCommand(BaseCommand): + def add_arguments(self, parser: CommandParser): + projects = parser.add_mutually_exclusive_group(required=True) + projects.add_argument( + "--all", + action="store_true", + help="Affect all projects", + ) + projects.add_argument("projects", nargs="*", type=str, default=[]) def handle(self, *args, **options): - run(self._handle(*args, **options)) + projects = [] + if not options["all"]: + nonexistant_projects = [] + project_names = options["projects"] + for name in project_names: + try: + projects.append(Project.objects.get(name__iexact=name)) + except Project.DoesNotExist: + nonexistant_projects.append(name) + + if nonexistant_projects: + self.stderr.write( + f"Projects with names {nonexistant_projects} do not exist" + ) + return + projects = ( + Project.objects.filter(id__in=[project.pk for project in projects]) + or Project.objects.all() + ) + + for project in projects: + self._handle(project) + + def _handle(self, project: Project): # pragma: no cover + raise NotImplementedError() diff --git a/api/outdated/outdated/management/commands/syncproject.py b/api/outdated/outdated/management/commands/syncproject.py index 486f2396..ad83a38d 100644 --- a/api/outdated/outdated/management/commands/syncproject.py +++ b/api/outdated/outdated/management/commands/syncproject.py @@ -1,21 +1,11 @@ -from django.core.management.base import BaseCommand +from outdated.commands import ProjectCommand +from outdated.outdated.synchroniser import Synchroniser -from ...models import Project -from ...synchroniser import Synchroniser - -class Command(BaseCommand): +class Command(ProjectCommand): help = "Syncs the given project with its remote counterpart." - def add_arguments(self, parser): - parser.add_argument("project_name", type=str) - - def handle(self, *args, **options): - project_name = options["project_name"] - try: - project = Project.objects.get(name__iexact=project_name) - self.stdout.write(f"Syncing project {project}") - Synchroniser(project).sync() - self.stdout.write(f"Finished syncing {project}") - except Project.DoesNotExist: - self.stdout.write(f"Project {project_name} not found") + def _handle(self, project): + self.stdout.write(f"Syncing project {project}") + Synchroniser(project).sync() + self.stdout.write(f"Finished syncing {project}") diff --git a/api/outdated/outdated/management/commands/syncprojects.py b/api/outdated/outdated/management/commands/syncprojects.py deleted file mode 100644 index 35ecc080..00000000 --- a/api/outdated/outdated/management/commands/syncprojects.py +++ /dev/null @@ -1,16 +0,0 @@ -from asyncio import gather - -from outdated.commands import AsyncCommand - -from ...models import Project -from ...synchroniser import Synchroniser - - -class Command(AsyncCommand): - help = "Syncs all projects with their remote counterparts." - - async def _handle(self, *args, **options): - projects = Project.objects.all() - project_tasks = [Synchroniser(project).a_sync() async for project in projects] - await gather(*project_tasks) - self.stdout.write("Finished syncing all projects")