-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_dummy_ward_details.py
254 lines (232 loc) · 9.04 KB
/
load_dummy_ward_details.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
This script loads dummy ward details for existing wards
"""
import os
import django
import random
from django.utils.timezone import datetime
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'here_for_change.settings')
django.setup()
from here_for_change.municipalities.models import (Ward,WardDetail)
DUMMY_DATA=[
{
'councillor': {
'name': {
'value': 'Danny Europa',
'type': 'string',
'updated': '2022/09/12 16:57'},
'political_party': {
'value': 'African National Congress (ANC)',
'type': 'string',
'updated': '2021/02/12 05:57'},
'elected_date': {
'value': datetime(2021, 1, 8),
'type': 'date',
'updated': '2022/09/12 16:57'},
'contacts': {
'email': {
'primary': {
'type': 'email',
'value': '[email protected]',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 24, 'negative': 24}}
},
'phone': {
'value': '+27(0)284255500',
'type': 'phone',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 5, 'negative': 0}
},
}
}
},
{
'councillor': {
'name': {
'value': 'Clever Harris',
'type': 'string',
'updated': '2022/09/12 16:57'},
'political_party': {
'value': 'Democratic Alliance Afrikaans (DA)',
'type': 'string',
'updated': '2021/02/12 05:57'},
'elected_date': {
'value': datetime(2022, 1, 8),
'type': 'date',
'updated': '2022/09/12 16:57'},
'contacts': {
'email': {
'primary': {
'type': 'email',
'value': '[email protected]',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 14, 'negative': 24}}
},
'phone': {
'value': '+27(0)369255500',
'type': 'phone',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 10, 'negative': 0}
},
}
}
},
{
'councillor': {
'name': {
'value': 'Jean Bulma',
'type': 'string',
'updated': '2022/09/12 16:57'},
'political_party': {
'value': 'Economic Freedom Fighters (EFF)',
'type': 'string',
'updated': '2021/02/12 05:57'},
'elected_date': {
'value': datetime(2019, 1, 8),
'type': 'date',
'updated': '2022/09/12 16:57'},
'contacts': {
'email': {
'primary': {
'type': 'email',
'value': '[email protected]',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 20, 'negative': 4}}
},
'phone': {
'value': '+27(0)284255500',
'type': 'phone',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 5, 'negative': 3}
},
}
}
},
{
'councillor': {
'name': {
'value': 'Clara Weatherman',
'type': 'string',
'updated': '2022/09/12 16:57'},
'political_party': {
'value': 'Economic Freedom Fighters (EFF)',
'type': 'string',
'updated': '2021/02/12 05:57'},
'elected_date': {
'value': datetime(2018, 1, 8),
'type': 'date',
'updated': '2022/09/12 16:57'},
'contacts': {
'email': {
'primary': {
'type': 'email',
'value': '[email protected]',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 3, 'negative': 0}}
},
'phone': {
'value': '+27(0)284255500',
'type': 'phone',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 1, 'negative': 4}
},
}
}
},
{
'councillor': {
'name': {
'value': 'Fitcher Jones',
'type': 'string',
'updated': '2022/09/12 16:57'},
'political_party': {
'value': 'Democratic Alliance Afrikaans (DA)',
'type': 'string',
'updated': '2021/02/12 05:57'},
'elected_date': {
'value': datetime(2017, 1, 8),
'type': 'date',
'updated': '2022/09/12 16:57'},
'contacts': {
'email': {
'primary': {
'type': 'email',
'value': '[email protected]',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 30, 'negative': 12}}
},
'phone': {
'value': '+27(0)284255500',
'type': 'phone',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 10, 'negative': 12}
},
}
}
},
{
'councillor': {
'name': {
'value': 'Emmanuel Malema',
'type': 'string',
'updated': '2022/09/12 16:57'},
'political_party': {
'value': 'African National Congress (ANC)',
'type': 'string',
'updated': '2021/02/12 05:57'},
'elected_date': {
'value': datetime(2018, 1, 8),
'type': 'date',
'updated': '2022/09/12 16:57'},
'contacts': {
'email': {
'primary': {
'type': 'email',
'value': '[email protected]',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 19, 'negative': 10}}
},
'phone': {
'value': '+27(0)284255500',
'type': 'phone',
'updated': '2022/09/12 16:57',
'feedback': {'positive': 11, 'negative': 19}
},
}
}
},
]
def create_detail(ward:Ward,data:dict,stage:str="staging"):
"""
Creates ward details for ward from data
:param Ward ward: ward for which details should be created
:param dict data: Dictionary containing fields to be added as ward detail
:param dict stage: Version of details
:rtype: None
"""
for data_key in data.keys():
for field_key in data[data_key].keys():
field=data[data_key][field_key]
if field_key!="contacts":
detail=WardDetail(ward=ward,field_name=f"{data_key}_{field_key}",field_type=field.get("type"),field_value=field.get("value"),updated_at=datetime.strptime(field.get("updated"),r"%Y/%m/%d %H:%M"),stage=stage,feedback=field.get("feedback",{"positive":0,"negative":0}))
detail.save()
else:
for contact_key in field.keys():
contact=field[contact_key]
if contact_key!="email":
detail=WardDetail(ward=ward,field_name=f"{data_key}_{field_key}_{contact_key}",field_type=contact.get("type"),field_value=contact.get("value"),updated_at=datetime.strptime(contact.get("updated"),r"%Y/%m/%d %H:%M"),stage=stage,feedback=contact.get("feedback",{"positive":0,"negative":0}))
detail.save()
else:
for email_type in contact.keys():
email=contact[email_type]
detail=WardDetail(ward=ward,field_name=f"{data_key}_{field_key}_{contact_key}_{email_type}",field_type=email.get("type"),field_value=email.get("value"),updated_at=datetime.strptime(email.get("updated"),r"%Y/%m/%d %H:%M"),stage=stage,feedback=email.get("feedback",{"positive":0,"negative":0}))
detail.save()
def load_dummy_ward_details():
WardDetail.objects.all().delete()
versions=[WardDetail.STAGING,WardDetail.PRODUCTION]
wards=Ward.objects.all()
for ward in wards:
for version in versions:
create_detail(ward,DUMMY_DATA[random.randint(0,len(DUMMY_DATA))-1],version)
if __name__=="__main__":
load_dummy_ward_details()