-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api/commands): added project base command
- Loading branch information
Showing
3 changed files
with
49 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file was deleted.
Oops, something went wrong.