|
1 | | -from asyncio import run |
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
2 | 4 |
|
3 | 5 | from django.core.management.base import BaseCommand |
4 | 6 |
|
| 7 | +from outdated.outdated.models import Project |
| 8 | + |
| 9 | +if TYPE_CHECKING: # pragma: no cover |
| 10 | + from django.core.management.base import CommandParser |
| 11 | + |
5 | 12 |
|
6 | | -class AsyncCommand(BaseCommand): |
7 | | - """Base command to run async code.""" |
| 13 | +class ProjectCommand(BaseCommand): |
| 14 | + def add_arguments(self, parser: CommandParser): |
| 15 | + projects = parser.add_mutually_exclusive_group(required=True) |
| 16 | + projects.add_argument( |
| 17 | + "--all", |
| 18 | + action="store_true", |
| 19 | + help="Affect all projects", |
| 20 | + ) |
| 21 | + projects.add_argument("projects", nargs="*", type=str, default=[]) |
8 | 22 |
|
9 | 23 | def handle(self, *args, **options): |
10 | | - run(self._handle(*args, **options)) |
| 24 | + projects = [] |
| 25 | + if not options["all"]: |
| 26 | + nonexistant_projects = [] |
| 27 | + project_names = options["projects"] |
| 28 | + for name in project_names: |
| 29 | + try: |
| 30 | + projects.append(Project.objects.get(name__iexact=name)) |
| 31 | + except Project.DoesNotExist: |
| 32 | + nonexistant_projects.append(name) |
| 33 | + |
| 34 | + if nonexistant_projects: |
| 35 | + self.stderr.write( |
| 36 | + f"Projects with names {nonexistant_projects} do not exist" |
| 37 | + ) |
| 38 | + return |
| 39 | + projects = ( |
| 40 | + Project.objects.filter(id__in=[project.pk for project in projects]) |
| 41 | + or Project.objects.all() |
| 42 | + ) |
| 43 | + |
| 44 | + for project in projects: |
| 45 | + self._handle(project) |
| 46 | + |
| 47 | + def _handle(self, project: Project): # pragma: no cover |
| 48 | + raise NotImplementedError() |
0 commit comments