Repository navigation
Expand file tree
/
Copy pathviews_admin.py
More file actions
2675 lines (2400 loc) · 151 KB
/
Copy pathviews_admin.py
File metadata and controls
2675 lines (2400 loc) · 151 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/views_admin.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-
from .controllers import augment_with_voter_guide_possibility_position_data, \
convert_candidate_endorsement_list_light_to_possible_endorsement_list, \
convert_organization_endorsement_list_light_to_possible_endorsement_list, \
convert_list_of_names_to_possible_endorsement_list, \
extract_import_position_list_from_voter_guide_possibility, \
extract_voter_guide_possibility_position_list_from_database, \
fix_sequence_of_possible_endorsement_list, \
match_endorsement_list_with_candidates_in_database, match_endorsement_list_with_measures_in_database, \
match_endorsement_list_with_organizations_in_database, modify_one_row_in_possible_endorsement_list, \
refresh_existing_voter_guides, take_in_possible_endorsement_list_from_form, voter_guides_import_from_master_server
from .controllers_possibility import candidates_found_on_url, organizations_found_on_url
from .models import INDIVIDUAL, VoterGuide, VoterGuideListManager, VoterGuideManager, VoterGuidePossibility, \
VoterGuidePossibilityManager, VoterGuidePossibilityPosition, \
ORGANIZATION_ENDORSING_CANDIDATES, ENDORSEMENTS_FOR_CANDIDATE, UNKNOWN_TYPE
from admin_tools.views import redirect_to_sign_in_page
from candidate.controllers import find_candidate_endorsements_on_one_candidate_web_page, \
find_organization_endorsements_of_candidates_on_one_web_page, retrieve_candidate_list_for_entire_year
from candidate.models import CandidateManager, CandidateListManager
from config.base import get_environment_variable
from datetime import date, datetime, timedelta, time
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.urls import reverse
from django.contrib.messages import get_messages
from django.http import HttpResponseRedirect
from django.db.models import Q
from django.shortcuts import render
from election.controllers import retrieve_this_years_election_id_list
from election.models import Election, ElectionManager, TIME_SPAN_LIST
from import_export_batches.models import BATCH_HEADER_MAP_FOR_POSITIONS, BatchManager, POSITION
from import_export_twitter.controllers import refresh_twitter_organization_details, scrape_social_media_from_one_site
from issue.models import IssueListManager
from measure.controllers import add_measure_name_alternatives_to_measure_list_light, \
retrieve_measure_list_for_all_upcoming_elections
from office.models import ContestOfficeManager
from organization.controllers import retrieve_organization_list_for_all_upcoming_elections
from organization.models import GROUP, Organization, OrganizationListManager, OrganizationManager, ORGANIZATION_TYPE_MAP
from organization.views_admin import organization_edit_process_view
from position.models import PositionEntered, PositionForFriends, PositionListManager
from twitter.models import TwitterUserManager
from voter.models import voter_has_authority, VoterManager, fetch_voter_we_vote_id_from_voter_device_link
from wevote_functions.functions import convert_to_int, convert_date_to_we_vote_date_string, \
extract_facebook_username_from_text_string, \
extract_twitter_handle_from_text_string, extract_website_from_url, positive_value_exists, \
STATE_CODE_MAP, get_voter_device_id, get_voter_api_device_id
from wevote_settings.models import RemoteRequestHistoryManager, SUGGESTED_VOTER_GUIDE_FROM_PRIOR
from django.http import HttpResponse
import json
VOTER_GUIDES_SYNC_URL = get_environment_variable("VOTER_GUIDES_SYNC_URL") # voterGuidesSyncOut
WE_VOTE_SERVER_ROOT_URL = get_environment_variable("WE_VOTE_SERVER_ROOT_URL")
@login_required
def create_possible_voter_guides_from_prior_elections_view(request):
# admin, analytics_admin, partner_organization, political_data_manager, political_data_viewer, verified_volunteer
authority_required = {'political_data_manager'}
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
# From Prior Elections - from_prior_election
# Endorsements from these urls are not the same from election to election, so bringing them forward
# to the current election is not helpful to the political data team
domains_to_not_consider = [
'adirondackdailyenterprise.com', 'about:blank', 'alternet.org', 'apnews.com', 'apple.com',
'baltimoresun.com', 'billboard.com', 'bloomberg.com', 'boston.com', 'bostonglobe.com',
'broadwayworld.com', 'buffalonews.com', 'businessinsider.com', 'buzzfeed.com',
'charlotteobserver.com', 'chicagotibune.com', 'cnbc.com', 'cnn.com',
'dailydot.com', 'dailykos.com', 'dallasnews.com', 'democracynow.org', 'denverpost.com',
'desmoinesregister.com', 'dispatch.com',
'docs.google.com', 'drive.google.com',
'essence.com',
'facebook.com', 'foxbusiness.com', 'foxnews.com',
'hollywoodreporter.com', 'houstonchronicle.com', 'huffpost.com',
'indystar.com', 'instagram.com',
'gayly.com',
'kansascity.com', 'kentucky.com', 'ksat.com',
'latimes.com', 'localhost:8000',
'mercurynews.com', 'miaminewtimes.com', 'motherjones.com', 'msnbc.com',
'nationalreview.com', 'nbcnews.com', 'newsweek.com', 'npr.org', 'nydailynews.com', 'nypost.com', 'nytimes.com',
'ocregister.org', 'opensecrets.org', 'orlandosentinel.com',
'palmbeachpost.com', 'people.com', 'politico.com',
'reviewjournal.com', 'rollingstone.com',
'sacbee.com', 'sfchronicle.com', 'snewsnet.com', 'spectator.us', 'sun-sentinel.com', 'suntimes.com',
'http://t.co', 'https://t.co', 'tampabay.com', 'techcrunch.com', 'texastribune.com', 'thehill.com',
'thenation.com', 'thestate.com', 'twitter.com',
'usatoday.com',
'vox.com',
'wapo.st', 'washingtonpost.com', 'westword.com', 'wsj.com',
'youtu.be', 'youtube.com',
]
voter_device_id = get_voter_device_id(request) # We look in the cookies for voter_api_device_id
voter_manager = VoterManager()
voter_who_submitted_name = ''
voter_who_submitted_we_vote_id = ''
voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
if voter_results['voter_found']:
voter = voter_results['voter']
voter_who_submitted_name = voter.get_full_name()
voter_who_submitted_we_vote_id = voter.we_vote_id
google_civic_election_id = convert_to_int(request.GET.get('google_civic_election_id', 0))
target_google_civic_election_id = 0
limit = 3 # While in development
status = ""
urls_already_stored = []
voter_guide_list = []
voter_guides_suggested = 0
position_list_manager = PositionListManager()
remote_request_history_manager = RemoteRequestHistoryManager()
voter_guide_possibility_manager = VoterGuidePossibilityManager()
# Cycle through prior voter guides
# Earlier than today, but not older than 5 years ago
today = datetime.now().date()
we_vote_date_string_today = convert_date_to_we_vote_date_string(today)
five_years_of_days = 5 * 365
five_years = timedelta(days=five_years_of_days)
five_years_ago = today - five_years
we_vote_date_string_five_years_ago = convert_date_to_we_vote_date_string(five_years_ago)
# Upcoming national election?
election_manager = ElectionManager()
if positive_value_exists(google_civic_election_id):
status += "RETRIEVING_VOTER_GUIDES_BY_STATE_FOR_ELECTION_ID "
results = election_manager.retrieve_election(google_civic_election_id)
if results['election_found']:
election = results['election']
target_google_civic_election_id = election.google_civic_election_id
status += "STATE: " + str(election.state_code) + " "
# Get a list of all voter_guides in last 5 years
voter_guide_query = VoterGuide.objects.all()
voter_guide_query = voter_guide_query.order_by('-twitter_followers_count')
voter_guide_query = voter_guide_query.exclude(vote_smart_ratings_only=True)
voter_guide_query = voter_guide_query.filter(state_code__iexact=election.state_code)
voter_guide_query = voter_guide_query.filter(election_day_text__lt=we_vote_date_string_today)
voter_guide_query = voter_guide_query.filter(election_day_text__gte=we_vote_date_string_five_years_ago)
voter_guide_list = list(voter_guide_query)
else:
status += "RETRIEVING_VOTER_GUIDES_BY_NEXT_NATIONAL_ELECTION "
results = election_manager.retrieve_next_national_election()
if results['election_found']:
status += "NATIONAL_ELECTION_FOUND "
national_election = results['election']
target_google_civic_election_id = national_election.google_civic_election_id
# Get a list of all voter_guides in last 5 years
voter_guide_query = VoterGuide.objects.all()
voter_guide_query = voter_guide_query.order_by('-twitter_followers_count')
voter_guide_query = voter_guide_query.exclude(vote_smart_ratings_only=True)
voter_guide_query = voter_guide_query.filter(election_day_text__lt=we_vote_date_string_today)
voter_guide_query = voter_guide_query.filter(election_day_text__gte=we_vote_date_string_five_years_ago)
voter_guide_list = list(voter_guide_query)
else:
status += "UPCOMING_NATIONAL_ELECTION_NOT_FOUND "
if not positive_value_exists(len(voter_guide_list)):
# National election not found, but upcoming state election?
status += "RETRIEVING_VOTER_GUIDES_BY_NEXT_UPCOMING_ELECTION "
results = election_manager.retrieve_next_election_with_state_optional()
if results['election_found']:
status += "UPCOMING_ELECTION_FOUND "
election = results['election']
target_google_civic_election_id = election.google_civic_election_id
status += "STATE: " + str(election.state_code) + " "
# Get a list of all voter_guides in last 5 years
voter_guide_query = VoterGuide.objects.all()
voter_guide_query = voter_guide_query.order_by('-twitter_followers_count')
voter_guide_query = voter_guide_query.exclude(vote_smart_ratings_only=True)
voter_guide_query = voter_guide_query.filter(state_code__iexact=election.state_code)
voter_guide_query = voter_guide_query.filter(election_day_text__lt=we_vote_date_string_today)
voter_guide_query = voter_guide_query.filter(election_day_text__gte=we_vote_date_string_five_years_ago)
voter_guide_list = list(voter_guide_query)
else:
status += "UPCOMING_ELECTION_NOT_FOUND "
if positive_value_exists(len(voter_guide_list)):
for voter_guide in voter_guide_list:
# Check to see if suggested entry has already been created for that org + election
if not positive_value_exists(voter_guide.organization_we_vote_id) \
or not positive_value_exists(target_google_civic_election_id):
# Do not try to proceed
continue
already_exists = remote_request_history_manager.remote_request_history_entry_exists(
SUGGESTED_VOTER_GUIDE_FROM_PRIOR,
google_civic_election_id=target_google_civic_election_id,
organization_we_vote_id=voter_guide.organization_we_vote_id)
if already_exists:
# Do not create another suggested entry if one already exists
continue
# If not, create Suggested entry
# Get a source URL
voter_guide_possibility_url = ""
limit_to_organization_we_vote_ids = [voter_guide.organization_we_vote_id]
position_list = position_list_manager.retrieve_all_positions_for_election(
voter_guide.google_civic_election_id,
public_only=True,
limit_to_organization_we_vote_ids=limit_to_organization_we_vote_ids)
if len(position_list):
for one_position in position_list:
if positive_value_exists(one_position.more_info_url):
voter_guide_possibility_url = one_position.more_info_url
break # Break out of this position loop
if positive_value_exists(voter_guide_possibility_url):
if any(domain in voter_guide_possibility_url.lower() for domain in domains_to_not_consider):
# If this URL is for a domain that always contains "single use" endorsements, don't suggest again
continue
if voter_guide_possibility_url in urls_already_stored:
# Has this URL already been suggested in urls_already_stored
continue
updated_values = {
'from_prior_election': True, # Mark as a possible entry, but don't show on "To Review" page yet
# 'organization_name': organization_name,
# 'organization_twitter_handle': organization_twitter_handle,
'organization_we_vote_id': voter_guide.organization_we_vote_id,
'voter_who_submitted_name': voter_who_submitted_name,
# 'voter_who_submitted_we_vote_id': voter_who_submitted_we_vote_id,
}
results = voter_guide_possibility_manager.update_or_create_voter_guide_possibility(
voter_guide_possibility_url,
target_google_civic_election_id=target_google_civic_election_id,
voter_who_submitted_we_vote_id=voter_who_submitted_we_vote_id,
updated_values=updated_values,
)
if results['voter_guide_possibility_saved']:
voter_guides_suggested += 1
urls_already_stored.append(voter_guide_possibility_url)
history_results = remote_request_history_manager.create_remote_request_history_entry(
SUGGESTED_VOTER_GUIDE_FROM_PRIOR,
target_google_civic_election_id,
organization_we_vote_id=voter_guide.organization_we_vote_id,
)
if positive_value_exists(limit):
# While developing we want to keep a limit on this
if voter_guides_suggested >= limit:
break
messages.add_message(request, messages.INFO,
'{voter_guides_suggested} voter guides suggested for '
'target_google_civic_election_id: {target_google_civic_election_id}. '
'status: {status}'.format(
target_google_civic_election_id=target_google_civic_election_id,
voter_guides_suggested=voter_guides_suggested,
status=status,
))
return HttpResponseRedirect(reverse('voter_guide:voter_guide_possibility_list', args=()) +
"?google_civic_election_id=" + str(google_civic_election_id) +
"&from_prior_election=1")
# We do not require login for this page
def voter_guide_create_view(request):
"""
Allow anyone on the internet to submit a possible voter guide for including with We Vote
:param request:
:return:
"""
# admin, analytics_admin, partner_organization, political_data_manager, political_data_viewer, verified_volunteer
authority_required = {'verified_volunteer'}
has_suggested_voter_guide_rights = voter_has_authority(request, authority_required)
voter_manager = VoterManager()
voter_device_id = get_voter_device_id(request) # We standardize how we take in the voter_device_id
voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
if voter_results['voter_found']:
voter = voter_results['voter']
voter_who_submitted_name = voter.get_full_name()
voter_who_submitted_we_vote_id = voter.we_vote_id
else:
voter_who_submitted_we_vote_id = ""
voter_who_submitted_name = ""
voter_guide_possibility_id = request.GET.get('voter_guide_possibility_id', 0)
# Take in these values, even though they will be overwritten if we've stored a voter_guide_possibility
ballot_items_raw = request.GET.get('ballot_items_raw', "")
candidate_name = request.GET.get('candidate_name', "")
candidate_twitter_handle = request.GET.get('candidate_twitter_handle', "")
candidate_we_vote_id = request.GET.get('candidate_we_vote_id', "")
candidates_missing_from_we_vote = request.GET.get('candidates_missing_from_we_vote', "")
candidates_missing_from_we_vote = positive_value_exists(candidates_missing_from_we_vote) # Force to be boolean
cannot_find_endorsements = request.GET.get('cannot_find_endorsements', False)
capture_detailed_comments = request.GET.get('capture_detailed_comments ', False)
clear_candidate_options = request.GET.get('clear_candidate_options', False)
clear_organization_options = request.GET.get('clear_organization_options', False)
contributor_comments = request.GET.get('contributor_comments', "")
contributor_email = request.GET.get('contributor_email', "")
hide_from_active_review = request.GET.get('hide_from_active_review', False)
ignore_stored_positions = request.GET.get('ignore_stored_positions', False)
internal_notes = request.GET.get('internal_notes', "")
organization_name = request.GET.get('organization_name', "")
organization_twitter_handle = request.GET.get('organization_twitter_handle', "")
organization_we_vote_id = request.GET.get('organization_we_vote_id', "")
state_code = request.GET.get('state_code', "")
target_google_civic_election_id = request.GET.get('target_google_civic_election_id', "")
voter_guide_possibility_url = request.GET.get('voter_guide_possibility_url', "")
if positive_value_exists(candidate_name) or \
positive_value_exists(candidate_twitter_handle) or \
positive_value_exists(candidate_we_vote_id):
is_organization_endorsing_candidates = False
is_list_of_endorsements_for_candidate = True
elif positive_value_exists(organization_name) or \
positive_value_exists(organization_twitter_handle) or \
positive_value_exists(organization_we_vote_id):
is_organization_endorsing_candidates = True
is_list_of_endorsements_for_candidate = False
else:
is_organization_endorsing_candidates = True
is_list_of_endorsements_for_candidate = False
batch_header_id = 0
ignore_this_source = False
candidate = None
candidate_found = False
organization = None
organization_found = False
positions_ready_to_save_as_batch = False
possible_endorsement_list = []
possible_endorsement_list_found = False
voter_guide_possibility_manager = VoterGuidePossibilityManager()
# Figure out the elections we care about
google_civic_election_id_list_this_year = retrieve_this_years_election_id_list()
if positive_value_exists(voter_guide_possibility_id):
try:
voter_guide_possibilities_query = VoterGuidePossibility.objects.all()
voter_guide_possibility = voter_guide_possibilities_query.get(id=voter_guide_possibility_id)
if positive_value_exists(voter_guide_possibility.id):
# Bring the latest VoterGuidePossibility data into local variables
ballot_items_raw = voter_guide_possibility.ballot_items_raw
batch_header_id = voter_guide_possibility.batch_header_id
candidates_missing_from_we_vote = voter_guide_possibility.candidates_missing_from_we_vote
candidate_twitter_handle = voter_guide_possibility.candidate_twitter_handle
candidate_we_vote_id = voter_guide_possibility.candidate_we_vote_id
cannot_find_endorsements = voter_guide_possibility.cannot_find_endorsements
capture_detailed_comments = voter_guide_possibility.capture_detailed_comments
contributor_comments = voter_guide_possibility.contributor_comments
contributor_email = voter_guide_possibility.contributor_email
hide_from_active_review = voter_guide_possibility.hide_from_active_review
ignore_stored_positions = voter_guide_possibility.ignore_stored_positions
ignore_this_source = voter_guide_possibility.ignore_this_source
internal_notes = voter_guide_possibility.internal_notes
organization_name = voter_guide_possibility.organization_name
organization_twitter_handle = voter_guide_possibility.organization_twitter_handle
organization_we_vote_id = voter_guide_possibility.organization_we_vote_id
positions_ready_to_save_as_batch = \
voter_guide_possibility_manager.positions_ready_to_save_as_batch(voter_guide_possibility)
state_code = voter_guide_possibility.state_code
target_google_civic_election_id = voter_guide_possibility.target_google_civic_election_id
voter_who_submitted_we_vote_id = voter_guide_possibility.voter_who_submitted_we_vote_id
voter_guide_possibility_url = voter_guide_possibility.voter_guide_possibility_url
voter_guide_possibility_type = voter_guide_possibility.voter_guide_possibility_type
# ORGANIZATION_ENDORSING_CANDIDATES, ENDORSEMENTS_FOR_CANDIDATE, UNKNOWN_TYPE
if voter_guide_possibility_type == ORGANIZATION_ENDORSING_CANDIDATES \
or voter_guide_possibility_type == UNKNOWN_TYPE:
is_organization_endorsing_candidates = True
is_list_of_endorsements_for_candidate = False
elif voter_guide_possibility_type == ENDORSEMENTS_FOR_CANDIDATE:
is_organization_endorsing_candidates = False
is_list_of_endorsements_for_candidate = True
else:
# Default to this
is_organization_endorsing_candidates = True
is_list_of_endorsements_for_candidate = False
# Fill the possible_endorsement_list with the latest data
# Note that this extraction is limited to 200 possibilities to avoid very slow page loads
results = extract_voter_guide_possibility_position_list_from_database(voter_guide_possibility)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
possible_endorsement_list_found = True
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
if is_organization_endorsing_candidates:
# Match incoming endorsements to candidates already in the database
results = match_endorsement_list_with_candidates_in_database(
possible_endorsement_list,
google_civic_election_id_list=google_civic_election_id_list_this_year,
state_code=state_code)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
# Match incoming endorsements to measures already in the database
results = match_endorsement_list_with_measures_in_database(
possible_endorsement_list,
google_civic_election_id_list=google_civic_election_id_list_this_year,
state_code=state_code)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
else:
# Match possible_endorsement_list to candidates already in the database
results = match_endorsement_list_with_candidates_in_database(
possible_endorsement_list,
google_civic_election_id_list=google_civic_election_id_list_this_year,
state_code=state_code)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
# Match possible_endorsement_list with organizations in database
results = match_endorsement_list_with_organizations_in_database(
possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
except VoterGuidePossibility.DoesNotExist:
pass
election_manager = ElectionManager()
upcoming_election_list = []
results = election_manager.retrieve_upcoming_elections()
if results['success']:
upcoming_election_list = results['election_list']
state_list = STATE_CODE_MAP
sorted_state_list = sorted(state_list.items())
candidate_manager = CandidateManager()
candidate_list_manager = CandidateListManager()
organization_list_manager = OrganizationListManager()
organization_manager = OrganizationManager()
organizations_list = []
owner_of_website_candidate_list = [] # A list of candidates who might be the subject of the webpage
# ###############################
# Find the subject of the page:
# 1) the organization that is making the endorsements, or
# 2) the candidate listing those who endorse the candidate
if is_organization_endorsing_candidates:
if positive_value_exists(organization_we_vote_id):
results = organization_manager.retrieve_organization_from_we_vote_id(organization_we_vote_id)
if results['organization_found']:
organization_found = True
organization = results['organization']
organization_name = organization.organization_name
twitter_user_manager = TwitterUserManager()
organization_twitter_handle = twitter_user_manager.fetch_twitter_handle_from_organization_we_vote_id(
organization_we_vote_id)
elif positive_value_exists(organization_name) or positive_value_exists(organization_twitter_handle) \
and not positive_value_exists(clear_organization_options):
# Search for organizations that match
results = organization_list_manager.organization_search_find_any_possibilities(
organization_name=organization_name,
organization_twitter_handle=organization_twitter_handle
)
if results['organizations_found']:
organizations_list = results['organizations_list']
organizations_count = len(organizations_list)
if organizations_count == 0:
messages.add_message(request, messages.INFO, 'We did not find any organizations that match.')
elif organizations_count == 1:
messages.add_message(request, messages.INFO,
'We found {count} organization that might match.'
''.format(count=organizations_count))
else:
messages.add_message(request, messages.INFO,
'We found {count} organizations that might match.'
''.format(count=organizations_count))
elif positive_value_exists(voter_guide_possibility_url) \
and not positive_value_exists(clear_organization_options):
scrape_results = organizations_found_on_url(voter_guide_possibility_url, state_code)
organizations_list = scrape_results['organization_list']
organization_count = scrape_results['organization_count']
if organization_count == 0:
pass
# messages.add_message(request, messages.INFO, 'We did not find any organizations that match.')
elif organization_count == 1:
messages.add_message(request, messages.INFO,
'We found {count} organization that might match.'
''.format(count=organization_count))
else:
messages.add_message(request, messages.INFO,
'We found {count} organizations that might match.'
''.format(count=organization_count))
else:
# is_list_of_endorsements_for_candidate == True
# #########################
# voter_guide_create_view: Find the candidate who is the subject of this page
if positive_value_exists(candidate_we_vote_id):
results = candidate_manager.retrieve_candidate_from_we_vote_id(candidate_we_vote_id)
if results['candidate_found']:
candidate = results['candidate']
candidate_found = True
candidate_name = candidate.display_candidate_name()
candidate_twitter_handle = candidate.candidate_twitter_handle
elif positive_value_exists(candidate_name) or positive_value_exists(candidate_twitter_handle) \
and not positive_value_exists(clear_candidate_options):
results = candidate_list_manager.retrieve_candidates_from_non_unique_identifiers(
google_civic_election_id_list=google_civic_election_id_list_this_year,
state_code=state_code,
candidate_twitter_handle=candidate_twitter_handle,
candidate_name=candidate_name)
if results['candidate_list_found']:
owner_of_website_candidate_list = results['candidate_list']
owner_of_website_candidate_list_count = len(owner_of_website_candidate_list)
if owner_of_website_candidate_list_count == 0:
messages.add_message(request, messages.INFO, 'We did not find any candidates that match.')
elif owner_of_website_candidate_list_count == 1:
messages.add_message(request, messages.INFO,
'We found {count} organization that might match.'
''.format(count=owner_of_website_candidate_list_count))
else:
messages.add_message(request, messages.INFO,
'We found {count} organizations that might match.'
''.format(count=owner_of_website_candidate_list_count))
elif positive_value_exists(voter_guide_possibility_url) \
and not positive_value_exists(clear_organization_options):
scrape_results = candidates_found_on_url(
voter_guide_possibility_url,
google_civic_election_id_list=google_civic_election_id_list_this_year,
state_code=state_code)
owner_of_website_candidate_list = scrape_results['candidate_list']
owner_of_website_candidate_list_count = scrape_results['candidate_count']
if owner_of_website_candidate_list_count == 0:
pass
# messages.add_message(request, messages.INFO, 'We did not find any organizations that match.')
elif owner_of_website_candidate_list_count == 1:
messages.add_message(request, messages.INFO,
'We found {count} candidate that might match.'
''.format(count=owner_of_website_candidate_list_count))
else:
messages.add_message(request, messages.INFO,
'We found {count} candidates that might match.'
''.format(count=owner_of_website_candidate_list_count))
number_of_ballot_items = voter_guide_possibility_manager.number_of_ballot_items(voter_guide_possibility_id)
possible_endorsement_list_modified = []
positions_stored_count = 0
positions_not_stored_count = 0
for one_possible_endorsement in possible_endorsement_list:
# Augment the information about the election this endorsement is related to
if positive_value_exists(one_possible_endorsement['google_civic_election_id']):
for one_election in upcoming_election_list:
if one_election.google_civic_election_id == one_possible_endorsement['google_civic_election_id']:
one_possible_endorsement['election_name'] = one_election.election_name
one_possible_endorsement['election_day_text'] = one_election.election_day_text
# Identify which endorsements already have positions stored, and attach that data to each possible_endorsement
if positive_value_exists(organization_we_vote_id) \
and 'candidate_we_vote_id' in one_possible_endorsement \
and positive_value_exists(one_possible_endorsement['candidate_we_vote_id']):
position_exists_query = PositionEntered.objects.filter(
organization_we_vote_id=organization_we_vote_id,
candidate_campaign_we_vote_id=one_possible_endorsement['candidate_we_vote_id'])
position_list = list(position_exists_query)
if positive_value_exists(len(position_list)):
one_possible_endorsement['position_we_vote_id'] = position_list[0].we_vote_id
one_possible_endorsement['statement_text_stored'] = position_list[0].statement_text
one_possible_endorsement['position_stance_stored'] = position_list[0].stance
one_possible_endorsement['more_info_url_stored'] = position_list[0].more_info_url
positions_stored_count += 1
else:
positions_not_stored_count += 1
elif positive_value_exists(organization_we_vote_id) \
and 'measure_we_vote_id' in one_possible_endorsement \
and positive_value_exists(one_possible_endorsement['measure_we_vote_id']):
position_exists_query = PositionEntered.objects.filter(
organization_we_vote_id=organization_we_vote_id,
contest_measure_we_vote_id=one_possible_endorsement['measure_we_vote_id'])
position_list = list(position_exists_query)
if positive_value_exists(len(position_list)):
one_possible_endorsement['position_we_vote_id'] = position_list[0].we_vote_id
one_possible_endorsement['statement_text_stored'] = position_list[0].statement_text
one_possible_endorsement['position_stance_stored'] = position_list[0].stance
one_possible_endorsement['more_info_url_stored'] = position_list[0].more_info_url
positions_stored_count += 1
else:
positions_not_stored_count += 1
elif positive_value_exists(candidate_we_vote_id) \
and 'organization_we_vote_id' in one_possible_endorsement \
and positive_value_exists(one_possible_endorsement['organization_we_vote_id']):
position_exists_query = PositionEntered.objects.filter(
organization_we_vote_id=one_possible_endorsement['organization_we_vote_id'],
candidate_campaign_we_vote_id=candidate_we_vote_id)
position_list = list(position_exists_query)
if positive_value_exists(len(position_list)):
one_possible_endorsement['position_we_vote_id'] = position_list[0].we_vote_id
one_possible_endorsement['statement_text_stored'] = position_list[0].statement_text
one_possible_endorsement['position_stance_stored'] = position_list[0].stance
one_possible_endorsement['more_info_url_stored'] = position_list[0].more_info_url
positions_stored_count += 1
else:
positions_not_stored_count += 1
possible_endorsement_list_modified.append(one_possible_endorsement)
messages_on_stage = get_messages(request)
if positive_value_exists(is_organization_endorsing_candidates):
type_of_website = "OrganizationWebsite"
else:
type_of_website = "CandidateWebsite"
template_values = {
'ballot_items_raw': ballot_items_raw,
'batch_header_id': batch_header_id,
'candidate': candidate,
'candidate_found': candidate_found,
'candidate_name': candidate_name,
'candidate_twitter_handle': candidate_twitter_handle,
'candidate_we_vote_id': candidate_we_vote_id,
'owner_of_website_candidate_list': owner_of_website_candidate_list,
'candidates_missing_from_we_vote': candidates_missing_from_we_vote,
'cannot_find_endorsements': cannot_find_endorsements,
'capture_detailed_comments': capture_detailed_comments,
'contributor_comments': contributor_comments,
'contributor_email': contributor_email,
'hide_from_active_review': hide_from_active_review,
'ignore_stored_positions': ignore_stored_positions,
'ignore_this_source': ignore_this_source,
'internal_notes': internal_notes,
'messages_on_stage': messages_on_stage,
'number_of_ballot_items': number_of_ballot_items,
'organization': organization,
'organization_found': organization_found,
'organization_name': organization_name,
'organization_twitter_handle': organization_twitter_handle,
'organization_we_vote_id': organization_we_vote_id,
'organizations_list': organizations_list,
'has_suggested_voter_guide_rights': has_suggested_voter_guide_rights,
'possible_endorsement_list': possible_endorsement_list_modified,
'possible_endorsement_list_found': possible_endorsement_list_found,
'positions_ready_to_save_as_batch': positions_ready_to_save_as_batch,
'positions_stored_count': positions_stored_count,
'positions_not_stored_count': positions_not_stored_count,
'state_code': state_code,
'state_list': sorted_state_list,
'target_google_civic_election_id': target_google_civic_election_id,
'type_of_website': type_of_website,
'upcoming_election_list': upcoming_election_list,
'voter_guide_possibility_id': voter_guide_possibility_id,
'voter_guide_possibility_url': voter_guide_possibility_url,
'voter_who_submitted_name': voter_who_submitted_name,
'voter_who_submitted_we_vote_id': voter_who_submitted_we_vote_id,
}
return render(request, 'voter_guide/voter_guide_create.html', template_values)
# We do not require login for this page
def voter_guide_create_process_view(request):
"""
:param request:
:return:
"""
confirm_delete = request.POST.get('confirm_delete', 0)
voter_guide_possibility_manager = VoterGuidePossibilityManager()
voter_guide_possibility_id = request.POST.get('voter_guide_possibility_id', 0)
# admin, analytics_admin, partner_organization, political_data_manager, political_data_viewer, verified_volunteer
authority_required = {'verified_volunteer'}
has_suggested_voter_guide_rights = voter_has_authority(request, authority_required)
if positive_value_exists(has_suggested_voter_guide_rights):
if positive_value_exists(confirm_delete):
results = voter_guide_possibility_manager.delete_voter_guide_possibility(
voter_guide_possibility_id=voter_guide_possibility_id)
if results['success']:
return HttpResponseRedirect(reverse('voter_guide:voter_guide_create', args=()))
status = ""
all_done_with_entry = request.POST.get('all_done_with_entry', 0)
ballot_items_raw = request.POST.get('ballot_items_raw', "")
ballot_items_additional = request.POST.get('ballot_items_additional', "")
candidates_missing_from_we_vote = request.POST.get('candidates_missing_from_we_vote', "")
candidates_missing_from_we_vote = positive_value_exists(candidates_missing_from_we_vote) # Force to be boolean
candidate_name = request.POST.get('candidate_name', '')
candidate_twitter_handle = request.POST.get('candidate_twitter_handle', '')
candidate_we_vote_id = request.POST.get('candidate_we_vote_id', None)
cannot_find_endorsements = request.POST.get('cannot_find_endorsements', False)
capture_detailed_comments = request.POST.get('capture_detailed_comments', False)
clear_candidate_options = request.POST.get('clear_candidate_options', 0)
clear_organization_options = request.POST.get('clear_organization_options', 0)
contributor_comments = request.POST.get('contributor_comments', "")
contributor_email = request.POST.get('contributor_email', "")
form_submitted = request.POST.get('form_submitted', False)
hide_from_active_review = request.POST.get('hide_from_active_review', False)
ignore_stored_positions = request.POST.get('ignore_stored_positions', False)
ignore_this_source = request.POST.get('ignore_this_source', False)
internal_notes = request.POST.get('internal_notes', "")
organization_name = request.POST.get('organization_name', '')
organization_twitter_handle = request.POST.get('organization_twitter_handle', '')
organization_we_vote_id = request.POST.get('organization_we_vote_id', None)
scan_url_again = request.POST.get('scan_url_again', False)
state_code = request.POST.get('state_code', '')
type_of_website = request.POST.get('type_of_website', 'OrganizationWebsite')
voter_guide_possibility_url = request.POST.get('voter_guide_possibility_url', '')
voter_who_submitted_we_vote_id = request.POST.get('voter_who_submitted_we_vote_id', '')
# Filter incoming data
candidate_twitter_handle = extract_twitter_handle_from_text_string(candidate_twitter_handle)
organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle)
voter_manager = VoterManager()
organization_twitter_followers_count = 0
voter_who_submitted_name = ""
voter_found = False
if not positive_value_exists(voter_who_submitted_we_vote_id):
voter_device_id = get_voter_device_id(request) # We standardize how we take in the voter_device_id
voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
if voter_results['voter_found']:
voter = voter_results['voter']
voter_found = True
voter_who_submitted_name = voter.get_full_name()
voter_who_submitted_we_vote_id = voter.we_vote_id
else:
voter_who_submitted_name = ""
voter_who_submitted_we_vote_id = ""
if not positive_value_exists(voter_who_submitted_we_vote_id):
generate_if_no_value = True
voter_device_id = get_voter_api_device_id(request, generate_if_no_value)
voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
if voter_results['voter_found']:
voter = voter_results['voter']
voter_found = True
voter_who_submitted_name = voter.get_full_name()
voter_who_submitted_we_vote_id = voter.we_vote_id
else:
voter_who_submitted_name = ""
voter_who_submitted_we_vote_id = ""
if not positive_value_exists(voter_found):
generate_if_no_value = True
voter_device_id = get_voter_api_device_id(request, generate_if_no_value)
voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id)
if voter_results['voter_found']:
voter = voter_results['voter']
voter_who_submitted_name = voter.get_full_name()
voter_who_submitted_we_vote_id = voter.we_vote_id
if not positive_value_exists(voter_guide_possibility_url) and positive_value_exists(form_submitted):
messages.add_message(request, messages.ERROR, 'Please include a link to where you found this voter guide.')
if positive_value_exists(clear_organization_options):
organization_we_vote_id = ""
organization_name = ""
organization_twitter_handle = ""
if positive_value_exists(clear_candidate_options):
candidate_we_vote_id = ""
candidate_name = ""
candidate_twitter_handle = ""
# ########################################
# Figure out if we are looking at an organization's page of endorsements
# or a candidate's page of people/organizations endorsing the candidate
candidate_manager = CandidateManager()
candidate_list_manager = CandidateListManager()
organization_manager = OrganizationManager()
twitter_user_manager = TwitterUserManager()
if type_of_website == "CandidateWebsite" or positive_value_exists(candidate_name) or \
positive_value_exists(candidate_twitter_handle) or positive_value_exists(candidate_we_vote_id):
is_organization_endorsing_candidates = False
is_list_of_endorsements_for_candidate = True
else:
is_organization_endorsing_candidates = True
is_list_of_endorsements_for_candidate = False
# Figure out the elections we care about
google_civic_election_id_list_this_year = retrieve_this_years_election_id_list()
# #########################################
# Figure out the Organization making the endorsements or Candidate listing their endorsements
candidate_found = False
organization_found = False
changes_made = False
possible_endorsement_list = []
possible_endorsement_list_found = False
if is_organization_endorsing_candidates:
# First, identify the organization that is the subject of the page we are analyzing
if positive_value_exists(organization_we_vote_id):
results = organization_manager.retrieve_organization_from_we_vote_id(organization_we_vote_id)
if results['organization_found']:
organization = results['organization']
organization_found = True
organization_name = organization.organization_name
organization_twitter_handle = twitter_user_manager.fetch_twitter_handle_from_organization_we_vote_id(
organization_we_vote_id)
organization_twitter_followers_count = organization.twitter_followers_count
if not positive_value_exists(organization_found):
one_organization_found = False
if positive_value_exists(organization_twitter_handle):
results = organization_manager.retrieve_organization_from_twitter_handle(organization_twitter_handle)
if results['organization_found']:
organization = results['organization']
organization_found = True
organization_name = organization.organization_name
organization_twitter_followers_count = organization.twitter_followers_count
organization_we_vote_id = organization.we_vote_id
if not positive_value_exists(organization_found):
results = twitter_user_manager.retrieve_twitter_link_to_organization_from_twitter_handle(
organization_twitter_handle)
if results['twitter_link_to_organization_found']:
twitter_link_to_organization = results['twitter_link_to_organization']
organization_results = organization_manager.retrieve_organization_from_we_vote_id(
twitter_link_to_organization.organization_we_vote_id)
if organization_results['organization_found']:
one_organization_found = True
organization = organization_results['organization']
organization_twitter_followers_count = organization.twitter_followers_count
organization_we_vote_id = organization.we_vote_id
twitter_user_id = 0
twitter_results = twitter_user_manager.retrieve_twitter_user_locally_or_remotely(
twitter_user_id, organization_twitter_handle)
if twitter_results['twitter_user_found']:
twitter_user = twitter_results['twitter_user']
twitter_user_id = twitter_user.twitter_id
if not one_organization_found and positive_value_exists(twitter_user_id):
organization_name = ""
if not positive_value_exists(state_code):
state_code = None
create_results = organization_manager.create_organization(
organization_name=organization_name,
organization_type=GROUP,
organization_twitter_handle=organization_twitter_handle,
state_served_code=state_code)
if create_results['organization_created']:
one_organization_found = True
organization = create_results['organization']
organization_we_vote_id = organization.we_vote_id
# Create TwitterLinkToOrganization
link_results = twitter_user_manager.create_twitter_link_to_organization(
twitter_user_id, organization.we_vote_id)
# Refresh the organization with the Twitter details
refresh_twitter_organization_details(organization, twitter_user_id)
# #########################################
# Figure out the Possible Candidates or Measures from one organization's perspective
possible_endorsement_list_from_form = []
possible_endorsement_list_results = take_in_possible_endorsement_list_from_form(request)
if possible_endorsement_list_results['possible_endorsement_list_found']:
possible_endorsement_list_from_form = possible_endorsement_list_results['possible_endorsement_list']
possible_endorsement_list_found = True
possible_endorsement_list = possible_endorsement_list + possible_endorsement_list_from_form
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
# We will need all candidates for all upcoming elections so we can search the HTML of
# the possible voter guide for these names
all_possible_candidates_list_light_found = False
all_possible_candidates_list_light = []
today = datetime.now().date()
candidate_year = today.year
results = retrieve_candidate_list_for_entire_year(
candidate_year=candidate_year,
limit_to_this_state_code=state_code,
)
if results['candidate_list_found']:
all_possible_candidates_list_light_found = True
all_possible_candidates_list_light = results['candidate_list_light']
# We need all measures for all upcoming elections
all_possible_measures_list_light_found = False
all_possible_measures_list_light = []
if positive_value_exists(google_civic_election_id_list_this_year):
# TODO: Add "shortened_identifier" to the model and this retrieve
results = retrieve_measure_list_for_all_upcoming_elections(
google_civic_election_id_list=google_civic_election_id_list_this_year,
limit_to_this_state_code=state_code)
if results['measure_list_found']:
all_possible_measures_list_light_found = True
all_possible_measures_list_light = results['measure_list_light']
expand_results = add_measure_name_alternatives_to_measure_list_light(all_possible_measures_list_light)
if expand_results['success']:
all_possible_measures_list_light = expand_results['measure_list_light']
ballot_item_list_light = all_possible_candidates_list_light + all_possible_measures_list_light
possible_endorsement_list_from_url_scan = []
first_scan_needed = positive_value_exists(voter_guide_possibility_url) and not possible_endorsement_list_found
scan_url_now = first_scan_needed or positive_value_exists(scan_url_again)
possibilities_found = all_possible_candidates_list_light_found or all_possible_measures_list_light_found
if scan_url_now and possibilities_found:
endorsement_scrape_results = find_organization_endorsements_of_candidates_on_one_web_page(
voter_guide_possibility_url, ballot_item_list_light)
if endorsement_scrape_results['at_least_one_endorsement_found']:
endorsement_list_light = endorsement_scrape_results['endorsement_list_light']
# Remove the candidates or measures from endorsement_list_light we have in possible_endorsement_list
endorsement_list_light_updated = []
for one_endorsement_light in endorsement_list_light:
one_endorsement_light_is_unique = True
for one_possible_endorsement in possible_endorsement_list:
if positive_value_exists(one_endorsement_light['candidate_we_vote_id']) \
and one_endorsement_light['candidate_we_vote_id'] == \
one_possible_endorsement['candidate_we_vote_id']:
one_endorsement_light_is_unique = False
break
elif positive_value_exists(one_endorsement_light['measure_we_vote_id']) \
and one_endorsement_light['measure_we_vote_id'] == \
one_possible_endorsement['measure_we_vote_id']:
one_endorsement_light_is_unique = False
break
if one_endorsement_light_is_unique:
endorsement_list_light_updated.append(one_endorsement_light)
possible_endorsement_list_results = \
convert_candidate_endorsement_list_light_to_possible_endorsement_list(
endorsement_list_light_updated)
if possible_endorsement_list_results['possible_endorsement_list_found']:
possible_endorsement_list_from_url_scan = \
possible_endorsement_list_results['possible_endorsement_list']
possible_endorsement_list_found = True
possible_endorsement_list = possible_endorsement_list + possible_endorsement_list_from_url_scan
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
# If we don't already have a list of possible endorsements, check the raw text entry field
# For subsequent passes, we use ballot_items_additional
possible_endorsement_list_from_ballot_items_raw = []
if not possible_endorsement_list_found and positive_value_exists(ballot_items_raw):
results = break_up_text_into_possible_endorsement_list(
ballot_items_raw, organization_we_vote_id_to_include=organization_we_vote_id)
if results['possible_endorsement_list_found']:
possible_endorsement_list_from_ballot_items_raw = results['possible_endorsement_list']
# possible_endorsement_list_found = True
changes_made = True
possible_endorsement_list = possible_endorsement_list + possible_endorsement_list_from_ballot_items_raw
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
remove_possibility_position_id = request.POST.get('remove_possibility_position_id', 0)
if positive_value_exists(remove_possibility_position_id):
results = modify_one_row_in_possible_endorsement_list(
possible_endorsement_list, remove_possibility_position_id)
if positive_value_exists(results['success']):
possible_endorsement_list = results['possible_endorsement_list']
for remove_possibility_position_id in request.POST.getlist('remove_possibility_position_ids'):
results = modify_one_row_in_possible_endorsement_list(
possible_endorsement_list, remove_possibility_position_id)
if positive_value_exists(results['success']):
possible_endorsement_list = results['possible_endorsement_list']
# Check for additional items to add (For the first pass, we use ballot_items_raw)
if positive_value_exists(ballot_items_additional):
number_of_current_possible_endorsements = len(possible_endorsement_list)
try:
starting_possibility_number = number_of_current_possible_endorsements + 1
results = break_up_text_into_possible_endorsement_list(
ballot_items_additional, starting_possibility_number,
organization_we_vote_id_to_include=organization_we_vote_id)
if results['possible_endorsement_list_found']:
additional_possible_endorsement_list = results['possible_endorsement_list']
possible_endorsement_list = possible_endorsement_list + additional_possible_endorsement_list
results = fix_sequence_of_possible_endorsement_list(possible_endorsement_list)
if results['possible_endorsement_list_found']:
possible_endorsement_list = results['possible_endorsement_list']
changes_made = True
except Exception as e:
# If there is a problem, continue
pass
# Match incoming endorsements to candidates already in the database AND look for new matches
if len(possible_endorsement_list):
# Match possible_endorsement_list to candidates already in the database
results = match_endorsement_list_with_candidates_in_database(
possible_endorsement_list,