-
Notifications
You must be signed in to change notification settings - Fork 31
/
import_local_parties.py
109 lines (97 loc) · 3.85 KB
/
import_local_parties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from dateutil.parser import parse
from django.core.management.base import BaseCommand
from django.db import transaction
from parties.importers import LocalElection, LocalPartyImporter
class Command(BaseCommand):
ELECTIONS = [
LocalElection(
date="2024-05-02",
csv_files=[
# LibDem
"https://docs.google.com/spreadsheets/d/e/2PACX-1vT7Z_xufAGGbnea8TtMI7tcsSt6IedrykKqqWSByN4HEHfiT-UOOCdUhS7Dn2m59B2R_JVSVzTMZgwj/pub?gid=1628145765&single=true&output=csv",
# Con
"https://docs.google.com/spreadsheets/d/e/2PACX-1vT7Z_xufAGGbnea8TtMI7tcsSt6IedrykKqqWSByN4HEHfiT-UOOCdUhS7Dn2m59B2R_JVSVzTMZgwj/pub?gid=0&single=true&output=csv",
# Lab
"https://docs.google.com/spreadsheets/d/e/2PACX-1vT7Z_xufAGGbnea8TtMI7tcsSt6IedrykKqqWSByN4HEHfiT-UOOCdUhS7Dn2m59B2R_JVSVzTMZgwj/pub?gid=583155279&single=true&output=csv",
# Green
"https://docs.google.com/spreadsheets/d/e/2PACX-1vT7Z_xufAGGbnea8TtMI7tcsSt6IedrykKqqWSByN4HEHfiT-UOOCdUhS7Dn2m59B2R_JVSVzTMZgwj/pub?gid=302547083&single=true&output=csv",
# Other
"https://docs.google.com/spreadsheets/d/e/2PACX-1vT7Z_xufAGGbnea8TtMI7tcsSt6IedrykKqqWSByN4HEHfiT-UOOCdUhS7Dn2m59B2R_JVSVzTMZgwj/pub?gid=74760747&single=true&output=csv",
],
),
]
def add_arguments(self, parser):
parser.add_argument(
"-f",
"--force-update",
action="store_true",
help="Will update regardless of whether there are current elections for the date",
)
parser.add_argument(
"-ff",
"--from-file",
nargs=2,
metavar=("election_date", "path_to_file"),
help="To import from a file, pass in an election date and the path to the file",
)
parser.add_argument(
"--date",
action="store",
help="date",
required=False,
)
parser.add_argument(
"--url",
action="store",
help="url",
required=False,
)
def valid_date(self, value):
return parse(value)
def import_from_file(self):
"""
Runs the importer for the file passed in arguments
"""
date, filepath = self.options["from_file"]
if not self.valid_date(value=date):
self.stdout.write("Date is invalid")
return
election = LocalElection(date=date, csv_files=[filepath])
importer = LocalPartyImporter(
election=election,
force_update=self.options["force_update"],
from_file=True,
)
importer.import_parties()
def import_from_elections(self):
"""
Runs the importer for all elections in the ELECTIONS list. This is the
default method of running the import process
"""
for election in self.ELECTIONS:
importer = LocalPartyImporter(
election=election,
force_update=self.options["force_update"],
)
importer.import_parties()
def import_from_url(self):
"""
Runs the importer for all elections in the ELECTIONS list. This is the
default method of running the import process
"""
election = LocalElection(
date=self.options["date"], csv_files=[self.options["url"]]
)
importer = LocalPartyImporter(
election=election,
)
importer.import_parties()
@transaction.atomic
def handle(self, **options):
self.options = options
if options["from_file"]:
return self.import_from_file()
if options["url"] and options["date"]:
return self.import_from_url()
self.import_from_elections()
return None