|
| 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 ( |
| 7 | + MarkingSession, |
| 8 | + PublicAuthority, |
| 9 | + QuestionGroup, |
| 10 | + ResponseType, |
| 11 | + Section, |
| 12 | +) |
| 13 | +from utils import mapit |
| 14 | + |
| 15 | + |
| 16 | +class Command(BaseCommand): |
| 17 | + help = "set up authorities and question groups" |
| 18 | + |
| 19 | + groups = ["MP"] |
| 20 | + |
| 21 | + sections = [ |
| 22 | + "Who Funds Them", |
| 23 | + ] |
| 24 | + |
| 25 | + session = "WhoFundsThem 2024" |
| 26 | + |
| 27 | + response_types = ["First Mark", "Right of Reply", "Audit"] |
| 28 | + |
| 29 | + def add_arguments(self, parser): |
| 30 | + parser.add_argument( |
| 31 | + "-q", "--quiet", action="store_true", help="Silence progress bars." |
| 32 | + ) |
| 33 | + |
| 34 | + def get_group(self, mp): |
| 35 | + return QuestionGroup.objects.get(description="MP") |
| 36 | + |
| 37 | + def get_do_not_mark_list(self): |
| 38 | + df = pd.read_csv(self.do_not_mark_file) |
| 39 | + return list(df["gss-code"]) |
| 40 | + |
| 41 | + def get_twfy_df(self): |
| 42 | + df = pd.read_csv("https://www.theyworkforyou.com/mps/?f=csv").rename( |
| 43 | + columns={"Person ID": "twfyid"} |
| 44 | + ) |
| 45 | + |
| 46 | + return df |
| 47 | + |
| 48 | + def handle(self, quiet: bool = False, *args, **options): |
| 49 | + session, _ = MarkingSession.objects.get_or_create( |
| 50 | + label=self.session, defaults={"start_date": "2024-06-01"} |
| 51 | + ) |
| 52 | + |
| 53 | + for section in self.sections: |
| 54 | + c, c = Section.objects.get_or_create(title=section, marking_session=session) |
| 55 | + |
| 56 | + for group in self.groups: |
| 57 | + g, c = QuestionGroup.objects.get_or_create(description=group) |
| 58 | + g.marking_session.set([session]) |
| 59 | + |
| 60 | + for r_type in self.response_types: |
| 61 | + r, c = ResponseType.objects.get_or_create(type=r_type, priority=1) |
| 62 | + |
| 63 | + mps = self.get_twfy_df() |
| 64 | + |
| 65 | + if not quiet: |
| 66 | + print("Importing MPs") |
| 67 | + for _, mp in mps.iterrows(): |
| 68 | + do_not_mark = False |
| 69 | + |
| 70 | + name = f"{mp['First name']} {mp['Last name']}" |
| 71 | + defaults = { |
| 72 | + "name": name, |
| 73 | + "questiongroup": self.get_group(mp), |
| 74 | + "do_not_mark": do_not_mark, |
| 75 | + "type": "MP", |
| 76 | + } |
| 77 | + |
| 78 | + a, created = PublicAuthority.objects.update_or_create( |
| 79 | + unique_id=mp["twfyid"], |
| 80 | + defaults=defaults, |
| 81 | + ) |
0 commit comments