|
| 1 | +from django.conf import settings |
| 2 | +from django.core.management.base import BaseCommand |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +from crowdsourcer.models import MarkingSession, PublicAuthority |
| 7 | + |
| 8 | +YELLOW = "\033[33m" |
| 9 | +RED = "\033[31m" |
| 10 | +GREEN = "\033[32m" |
| 11 | +NOBOLD = "\033[0m" |
| 12 | + |
| 13 | + |
| 14 | +class Command(BaseCommand): |
| 15 | + help = "add authorities to marking session" |
| 16 | + |
| 17 | + def add_arguments(self, parser): |
| 18 | + parser.add_argument( |
| 19 | + "-q", "--quiet", action="store_true", help="Silence progress bars." |
| 20 | + ) |
| 21 | + |
| 22 | + parser.add_argument( |
| 23 | + "--all", action="store_true", help="add all existing authorities to session" |
| 24 | + ) |
| 25 | + |
| 26 | + parser.add_argument( |
| 27 | + "--session", required=True, help="marking session to add to" |
| 28 | + ) |
| 29 | + |
| 30 | + parser.add_argument("--council_list", help="list of councils to add") |
| 31 | + |
| 32 | + def add_to_session(self, authority, session): |
| 33 | + authority.marking_session.add(session) |
| 34 | + |
| 35 | + def handle(self, quiet: bool = False, *args, **options): |
| 36 | + try: |
| 37 | + session = MarkingSession.objects.get(label=options["session"]) |
| 38 | + except MarkingSession.DoesNotExist: |
| 39 | + self.stderr.write(f"No such session {options['session']}") |
| 40 | + return |
| 41 | + |
| 42 | + if options["all"]: |
| 43 | + for authority in PublicAuthority.objects.all(): |
| 44 | + self.add_to_session(authority, session) |
| 45 | + |
| 46 | + self.stdout.write( |
| 47 | + f"added {PublicAuthority.objects.count()} authorities to {session}" |
| 48 | + ) |
| 49 | + |
| 50 | + elif options["council_list"]: |
| 51 | + council_file = options["council_list"] |
| 52 | + |
| 53 | + council_file = settings.BASE_DIR / "data" / council_file |
| 54 | + |
| 55 | + df = pd.read_csv( |
| 56 | + council_file, |
| 57 | + usecols=[ |
| 58 | + "council", |
| 59 | + "gssNumber", |
| 60 | + ], |
| 61 | + ) |
| 62 | + count = 0 |
| 63 | + for index, row in df.iterrows(): |
| 64 | + try: |
| 65 | + authority = PublicAuthority.options.get(id=row["gssNumber"]) |
| 66 | + count += 1 |
| 67 | + except PublicAuthority.DoesNotExist: |
| 68 | + self.stderr.write(f"No authority found for {row['gssNumber']}") |
| 69 | + continue |
| 70 | + |
| 71 | + self.add_to_session(authority, session) |
| 72 | + |
| 73 | + self.stdout.write(f"added {count} authorities to {session}") |
0 commit comments