Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
2904 lines (2612 loc) · 153 KB
/
Copy pathmodels.py
File metadata and controls
2904 lines (2612 loc) · 153 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# voter_guide/models.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-
from datetime import datetime, timedelta
from django.db import models
from django.db.models import Q
from election.models import ElectionManager, TIME_SPAN_LIST
from exception.models import handle_exception, handle_record_not_found_exception, \
handle_record_found_more_than_one_exception
import operator
from organization.models import Organization, OrganizationManager, \
CORPORATION, GROUP, INDIVIDUAL, NEWS_ORGANIZATION, NONPROFIT, NONPROFIT_501C3, NONPROFIT_501C4, \
POLITICAL_ACTION_COMMITTEE, PUBLIC_FIGURE, TRADE_ASSOCIATION, UNKNOWN, ORGANIZATION_TYPE_CHOICES
from pledge_to_vote.models import PledgeToVoteManager
import pytz
from voter.models import VoterManager
import wevote_functions.admin
from wevote_functions.functions import convert_to_int, convert_to_str, positive_value_exists
from wevote_settings.models import fetch_site_unique_id_prefix, fetch_next_we_vote_id_voter_guide_integer
logger = wevote_functions.admin.get_logger(__name__)
ORGANIZATION = 'O' # Deprecated
ORGANIZATION_WORD = 'ORGANIZATION'
VOTER = 'V' # Deprecated
SUPPORT = 'SUPPORT'
NO_STANCE = 'NO_STANCE'
OPPOSE = 'OPPOSE'
POSITION_CHOICES = (
(SUPPORT, 'Supports'),
(NO_STANCE, 'No stance'),
(OPPOSE, 'Opposes'),
)
ORGANIZATION_ENDORSING_CANDIDATES = 'ORG'
ENDORSEMENTS_FOR_CANDIDATE = 'CAND'
UNKNOWN_TYPE = 'UNKNOWN'
VOTER_GUIDE_POSSIBILITY_TYPES = (
(ORGANIZATION_ENDORSING_CANDIDATES, 'Organization or News Website Endorsing Candidates'),
(ENDORSEMENTS_FOR_CANDIDATE, 'List of Endorsements for One Candidate'),
(UNKNOWN_TYPE, 'List of Endorsements for One Candidate'),
)
POSSIBLE_ENDORSEMENT_NUMBER_LIST = [
"001", "002", "003", "004", "005", "006", "007", "008", "009", "010",
"011", "012", "013", "014", "015", "016", "017", "018", "019", "020",
"021", "022", "023", "024", "025", "026", "027", "028", "029", "030",
"031", "032", "033", "034", "035", "036", "037", "038", "039", "040",
"041", "042", "043", "044", "045", "046", "047", "048", "049", "050",
"051", "052", "053", "054", "055", "056", "057", "058", "059", "060",
"061", "062", "063", "064", "065", "066", "067", "068", "069", "070",
"071", "072", "073", "074", "075", "076", "077", "078", "079", "080",
"081", "082", "083", "084", "085", "086", "087", "088", "089", "090",
"091", "092", "093", "094", "095", "096", "097", "098", "099", "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"]
# Since there are some scenarios where we remove candidates where positions are already stored, we need to review more
# positions than we are prepared to store
POSSIBLE_ENDORSEMENT_NUMBER_LIST_FULL = [
"001", "002", "003", "004", "005", "006", "007", "008", "009", "010",
"011", "012", "013", "014", "015", "016", "017", "018", "019", "020",
"021", "022", "023", "024", "025", "026", "027", "028", "029", "030",
"031", "032", "033", "034", "035", "036", "037", "038", "039", "040",
"041", "042", "043", "044", "045", "046", "047", "048", "049", "050",
"051", "052", "053", "054", "055", "056", "057", "058", "059", "060",
"061", "062", "063", "064", "065", "066", "067", "068", "069", "070",
"071", "072", "073", "074", "075", "076", "077", "078", "079", "080",
"081", "082", "083", "084", "085", "086", "087", "088", "089", "090",
"091", "092", "093", "094", "095", "096", "097", "098", "099", "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",
"251", "252", "253", "254", "255", "256", "257", "258", "259", "260",
"261", "262", "263", "264", "265", "266", "267", "268", "269", "270",
"271", "272", "273", "274", "275", "276", "277", "278", "279", "280",
"281", "282", "283", "284", "285", "286", "287", "288", "289", "290",
"291", "292", "293", "294", "295", "296", "297", "298", "299", "300",
"301", "302", "303", "304", "305", "306", "307", "308", "309", "310",
"311", "312", "313", "314", "315", "316", "317", "318", "319", "320",
"321", "322", "323", "324", "325", "326", "327", "328", "329", "330",
"331", "332", "333", "334", "335", "336", "337", "338", "339", "340",
"341", "342", "343", "344", "345", "346", "347", "348", "349", "350",
"351", "352", "353", "354", "355", "356", "357", "358", "359", "360",
"361", "362", "363", "364", "365", "366", "367", "368", "369", "370",
"371", "372", "373", "374", "375", "376", "377", "378", "379", "380",
"381", "382", "383", "384", "385", "386", "387", "388", "389", "390",
"391", "392", "393", "394", "395", "396", "397", "398", "399", "400"]
class VoterGuideManager(models.Manager):
"""
A class for working with the VoterGuide model
"""
def update_or_create_organization_voter_guide_by_election_id(self, voter_guide_we_vote_id='',
organization_we_vote_id='',
google_civic_election_id=0,
state_code='',
pledge_goal=0,
we_vote_hosted_profile_image_url_large='',
we_vote_hosted_profile_image_url_medium='',
we_vote_hosted_profile_image_url_tiny='',
vote_smart_ratings_only=False,
elections_dict={},
organizations_dict={},
voter_we_vote_id_dict={},
):
"""
This creates voter_guides, and also refreshes voter guides with updated organization data
"""
google_civic_election_id = convert_to_int(google_civic_election_id)
exception_multiple_object_returned = False
voter_guide_on_stage = None
organization = Organization()
organization_found = False
new_voter_guide_created = False
voter_we_vote_id = None
status = ''
success = True
if not google_civic_election_id or not organization_we_vote_id:
status += 'ERROR_VARIABLES_MISSING_FOR_ORGANIZATION_VOTER_GUIDE '
success = False
new_voter_guide_created = False
else:
# Retrieve the organization object so we can bring over values
# NOTE: If we don't have this organization in the local database, we won't create a voter guide
organization_manager = OrganizationManager()
voter_manager = VoterManager()
if organization_we_vote_id in organizations_dict:
organization = organizations_dict[organization_we_vote_id]
organization_found = True
else:
results = organization_manager.retrieve_organization(0, organization_we_vote_id)
if results['organization_found']:
organization = results['organization']
organizations_dict[organization_we_vote_id] = organization
organization_found = True
if organization_found:
if organization_we_vote_id in voter_we_vote_id_dict:
voter_we_vote_id = voter_we_vote_id_dict[organization_we_vote_id]
else:
voter_results = voter_manager.retrieve_voter_by_organization_we_vote_id(organization_we_vote_id)
if voter_results['voter_found']:
try:
voter_we_vote_id = voter_results['voter'].we_vote_id
voter_we_vote_id_dict[organization_we_vote_id] = voter_we_vote_id
except Exception as e:
status += 'COULD_NOT_RETRIEVE_VOTER_WE_VOTE_ID ' + str(e) + ' '
# Retrieve the election so we can bring over the state_code if needed
if positive_value_exists(state_code):
election_state_code = state_code
else:
election_state_code = ''
if google_civic_election_id in elections_dict:
election = elections_dict[google_civic_election_id]
if election:
election_state_code = election.state_code
else:
try:
del elections_dict[google_civic_election_id]
except Exception as e:
pass
else:
election_manager = ElectionManager()
election_results = election_manager.retrieve_election(google_civic_election_id)
if election_results['election_found']:
election = election_results['election']
election_state_code = election.state_code
if not positive_value_exists(election_state_code):
election_state_code = ''
elections_dict[google_civic_election_id] = election
# Now update voter_guide
if organization_found:
pledge_to_vote_manager = PledgeToVoteManager()
pledge_results = pledge_to_vote_manager.retrieve_pledge_count_from_organization_we_vote_id(
organization_we_vote_id)
if pledge_results['pledge_count_found']:
pledge_count = pledge_results['pledge_count']
else:
pledge_count = 0
if positive_value_exists(election_state_code):
election_state_code = election_state_code.lower()
updated_values = {
'google_civic_election_id': google_civic_election_id,
'organization_we_vote_id': organization_we_vote_id,
'image_url': organization.organization_photo_url(),
'twitter_handle': organization.organization_twitter_handle,
'twitter_description': organization.twitter_description,
'twitter_followers_count': organization.twitter_followers_count,
'display_name': organization.organization_name,
'voter_guide_owner_type': organization.organization_type,
'vote_smart_ratings_only': vote_smart_ratings_only,
'state_code': election_state_code,
'we_vote_hosted_profile_image_url_large': organization.we_vote_hosted_profile_image_url_large,
'we_vote_hosted_profile_image_url_medium': organization.we_vote_hosted_profile_image_url_medium,
'we_vote_hosted_profile_image_url_tiny': organization.we_vote_hosted_profile_image_url_tiny,
'pledge_count': pledge_count,
}
if positive_value_exists(voter_guide_we_vote_id):
updated_values['we_vote_id'] = voter_guide_we_vote_id
if positive_value_exists(voter_we_vote_id):
updated_values['voter_we_vote_id'] = voter_we_vote_id
if positive_value_exists(pledge_goal):
updated_values['pledge_goal'] = pledge_goal
if positive_value_exists(we_vote_hosted_profile_image_url_large):
updated_values['we_vote_hosted_profile_image_url_large'] = \
we_vote_hosted_profile_image_url_large
if positive_value_exists(we_vote_hosted_profile_image_url_medium):
updated_values['we_vote_hosted_profile_image_url_medium'] = \
we_vote_hosted_profile_image_url_medium
if positive_value_exists(we_vote_hosted_profile_image_url_tiny):
updated_values['we_vote_hosted_profile_image_url_tiny'] = we_vote_hosted_profile_image_url_tiny
if positive_value_exists(google_civic_election_id):
if google_civic_election_id in elections_dict:
election = elections_dict[google_civic_election_id]
if election:
updated_values['election_day_text'] = election.election_day_text
else:
try:
del elections_dict[google_civic_election_id]
except Exception as e:
pass
else:
election_manager = ElectionManager()
election_results = election_manager.retrieve_election(google_civic_election_id)
if election_results['election_found']:
election = election_results['election']
updated_values['election_day_text'] = election.election_day_text
elections_dict[google_civic_election_id] = election
try:
voter_guide_on_stage, new_voter_guide_created = VoterGuide.objects.update_or_create(
google_civic_election_id__exact=google_civic_election_id,
organization_we_vote_id__iexact=organization_we_vote_id,
defaults=updated_values)
except VoterGuide.MultipleObjectsReturned as e:
handle_record_found_more_than_one_exception(e, logger=logger)
status += 'MULTIPLE_MATCHING_VOTER_GUIDES_FOUND_FOR_ORGANIZATION '
exception_multiple_object_returned = True
new_voter_guide_created = False
except Exception as e:
handle_exception(e, logger=logger)
success = False
status += 'UPDATE_OR_CREATE_ORGANIZATION_VOTER_GUIDE_BY_ELECTION_ID: ' + str(e) + ' '
new_voter_guide_created = False
if new_voter_guide_created:
if not positive_value_exists(voter_guide_on_stage.id):
# Advance the we_vote_id_last_voter_guide_integer
next_integer = fetch_next_we_vote_id_voter_guide_integer()
status += 'UPDATE_OR_CREATE_ORGANIZATION_VOTER_GUIDE_BY_ELECTION_ID, NEXT_INTEGER: ' \
'' + str(next_integer) + ' '
try:
voter_guide_on_stage.we_vote_id = None
voter_guide_on_stage.save()
except Exception as e:
handle_exception(e, logger=logger)
status += 'UPDATE_OR_CREATE_ORGANIZATION_VOTER_GUIDE_AFTER_NEXT_INTEGER: ' + str(e) + ' '
new_voter_guide_created = False
if positive_value_exists(voter_guide_on_stage.id):
status += 'VOTER_GUIDE_CREATED_FOR_ORGANIZATION '
success = True
else:
status += 'COULD_NOT_CREATE_VOTER_GUIDE '
success = False
else:
status += 'VOTER_GUIDE_UPDATED_FOR_ORGANIZATION '
success = True
else:
success = False
status += 'VOTER_GUIDE_NOT_CREATED_BECAUSE_ORGANIZATION_NOT_FOUND_LOCALLY '
results = {
'success': success,
'status': status,
'MultipleObjectsReturned': exception_multiple_object_returned,
'voter_guide_saved': success,
'voter_guide': voter_guide_on_stage,
'new_voter_guide_created': new_voter_guide_created,
'elections_dict': elections_dict,
'organizations_dict': organizations_dict,
'voter_we_vote_id_dict': voter_we_vote_id_dict,
}
return results
def update_or_create_organization_voter_guide_by_time_span(self, voter_guide_we_vote_id,
organization_we_vote_id, vote_smart_time_span,
pledge_goal='',
we_vote_hosted_profile_image_url_large='',
we_vote_hosted_profile_image_url_medium='',
we_vote_hosted_profile_image_url_tiny=''
):
organization_found = False
voter_guide_owner_type = ORGANIZATION
exception_multiple_object_returned = False
new_voter_guide_created = False
status = ''
if not vote_smart_time_span or not organization_we_vote_id:
status = 'ERROR_VARIABLES_MISSING_FOR_ORGANIZATION_VOTER_GUIDE_BY_TIME_SPAN'
success = False
new_voter_guide_created = False
else:
# Retrieve the organization object so we can bring over values
organization_manager = OrganizationManager()
results = organization_manager.retrieve_organization(0, organization_we_vote_id)
if results['organization_found']:
organization_found = True
organization = results['organization']
# Now update voter_guide # TODO DALE Get from TwitterLinkToOrganization, not organization_twitter_handle
try:
if organization_found:
if organization.twitter_followers_count is None:
twitter_followers_count = 0
else:
twitter_followers_count = convert_to_int(organization.twitter_followers_count)
pledge_to_vote_manager = PledgeToVoteManager()
pledge_results = pledge_to_vote_manager.retrieve_pledge_count_from_organization_we_vote_id(
organization_we_vote_id)
if pledge_results['pledge_count_found']:
pledge_count = pledge_results['pledge_count']
else:
pledge_count = 0
updated_values = {
# Values we search against below
'vote_smart_time_span': vote_smart_time_span,
'organization_we_vote_id': organization_we_vote_id,
# The rest of the values
'voter_guide_owner_type': voter_guide_owner_type,
'twitter_handle': organization.organization_twitter_handle,
'display_name': organization.organization_name,
'image_url': organization.organization_photo_url(),
'twitter_description': organization.twitter_description,
'twitter_followers_count': twitter_followers_count,
'pledge_count': pledge_count,
}
if positive_value_exists(voter_guide_we_vote_id):
updated_values['we_vote_id'] = voter_guide_we_vote_id
if positive_value_exists(pledge_goal):
updated_values['pledge_goal'] = pledge_goal
if positive_value_exists(we_vote_hosted_profile_image_url_large):
updated_values['we_vote_hosted_profile_image_url_large'] = \
we_vote_hosted_profile_image_url_large
if positive_value_exists(we_vote_hosted_profile_image_url_medium):
updated_values['we_vote_hosted_profile_image_url_medium'] = \
we_vote_hosted_profile_image_url_medium
if positive_value_exists(we_vote_hosted_profile_image_url_tiny):
updated_values['we_vote_hosted_profile_image_url_tiny'] = we_vote_hosted_profile_image_url_tiny
voter_guide_on_stage, new_voter_guide_created = VoterGuide.objects.update_or_create(
vote_smart_time_span__exact=vote_smart_time_span,
organization_we_vote_id__iexact=organization_we_vote_id,
defaults=updated_values)
success = True
if new_voter_guide_created:
status += 'VOTER_GUIDE_CREATED_FOR_ORGANIZATION_BY_TIME_SPAN '
else:
status += 'VOTER_GUIDE_UPDATED_FOR_ORGANIZATION_BY_TIME_SPAN '
else:
success = False
status += 'VOTER_GUIDE_NOT_CREATED_BECAUSE_ORGANIZATION_NOT_FOUND_LOCALLY '
except VoterGuide.MultipleObjectsReturned as e:
handle_record_found_more_than_one_exception(e, logger=logger)
success = False
status += 'MULTIPLE_MATCHING_VOTER_GUIDES_FOUND_FOR_ORGANIZATION_BY_TIME_SPAN '
exception_multiple_object_returned = True
new_voter_guide_created = False
except Exception as e:
handle_exception(e, logger=logger)
success = False
status += 'UPDATE_OR_CREATE_ORGANIZATION_VOTER_GUIDE_BY_ELECTION_ID: ' + str(e) + ' '
new_voter_guide_created = False
results = {
'success': success,
'status': status,
'MultipleObjectsReturned': exception_multiple_object_returned,
'voter_guide_saved': success,
'new_voter_guide_created': new_voter_guide_created,
}
return results
def update_or_create_voter_voter_guide(self, google_civic_election_id, voter):
"""
:param google_civic_election_id:
:param voter:
:param organization:
:return:
"""
google_civic_election_id = convert_to_int(google_civic_election_id)
success = False
status = ""
exception_multiple_object_returned = False
new_voter_guide_created = False
linked_organization_we_vote_id = ""
organization_manager = OrganizationManager()
voter_guide = VoterGuide()
try:
voter
except NameError:
voter_exists = False
else:
voter_exists = True
if voter_exists and positive_value_exists(voter.linked_organization_we_vote_id):
organization_results = \
organization_manager.retrieve_organization_from_we_vote_id(voter.linked_organization_we_vote_id)
if organization_results['organization_found']:
organization = organization_results['organization']
linked_organization_we_vote_id = organization.we_vote_id
try:
organization
except NameError:
organization_exists = False
else:
organization_exists = True
if positive_value_exists(linked_organization_we_vote_id) and voter_exists and organization_exists \
and positive_value_exists(google_civic_election_id):
try:
updated_values = {
# Values we search against below
'google_civic_election_id': google_civic_election_id,
'organization_we_vote_id': linked_organization_we_vote_id,
# The rest of the values
'voter_guide_owner_type': organization.organization_type,
'owner_voter_id': voter.id,
'owner_we_vote_id': voter.we_vote_id,
'voter_we_vote_id': voter.we_vote_id,
}
voter_guide, new_voter_guide_created = VoterGuide.objects.update_or_create(
google_civic_election_id__exact=google_civic_election_id,
organization_we_vote_id__iexact=linked_organization_we_vote_id,
defaults=updated_values)
success = True
if new_voter_guide_created:
status += 'VOTER_GUIDE_CREATED_FOR_VOTER '
else:
status += 'VOTER_GUIDE_UPDATED_FOR_VOTER '
except VoterGuide.MultipleObjectsReturned as e:
success = False
status += 'MULTIPLE_MATCHING_VOTER_GUIDES_FOUND_FOR_VOTER '
exception_multiple_object_returned = True
new_voter_guide_created = False
except Exception as e:
handle_exception(e, logger=logger)
success = False
status += 'UPDATE_OR_CREATE_VOTER_VOTER_GUIDE: ' + str(e) + ' '
new_voter_guide_created = False
results = {
'success': success,
'status': status,
'MultipleObjectsReturned': exception_multiple_object_returned,
'voter_guide': voter_guide,
'voter_guide_saved': success,
'voter_guide_created': new_voter_guide_created,
}
return results
def voter_guide_exists(self, organization_we_vote_id, google_civic_election_id):
voter_guide_found = False
google_civic_election_id = int(google_civic_election_id)
if not positive_value_exists(organization_we_vote_id) or not positive_value_exists(google_civic_election_id):
return False
try:
if positive_value_exists(organization_we_vote_id) and positive_value_exists(google_civic_election_id):
voter_guide_query = VoterGuide.objects.filter(google_civic_election_id=google_civic_election_id,
organization_we_vote_id__iexact=organization_we_vote_id)
voter_guide_found = True if voter_guide_query.count() > 0 else False
except VoterGuide.MultipleObjectsReturned as e:
handle_exception(e, logger=logger)
voter_guide_found = True
except VoterGuide.DoesNotExist as e:
handle_exception(e, logger=logger)
voter_guide_found = False
except Exception as e:
handle_exception(e, logger=logger)
return voter_guide_found
def retrieve_voter_guide(self, voter_guide_id=0, voter_guide_we_vote_id="", google_civic_election_id=0,
vote_smart_time_span=None,
organization_we_vote_id=None, public_figure_we_vote_id=None, owner_we_vote_id=None,
read_only=False):
voter_guide_id = convert_to_int(voter_guide_id)
google_civic_election_id = convert_to_int(google_civic_election_id)
organization_we_vote_id = convert_to_str(organization_we_vote_id)
public_figure_we_vote_id = convert_to_str(public_figure_we_vote_id)
owner_we_vote_id = convert_to_str(owner_we_vote_id)
error_result = False
exception_does_not_exist = False
exception_multiple_object_returned = False
status = ""
success = True
voter_guide_on_stage = VoterGuide()
voter_guide_on_stage_id = 0
voter_guide_on_stage_we_vote_id = ''
try:
if positive_value_exists(voter_guide_id):
status = "ERROR_RETRIEVING_VOTER_GUIDE_WITH_ID " # Set this in case the get fails
if read_only:
voter_guide_on_stage = VoterGuide.objects.using('readonly').get(id=voter_guide_id)
else:
voter_guide_on_stage = VoterGuide.objects.get(id=voter_guide_id)
voter_guide_on_stage_id = voter_guide_on_stage.id
voter_guide_on_stage_we_vote_id = voter_guide_on_stage.we_vote_id
status = "VOTER_GUIDE_FOUND_WITH_ID "
elif positive_value_exists(voter_guide_we_vote_id):
status = "ERROR_RETRIEVING_VOTER_GUIDE_WITH_WE_VOTE_ID " # Set this in case the get fails
if read_only:
voter_guide_on_stage = VoterGuide.objects.using('readonly').get(we_vote_id=voter_guide_we_vote_id)
else:
voter_guide_on_stage = VoterGuide.objects.get(we_vote_id=voter_guide_we_vote_id)
voter_guide_on_stage_id = voter_guide_on_stage.id
voter_guide_on_stage_we_vote_id = voter_guide_on_stage.we_vote_id
status = "VOTER_GUIDE_FOUND_WITH_WE_VOTE_ID "
elif positive_value_exists(organization_we_vote_id) and positive_value_exists(google_civic_election_id):
status = "ERROR_RETRIEVING_VOTER_GUIDE_WITH_ORGANIZATION_WE_VOTE_ID " # Set this in case the get fails
if read_only:
voter_guide_on_stage = VoterGuide.objects.using('readonly').get(
google_civic_election_id=google_civic_election_id,
organization_we_vote_id__iexact=organization_we_vote_id)
else:
voter_guide_on_stage = VoterGuide.objects.get(
google_civic_election_id=google_civic_election_id,
organization_we_vote_id__iexact=organization_we_vote_id)
voter_guide_on_stage_id = voter_guide_on_stage.id
voter_guide_on_stage_we_vote_id = voter_guide_on_stage.we_vote_id
status = "VOTER_GUIDE_FOUND_WITH_ORGANIZATION_WE_VOTE_ID "
elif positive_value_exists(organization_we_vote_id) and positive_value_exists(vote_smart_time_span):
status = "ERROR_RETRIEVING_VOTER_GUIDE_WITH_ORGANIZATION_WE_VOTE_ID_AND_TIME_SPAN "
if read_only:
voter_guide_on_stage = VoterGuide.objects.using('readonly').get(
vote_smart_time_span=vote_smart_time_span,
organization_we_vote_id__iexact=organization_we_vote_id)
else:
voter_guide_on_stage = VoterGuide.objects.get(
vote_smart_time_span=vote_smart_time_span,
organization_we_vote_id__iexact=organization_we_vote_id)
voter_guide_on_stage_id = voter_guide_on_stage.id
voter_guide_on_stage_we_vote_id = voter_guide_on_stage.we_vote_id
status = "VOTER_GUIDE_FOUND_WITH_ORGANIZATION_WE_VOTE_ID_AND_TIME_SPAN "
elif positive_value_exists(public_figure_we_vote_id) and positive_value_exists(google_civic_election_id):
status = "ERROR_RETRIEVING_VOTER_GUIDE_WITH_PUBLIC_FIGURE_WE_VOTE_ID " # Set this in case the get fails
if read_only:
voter_guide_on_stage = VoterGuide.objects.using('readonly').get(
google_civic_election_id=google_civic_election_id,
public_figure_we_vote_id__iexact=public_figure_we_vote_id)
else:
voter_guide_on_stage = VoterGuide.objects.get(
google_civic_election_id=google_civic_election_id,
public_figure_we_vote_id__iexact=public_figure_we_vote_id)
voter_guide_on_stage_id = voter_guide_on_stage.id
voter_guide_on_stage_we_vote_id = voter_guide_on_stage.we_vote_id
status = "VOTER_GUIDE_FOUND_WITH_PUBLIC_FIGURE_WE_VOTE_ID "
elif positive_value_exists(owner_we_vote_id) and positive_value_exists(google_civic_election_id):
status = "ERROR_RETRIEVING_VOTER_GUIDE_WITH_VOTER_WE_VOTE_ID " # Set this in case the get fails
if read_only:
voter_guide_on_stage = VoterGuide.objects.using('readonly').get(
google_civic_election_id=google_civic_election_id,
owner_we_vote_id__iexact=owner_we_vote_id)
else:
voter_guide_on_stage = VoterGuide.objects.get(
google_civic_election_id=google_civic_election_id,
owner_we_vote_id__iexact=owner_we_vote_id)
voter_guide_on_stage_id = voter_guide_on_stage.id
voter_guide_on_stage_we_vote_id = voter_guide_on_stage.we_vote_id
status = "VOTER_GUIDE_FOUND_WITH_VOTER_WE_VOTE_ID "
else:
status = "Insufficient variables included to retrieve one voter guide."
except VoterGuide.MultipleObjectsReturned as e:
handle_record_found_more_than_one_exception(e, logger)
error_result = True
exception_multiple_object_returned = True
status += ", ERROR_MORE_THAN_ONE_VOTER_GUIDE_FOUND "
success = False
except VoterGuide.DoesNotExist:
error_result = True
exception_does_not_exist = True
status += ", VOTER_GUIDE_DOES_NOT_EXIST "
success = False
voter_guide_on_stage_found = True if positive_value_exists(voter_guide_on_stage_we_vote_id) else False
results = {
'success': success,
'status': status,
'voter_guide_found': voter_guide_on_stage_found,
'voter_guide_id': voter_guide_on_stage_id,
'voter_guide_we_vote_id': voter_guide_on_stage_we_vote_id,
'organization_we_vote_id': voter_guide_on_stage.organization_we_vote_id,
'public_figure_we_vote_id': voter_guide_on_stage.public_figure_we_vote_id,
'owner_we_vote_id': voter_guide_on_stage.owner_we_vote_id,
'voter_guide': voter_guide_on_stage,
'error_result': error_result,
'DoesNotExist': exception_does_not_exist,
'MultipleObjectsReturned': exception_multiple_object_returned,
}
return results
def retrieve_most_recent_voter_guide_for_org(self, organization_we_vote_id, read_only=False):
status = 'ENTERING_RETRIEVE_MOST_RECENT_VOTER_GUIDE_FOR_ORG'
voter_guide_found = False
voter_guide = VoterGuide()
voter_guide_manager = VoterGuideManager()
for time_span in TIME_SPAN_LIST:
voter_guide_by_time_span_results = voter_guide_manager.retrieve_voter_guide(
vote_smart_time_span=time_span,
organization_we_vote_id=organization_we_vote_id,
read_only=read_only)
if voter_guide_by_time_span_results['voter_guide_found']:
voter_guide_found = True
voter_guide = voter_guide_by_time_span_results['voter_guide']
status = 'MOST_RECENT_VOTER_GUIDE_FOUND_FOR_ORG_BY_TIME_SPAN'
results = {
'success': voter_guide_found,
'status': status,
'voter_guide_found': voter_guide_found,
'voter_guide': voter_guide,
}
return results
election_manager = ElectionManager()
results = election_manager.retrieve_elections_by_date()
if results['success']:
election_list = results['election_list']
for one_election in election_list:
voter_guide_results = voter_guide_manager.retrieve_voter_guide(
google_civic_election_id=one_election.google_civic_election_id,
organization_we_vote_id=organization_we_vote_id,
read_only=read_only)
if voter_guide_results['voter_guide_found']:
voter_guide_found = True
voter_guide = voter_guide_results['voter_guide']
status = 'MOST_RECENT_VOTER_GUIDE_FOUND_FOR_ORG_BY_ELECTION_ID'
results = {
'success': voter_guide_found,
'status': status,
'voter_guide_found': voter_guide_found,
'voter_guide': voter_guide,
}
return results
results = {
'success': False,
'status': status,
'voter_guide_found': voter_guide_found,
'voter_guide': voter_guide,
}
return results
def reset_voter_guide_image_details(self, organization, twitter_profile_image_url_https=None,
facebook_profile_image_url_https=None):
"""
Reset Voter guide entry with original we vote image details
:param organization:
:param twitter_profile_image_url_https:
:param facebook_profile_image_url_https:
:return:
"""
image_url = None
success = False
status = ""
voter_guide = VoterGuide()
if positive_value_exists(twitter_profile_image_url_https):
image_url = twitter_profile_image_url_https
elif positive_value_exists(facebook_profile_image_url_https):
image_url = facebook_profile_image_url_https
if organization:
voter_guide_list_manager = VoterGuideListManager()
results = voter_guide_list_manager.retrieve_all_voter_guides_by_organization_we_vote_id(
organization.we_vote_id, read_only=False)
voter_guide_list = results['voter_guide_list']
if positive_value_exists(results['voter_guide_list_found']):
for voter_guide in voter_guide_list:
voter_guide.image_url = image_url
voter_guide.we_vote_hosted_profile_image_url_large = ''
voter_guide.we_vote_hosted_profile_image_url_medium = ''
voter_guide.we_vote_hosted_profile_image_url_tiny = ''
voter_guide.save()
success = True
status += " RESET_ORG_IMAGE_DETAILS-EARLIER VERSION"
else:
success = True
status += "NO_VOTER_GUIDES_FOUND_FOR_RESET_IMAGE_DETAILS"
results = {
'success': success,
'status': status,
'organization': organization,
'voter_guide': voter_guide,
}
return results
def update_organization_voter_guides_with_organization_data(self, organization):
"""
Update voter_guide entry with the latest information from an organization
"""
success = False
status = ""
voter_guides_updated = 0
if organization:
voter_guide_list_manager = VoterGuideListManager()
voter_manager = VoterManager()
results = voter_guide_list_manager.retrieve_all_voter_guides_by_organization_we_vote_id(
organization.we_vote_id, read_only=False)
if positive_value_exists(results['voter_guide_list_found']):
voter_guide_list = results['voter_guide_list']
for voter_guide in voter_guide_list:
# Note that "refresh_one_voter_guide_from_organization" doesn't save changes
refresh_results = self.refresh_one_voter_guide_from_organization(voter_guide, organization)
if positive_value_exists(refresh_results['values_changed']) \
or not positive_value_exists(voter_guide.voter_we_vote_id):
voter_guide = refresh_results['voter_guide']
if not positive_value_exists(voter_guide.voter_we_vote_id):
voter_results = voter_manager.retrieve_voter_by_organization_we_vote_id(
organization.we_vote_id)
if voter_results['voter_found']:
try:
voter_we_vote_id = voter_results['voter'].we_vote_id
voter_guide.voter_we_vote_id = voter_we_vote_id
except Exception as e:
status += 'COULD_NOT_RETRIEVE_VOTER_WE_VOTE_ID ' + str(e) + ' '
voter_guide.save()
success = True
voter_guides_updated += 1
status += "UPDATED_VOTER_GUIDES_WITH_ORG_DATA: " + str(voter_guides_updated) + " "
results = {
'success': success,
'status': status,
'organization': organization,
}
return results
def update_voter_guide_social_media_statistics(self, organization):
"""
Update voter_guide entry with details retrieved from Twitter, Facebook, or ???
DALE 2017-11-06 This function needs to be refactored
"""
success = False
status = ""
voter_guide = VoterGuide()
if organization:
if positive_value_exists(organization.twitter_followers_count):
results = self.retrieve_most_recent_voter_guide_for_org(organization_we_vote_id=organization.we_vote_id)
if results['voter_guide_found']:
voter_guide = results['voter_guide']
if positive_value_exists(voter_guide.id):
refresh_results = self.refresh_one_voter_guide_from_organization(voter_guide, organization)
if positive_value_exists(refresh_results['values_changed']):
voter_guide = refresh_results['voter_guide']
voter_guide.save()
success = True
status += " SAVED_ORG_TWITTER_DETAILS"
else:
success = True
status = " NO_CHANGES_SAVED_TO_ORG_TWITTER_DETAILS"
voter_guide_list_manager = VoterGuideListManager()
results = voter_guide_list_manager.retrieve_all_voter_guides_by_organization_we_vote_id(
organization.we_vote_id, read_only=False)
if positive_value_exists(results['voter_guide_list_found']):
voter_guide_list = results['voter_guide_list']
for voter_guide in voter_guide_list:
if not positive_value_exists(voter_guide.we_vote_hosted_profile_image_url_large) or \
not positive_value_exists(voter_guide.we_vote_hosted_profile_image_url_medium) or \
not positive_value_exists(voter_guide.we_vote_hosted_profile_image_url_tiny):
refresh_results = self.refresh_one_voter_guide_from_organization(voter_guide, organization)
if positive_value_exists(refresh_results['values_changed']):
voter_guide = refresh_results['voter_guide']
voter_guide.save()
success = True
status += " SAVED_ORG_TWITTER_DETAILS-EARLIER VERSION"
results = {
'success': success,
'status': status,
'organization': organization,
'voter_guide': voter_guide,
}
return results
def refresh_one_voter_guide_from_organization(self, voter_guide, organization):
"""
This function does not save voter_guide
:param voter_guide:
:param organization:
:return:
"""
success = True
status = ""
values_changed = False
if voter_guide.display_name != organization.organization_name:
voter_guide.display_name = organization.organization_name
values_changed = True
if voter_guide.voter_guide_owner_type != organization.organization_type:
voter_guide.voter_guide_owner_type = organization.organization_type
values_changed = True
if voter_guide.twitter_followers_count != organization.twitter_followers_count:
voter_guide.twitter_followers_count = organization.twitter_followers_count
values_changed = True
if voter_guide.twitter_description != organization.twitter_description:
voter_guide.twitter_description = organization.twitter_description
values_changed = True
if voter_guide.twitter_handle != organization.organization_twitter_handle:
voter_guide.twitter_handle = organization.organization_twitter_handle
values_changed = True
if voter_guide.image_url != organization.organization_photo_url():
voter_guide.image_url = organization.organization_photo_url()
values_changed = True
if voter_guide.we_vote_hosted_profile_image_url_large != \
organization.we_vote_hosted_profile_image_url_large:
voter_guide.we_vote_hosted_profile_image_url_large = \
organization.we_vote_hosted_profile_image_url_large
values_changed = True
if voter_guide.we_vote_hosted_profile_image_url_medium != \
organization.we_vote_hosted_profile_image_url_medium:
voter_guide.we_vote_hosted_profile_image_url_medium = \
organization.we_vote_hosted_profile_image_url_medium
values_changed = True
if voter_guide.we_vote_hosted_profile_image_url_tiny != \
organization.we_vote_hosted_profile_image_url_tiny:
voter_guide.we_vote_hosted_profile_image_url_tiny = \
organization.we_vote_hosted_profile_image_url_tiny
values_changed = True
if positive_value_exists(voter_guide.google_civic_election_id) \
and not positive_value_exists(voter_guide.election_day_text):
election_manager = ElectionManager()
election_results = election_manager.retrieve_election(voter_guide.google_civic_election_id)
if election_results['election_found']:
election = election_results['election']
voter_guide.election_day_text = election.election_day_text
values_changed = True
results = {
'values_changed': values_changed,
'voter_guide': voter_guide,
'status': status,
'success': success,
}
return results
def delete_voter_guide(self, voter_guide_id):
voter_guide_id = convert_to_int(voter_guide_id)
voter_guide_deleted = False
try:
if voter_guide_id:
results = self.retrieve_voter_guide(voter_guide_id)
if results['voter_guide_found']:
voter_guide = results['voter_guide']
voter_guide_id = voter_guide.id
voter_guide.delete()
voter_guide_deleted = True
except Exception as e:
handle_exception(e, logger=logger)
results = {
'success': voter_guide_deleted,
'voter_guide_deleted': voter_guide_deleted,
'voter_guide_id': voter_guide_id,
}
return results
def merge_duplicate_voter_guides_for_organization_and_election(
self, organization_we_vote_id, google_civic_election_id):
success = True
status = ''
number_of_voter_guides_found = 0
number_of_voter_guides_deleted = 0
voter_guide_found = False
voter_guide = None
voter_guide_list = []
# Make sure there are both variables
if not organization_we_vote_id or not google_civic_election_id:
success = False
status += 'MERGE_DUPLICATE_VOTER_GUIDES_MISSING_REQUIRED_VARIABLE '
results = {
'success': success,
'status': status,
'number_of_voter_guides_found': number_of_voter_guides_found,
'number_of_voter_guides_deleted': number_of_voter_guides_deleted,
'voter_guide_found': voter_guide_found,
'voter_guide': voter_guide,
}
return results
# Find full list of voter guides
try:
voter_guide_query = VoterGuide.objects.order_by('pk')
voter_guide_query = voter_guide_query.exclude(vote_smart_ratings_only=True)
voter_guide_query = voter_guide_query.filter(organization_we_vote_id=organization_we_vote_id)
voter_guide_list = voter_guide_query.filter(google_civic_election_id=google_civic_election_id)
number_of_voter_guides_found = len(voter_guide_list)
if positive_value_exists(number_of_voter_guides_found):
status += 'MERGE_DUPLICATE_VOTER_GUIDES_VOTER_GUIDES_FOUND: ' + str(number_of_voter_guides_found) + ' '
else:
status += 'MERGE_DUPLICATE_VOTER_GUIDES_NO_VOTER_GUIDES_FOUND '
success = True
except Exception as e:
status += 'merge_duplicate_voter_guides_for_organization_and_election: ' \
'Unable to retrieve voter guides from db. ' \
'{error} [type: {error_type}] '.format(error=e, error_type=type(e))
success = False
# If there are more than one voter_guides found, delete extras after merging in data
new_master_voter_guide = None
if success:
is_first_voter_guide = True
if number_of_voter_guides_found > 1:
for one_voter_guide in voter_guide_list:
if is_first_voter_guide:
new_master_voter_guide = one_voter_guide
is_first_voter_guide = False
continue
else:
new_master_voter_guide_changed = False
# Take oldest one, and add any new information from the later entries
# Merge in duplicate voter guide before deleting
try:
if not new_master_voter_guide.display_name:
new_master_voter_guide_changed = True
new_master_voter_guide.display_name = one_voter_guide.display_name
if not new_master_voter_guide.election_day_text:
new_master_voter_guide_changed = True
new_master_voter_guide.election_day_text = one_voter_guide.election_day_text
if not new_master_voter_guide.state_code:
new_master_voter_guide_changed = True
new_master_voter_guide.state_code = one_voter_guide.state_code
if new_master_voter_guide.vote_smart_ratings_only:
new_master_voter_guide_changed = True
new_master_voter_guide.vote_smart_ratings_only = False
if new_master_voter_guide_changed:
new_master_voter_guide.save()
one_voter_guide.delete()
number_of_voter_guides_deleted += 1
except Exception as e:
status += 'MERGE_DUPLICATE_VOTER_GUIDES-MERGE_FAILED {error} [type: {error_type}] ' \
''.format(error=e, error_type=type(e))
success = False
if success:
if number_of_voter_guides_found == 1:
status += 'ONE_FOUND '
voter_guide_found = True
voter_guide = voter_guide_list[0]
else:
status += 'VOTER_GUIDES_MERGED '
voter_guide_found = True
voter_guide = new_master_voter_guide
results = {
'success': success,
'status': status,
'number_of_voter_guides_found': number_of_voter_guides_found,
'number_of_voter_guides_deleted': number_of_voter_guides_deleted,
'voter_guide_found': voter_guide_found,
'voter_guide': voter_guide,
}
return results
def save_voter_guide_object(self, voter_guide):
"""
"""
try:
voter_guide.save()