-
Notifications
You must be signed in to change notification settings - Fork 542
/
Copy pathviews_admin.py
519 lines (451 loc) · 27.5 KB
/
views_admin.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
# share/views_admin.py
# Brought to you by We Vote. Be good.
# -*- coding: UTF-8 -*-
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.messages import get_messages
from django.db.models import Q
from django.shortcuts import render
from django.utils.timezone import now
import wevote_functions.admin
from admin_tools.views import redirect_to_sign_in_page
from datetime import timedelta
from email_outbound.models import EmailAddress, EmailManager
from sms.models import SMSPhoneNumber
from voter.models import voter_has_authority, VoterManager
from wevote_functions.functions import convert_to_int, get_voter_api_device_id, \
positive_value_exists
from wevote_settings.constants import ELECTION_YEARS_AVAILABLE
from .controllers import update_shared_item_shared_by_info_from_shared_item, \
update_shared_item_statistics_from_shared_link_clicked, update_shared_link_clicked_year_as_integer, \
update_who_shares_from_shared_item, update_who_shares_from_shared_link_clicked
from .models import SharedItem, VoterWhoSharesSummaryAllTime, VoterWhoSharesSummaryOneYear
logger = wevote_functions.admin.get_logger(__name__)
@login_required
def shared_item_list_view(request):
# admin, analytics_admin, partner_organization, political_data_manager, political_data_viewer, verified_volunteer
authority_required = {'admin', 'analytics_admin', 'voter_manager'}
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
exclude_remind_contact = positive_value_exists(request.GET.get('exclude_remind_contact', False))
limit_to_last_90_days = positive_value_exists(request.GET.get('limit_to_last_90_days', False))
number_to_update = convert_to_int(request.GET.get('number_to_update', 10000))
show_shares_with_zero_clicks = positive_value_exists(request.GET.get('show_shares_with_zero_clicks', False))
shared_item_search = request.GET.get('shared_item_search', '')
show_more = positive_value_exists(request.GET.get('show_more', False))
show_this_year = request.GET.get('show_this_year', False)
voter_api_device_id = get_voter_api_device_id(request) # We look in the cookies for voter_api_device_id
voter_manager = VoterManager()
results = voter_manager.retrieve_voter_from_voter_device_id(voter_api_device_id, read_only=True)
voter_id = 0
if results['voter_found']:
voter = results['voter']
voter_id = voter.id
voter_id = convert_to_int(voter_id)
fix_year_as_integer = True
if fix_year_as_integer:
year_results = update_shared_link_clicked_year_as_integer(number_to_update=number_to_update)
if not year_results['success']:
message_to_print = "FAILED update_shared_item_statistics_from_shared_link_clicked: {status}".format(
status=year_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(year_results['shared_link_clicked_updates_remaining']) or \
positive_value_exists(year_results['shared_link_clicked_changed']) or \
positive_value_exists(year_results['shared_link_clicked_not_changed']):
message_to_print = \
"UPDATE_SHARED_ITEM_YEAR_AS_INTEGER: \n" \
"shared_link_clicked_changed: {shared_link_clicked_changed:,}, " \
"shared_link_clicked_not_changed: {shared_link_clicked_not_changed:,}, "\
"shared_link_clicked_updates_remaining: {shared_link_clicked_updates_remaining:,}\n" \
"status: {status}\n" \
"".format(
shared_link_clicked_updates_remaining=year_results['shared_link_clicked_updates_remaining'],
shared_link_clicked_changed=year_results['shared_link_clicked_changed'],
shared_link_clicked_not_changed=year_results['shared_link_clicked_not_changed'],
status=year_results['status'],
)
messages.add_message(request, messages.INFO, message_to_print)
update_statistics = True
if update_statistics:
statistics_results = update_shared_item_statistics_from_shared_link_clicked(number_to_update=number_to_update)
if not statistics_results['success']:
message_to_print = "FAILED update_shared_item_statistics_from_shared_link_clicked: {status}".format(
status=statistics_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(statistics_results['count_updates_remaining']) or \
positive_value_exists(statistics_results['shared_items_changed']) or \
positive_value_exists(statistics_results['shared_items_not_changed']):
message_to_print = \
"UPDATE_SHARED_ITEM_STATISTICS_FROM_SHARED_LINK_CLICKED: \n" \
"shared_items_changed: {shared_items_changed:,}, " \
"shared_items_not_changed: {shared_items_not_changed:,}, "\
"count_updates_remaining: {count_updates_remaining:,}" \
"".format(
count_updates_remaining=statistics_results['count_updates_remaining'],
shared_items_changed=statistics_results['shared_items_changed'],
shared_items_not_changed=statistics_results['shared_items_not_changed'],
)
messages.add_message(request, messages.INFO, message_to_print)
shared_by_results = update_shared_item_shared_by_info_from_shared_item(number_to_update=number_to_update)
if not shared_by_results['success']:
message_to_print = "FAILED update_shared_item_shared_by_info_from_shared_item: {status}".format(
status=shared_by_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(shared_by_results['shared_by_updates_remaining']) or \
positive_value_exists(shared_by_results['shared_items_changed']) or \
positive_value_exists(shared_by_results['shared_items_not_changed']):
message_to_print = \
"UPDATE_SHARED_ITEM_SHARED_BY_INFO_FROM_SHARED_ITEM: \n" \
"shared_items_changed: {shared_items_changed:,}, " \
"shared_items_not_changed: {shared_items_not_changed:,}, "\
"shared_by_updates_remaining: {shared_by_updates_remaining:,}" \
"".format(
shared_by_updates_remaining=shared_by_results['shared_by_updates_remaining'],
shared_items_changed=shared_by_results['shared_items_changed'],
shared_items_not_changed=shared_by_results['shared_items_not_changed'],
)
messages.add_message(request, messages.INFO, message_to_print)
if positive_value_exists(shared_item_search):
# Search for an email address - do not require to be verified
voter_we_vote_ids_with_email_query = EmailAddress.objects.using('readonly').filter(
normalized_email_address__icontains=shared_item_search,
).values_list('voter_we_vote_id', flat=True)
voter_we_vote_ids_with_email = list(voter_we_vote_ids_with_email_query)
# Search for a phone number
voter_we_vote_ids_with_sms_phone_number_query = SMSPhoneNumber.objects.using('readonly').filter(
normalized_sms_phone_number__icontains=shared_item_search,
).values_list('voter_we_vote_id', flat=True)
voter_we_vote_ids_with_sms_phone_number = list(voter_we_vote_ids_with_sms_phone_number_query)
# Now search SharedItem object
shared_item_query = SharedItem.objects.using('readonly').all()
search_words = shared_item_search.split()
for one_word in search_words:
filters = [] # Reset for each search word
new_filter = Q(campaignx_we_vote_id=one_word)
filters.append(new_filter)
new_filter = Q(candidate_we_vote_id=one_word)
filters.append(new_filter)
new_filter = Q(destination_full_url__icontains=one_word)
filters.append(new_filter)
new_filter = Q(measure_we_vote_id=one_word)
filters.append(new_filter)
new_filter = Q(other_voter_display_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(other_voter_first_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(other_voter_last_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(other_voter_we_vote_id=one_word)
filters.append(new_filter)
if len(voter_we_vote_ids_with_email) > 0:
new_filter = Q(other_voter_we_vote_id__in=voter_we_vote_ids_with_email)
filters.append(new_filter)
new_filter = Q(shared_by_voter_we_vote_id__in=voter_we_vote_ids_with_email)
filters.append(new_filter)
if len(voter_we_vote_ids_with_sms_phone_number) > 0:
new_filter = Q(other_voter_we_vote_id__in=voter_we_vote_ids_with_sms_phone_number)
filters.append(new_filter)
new_filter = Q(shared_by_voter_we_vote_id__in=voter_we_vote_ids_with_sms_phone_number)
filters.append(new_filter)
new_filter = Q(other_voter_email_address_text__icontains=one_word)
filters.append(new_filter)
new_filter = Q(office_we_vote_id=one_word)
filters.append(new_filter)
new_filter = Q(shared_by_display_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(shared_by_first_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(shared_by_last_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(shared_by_voter_we_vote_id=one_word)
filters.append(new_filter)
new_filter = Q(shared_message__icontains=one_word)
filters.append(new_filter)
# Add the first query
if len(filters):
final_filters = filters.pop()
# ...and "OR" the remaining items in the list
for item in filters:
final_filters |= item
shared_item_query = shared_item_query.filter(final_filters)
shared_item_query = shared_item_query\
.order_by('-shared_link_clicked_unique_viewer_count', '-shared_link_clicked_count')
else:
shared_item_query = SharedItem.objects\
.order_by('-shared_link_clicked_unique_viewer_count', '-shared_link_clicked_count')
if positive_value_exists(exclude_remind_contact):
shared_item_query = shared_item_query.exclude(is_remind_contact_share=True)
if positive_value_exists(limit_to_last_90_days):
when_process_must_stop = now() - timedelta(days=90)
shared_item_query = shared_item_query.filter(date_first_shared__gt=when_process_must_stop)
show_this_year = 0
elif positive_value_exists(show_this_year):
shared_item_query = shared_item_query.filter(date_first_shared__year=show_this_year)
if not positive_value_exists(show_shares_with_zero_clicks):
shared_item_query = shared_item_query.filter(shared_link_clicked_count__gt=0)
shared_item_list_found_count = shared_item_query.count()
if positive_value_exists(show_more):
shared_item_list = shared_item_query[:1000]
else:
shared_item_list = shared_item_query[:200]
message_to_print = "{count:,} shared items found.".format(count=shared_item_list_found_count)
messages.add_message(request, messages.INFO, message_to_print)
messages_on_stage = get_messages(request)
template_values = {
'election_years_available': ELECTION_YEARS_AVAILABLE,
'exclude_remind_contact': exclude_remind_contact,
'limit_to_last_90_days': limit_to_last_90_days,
'messages_on_stage': messages_on_stage,
'show_shares_with_zero_clicks': show_shares_with_zero_clicks,
'shared_item_list': shared_item_list,
'shared_item_list_found_count': shared_item_list_found_count,
'shared_item_search': shared_item_search,
'show_more': show_more,
'show_this_year': show_this_year,
'voter_id_signed_in': voter_id,
}
return render(request, 'share/shared_item_list.html', template_values)
@login_required
def voter_who_shares_summary_list_view(request):
# admin, analytics_admin, partner_organization, political_data_manager, political_data_viewer, verified_volunteer
authority_required = {'admin', 'voter_manager'}
if not voter_has_authority(request, authority_required):
return redirect_to_sign_in_page(request, authority_required)
exclude_remind_contact = positive_value_exists(request.GET.get('exclude_remind_contact', False))
limit_to_last_90_days = positive_value_exists(request.GET.get('limit_to_last_90_days', False))
number_to_update = convert_to_int(request.GET.get('number_to_update', False))
show_shares_with_zero_clicks = positive_value_exists(request.GET.get('show_shares_with_zero_clicks', False))
voter_summary_search = request.GET.get('voter_summary_search', '')
show_more = positive_value_exists(request.GET.get('show_more', False))
show_this_year = convert_to_int(request.GET.get('show_this_year', 0))
voter_api_device_id = get_voter_api_device_id(request) # We look in the cookies for voter_api_device_id
voter_manager = VoterManager()
results = voter_manager.retrieve_voter_from_voter_device_id(voter_api_device_id, read_only=True)
voter_id = 0
if results['voter_found']:
voter = results['voter']
voter_id = voter.id
voter_id = convert_to_int(voter_id)
update_statistics = True
if update_statistics:
# Update VoterWhoSharesSummaryAllTime based on SharedItem activity
shared_by_results = update_who_shares_from_shared_item(
number_to_update=number_to_update,
table_name='VoterWhoSharesSummaryAllTime',
)
if not shared_by_results['success']:
message_to_print = "FAILED update_who_shares_all_time_from_shared_item: {status}".format(
status=shared_by_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(shared_by_results['sharing_summary_items_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_items_not_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_updates_remaining']):
message_to_print = \
"WHO_SHARES_ALL_TIME_FROM_SHARED_ITEM: \n" \
"sharing_summary_items_changed: {sharing_summary_items_changed:,}, " \
"sharing_summary_items_not_changed: {sharing_summary_items_not_changed:,}, " \
"sharing_summary_updates_remaining: {sharing_summary_updates_remaining:,} \n" \
"shared_by_results['status']: {status}" \
"".format(
status=shared_by_results['status'],
sharing_summary_items_changed=shared_by_results['sharing_summary_items_changed'],
sharing_summary_items_not_changed=shared_by_results['sharing_summary_items_not_changed'],
sharing_summary_updates_remaining=shared_by_results['sharing_summary_updates_remaining'],
)
messages.add_message(request, messages.INFO, message_to_print)
# Update 'VoterWhoSharesSummaryAllTime' based on ShareLinkClicked activity
shared_by_results = update_who_shares_from_shared_link_clicked(
number_to_update=number_to_update,
table_name='VoterWhoSharesSummaryAllTime',
)
if not shared_by_results['success']:
message_to_print = "FAILED update_who_shares_all_time_from_shared_link_clicked: {status}".format(
status=shared_by_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(shared_by_results['sharing_summary_items_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_items_not_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_updates_remaining']):
message_to_print = \
"WHO_SHARES_ALL_TIME_FROM_SHARED_LINK_CLICKED: \n" \
"sharing_summary_items_changed: {sharing_summary_items_changed:,}, " \
"sharing_summary_items_not_changed: {sharing_summary_items_not_changed:,}, " \
"sharing_summary_updates_remaining: {sharing_summary_updates_remaining:,} \n" \
"shared_by_results['status']: {status}" \
"".format(
status=shared_by_results['status'],
sharing_summary_items_changed=shared_by_results['sharing_summary_items_changed'],
sharing_summary_items_not_changed=shared_by_results['sharing_summary_items_not_changed'],
sharing_summary_updates_remaining=shared_by_results['sharing_summary_updates_remaining'],
)
messages.add_message(request, messages.INFO, message_to_print)
# Update VoterWhoSharesSummaryOneYear based on SharedItem activity
shared_by_results = update_who_shares_from_shared_item(
number_to_update=number_to_update,
table_name='VoterWhoSharesSummaryOneYear',
)
if not shared_by_results['success']:
message_to_print = "FAILED update_who_shares_by_year_from_shared_item: {status}".format(
status=shared_by_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(shared_by_results['sharing_summary_items_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_items_not_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_updates_remaining']):
message_to_print = \
"WHO_SHARES_BY_YEAR_FROM_SHARED_ITEM: \n" \
"sharing_summary_items_changed: {sharing_summary_items_changed:,}, " \
"sharing_summary_items_not_changed: {sharing_summary_items_not_changed:,}, " \
"sharing_summary_updates_remaining: {sharing_summary_updates_remaining:,} \n" \
"shared_by_results['status']: {status}" \
"".format(
status=shared_by_results['status'],
sharing_summary_items_changed=shared_by_results['sharing_summary_items_changed'],
sharing_summary_items_not_changed=shared_by_results['sharing_summary_items_not_changed'],
sharing_summary_updates_remaining=shared_by_results['sharing_summary_updates_remaining'],
)
messages.add_message(request, messages.INFO, message_to_print)
# Update 'VoterWhoSharesSummaryOneYear' based on ShareLinkClicked activity
shared_by_results = update_who_shares_from_shared_link_clicked(
number_to_update=number_to_update,
table_name='VoterWhoSharesSummaryOneYear')
if not shared_by_results['success']:
message_to_print = "FAILED update_who_shares_by_year_from_shared_link_clicked: {status}".format(
status=shared_by_results['status']
)
messages.add_message(request, messages.ERROR, message_to_print)
elif positive_value_exists(shared_by_results['sharing_summary_items_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_items_not_changed']) or \
positive_value_exists(shared_by_results['sharing_summary_updates_remaining']):
message_to_print = \
"WHO_SHARES_BY_YEAR_FROM_SHARED_LINK_CLICKED: \n" \
"sharing_summary_items_changed: {sharing_summary_items_changed:,}, " \
"sharing_summary_items_not_changed: {sharing_summary_items_not_changed:,}, " \
"sharing_summary_updates_remaining: {sharing_summary_updates_remaining:,} \n" \
"shared_by_results['status']: {status}" \
"".format(
status=shared_by_results['status'],
sharing_summary_items_changed=shared_by_results['sharing_summary_items_changed'],
sharing_summary_items_not_changed=shared_by_results['sharing_summary_items_not_changed'],
sharing_summary_updates_remaining=shared_by_results['sharing_summary_updates_remaining'],
)
messages.add_message(request, messages.INFO, message_to_print)
if positive_value_exists(show_this_year):
# If filtering by year, use VoterWhoSharesSummaryOneYear object
voter_who_shares_query = VoterWhoSharesSummaryOneYear.objects.using('readonly').all()
voter_who_shares_query = voter_who_shares_query.filter(year_as_integer=show_this_year)
else:
# Otherwise, use VoterWhoSharesSummaryAllTime object
voter_who_shares_query = VoterWhoSharesSummaryAllTime.objects.using('readonly').all()
if positive_value_exists(voter_summary_search):
# Search for an email address - do not require to be verified
voter_we_vote_ids_with_email_query = EmailAddress.objects.using('readonly').filter(
normalized_email_address__icontains=voter_summary_search,
).values_list('voter_we_vote_id', flat=True)
voter_we_vote_ids_with_email = list(voter_we_vote_ids_with_email_query)
# Search for a phone number
voter_we_vote_ids_with_sms_phone_number_query = SMSPhoneNumber.objects.using('readonly').filter(
normalized_sms_phone_number__icontains=voter_summary_search,
).values_list('voter_we_vote_id', flat=True)
voter_we_vote_ids_with_sms_phone_number = list(voter_we_vote_ids_with_sms_phone_number_query)
search_words = voter_summary_search.split()
for one_word in search_words:
filters = [] # Reset for each search word
if len(voter_we_vote_ids_with_email) > 0:
new_filter = Q(voter_we_vote_id__in=voter_we_vote_ids_with_email)
filters.append(new_filter)
if len(voter_we_vote_ids_with_sms_phone_number) > 0:
new_filter = Q(voter_we_vote_id__in=voter_we_vote_ids_with_sms_phone_number)
filters.append(new_filter)
new_filter = Q(shared_by_display_name__icontains=one_word)
filters.append(new_filter)
new_filter = Q(voter_we_vote_id=one_word)
filters.append(new_filter)
# Add the first query
if len(filters):
final_filters = filters.pop()
# ...and "OR" the remaining items in the list
for item in filters:
final_filters |= item
voter_who_shares_query = voter_who_shares_query.filter(final_filters)
else:
# If not searching, don't return entries with only one shared_item and 0 clicks.
# This is because some shared item entries are created automatically and don't mean the person intended
# to share.
voter_who_shares_query = voter_who_shares_query.exclude(
shared_item_count__lte=1,
shared_link_clicked_count=0,
)
if positive_value_exists(limit_to_last_90_days):
when_process_must_stop = now() - timedelta(days=90)
voter_who_shares_query = voter_who_shares_query.filter(shared_link_clicked_count_last_updated__gt=when_process_must_stop)
show_this_year = 0
if not positive_value_exists(show_shares_with_zero_clicks):
voter_who_shares_query = voter_who_shares_query.filter(shared_link_clicked_count__gt=0)
voter_who_shares_query = \
voter_who_shares_query.order_by(
'-shared_link_clicked_unique_viewer_count', '-shared_item_count', '-shared_link_clicked_count')
voter_who_shares_summary_list_found_count = voter_who_shares_query.count()
if positive_value_exists(show_more):
voter_who_shares_summary_list = voter_who_shares_query[:1000]
else:
voter_who_shares_summary_list = voter_who_shares_query[:200]
voter_who_shares_summary_list_modified = []
for voter_who_shares_summary in voter_who_shares_summary_list:
# Now retrieve all shared items to show under this voter summary
shared_item_query = SharedItem.objects.using('readonly').all()
shared_item_query = \
shared_item_query.filter(shared_by_voter_we_vote_id=voter_who_shares_summary.voter_we_vote_id)
shared_item_query = shared_item_query.order_by('-date_first_shared')
# ######################
# To support searching for a specific URL, we would need a SharedItem-specific search box
# search_words = voter_summary_search.split()
# for one_word in search_words:
# filters = [] # Reset for each search word
#
# new_filter = Q(destination_full_url__icontains=one_word)
# filters.append(new_filter)
#
# # Add the first query
# if len(filters):
# final_filters = filters.pop()
#
# # ...and "OR" the remaining items in the list
# for item in filters:
# final_filters |= item
#
# shared_item_query = shared_item_query.filter(final_filters)
if positive_value_exists(exclude_remind_contact):
shared_item_query = shared_item_query.exclude(is_remind_contact_share=True)
if not positive_value_exists(show_shares_with_zero_clicks):
shared_item_query = shared_item_query.filter(shared_link_clicked_count__gt=0)
if positive_value_exists(limit_to_last_90_days):
when_process_must_stop = now() - timedelta(days=90)
shared_item_query = shared_item_query.filter(date_first_shared__gt=when_process_must_stop)
if positive_value_exists(show_this_year):
shared_item_query = shared_item_query.filter(date_first_shared__year=show_this_year)
voter_who_shares_summary.shared_item_list_count = shared_item_query.count()
shared_item_list = shared_item_query[:25]
voter_who_shares_summary.shared_item_list = shared_item_list
voter_who_shares_summary_list_modified.append(voter_who_shares_summary)
message_to_print = "{count:,} voters found who shared.".format(count=voter_who_shares_summary_list_found_count)
messages.add_message(request, messages.INFO, message_to_print)
messages_on_stage = get_messages(request)
template_values = {
'election_years_available': ELECTION_YEARS_AVAILABLE,
'exclude_remind_contact': exclude_remind_contact,
'limit_to_last_90_days': limit_to_last_90_days,
'messages_on_stage': messages_on_stage,
'show_shares_with_zero_clicks': show_shares_with_zero_clicks,
'voter_who_shares_summary_list': voter_who_shares_summary_list_modified,
'voter_who_shares_summary_list_found_count': voter_who_shares_summary_list_found_count,
'voter_summary_search': voter_summary_search,
'show_more': show_more,
'show_this_year': show_this_year,
'voter_id_signed_in': voter_id,
}
return render(request, 'share/voter_who_shares_summary_list.html', template_values)