|
| 1 | +from dataclasses import dataclass |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from django.core.management.base import CommandError |
| 5 | + |
| 6 | +from benefits.cli.commands import BaseOptions, BenefitsCommand |
| 7 | +from benefits.core.models import TransitAgency, TransitProcessor |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class Options(BaseOptions): |
| 12 | + active: bool = False |
| 13 | + info_url: str = None |
| 14 | + long_name: str = None |
| 15 | + phone: str = None |
| 16 | + short_name: str = None |
| 17 | + slug: str = None |
| 18 | + templates: bool = False |
| 19 | + templates_only: bool = False |
| 20 | + transit_processor: int = None |
| 21 | + |
| 22 | + def __post_init__(self): |
| 23 | + if not self.short_name: |
| 24 | + self.short_name = self.slug.upper() |
| 25 | + if not self.long_name: |
| 26 | + self.long_name = self.slug.upper() |
| 27 | + |
| 28 | + |
| 29 | +class Create(BenefitsCommand): |
| 30 | + """Create a new transit agency.""" |
| 31 | + |
| 32 | + help = __doc__ |
| 33 | + name = "create" |
| 34 | + options_cls = Options |
| 35 | + sample_slug = "cst" |
| 36 | + templates = [ |
| 37 | + f"core/index--{sample_slug}.html", |
| 38 | + f"eligibility/index--{sample_slug}.html", |
| 39 | + ] |
| 40 | + |
| 41 | + @property |
| 42 | + def template_paths(self): |
| 43 | + return [self.template_path(t) for t in self.templates] |
| 44 | + |
| 45 | + def _create_agency(self, opts: Options) -> TransitAgency: |
| 46 | + if isinstance(opts.transit_processor, int): |
| 47 | + transit_processor = TransitProcessor.objects.get(id=opts.transit_processor) |
| 48 | + else: |
| 49 | + transit_processor = None |
| 50 | + |
| 51 | + agency = TransitAgency.objects.create( |
| 52 | + active=opts.active, |
| 53 | + slug=opts.slug, |
| 54 | + info_url=opts.info_url, |
| 55 | + long_name=opts.long_name, |
| 56 | + phone=opts.phone, |
| 57 | + short_name=opts.short_name, |
| 58 | + transit_processor=transit_processor, |
| 59 | + ) |
| 60 | + agency.save() |
| 61 | + |
| 62 | + return agency |
| 63 | + |
| 64 | + def _create_templates(self, agency: TransitAgency): |
| 65 | + for template in self.template_paths: |
| 66 | + content = template.read_text().replace(self.sample_slug, agency.slug) |
| 67 | + content = content.replace(self.sample_slug.upper(), agency.slug.upper()) |
| 68 | + |
| 69 | + path = str(template.resolve()).replace(self.sample_slug, agency.slug) |
| 70 | + |
| 71 | + new_template = Path(path) |
| 72 | + new_template.write_text(content) |
| 73 | + |
| 74 | + def _raise_for_slug(self, opts: Options) -> bool: |
| 75 | + if TransitAgency.by_slug(opts.slug): |
| 76 | + raise CommandError(f"TransitAgency with slug already exists: {opts.slug}") |
| 77 | + |
| 78 | + def add_arguments(self, parser): |
| 79 | + parser.add_argument("-a", "--active", action="store_true", default=False, help="Activate the agency") |
| 80 | + parser.add_argument( |
| 81 | + "-i", "--info-url", type=str, default="https://agency.com", help="The agency's informational website URL" |
| 82 | + ) |
| 83 | + parser.add_argument("-l", "--long-name", type=str, default="Regional Transit Agency", help="The agency's long name") |
| 84 | + parser.add_argument("-p", "--phone", type=str, default="800-555-5555", help="The agency's phone number") |
| 85 | + parser.add_argument("-s", "--short-name", type=str, default="Agency", help="The agency's short name") |
| 86 | + parser.add_argument("--templates", action="store_true", default=False, help="Also create templates for the agency") |
| 87 | + parser.add_argument( |
| 88 | + "--templates-only", |
| 89 | + action="store_true", |
| 90 | + default=False, |
| 91 | + help="Don't create the agency in the database, but scaffold templates", |
| 92 | + ) |
| 93 | + parser.add_argument( |
| 94 | + "--transit-processor", |
| 95 | + type=int, |
| 96 | + choices=[t.id for t in TransitProcessor.objects.all()], |
| 97 | + default=TransitProcessor.objects.first().id, |
| 98 | + help="The id of a TransitProcessor instance to link to this agency", |
| 99 | + ) |
| 100 | + parser.add_argument("slug", help="The agency's slug", type=str) |
| 101 | + |
| 102 | + def handle(self, *args, **options): |
| 103 | + opts = self.parse_opts(**options) |
| 104 | + self._raise_for_slug(opts) |
| 105 | + |
| 106 | + if not opts.templates_only: |
| 107 | + self.stdout.write(self.style.NOTICE("Creating new agency...")) |
| 108 | + agency = self._create_agency(opts) |
| 109 | + self.stdout.write(self.style.SUCCESS(f"Agency created: {agency.slug} (id={agency.id})")) |
| 110 | + |
| 111 | + if opts.templates: |
| 112 | + self.stdout.write(self.style.NOTICE("Creating new agency templates...")) |
| 113 | + self._create_templates(agency) |
| 114 | + self.stdout.write(self.style.SUCCESS("Templates created")) |
0 commit comments