Skip to content

Commit b86a540

Browse files
committed
Added refresh status for state elections on main election_list.html.
1 parent d6b547d commit b86a540

File tree

2 files changed

+134
-23
lines changed

2 files changed

+134
-23
lines changed

election/views_admin.py

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ def election_list_view(request):
897897
new_filter = Q(state_code__icontains=election_search)
898898
filters.append(new_filter)
899899

900+
new_filter = Q(vote_usa_election_id__iexact=election_search)
901+
filters.append(new_filter)
902+
900903
# Add the first query
901904
if len(filters):
902905
final_filters = filters.pop()
@@ -911,6 +914,7 @@ def election_list_view(request):
911914
election_list_modified = []
912915
ballot_returned_list_manager = BallotReturnedListManager()
913916
candidate_list_manager = CandidateListManager()
917+
data_stale_if_older_than = now() - timedelta(days=30)
914918
for election in election_list:
915919
if positive_value_exists(election.election_day_text):
916920
try:
@@ -1000,6 +1004,110 @@ def election_list_view(request):
10001004
# As of Aug 2018 we are no longer using PERCENT_RATING
10011005
position_query = position_query.exclude(stance__iexact='PERCENT_RATING')
10021006
election.public_positions_count = position_query.count()
1007+
1008+
# ############################
1009+
# Figure out the last dates we retrieved data
1010+
refresh_date_started = None
1011+
refresh_date_completed = None
1012+
retrieve_date_started = None
1013+
retrieve_date_completed = None
1014+
try:
1015+
batch_process_queryset = BatchProcess.objects.all()
1016+
batch_process_queryset = \
1017+
batch_process_queryset.filter(google_civic_election_id=election.google_civic_election_id)
1018+
batch_process_queryset = batch_process_queryset.filter(date_started__isnull=False)
1019+
batch_process_queryset = batch_process_queryset.exclude(batch_process_paused=True)
1020+
ballot_item_processes = [
1021+
'REFRESH_BALLOT_ITEMS_FROM_POLLING_LOCATIONS',
1022+
'RETRIEVE_BALLOT_ITEMS_FROM_POLLING_LOCATIONS']
1023+
batch_process_queryset = batch_process_queryset.filter(kind_of_process__in=ballot_item_processes)
1024+
batch_process_queryset = batch_process_queryset.order_by("-id")
1025+
1026+
batch_process_queryset = batch_process_queryset[:3]
1027+
batch_process_list = list(batch_process_queryset)
1028+
1029+
if len(batch_process_list):
1030+
for one_batch_process in batch_process_list:
1031+
if one_batch_process.kind_of_process == 'REFRESH_BALLOT_ITEMS_FROM_POLLING_LOCATIONS':
1032+
if not refresh_date_completed and one_batch_process.date_completed:
1033+
refresh_date_completed = one_batch_process.date_completed
1034+
if not refresh_date_started and one_batch_process.date_started:
1035+
refresh_date_started = one_batch_process.date_started
1036+
elif one_batch_process.kind_of_process == 'RETRIEVE_BALLOT_ITEMS_FROM_POLLING_LOCATIONS':
1037+
if not retrieve_date_completed and one_batch_process.date_completed:
1038+
retrieve_date_completed = one_batch_process.date_completed
1039+
if not retrieve_date_started and one_batch_process.date_started:
1040+
retrieve_date_started = one_batch_process.date_started
1041+
# if refresh_date_completed and retrieve_date_completed:
1042+
# break # Break out of this batch_process loop only
1043+
except BatchProcess.DoesNotExist:
1044+
# No offices found. Not a problem.
1045+
batch_process_list = []
1046+
except Exception as e:
1047+
pass
1048+
1049+
# Upcoming refresh date scheduled?
1050+
refresh_date_added_to_queue = None
1051+
try:
1052+
batch_process_queryset = BatchProcess.objects.all()
1053+
batch_process_queryset = \
1054+
batch_process_queryset.filter(google_civic_election_id=election.google_civic_election_id)
1055+
batch_process_queryset = batch_process_queryset.filter(date_completed__isnull=True)
1056+
batch_process_queryset = batch_process_queryset.exclude(batch_process_paused=True)
1057+
ballot_item_processes = [
1058+
'REFRESH_BALLOT_ITEMS_FROM_POLLING_LOCATIONS']
1059+
batch_process_queryset = batch_process_queryset.filter(kind_of_process__in=ballot_item_processes)
1060+
batch_process_queryset = batch_process_queryset.order_by("-id")
1061+
1062+
batch_process_queryset = batch_process_queryset[:3]
1063+
batch_process_list = list(batch_process_queryset)
1064+
1065+
if len(batch_process_list):
1066+
for one_batch_process in batch_process_list:
1067+
if one_batch_process.kind_of_process == 'REFRESH_BALLOT_ITEMS_FROM_POLLING_LOCATIONS':
1068+
if not refresh_date_added_to_queue and one_batch_process.date_added_to_queue:
1069+
refresh_date_added_to_queue = one_batch_process.date_added_to_queue
1070+
except BatchProcess.DoesNotExist:
1071+
# No offices found. Not a problem.
1072+
batch_process_list = []
1073+
except Exception as e:
1074+
pass
1075+
1076+
# election_for_one_state = copy.deepcopy(national_election)
1077+
election.refresh_date_completed = refresh_date_completed
1078+
election.refresh_date_started = refresh_date_started
1079+
election.refresh_date_added_to_queue = refresh_date_added_to_queue
1080+
election.retrieve_date_completed = retrieve_date_completed
1081+
election.retrieve_date_started = retrieve_date_started
1082+
1083+
if refresh_date_completed:
1084+
most_recent_time = refresh_date_completed
1085+
elif refresh_date_started:
1086+
most_recent_time = refresh_date_started
1087+
elif retrieve_date_completed:
1088+
most_recent_time = retrieve_date_completed
1089+
elif retrieve_date_started:
1090+
most_recent_time = retrieve_date_started
1091+
else:
1092+
most_recent_time = None
1093+
1094+
if most_recent_time:
1095+
if refresh_date_completed and refresh_date_completed > most_recent_time:
1096+
most_recent_time = refresh_date_completed
1097+
if refresh_date_started and refresh_date_started > most_recent_time:
1098+
most_recent_time = refresh_date_started
1099+
if retrieve_date_completed and retrieve_date_completed > most_recent_time:
1100+
most_recent_time = retrieve_date_completed
1101+
if retrieve_date_started and retrieve_date_started > most_recent_time:
1102+
most_recent_time = retrieve_date_started
1103+
1104+
if most_recent_time > data_stale_if_older_than:
1105+
election.data_getting_stale = False
1106+
else:
1107+
election.data_getting_stale = True
1108+
else:
1109+
election.data_getting_stale = True
1110+
10031111
if positive_value_exists(refresh_states):
10041112
if election and positive_value_exists(election.google_civic_election_id) \
10051113
and hasattr(election, 'state_code_list_raw'):
@@ -1081,12 +1189,12 @@ def nationwide_election_list_view(request):
10811189
if not positive_value_exists(is_national_election):
10821190
national_election = None
10831191

1192+
data_stale_if_older_than = now() - timedelta(days=30)
10841193
if is_national_election:
10851194
from election.models import fetch_next_election_for_state, fetch_prior_election_for_state
10861195
state_list = STATE_CODE_MAP
10871196
election_list = []
10881197
cached_national_election_list = False
1089-
data_stale_if_older_than = now() - timedelta(days=30)
10901198
for one_state_code, one_state_name in state_list.items():
10911199
# ############################
10921200
# Figure out the last dates we retrieved data for this state

templates/election/election_list.html

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ <h1>{% if is_national_election %}{{ national_election.election_name }} &mdash; {
9595
{% endif %}
9696
<th>State(s)</th>
9797
{% if not is_national_election %}
98-
<th>Google Civic ID</th>
98+
<th>Election ID</th>
9999
{% endif %}
100100
<th align="middle">
101101
Offices
@@ -104,11 +104,11 @@ <h1>{% if is_national_election %}{{ national_election.election_name }} &mdash; {
104104
(w/o Candidates)
105105
{% endif %}
106106
</th>
107-
{% if is_national_election %}
107+
{% if is_national_election or show_election_statistics %}
108108
<th>Last Refresh</th>
109109
<th>Refresh Added to Queue</th>
110-
<th>Last Election</th>
111-
<th>Next Election</th>
110+
{# <th>Last Election</th>#}
111+
{# <th>Next Election</th>#}
112112
{% endif %}
113113
{% if show_election_statistics %}
114114
<th align="middle">Candidates<br />
@@ -172,7 +172,10 @@ <h1>{% if is_national_election %}{{ national_election.election_name }} &mdash; {
172172
{% endif %}
173173
</td>
174174
{% if not is_national_election %}
175-
<td align="middle">{{ election.google_civic_election_id }}</td>
175+
<td align="middle">
176+
{{ election.google_civic_election_id }}
177+
{% if election.vote_usa_election_id %} / {{ election.vote_usa_election_id }}{% endif %}
178+
</td>
176179
{% endif %}
177180
<td align="middle">
178181
{% if not show_election_statistics and election.office_count %}
@@ -188,7 +191,7 @@ <h1>{% if is_national_election %}{{ national_election.election_name }} &mdash; {
188191
<span style="color: darkgray">({{ election.offices_without_candidates_count|default_if_none:"" }})</span>
189192
{% endif %}
190193
</td>
191-
{% if is_national_election %}{# Last Retrieve / Refresh #}
194+
{% if is_national_election or show_election_statistics %} {# Last Retrieve / Refresh #}
192195
<td>
193196
{# {% if election.retrieve_date_completed %}#}
194197
{# retrieve: {{ election.retrieve_date_completed }}#}
@@ -220,22 +223,22 @@ <h1>{% if is_national_election %}{{ national_election.election_name }} &mdash; {
220223
</span>
221224
{% endif %}
222225
</td>
223-
<td>
224-
{% if election.prior_election_in_state and election.prior_election_in_state.election_day_text %}
225-
{{ election.prior_election_in_state_date|date:"N j, Y" }}
226-
{% endif %}
227-
</td>
228-
<td>
229-
{% if election.next_election_in_state and election.next_election_in_state.election_day_text %}
230-
{% if election.next_election_in_state.election_day_text == election.election_day_text %}
231-
<span style="color: darkgray">
232-
This election
233-
</span>
234-
{% else %}
235-
{{ election.next_election_in_state_date|date:"N j, Y" }}
236-
{% endif %}
237-
{% endif %}
238-
</td>
226+
{# <td>#}
227+
{# {% if election.prior_election_in_state and election.prior_election_in_state.election_day_text %}#}
228+
{# {{ election.prior_election_in_state_date|date:"N j, Y" }}#}
229+
{# {% endif %}#}
230+
{# </td>#}
231+
{# <td>#}
232+
{# {% if election.next_election_in_state and election.next_election_in_state.election_day_text %}#}
233+
{# {% if election.next_election_in_state.election_day_text == election.election_day_text %}#}
234+
{# <span style="color: darkgray">#}
235+
{# This election#}
236+
{# </span>#}
237+
{# {% else %}#}
238+
{# {{ election.next_election_in_state_date|date:"N j, Y" }}#}
239+
{# {% endif %}#}
240+
{# {% endif %}#}
241+
{# </td>#}
239242
{% endif %}
240243
{% if show_election_statistics %}
241244
<td align="middle">

0 commit comments

Comments
 (0)