-
Notifications
You must be signed in to change notification settings - Fork 542
/
Copy pathmodels.py
1612 lines (1519 loc) · 85.4 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
# share/models.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-
from django.db import models
from django.db.models import Q
from django.utils import timezone
from django.utils.timezone import localtime, now
from datetime import datetime, time, timedelta
from organization.models import ORGANIZATION_TYPE_CHOICES, UNKNOWN
import pytz
import sys
from wevote_functions.functions import convert_to_int, generate_random_string, positive_value_exists
class SharedItem(models.Model):
"""
When a voter shares a link to a candidate, measure, office, or a ballot, map
the
"""
# The ending destination -- meaning the link that is being shared
destination_full_url = models.TextField(null=True)
# A short code that is part of the link that is sent out. For example rFx5 as part of https://WeVote.US/-rFx5
shared_item_code_no_opinions = models.CharField(max_length=50, null=True, blank=True, unique=True, db_index=True)
# Code for include_public_positions - Not implemented yet
# shared_item_code_public_opinions = \
# models.CharField(max_length=50, null=True, blank=True, unique=True, db_index=True)
# Code for include_friends_only_positions
shared_item_code_all_opinions = models.CharField(max_length=50, null=True, blank=True, unique=True, db_index=True)
# Returns link to Democracy Challenge URL
shared_item_code_challenge = models.CharField(max_length=50, null=True, unique=True, db_index=True)
# Returns link to /friends/remind URL
shared_item_code_remind_contacts = models.CharField(max_length=50, null=True, unique=True, db_index=True)
# Returns link to /ready URL
shared_item_code_ready = models.CharField(max_length=50, null=True, unique=True, db_index=True)
# Analytics for easy sorting (this is not master data, but calculated from SharedLinkClicked data)
shared_link_clicked_count = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_count_last_updated = models.DateTimeField(null=True)
shared_link_clicked_unique_viewer_count = models.PositiveIntegerField(default=0, null=True, blank=True)
# Analytics for each kind of click offered by a SharedItem
shared_link_clicked_count_all_opinions = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_unique_viewer_count_all_opinions = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_count_no_opinions = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_unique_viewer_count_no_opinions = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_count_ready = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_unique_viewer_count_ready = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_count_remind_contacts = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_link_clicked_unique_viewer_count_remind_contacts = \
models.PositiveIntegerField(default=0, null=True, blank=True)
# secret key to verify ownership of email on first click
email_secret_key = models.CharField(max_length=255, null=True, db_index=True)
# secret key to verify ownership of phone number on first click
sms_secret_key = models.CharField(max_length=255, null=True, db_index=True)
# The voter and organization id of the person initiating the share
shared_by_display_name = models.TextField(blank=True, null=True)
shared_by_first_name = models.CharField(max_length=255, null=True, blank=True)
shared_by_last_name = models.CharField(max_length=255, null=True, blank=True)
shared_by_voter_we_vote_id = models.CharField(max_length=255, null=True, db_index=True)
shared_by_organization_type = models.CharField(
verbose_name="type of org", max_length=2, choices=ORGANIZATION_TYPE_CHOICES, default=UNKNOWN)
shared_by_organization_we_vote_id = models.CharField(max_length=255, null=True, blank=True, db_index=True)
shared_by_state_code = models.CharField(max_length=2, null=True, db_index=True)
shared_by_we_vote_hosted_profile_image_url_large = models.TextField(blank=True, null=True)
shared_by_we_vote_hosted_profile_image_url_medium = models.TextField(blank=True, null=True)
shared_by_we_vote_hosted_profile_image_url_tiny = models.TextField(blank=True, null=True)
shared_message = models.TextField(blank=True, null=True) # Added for remind
# The owner of the custom site this share was from
site_owner_organization_we_vote_id = models.CharField(max_length=255, null=True, blank=False, db_index=True)
google_civic_election_id = models.PositiveIntegerField(default=0, null=True, blank=True)
hide_introduction = models.BooleanField(default=False)
is_ballot_share = models.BooleanField(default=False)
is_campaignx_share = models.BooleanField(default=False)
is_challenge_share = models.BooleanField(default=False)
is_candidate_share = models.BooleanField(default=False)
is_measure_share = models.BooleanField(default=False)
is_office_share = models.BooleanField(default=False)
is_organization_share = models.BooleanField(default=False)
is_ready_share = models.BooleanField(default=False)
is_remind_contact_share = models.BooleanField(default=False)
# When reminding a contact to vote, we save info about them, so we can auto-create account
other_voter_email_address_text = models.TextField(blank=True, null=True, db_index=True)
other_voter_display_name = models.CharField(max_length=255, null=True, blank=True)
other_voter_first_name = models.CharField(max_length=255, null=True, blank=True)
other_voter_last_name = models.CharField(max_length=255, null=True, blank=True)
other_voter_we_vote_id = models.CharField(max_length=255, null=True, db_index=True)
# What is being shared
campaignx_we_vote_id = models.CharField(max_length=255, null=True, blank=True, unique=False)
candidate_we_vote_id = models.CharField(max_length=255, null=True, blank=True, unique=False)
measure_we_vote_id = models.CharField(max_length=255, null=True, blank=True, unique=False)
office_we_vote_id = models.CharField(max_length=255, null=True, blank=True, unique=False)
date_first_shared = models.DateTimeField(null=True, auto_now_add=True, db_index=True)
# We store YYYY as an integer for very fast lookup (ex/ "2017" for permissions in the year 2017)
year_as_integer = models.PositiveIntegerField(null=True, unique=False, db_index=True)
deleted = models.BooleanField(default=False)
# We override the save function to auto-generate date_as_integer
def save(self, *args, **kwargs):
if self.year_as_integer:
self.year_as_integer = convert_to_int(self.year_as_integer)
if self.year_as_integer == "" or self.year_as_integer is None: # If there isn't a value...
self.generate_year_as_integer()
super(SharedItem, self).save(*args, **kwargs)
def generate_year_as_integer(self):
# We want to store the day as an integer for extremely quick database indexing and lookup
datetime_now = localtime(now()).date() # We Vote uses Pacific Time for TIME_ZONE
year_as_string = "{:d}".format(
datetime_now.year,
)
self.year_as_integer = convert_to_int(year_as_string)
return
class SharedPermissionsGranted(models.Model):
"""
Keep track of the permissions a voter has been granted from
clicking a link that has been shared with them.
Note: We use SharedPermissionsGranted to keep track of organizations that are on a voter's radar, even if
include_friends_only_positions is False (which means they can only see PUBLIC_ONLY positions)
"""
# The voter and organization id of the person initiating the share
shared_by_voter_we_vote_id = models.CharField(max_length=255, null=True, db_index=True)
shared_by_organization_type = models.CharField(
verbose_name="type of org", max_length=2, choices=ORGANIZATION_TYPE_CHOICES, default=UNKNOWN)
shared_by_organization_we_vote_id = models.CharField(max_length=255, null=True, blank=True, db_index=True)
# The person being granted the permissions
shared_to_voter_we_vote_id = models.CharField(max_length=255, null=True, db_index=True)
shared_to_organization_we_vote_id = models.CharField(max_length=255, null=True, blank=True, db_index=True)
google_civic_election_id = models.PositiveIntegerField(default=0, null=True, blank=True)
# We store YYYY as an integer for very fast lookup (ex/ "2017" for permissions in the year 2017)
year_as_integer = models.PositiveIntegerField(null=True, unique=False, db_index=True)
date_last_changed = models.DateTimeField(verbose_name='date last changed', null=True, auto_now=True)
# Having an entry in this table assumes include_public_positions is True
include_friends_only_positions = models.BooleanField(default=False)
deleted = models.BooleanField(default=False)
class SharedLinkClicked(models.Model):
"""
Keep track of each time the shared link was clicked
"""
# The ending destination -- meaning the link that is being shared
destination_full_url = models.TextField(null=True)
# The voter and organization id of the person initiating the share
shared_by_voter_we_vote_id = models.CharField(max_length=255, null=True, db_index=True)
shared_by_organization_type = models.CharField(
verbose_name="type of org", max_length=2, choices=ORGANIZATION_TYPE_CHOICES, default=UNKNOWN)
shared_by_organization_we_vote_id = models.CharField(max_length=255, null=True, blank=True, db_index=True)
shared_by_state_code = models.CharField(max_length=2, null=True, db_index=True)
# The person clicking the link
viewed_by_voter_we_vote_id = models.CharField(max_length=255, null=True, db_index=True)
viewed_by_organization_we_vote_id = models.CharField(max_length=255, null=True, blank=True, db_index=True)
viewed_by_state_code = models.CharField(max_length=2, null=True, db_index=True)
# Information about the share item clicked
shared_item_id = models.PositiveIntegerField(default=0, null=True, blank=True)
shared_item_code = models.CharField(max_length=50, null=True, blank=True, db_index=True)
site_owner_organization_we_vote_id = models.CharField(max_length=255, null=True)
include_public_positions = models.BooleanField(default=False)
include_friends_only_positions = models.BooleanField(default=False)
date_clicked = models.DateTimeField(null=True, auto_now_add=True, db_index=True)
# We store YYYY as an integer for very fast lookup (ex/ "2017" for the year 2017)
year_as_integer = models.PositiveIntegerField(null=True, unique=False, db_index=True)
class ShareManager(models.Manager):
def __unicode__(self):
return "ShareManager"
def add_and_remove_email_recipients(
self,
campaignx_we_vote_id='',
email_recipient_list=[],
shared_by_voter_we_vote_id='',
super_share_item_id=0,
):
success = True
status = ''
existing_emails_in_email_recipient_list = []
# Get the existing list of SuperShareEmailList recipients for this super_share_item_id
results = self.retrieve_super_share_email_recipient_list(
super_share_item_id=super_share_item_id,
read_only=False,
)
if results['email_recipient_list_found']:
recipient_list = results['email_recipient_list']
for super_share_email_recipient in recipient_list:
if super_share_email_recipient.email_address_text.lower() not in email_recipient_list:
try:
super_share_email_recipient.delete()
except Exception as e:
status += "DELETE_FAIL: " + str(e) + " "
else:
existing_emails_in_email_recipient_list.append(
super_share_email_recipient.email_address_text.lower())
# At the end, calculate email_recipient_list_to_add by comparing email_recipient_list with
# existing_email_recipient_list. Retrieve/augment data from VoterContactEmail
# and create SuperShareEmailRecipient from email_recipient_list_to_add
email_recipient_list_to_add = list(set(email_recipient_list) - set(existing_emails_in_email_recipient_list))
existing_voter_contact_emails = {}
if len(email_recipient_list_to_add) > 0:
# We need to augment the email addresses we are sending to
from voter.models import VoterManager
voter_manager = VoterManager()
voter_contact_results = voter_manager.retrieve_voter_contact_email_list(
imported_by_voter_we_vote_id=shared_by_voter_we_vote_id,
read_only=True)
if voter_contact_results['voter_contact_email_list_found']:
voter_contact_email_list = voter_contact_results['voter_contact_email_list']
for voter_contact_email in voter_contact_email_list:
existing_voter_contact_emails[voter_contact_email.email_address_text.lower()] = voter_contact_email
for new_email in email_recipient_list_to_add:
if new_email.lower() in existing_voter_contact_emails:
voter_contact_email = existing_voter_contact_emails[new_email.lower()]
google_contact_id = voter_contact_email.google_contact_id
recipient_display_name = voter_contact_email.google_display_name
recipient_first_name = voter_contact_email.google_first_name
recipient_last_name = voter_contact_email.google_last_name
recipient_state_code = voter_contact_email.state_code
else:
google_contact_id = 0
recipient_display_name = ''
recipient_first_name = ''
recipient_last_name = ''
recipient_state_code = ''
defaults = {
'campaignx_we_vote_id': campaignx_we_vote_id,
'google_contact_id': google_contact_id,
'recipient_display_name': recipient_display_name,
'recipient_first_name': recipient_first_name,
'recipient_last_name': recipient_last_name,
'recipient_state_code': recipient_state_code,
'shared_by_voter_we_vote_id': shared_by_voter_we_vote_id,
}
new_results = self.update_or_create_super_share_email_recipient(
email_address_text=new_email,
super_share_item_id=super_share_item_id,
defaults=defaults,
)
results = {
'success': success,
'status': status,
}
return results
def create_shared_link_clicked(
self,
destination_full_url='',
shared_item_code='',
shared_item_id=0,
shared_by_voter_we_vote_id='',
shared_by_organization_type='',
shared_by_organization_we_vote_id='',
site_owner_organization_we_vote_id='',
viewed_by_voter_we_vote_id='',
viewed_by_organization_we_vote_id='',
include_public_positions=False,
include_friends_only_positions=False):
status = ""
try:
include_public_positions = positive_value_exists(include_public_positions)
include_friends_only_positions = positive_value_exists(include_friends_only_positions)
year_as_integer = self.generate_year_as_integer()
shared_link_clicked = SharedLinkClicked.objects.create(
destination_full_url=destination_full_url,
include_public_positions=include_public_positions,
include_friends_only_positions=include_friends_only_positions,
shared_item_code=shared_item_code,
shared_item_id=shared_item_id,
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
shared_by_organization_type=shared_by_organization_type,
shared_by_organization_we_vote_id=shared_by_organization_we_vote_id,
site_owner_organization_we_vote_id=site_owner_organization_we_vote_id,
viewed_by_voter_we_vote_id=viewed_by_voter_we_vote_id,
viewed_by_organization_we_vote_id=viewed_by_organization_we_vote_id,
year_as_integer=year_as_integer,
)
shared_link_clicked_saved = True
success = True
status += "SHARED_LINK_CLICKED_CREATED "
except Exception as e:
shared_link_clicked_saved = False
shared_link_clicked = None
success = False
status += "SHARED_LINK_CLICKED_NOT_CREATED: " + str(e) + ' '
results = {
'success': success,
'status': status,
'shared_link_clicked_saved': shared_link_clicked_saved,
'shared_link_clicked': shared_link_clicked,
}
return results
def update_or_create_shared_item(
self,
destination_full_url='',
force_create_new=False,
shared_by_voter_we_vote_id='',
google_civic_election_id=None,
defaults={}):
create_shared_item_code_no_opinions = False
create_shared_item_code_all_opinions = False
create_shared_item_code_ready = False
create_shared_item_code_remind_contacts = False
is_ballot_share = True \
if 'is_ballot_share' in defaults and positive_value_exists(defaults['is_ballot_share']) else False
is_candidate_share = True \
if 'is_candidate_share' in defaults and positive_value_exists(defaults['is_candidate_share']) else False
is_challenge_share = True \
if 'is_challenge_share' in defaults and positive_value_exists(defaults['is_challenge_share']) else False
is_measure_share = True \
if 'is_measure_share' in defaults and positive_value_exists(defaults['is_measure_share']) else False
is_office_share = True \
if 'is_office_share' in defaults and positive_value_exists(defaults['is_office_share']) else False
is_organization_share = True \
if 'is_organization_share' in defaults and positive_value_exists(defaults['is_organization_share'])\
else False
is_ready_share = True \
if 'is_ready_share' in defaults and positive_value_exists(defaults['is_ready_share']) else False
is_remind_contact_share = True \
if 'is_remind_contact_share' in defaults and positive_value_exists(defaults['is_remind_contact_share']) \
else False
shared_item_code_no_opinions = None
shared_item_code_all_opinions = None
shared_item_code_challenge = None
shared_item_code_ready = None
shared_item_code_remind_contacts = None
shared_item_created = False
shared_item_found = False
status = ""
success = True
if positive_value_exists(google_civic_election_id):
google_civic_election_id = convert_to_int(google_civic_election_id)
else:
google_civic_election_id = 0
if is_ballot_share:
create_shared_item_code_no_opinions = True
create_shared_item_code_all_opinions = True
elif is_candidate_share:
pass
elif is_challenge_share:
if 'shared_item_code_challenge' in defaults and positive_value_exists(
defaults['shared_item_code_challenge']):
# This shared item code is generated as next_invitee_url_code (on the API server),
# and then passed back to API server as invitee_url_code
shared_item_code_challenge = defaults['shared_item_code_challenge']
elif is_measure_share:
pass
elif is_office_share:
pass
elif is_organization_share:
pass
elif is_ready_share:
create_shared_item_code_ready = True
elif is_remind_contact_share:
create_shared_item_code_remind_contacts = True
force_create_new = True
if positive_value_exists(is_remind_contact_share):
# destination_full_url is optional because by for remind_contact_share we only require the
# built-in /ready and /friends/remind links
required_variables = positive_value_exists(shared_by_voter_we_vote_id)
else:
required_variables = positive_value_exists(shared_by_voter_we_vote_id) and \
positive_value_exists(destination_full_url)
if not positive_value_exists(required_variables):
if positive_value_exists(is_remind_contact_share):
status += "CREATE_OR_UPDATE_SHARED_ITEM-MISSING_REMIND_CONTACT_REQUIRED_VARIABLE "
else:
status += "CREATE_OR_UPDATE_SHARED_ITEM-MISSING_REQUIRED_VARIABLES "
results = {
'success': False,
'status': status,
'shared_item_found': shared_item_found,
'shared_item_created': shared_item_created,
'shared_item': None,
}
return results
if is_challenge_share:
results = self.retrieve_shared_item(
destination_full_url=destination_full_url,
shared_item_code=shared_item_code_challenge,
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
google_civic_election_id=google_civic_election_id,
read_only=False)
shared_item_found = results['shared_item_found']
shared_item = results['shared_item']
status += results['status']
elif force_create_new:
shared_item = None
shared_item_found = False
else:
results = self.retrieve_shared_item(
destination_full_url=destination_full_url,
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
google_civic_election_id=google_civic_election_id,
read_only=False)
shared_item_found = results['shared_item_found']
shared_item = results['shared_item']
success = results['success']
status += results['status']
if shared_item_found:
if positive_value_exists(shared_item.shared_item_code_no_opinions):
create_shared_item_code_no_opinions = False
elif positive_value_exists(shared_item.shared_item_code_all_opinions):
create_shared_item_code_all_opinions = False
elif positive_value_exists(shared_item.shared_item_code_ready):
create_shared_item_code_ready = False
elif positive_value_exists(shared_item.shared_item_code_remind_contacts):
create_shared_item_code_remind_contacts = False
elif not positive_value_exists(defaults['shared_by_organization_we_vote_id']):
pass
if create_shared_item_code_no_opinions:
random_string = generate_random_string(6)
# TODO: Confirm its not in use
shared_item_code_no_opinions = random_string
elif create_shared_item_code_all_opinions:
random_string = generate_random_string(10)
# TODO: Confirm its not in use
shared_item_code_all_opinions = random_string
elif 'shared_item_code_challenge' in defaults and positive_value_exists(defaults['shared_item_code_challenge']):
# This shared item code is generated as next_invitee_url_code (on the API server),
# and then passed back to API server as invitee_url_code
shared_item_code_challenge = defaults['shared_item_code_challenge']
elif create_shared_item_code_ready:
random_string = generate_random_string(8)
# TODO: Confirm its not in use
shared_item_code_ready = random_string
elif create_shared_item_code_remind_contacts:
random_string = generate_random_string(8)
# TODO: Confirm its not in use
shared_item_code_remind_contacts = random_string
email_secret_key = defaults['email_secret_key'] if 'email_secret_key' in defaults else None
other_voter_email_address_text = defaults['other_voter_email_address_text'] \
if 'other_voter_email_address_text' in defaults else None
other_voter_display_name = defaults['other_voter_display_name'] \
if 'other_voter_display_name' in defaults else None
other_voter_first_name = defaults['other_voter_first_name'] \
if 'other_voter_first_name' in defaults else None
other_voter_last_name = defaults['other_voter_last_name'] \
if 'other_voter_last_name' in defaults else None
other_voter_we_vote_id = defaults['other_voter_we_vote_id'] if 'other_voter_we_vote_id' in defaults else None
shared_by_display_name = defaults['shared_by_display_name'] if 'shared_by_display_name' in defaults else None
shared_by_first_name = defaults['shared_by_first_name'] if 'shared_by_first_name' in defaults else None
shared_by_last_name = defaults['shared_by_last_name'] if 'shared_by_last_name' in defaults else None
shared_by_we_vote_hosted_profile_image_url_large = \
defaults['shared_by_we_vote_hosted_profile_image_url_large'] \
if 'shared_by_we_vote_hosted_profile_image_url_large' in defaults else None
shared_by_we_vote_hosted_profile_image_url_medium = \
defaults['shared_by_we_vote_hosted_profile_image_url_medium'] \
if 'shared_by_we_vote_hosted_profile_image_url_medium' in defaults else None
shared_by_we_vote_hosted_profile_image_url_tiny = \
defaults['shared_by_we_vote_hosted_profile_image_url_tiny'] \
if 'shared_by_we_vote_hosted_profile_image_url_tiny' in defaults else None
shared_message = defaults['shared_message'] if 'shared_message' in defaults else None
sms_secret_key = defaults['sms_secret_key'] if 'sms_secret_key' in defaults else None
if shared_item_found:
try:
change_to_save = False
if shared_item.other_voter_display_name != other_voter_display_name:
if not positive_value_exists(other_voter_display_name):
other_voter_display_name = None
shared_item.other_voter_display_name = other_voter_display_name
change_to_save = True
if shared_item.other_voter_first_name != other_voter_first_name:
if not positive_value_exists(other_voter_first_name):
other_voter_first_name = None
shared_item.other_voter_first_name = other_voter_first_name
change_to_save = True
if shared_item.other_voter_last_name != other_voter_last_name:
if not positive_value_exists(other_voter_last_name):
other_voter_last_name = None
shared_item.other_voter_last_name = other_voter_last_name
change_to_save = True
if shared_item.other_voter_email_address_text != other_voter_email_address_text:
if not positive_value_exists(other_voter_email_address_text):
other_voter_email_address_text = None
shared_item.other_voter_email_address_text = other_voter_email_address_text
change_to_save = True
if shared_item.other_voter_we_vote_id != other_voter_we_vote_id:
if not positive_value_exists(other_voter_we_vote_id):
other_voter_we_vote_id = None
shared_item.other_voter_we_vote_id = other_voter_we_vote_id
change_to_save = True
if shared_item.shared_by_display_name != shared_by_display_name:
shared_item.shared_by_display_name = shared_by_display_name
change_to_save = True
if shared_item.shared_by_first_name != shared_by_first_name:
shared_item.shared_by_first_name = shared_by_first_name
change_to_save = True
if shared_item.shared_by_last_name != shared_by_last_name:
shared_item.shared_by_last_name = shared_by_last_name
change_to_save = True
if shared_item.shared_by_we_vote_hosted_profile_image_url_large \
!= shared_by_we_vote_hosted_profile_image_url_large:
shared_item.shared_by_we_vote_hosted_profile_image_url_large = \
shared_by_we_vote_hosted_profile_image_url_large
change_to_save = True
if shared_item.shared_by_we_vote_hosted_profile_image_url_medium \
!= shared_by_we_vote_hosted_profile_image_url_medium:
shared_item.shared_by_we_vote_hosted_profile_image_url_medium = \
shared_by_we_vote_hosted_profile_image_url_medium
change_to_save = True
if shared_item.shared_by_we_vote_hosted_profile_image_url_tiny \
!= shared_by_we_vote_hosted_profile_image_url_tiny:
shared_item.shared_by_we_vote_hosted_profile_image_url_tiny = \
shared_by_we_vote_hosted_profile_image_url_tiny
change_to_save = True
if positive_value_exists(shared_item_code_no_opinions) and \
shared_item.shared_item_code_no_opinions != shared_item_code_no_opinions:
shared_item.shared_item_code_no_opinions = shared_item_code_no_opinions
change_to_save = True
if positive_value_exists(shared_item_code_all_opinions) and \
shared_item.shared_item_code_all_opinions != shared_item_code_all_opinions:
shared_item.shared_item_code_all_opinions = shared_item_code_all_opinions
change_to_save = True
if positive_value_exists(shared_item_code_challenge) and \
shared_item.shared_item_code_challenge != shared_item_code_challenge:
shared_item.shared_item_code_challenge = shared_item_code_challenge
change_to_save = True
if positive_value_exists(shared_item_code_ready) and \
shared_item.shared_item_code_ready != shared_item_code_ready:
shared_item.shared_item_code_ready = shared_item_code_ready
change_to_save = True
if positive_value_exists(shared_item_code_remind_contacts) and \
shared_item.shared_item_code_remind_contacts != shared_item_code_remind_contacts:
shared_item.shared_item_code_remind_contacts = shared_item_code_remind_contacts
change_to_save = True
if shared_item.shared_message != shared_message:
if not positive_value_exists(shared_message):
shared_message = None
shared_item.shared_message = shared_message
change_to_save = True
if not positive_value_exists(shared_item.year_as_integer):
shared_item.generate_year_as_integer()
change_to_save = True
if change_to_save:
shared_item.save()
shared_item_created = True
success = True
status += "SHARED_ITEM_UPDATED "
except Exception as e:
shared_item_created = False
shared_item = None
success = False
status += "SHARED_ITEM_NOT_UPDATED: " + str(e) + " "
else:
try:
candidate_we_vote_id = defaults['candidate_we_vote_id'] if 'candidate_we_vote_id' in defaults else None
measure_we_vote_id = defaults['measure_we_vote_id'] if 'measure_we_vote_id' in defaults else None
office_we_vote_id = defaults['office_we_vote_id'] if 'office_we_vote_id' in defaults else None
shared_by_organization_type = \
defaults['shared_by_organization_type'] if 'shared_by_organization_type' in defaults else UNKNOWN
shared_by_organization_we_vote_id = \
defaults['shared_by_organization_we_vote_id'] if 'shared_by_organization_we_vote_id' in defaults \
else None
site_owner_organization_we_vote_id = \
defaults['site_owner_organization_we_vote_id'] if 'site_owner_organization_we_vote_id' in defaults \
else None
shared_item = SharedItem.objects.create(
candidate_we_vote_id=candidate_we_vote_id,
destination_full_url=destination_full_url,
email_secret_key=email_secret_key,
google_civic_election_id=google_civic_election_id,
is_ballot_share=is_ballot_share,
is_candidate_share=is_candidate_share,
is_challenge_share=is_challenge_share,
is_measure_share=is_measure_share,
is_office_share=is_office_share,
is_organization_share=is_organization_share,
is_ready_share=is_ready_share,
is_remind_contact_share=is_remind_contact_share,
measure_we_vote_id=measure_we_vote_id,
office_we_vote_id=office_we_vote_id,
other_voter_display_name=other_voter_display_name,
other_voter_first_name=other_voter_first_name,
other_voter_last_name=other_voter_last_name,
other_voter_email_address_text=other_voter_email_address_text,
other_voter_we_vote_id=other_voter_we_vote_id,
shared_by_display_name=shared_by_display_name,
shared_by_first_name=shared_by_first_name,
shared_by_last_name=shared_by_last_name,
shared_by_organization_type=shared_by_organization_type,
shared_by_organization_we_vote_id=shared_by_organization_we_vote_id,
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
shared_by_we_vote_hosted_profile_image_url_large=shared_by_we_vote_hosted_profile_image_url_large,
shared_by_we_vote_hosted_profile_image_url_medium=shared_by_we_vote_hosted_profile_image_url_medium,
shared_by_we_vote_hosted_profile_image_url_tiny=shared_by_we_vote_hosted_profile_image_url_tiny,
shared_item_code_no_opinions=shared_item_code_no_opinions,
shared_item_code_all_opinions=shared_item_code_all_opinions,
shared_item_code_challenge=shared_item_code_challenge,
shared_item_code_ready=shared_item_code_ready,
shared_item_code_remind_contacts=shared_item_code_remind_contacts,
shared_message=shared_message,
site_owner_organization_we_vote_id=site_owner_organization_we_vote_id,
sms_secret_key=sms_secret_key,
)
shared_item_created = True
shared_item_found = True
status += "SHARED_ITEM_CREATED "
except Exception as e:
shared_item_created = False
shared_item = None
success = False
status += "SHARED_ITEM_NOT_CREATED: " + str(e) + " "
results = {
'success': success,
'status': status,
'shared_item_found': shared_item_found,
'shared_item_created': shared_item_created,
'shared_item': shared_item,
}
return results
def update_or_create_shared_permissions_granted(
self,
shared_by_voter_we_vote_id='',
shared_by_organization_type='',
shared_by_organization_we_vote_id='',
shared_to_voter_we_vote_id='',
shared_to_organization_we_vote_id='',
google_civic_election_id=None,
year_as_integer=None,
include_friends_only_positions=False):
shared_permissions_granted_created = False
shared_permissions_granted_found = False
status = ""
success = True
if positive_value_exists(google_civic_election_id):
google_civic_election_id = convert_to_int(google_civic_election_id)
else:
google_civic_election_id = 0
if not positive_value_exists(shared_by_voter_we_vote_id) \
or not positive_value_exists(shared_to_voter_we_vote_id) or not positive_value_exists(year_as_integer):
status += "CREATE_OR_UPDATE_SHARED_PERMISSIONS_GRANTED-MISSING_REQUIRED_VARIABLE "
results = {
'success': False,
'status': status,
'shared_permissions_granted_found': shared_permissions_granted_found,
'shared_permissions_granted_created': shared_permissions_granted_created,
'shared_permissions_granted': None,
}
return results
results = self.retrieve_shared_permissions_granted(
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
shared_to_voter_we_vote_id=shared_to_voter_we_vote_id,
google_civic_election_id=google_civic_election_id,
year_as_integer=year_as_integer,
read_only=False)
shared_permissions_granted_found = results['shared_permissions_granted_found']
shared_permissions_granted = results['shared_permissions_granted']
success = results['success']
status += results['status']
if shared_permissions_granted_found:
# Are we going from include_friends_only_positions == False to include_friends_only_positions == True?
include_friends_only_positions_added = \
not positive_value_exists(shared_permissions_granted.include_friends_only_positions) \
and positive_value_exists(include_friends_only_positions)
if not positive_value_exists(shared_permissions_granted.year_as_integer) \
or include_friends_only_positions_added:
# There is a reason to update
try:
change_to_save = False
if not positive_value_exists(shared_permissions_granted.year_as_integer):
shared_permissions_granted.year_as_integer = year_as_integer
change_to_save = True
if include_friends_only_positions_added:
shared_permissions_granted.include_friends_only_positions = True
change_to_save = True
if change_to_save:
shared_permissions_granted.save()
shared_permissions_granted_created = True
success = True
status += "SHARED_PERMISSIONS_GRANTED_UPDATED "
except Exception as e:
shared_permissions_granted_created = False
shared_permissions_granted = None
success = False
status += "SHARED_PERMISSIONS_GRANTED_NOT_UPDATED: " + str(e) + " "
else:
try:
shared_permissions_granted = SharedPermissionsGranted.objects.create(
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
shared_by_organization_type=shared_by_organization_type,
shared_by_organization_we_vote_id=shared_by_organization_we_vote_id,
shared_to_voter_we_vote_id=shared_to_voter_we_vote_id,
shared_to_organization_we_vote_id=shared_to_organization_we_vote_id,
google_civic_election_id=google_civic_election_id,
year_as_integer=year_as_integer,
include_friends_only_positions=include_friends_only_positions,
)
shared_permissions_granted_created = True
shared_permissions_granted_found = True
status += "SHARED_PERMISSIONS_GRANTED_CREATED "
except Exception as e:
shared_permissions_granted_created = False
shared_permissions_granted = None
success = False
status += "SHARED_PERMISSIONS_GRANTED_NOT_CREATED: " + str(e) + " "
results = {
'success': success,
'status': status,
'shared_permissions_granted_found': shared_permissions_granted_found,
'shared_permissions_granted_created': shared_permissions_granted_created,
'shared_permissions_granted': shared_permissions_granted,
}
return results
def update_or_create_super_share_email_recipient(
self,
email_address_text='',
super_share_item_id=0,
defaults={}):
super_share_email_recipient_created = False
super_share_email_recipient_found = False
status = ""
if not positive_value_exists(super_share_item_id) or not positive_value_exists(email_address_text):
status += "CREATE_OR_UPDATE_EMAIL_RECIPIENT-MISSING_REQUIRED_VARIABLE "
results = {
'success': False,
'status': status,
'super_share_email_recipient_found': super_share_email_recipient_found,
'super_share_email_recipient_created': super_share_email_recipient_created,
'super_share_email_recipient': None,
}
return results
results = self.retrieve_super_share_email_recipient_list(
email_address_text=email_address_text,
super_share_item_id=super_share_item_id,
read_only=False)
super_share_email_recipient_found = results['email_recipient_found']
super_share_email_recipient = results['email_recipient']
success = results['success']
status += results['status']
if super_share_email_recipient_found:
try:
change_to_save = False
if 'campaignx_we_vote_id' in defaults:
super_share_email_recipient.campaignx_we_vote_id = defaults['campaignx_we_vote_id']
change_to_save = True
if 'date_sent_to_email' in defaults:
super_share_email_recipient.date_sent_to_email = defaults['date_sent_to_email']
change_to_save = True
if 'google_contact_id' in defaults:
super_share_email_recipient.google_contact_id = defaults['google_contact_id']
change_to_save = True
if 'recipient_first_name' in defaults:
super_share_email_recipient.recipient_first_name = defaults['recipient_first_name']
change_to_save = True
if 'recipient_last_name' in defaults:
super_share_email_recipient.recipient_last_name = defaults['recipient_last_name']
change_to_save = True
if 'recipient_state_code' in defaults:
super_share_email_recipient.recipient_state_code = defaults['recipient_state_code']
change_to_save = True
if 'recipient_voter_we_vote_id' in defaults:
super_share_email_recipient.recipient_voter_we_vote_id = defaults['recipient_voter_we_vote_id']
change_to_save = True
if 'shared_by_voter_we_vote_id' in defaults:
super_share_email_recipient.shared_by_voter_we_vote_id = defaults['shared_by_voter_we_vote_id']
change_to_save = True
if change_to_save:
super_share_email_recipient.save()
super_share_email_recipient_created = True
success = True
status += "SUPER_SHARE_EMAIL_RECIPIENT_UPDATED "
except Exception as e:
super_share_email_recipient_created = False
super_share_email_recipient = None
success = False
status += "SUPER_SHARE_EMAIL_RECIPIENT_NOT_UPDATED: " + str(e) + " "
if success and not super_share_email_recipient_found:
try:
super_share_email_recipient = SuperShareEmailRecipient.objects.create(
campaignx_we_vote_id=defaults['campaignx_we_vote_id']
if 'campaignx_we_vote_id' in defaults else None,
date_sent_to_email=defaults['date_sent_to_email']
if 'date_sent_to_email' in defaults else None,
email_address_text=email_address_text.lower(),
google_contact_id=defaults['google_contact_id']
if 'google_contact_id' in defaults else None,
recipient_display_name=defaults['recipient_display_name']
if 'recipient_display_name' in defaults else None,
recipient_first_name=defaults['recipient_first_name']
if 'recipient_first_name' in defaults else None,
recipient_last_name=defaults['recipient_last_name']
if 'recipient_last_name' in defaults else None,
recipient_state_code=defaults['recipient_state_code']
if 'recipient_state_code' in defaults else None,
recipient_voter_we_vote_id=defaults['recipient_voter_we_vote_id']
if 'recipient_voter_we_vote_id' in defaults else None,
shared_by_voter_we_vote_id=defaults['shared_by_voter_we_vote_id']
if 'shared_by_voter_we_vote_id' in defaults else None,
super_share_item_id=super_share_item_id,
)
super_share_email_recipient_created = True
super_share_email_recipient_found = True
status += "SUPER_SHARE_EMAIL_RECIPIENT_CREATED "
except Exception as e:
super_share_email_recipient_created = False
super_share_email_recipient = None
success = False
status += "SUPER_SHARE_EMAIL_RECIPIENT_NOT_CREATED: " + str(e) + " "
results = {
'success': success,
'status': status,
'super_share_email_recipient_found': super_share_email_recipient_found,
'super_share_email_recipient_created': super_share_email_recipient_created,
'super_share_email_recipient': super_share_email_recipient,
}
return results
def update_or_create_super_share_item(
self,
campaignx_we_vote_id='',
shared_by_voter_we_vote_id='',
super_share_item_id=0,
defaults={}):
super_share_item_created = False
super_share_item_found = False
status = ""
success = True
if not positive_value_exists(campaignx_we_vote_id) or not positive_value_exists(shared_by_voter_we_vote_id):
status += "CREATE_OR_UPDATE_SUPER_SHARE_ITEM-MISSING_REQUIRED_VARIABLE "
results = {
'success': False,
'status': status,
'super_share_item_found': super_share_item_found,
'super_share_item_created': super_share_item_created,
'super_share_item': None,
}
return results
results = self.retrieve_super_share_item(
campaignx_we_vote_id=campaignx_we_vote_id,
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
super_share_item_id=super_share_item_id,
read_only=False)
super_share_item_found = results['super_share_item_found']
super_share_item = results['super_share_item']
success = results['success']
status += results['status']
# if create_super_share_item_code_no_opinions:
# random_string = generate_random_string(6)
# # TODO: Confirm its not in use
# super_share_item_code_no_opinions = random_string
#
# if create_super_share_item_code_all_opinions:
# random_string = generate_random_string(10)
# # TODO: Confirm its not in use
# super_share_item_code_all_opinions = random_string
if super_share_item_found:
try:
change_to_save = False
personalized_message_changed = defaults['personalized_message_changed'] \
if 'personalized_message_changed' in defaults else False
if positive_value_exists(personalized_message_changed):
super_share_item.personalized_message = defaults['personalized_message'] \
if 'personalized_message' in defaults else ''
change_to_save = True
personalized_subject_changed = defaults['personalized_subject_changed'] \
if 'personalized_subject_changed' in defaults else False
if positive_value_exists(personalized_subject_changed):
super_share_item.personalized_subject = defaults['personalized_subject'] \
if 'personalized_subject' in defaults else ''
change_to_save = True
if change_to_save:
super_share_item.save()
super_share_item_created = True
success = True
status += "SUPER_SHARE_ITEM_UPDATED "
except Exception as e:
super_share_item_created = False
super_share_item = None
success = False
status += "SUPER_SHARE_ITEM_NOT_UPDATED: " + str(e) + " "
if success and not super_share_item_found:
try:
super_share_item = SuperShareItem.objects.create(
campaignx_we_vote_id=defaults['campaignx_we_vote_id']
if 'campaignx_we_vote_id' in defaults else None,
destination_full_url=defaults['destination_full_url']
if 'destination_full_url' in defaults else None,
in_draft_mode=defaults['in_draft_mode']
if 'in_draft_mode' in defaults else True,
personalized_message=defaults['personalized_message']
if 'personalized_message' in defaults else None,
personalized_subject=defaults['personalized_subject']
if 'personalized_subject' in defaults else None,
shared_by_voter_we_vote_id=shared_by_voter_we_vote_id,
site_owner_organization_we_vote_id=defaults['site_owner_organization_we_vote_id']
if 'site_owner_organization_we_vote_id' in defaults else None,
)
super_share_item_created = True
super_share_item_found = True
status += "SUPER_SHARE_ITEM_CREATED "
except Exception as e:
super_share_item_created = False
super_share_item = None
success = False
status += "SUPER_SHARE_ITEM_NOT_CREATED: " + str(e) + " "
results = {
'success': success,
'status': status,
'super_share_item_found': super_share_item_found,
'super_share_item_created': super_share_item_created,
'super_share_item': super_share_item,
}
return results
def fetch_shared_link_clicked_unique_sharer_count(
self, shared_by_state_code_list=[], viewed_by_state_code_list=[], year_as_integer_list=[]):
return self.fetch_shared_link_clicked_count(
shared_by_state_code_list=shared_by_state_code_list,
viewed_by_state_code_list=viewed_by_state_code_list,
year_as_integer_list=year_as_integer_list,
field_for_distinct_filter='shared_by_voter_we_vote_id')
def fetch_shared_link_clicked_unique_viewer_count(
self,
shared_by_state_code_list=[],
shared_by_voter_we_vote_id_list=[],
viewed_by_state_code_list=[],
year_as_integer_list=[]):
return self.fetch_shared_link_clicked_count(
shared_by_state_code_list=shared_by_state_code_list,
shared_by_voter_we_vote_id_list=shared_by_voter_we_vote_id_list,
viewed_by_state_code_list=viewed_by_state_code_list,
year_as_integer_list=year_as_integer_list,
field_for_distinct_filter='viewed_by_voter_we_vote_id')
def fetch_shared_link_clicked_shared_links_count(
self, shared_by_state_code_list=[], viewed_by_state_code_list=[], year_as_integer_list=[]):
return self.fetch_shared_link_clicked_count(
shared_by_state_code_list=shared_by_state_code_list,
viewed_by_state_code_list=viewed_by_state_code_list,
year_as_integer_list=year_as_integer_list,
field_for_distinct_filter='shared_item_id')
def fetch_shared_link_clicked_shared_links_click_count(
self,
shared_by_state_code_list=[],
shared_by_voter_we_vote_id_list=[],
viewed_by_state_code_list=[],
year_as_integer_list=[]):
return self.fetch_shared_link_clicked_count(
shared_by_state_code_list=shared_by_state_code_list,
shared_by_voter_we_vote_id_list=shared_by_voter_we_vote_id_list,
viewed_by_state_code_list=viewed_by_state_code_list,
year_as_integer_list=year_as_integer_list,
field_for_distinct_filter='id')
def fetch_shared_items_clicked_count_for_one_day(
self,
date_as_integer=0):
"""
Used for SitewideDailyMetrics
:param date_as_integer:
:return:
"""
return self.fetch_shared_link_clicked_count(
date_as_integer=date_as_integer,
field_for_distinct_filter='shared_item_id')
def fetch_shared_link_clicked_count_for_one_day(
self,
date_as_integer=0):
"""
Used for SitewideDailyMetrics
:param date_as_integer:
:return:
"""
return self.fetch_shared_link_clicked_count(
date_as_integer=date_as_integer,
field_for_distinct_filter='id')