Skip to content

Commit

Permalink
feat(api/commands): added project base command
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Oct 30, 2023
1 parent fdfb783 commit 89c10b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
43 changes: 43 additions & 0 deletions api/outdated/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from functools import reduce
from operator import or_

from django.core.management.base import BaseCommand, CommandParser
from django.db.models import Q

from outdated.outdated.models import Project


class ProjectCommand(BaseCommand):
def add_arguments(self, parser: CommandParser) -> None:
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):
if not options["all"]:
project_names = options["projects"]
q = reduce(or_, (Q(name__iexact=name) for name in project_names))
projects = Project.objects.filter(q)
if projects.count() != len(project_names):
missing_projects = list(
set(project_names).difference(
projects.values_list("name", flat=True),
),
)
missing_projects.sort()
self.stderr.write(
f"Projects with names {', '.join(missing_projects)} do not exist",
)
return
else:
projects = Project.objects.all()

for project in projects:
self._handle(project)

def _handle(self, project: Project) -> None: # pragma: no cover
raise NotImplementedError
22 changes: 6 additions & 16 deletions api/outdated/outdated/management/commands/syncproject.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
from django.core.management.base import BaseCommand

from outdated.outdated.models import Project
from outdated.commands import ProjectCommand
from outdated.outdated.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, *_, **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}")
16 changes: 0 additions & 16 deletions api/outdated/outdated/management/commands/syncprojects.py

This file was deleted.

0 comments on commit 89c10b5

Please sign in to comment.