-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.py
255 lines (204 loc) · 11.9 KB
/
reports.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
from models import State, Deliverable, Submission, SubmissionComment, CATEGORIES, HOSTS, STATUS, PROJECT_YEARS, completed_deliverables, get_completion_percent
from views import standard_context
from django.shortcuts import get_object_or_404, get_list_or_404, render_to_response
from django.http import HttpResponse, HttpResponseNotAllowed, Http404
import json
def all_data(request):
# Only GET commands are allowed
valid_requests = ['GET']
if request.META.get('REQUEST_METHOD') not in valid_requests:
return HttpResponseNotAllowed(valid_requests)
# Check that the requested state is valid
state_abbr = request.GET.get('state', 'none')
if state_abbr == 'none':
states = State.objects.exclude(abbreviation='DC')
else:
states = get_list_or_404(State, abbreviation__iexact=state_abbr)
# Generate a list of records for the GridPanel
records = []
grandTotalRecords = 0
grandTotalDeliverables = 0
for this_state in states:
stateCompletedDeliverables = 0
stateTotalRecords = 0
for category in CATEGORIES:
categoryRecordCount = 0
categoryCompletedDeliverables = 0
these_deliverables = Deliverable.objects.filter(state=this_state).filter(category=category)
if these_deliverables.count() == 0: continue
these_submissions = Submission.objects.filter(satisfies_deliverable__in=these_deliverables).distinct()
record = { 'state': this_state.abbreviation, 'state_name': this_state.name, 'category': CATEGORIES.get(category) }
satisfied, total = completed_deliverables(this_state, category)
record['deliverableCount'] = total
record['deliverablesComplete'] = satisfied
categoryCompletedDeliverables = categoryCompletedDeliverables + satisfied
stateCompletedDeliverables = stateCompletedDeliverables + satisfied
grandTotalDeliverables = grandTotalDeliverables + satisfied
record['completion'] = get_completion_percent(satisfied, total)
if these_submissions:
record['recentSubmission'] = these_submissions[0].date_submitted.isoformat()
else:
record['recentSubmission'] = 'None'
# Count the number of records available online
for online_submission in these_submissions.filter(status__in=['online', 'approved']):
if online_submission.number_of_records:
record_count = online_submission.number_of_records
categoryRecordCount = categoryRecordCount + record_count
stateTotalRecords = stateTotalRecords + record_count
grandTotalRecords = grandTotalRecords + record_count
record['onlineCount'] = categoryRecordCount
# Add the group-label
record['groupLabel'] = '<a href="/track/report/' + this_state.abbreviation + '">State: ' + this_state.name + '</a>'
records.append(record)
# Create a "summary" record for this state
total_state_deliverables = this_state.deliverable_set.all().count()
record = { 'state': this_state.abbreviation, 'state_name': this_state.name, 'category': 'Totals for ' + this_state.name }
record['deliverableCount'] = total_state_deliverables
record['deliverablesComplete'] = stateCompletedDeliverables
record['completion'] = ( float(stateCompletedDeliverables) / total_state_deliverables ) * 100
record['onlineCount'] = stateTotalRecords
record['summary'] = True
records.append(record)
# Create a system-wide summary record if we were asked for all the data
if len(states) != 1:
record = { 'state': 'AA', 'state_name': ' System-Wide', 'category': 'All States, All Cateogries' }
record['deliverableCount'] = len(Deliverable.objects.all())
record['deliverablesComplete'] = grandTotalDeliverables
record['completion'] = ( float(grandTotalDeliverables) / len(Deliverable.objects.all()) ) * 100
record['onlineCount'] = grandTotalRecords
record['groupLabel'] = 'State-Wide System Totals'
record['summary'] = True
records.insert(0, record)
# Build the JSON object
response = { 'results': len(records), 'rows': records }
return HttpResponse(json.dumps(response), mimetype="application/json")
def online_state_data(request):
# Only GET commands are allowed
valid_requests = ['GET']
if request.META.get('REQUEST_METHOD') not in valid_requests:
return HttpResponseNotAllowed(valid_requests)
# Check that the requested state is valid
if not request.GET: raise Http404('Please provide an appropriate "state" parameter. For example, track/report/data?state=al.')
state_abbr = request.GET.get('state', 'none')
state = get_object_or_404(State, abbreviation__iexact=state_abbr)
# Build a list of records for the GridPanel
records = []
downloads = 0
services = 0
for submission in state.submission_set.filter(status='online'):
# Grab some information about the deliverables that the submission satisfies
deliverables = []
for deliverable in submission.satisfies_deliverable.all():
deliverables.append({'year': PROJECT_YEARS.get(deliverable.year), 'name': deliverable.data_item, 'category': CATEGORIES.get(deliverable.category)})
# Add a record for a service (if present)
if submission.service_url:
record = {'submissionName': submission.file_name }
record['urlType'] = 'Services'
record['url'] = submission.service_url
record['label'] = '<a href="' + submission.service_url + '">' + submission.title + '</a>'
record['deliverables'] = deliverables
# Append the record
records.append(record)
services = services + 1
# Add a record for a download (if present)
if submission.download_url:
record = {'submissionName': submission.file_name }
record['urlType'] = 'Downloads'
record['url'] = submission.download_url
record['label'] = '<a href="' + submission.download_url + '">' + submission.title + '</a>'
record['deliverables'] = deliverables
# Append the record
records.append(record)
downloads = downloads + 1
# Generate dummy records if there is nothing to show
if services == 0:
record = {'label': 'No services online', 'urlType': 'Services', }
records.append(record)
if downloads == 0:
record = {'label': 'No downloads available', 'urlType': 'Downloads'}
records.append(record)
# Build the JSON object
response = { 'results': len(records), 'rows': records }
return HttpResponse(json.dumps(response), mimetype="application/json")
def state_data(request):
# Only GET commands are allowed
valid_requests = ['GET']
if request.META.get('REQUEST_METHOD') not in valid_requests:
return HttpResponseNotAllowed(valid_requests)
# Check that the requested state is valid
if not request.GET: raise Http404('Please provide an appropriate "state" parameter. For example, track/report/data?state=al.')
state_abbr = request.GET.get('state', 'none')
state = get_object_or_404(State, abbreviation__iexact=state_abbr)
# Check if a year was specified
year = request.GET.get('year')
if year: year = int(year)
# Build a list of records for the GridPanel
deliverables_with_submissions = []
records = []
for submission in state.submission_set.all():
for deliverable in submission.satisfies_deliverable.all():
# Skip this deliverable if it is not of the specified year
if year and deliverable.year != year:
continue
if deliverable not in deliverables_with_submissions:
deliverables_with_submissions.append(deliverable)
# Add information to the record about the deliverable
record = { 'state': state.abbreviation }
record['deliverableId'] = deliverable.pk
record['deliverableYear'] = PROJECT_YEARS.get(deliverable.year)
record['deliverableName'] = deliverable.category + '|' + deliverable.data_item
record['deliverableDef'] = deliverable.definition
record['deliverablePlan'] = deliverable.delivery_plan
record['deliverableCategory'] = CATEGORIES.get(deliverable.category)
# Add information to the record about the submission
record['submissionFile'] = submission.file_name
record['submissionStatus'] = '<span class="submission-status">' + STATUS.get(submission.status) + '</span> as of ' + submission.status_date.strftime('%b %d, %Y')
record['submissionStatDate'] = submission.status_date.isoformat()
record['submissionSubDate'] = submission.date_submitted.isoformat()
record['submissionTitle'] = submission.title
record['submissionRecords'] = submission.number_of_records
# Add information to the record about comments on the submission
record['submissionComments'] = []
for comment in submission.submissioncomment_set.all():
record['submissionComments'].append(comment.date.strftime('%b %d, %Y') + ': ' + comment.comment)
# Add the record to the set of records to be JSON-ificated
records.append(record)
# Should check that all deliverables are represented. Add a blank row for deliverables with no submissions.
for deliverable in state.deliverable_set.all():
if deliverable not in deliverables_with_submissions:
# Again, skip if it isn't from the specified year
if year and deliverable.year != year:
continue
record = { 'state': state.abbreviation }
record['deliverableId'] = deliverable.pk
record['deliverableYear'] = PROJECT_YEARS.get(deliverable.year)
record['deliverableName'] = deliverable.category + '|' + deliverable.data_item
record['deliverableDef'] = deliverable.definition
record['deliverablePlan'] = deliverable.delivery_plan
record['deliverableCategory'] = CATEGORIES.get(deliverable.category)
record['submissionTitle'] = 'No submissions yet...'
record['noExpansion'] = True
records.append(record)
# Generate dummy records if there is nothing to show
if len(records) == 0:
record = { 'state': state.abbreviation }
record['deliverableName'] = '|No contracted deliverables yet...'
record['noExpansion'] = True
records.append(record)
# Build the JSON object
response = { 'results': len(records), 'rows': records }
return HttpResponse(json.dumps(response), mimetype="application/json")
def full_report(request):
# Only GET commands are allowed
valid_requests = ['GET']
if request.META.get('REQUEST_METHOD') not in valid_requests:
return HttpResponseNotAllowed(valid_requests)
return render_to_response('aasgtrack/reports/full-report.html', standard_context({}))
def state_report(request, state):
# Only GET commands are allowed
valid_requests = ['GET']
if request.META.get('REQUEST_METHOD') not in valid_requests:
return HttpResponseNotAllowed(valid_requests)
# Check that the requested state is valid
this_state = get_object_or_404(State, abbreviation__iexact=state)
return render_to_response('aasgtrack/reports/state-report.html', standard_context({'state': this_state}))