-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
1009 lines (712 loc) · 46.1 KB
/
views.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
from __future__ import division
from django.conf import settings
from django.shortcuts import get_object_or_404, render_to_response, render
from django.template.loader import render_to_string
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404
from django.views.decorators.clickjacking import xframe_options_exempt, xframe_options_sameorigin
from django.core.urlresolvers import reverse
from django.template import RequestContext, Context, loader
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.generic import View, ListView, DetailView
from django.db.models import Q, Avg, Max, Min, Sum, Count
from cali_water.models import WaterSupplier, WaterSupplierMonthlyReport, WaterEnforcementMonthlyReport, WaterIncentive, WaterRestriction, WaterConservationMethod, HydrologicRegion
from bakery.views import BuildableListView, BuildableDetailView
import os
import calculate
import datetime
import logging
import yaml
import calendar
logger = logging.getLogger("accountability_tracker")
# Create your views here.
class InitialIndex(BuildableListView):
model = WaterSupplierMonthlyReport
template_name = "cali_water/index.html"
def get_object(self):
object = super(InitialIndex, self).get_object()
return object
def get_queryset(self):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# get all the reports
queryset = super(InitialIndex, self).get_queryset()
# get all of the water suppliers
water_suppliers = WaterSupplier.objects.all()
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
# get the most recent data from the most recent report
all_months_latest_report = new_queries._all_months_latest_report(queryset)
# get a queryset of the months represented in the database
months_in_report = all_months_latest_report.exclude(reporting_month__isnull=True).values("reporting_month").distinct().order_by("-reporting_month")
# get the max value of calculated_rgpcd_2014 in the latest report
global_max = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
# get the min value of calculated_rgpcd_2014 in the latest report
global_min = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
# create the hydrologic_region option list
hydrologic_regions = water_suppliers.exclude(hydrologic_region__isnull=True).values("hydrologic_region").distinct()
for item in hydrologic_regions:
item["suppliers"] = water_suppliers.filter(hydrologic_region = item["hydrologic_region"]).order_by("supplier_name")
# create the hydrologic_region data for the maps
map_data = water_suppliers.exclude(hydrologic_region__isnull=True).values("hydrologic_region").distinct()
for item in map_data:
this_month = all_months_latest_report.filter(hydrologic_region = item["hydrologic_region"]).filter(reporting_month = months_in_report[0]["reporting_month"])
this_month_avg = new_queries._get_avg_rgcpd(this_month)
item["this_month_avg"] = this_month_avg
this_month_baseline_avg = new_queries._get_last_year_avg_rgcpd(this_month)
item["this_month_baseline_avg"] = this_month_baseline_avg
last_month = all_months_latest_report.filter(hydrologic_region = item["hydrologic_region"]).filter(reporting_month = months_in_report[1]["reporting_month"])
last_month_avg = new_queries._get_avg_rgcpd(last_month)
item["last_month_avg"] = last_month_avg
item["suppliers"] = list(latest_month_latest_report.filter(hydrologic_region = item["hydrologic_region"]).values("supplier_name", "calculated_rgpcd_2014").order_by("calculated_rgpcd_2014"))
item["count"] = latest_month_latest_report.filter(hydrologic_region = item["hydrologic_region"]).count()
item["this_max"] = latest_month_latest_report.filter(hydrologic_region = item["hydrologic_region"]).values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
item["this_min"] = latest_month_latest_report.filter(hydrologic_region = item["hydrologic_region"]).values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
item["median"] = latest_month_latest_report.filter(hydrologic_region = item["hydrologic_region"]).values_list("calculated_rgpcd_2014", flat=True).order_by("calculated_rgpcd_2014")[int(round(item["count"]/2))]
item["min_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_min"]["calculated_rgpcd_2014__min"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["max_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_max"]["calculated_rgpcd_2014__max"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["median_range"] = new_queries.pct_value_inside_arbitrary_range(item["median"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["average_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_month_avg"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
for supplier in item["suppliers"]:
supplier["distribution_precent"] = new_queries.pct_value_inside_arbitrary_range(supplier["calculated_rgpcd_2014"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
# calculate the state average rgcpd for the current month
state_this_month = all_months_latest_report.filter(reporting_month = months_in_report[0]["reporting_month"])
state_avg_latest = new_queries._get_avg_rgcpd(state_this_month)
# calculate the state average rgcpd for last month
state_last_month = all_months_latest_report.filter(reporting_month = months_in_report[1]["reporting_month"])
state_avg_last = new_queries._get_avg_rgcpd(state_last_month)
return {
"article_content": config["article_content"],
"about_content": config["about_content"],
"config_object": config["config_object"],
"target_report": latest_month_latest_report[0].reporting_month,
"option_list": hydrologic_regions,
"map_data": list(map_data),
"global_max": global_max,
"global_min": global_min,
"state_avg_latest": state_avg_latest,
"state_avg_last": state_avg_last,
}
class ComparisonIndex(BuildableDetailView):
model = HydrologicRegion
template_name = "cali_water/region_reduction_comparison.html"
slug_field = "hydrologic_region_slug"
sub_directory = "region/"
def get_object(self):
object = super(ComparisonIndex, self).get_object()
return object
def get_build_path(self, obj):
"""
used to determine where to build the detail page. override this if you
would like your detail page at a different location. by default it
will be built at get_url() + "index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.sub_directory, self.get_url(obj)[1:])
path = "%sreduction-comparison/" % (path)
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, "index.html")
def get_context_data(self, **kwargs):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# create the context object
context = super(ComparisonIndex, self).get_context_data(**kwargs)
# add the article content config to the context
context["article_content"] = config["article_content"]
# add the about this to the context
context["about_content"] = config["about_content"]
# add the javascript config to the context
context["config_object"] = config["config_object"]
# get the supplier slug
context["region_slug"] = self.object.hydrologic_region_slug
# get the supplier slug
context["region_name"] = self.object.hydrologic_region
# get all the reports
queryset = WaterSupplierMonthlyReport.objects.all()
# get all of the water suppliers
water_suppliers = WaterSupplier.objects.all().filter(hydrologic_region = context["region_name"]).order_by("hydrologic_region", "supplier_name")
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
# get the most recent data from the most recent report
all_months_latest_report = new_queries._all_months_latest_report(queryset)
context["target_report"] = latest_month_latest_report[0].reporting_month
context["water_suppliers"] = water_suppliers
return context
class EnforcementIndex(BuildableDetailView):
model = HydrologicRegion
template_name = "cali_water/region_enforcement_comparison.html"
slug_field = "hydrologic_region_slug"
sub_directory = "region/"
def get_object(self):
object = super(EnforcementIndex, self).get_object()
return object
def get_build_path(self, obj):
"""
used to determine where to build the detail page. override this if you
would like your detail page at a different location. by default it
will be built at get_url() + "index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.sub_directory, self.get_url(obj)[1:])
path = "%senforcement-comparison/" % (path)
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, "index.html")
def get_context_data(self, **kwargs):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# create the context object
context = super(EnforcementIndex, self).get_context_data(**kwargs)
# add the article content config to the context
context["article_content"] = config["article_content"]
# add the about this to the context
context["about_content"] = config["about_content"]
# add the javascript config to the context
context["config_object"] = config["config_object"]
# get the supplier slug
context["region_slug"] = self.object.hydrologic_region_slug
# get the supplier slug
context["region_name"] = self.object.hydrologic_region
# get all the reports
queryset = WaterEnforcementMonthlyReport.objects.filter(hydrologic_region = context["region_name"]).order_by("hydrologic_region", "supplier_name")
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
context["target_report"] = latest_month_latest_report[0].reporting_month
context["water_suppliers"] = queryset
return context
class RegionDetailView(BuildableDetailView):
model = HydrologicRegion
template_name = "cali_water/region_detail.html"
slug_field = "hydrologic_region_slug"
sub_directory = "region/"
def get_object(self):
object = super(RegionDetailView, self).get_object()
return object
def get_build_path(self, obj):
"""
used to determine where to build the detail page. override this if you
would like your detail page at a different location. by default it
will be built at get_url() + "index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.sub_directory, self.get_url(obj)[1:])
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, "index.html")
def get_context_data(self, **kwargs):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# create the context object
context = super(RegionDetailView, self).get_context_data(**kwargs)
# add the article content config to the context
context["article_content"] = config["article_content"]
# add the about this to the context
context["about_content"] = config["about_content"]
# add the javascript config to the context
context["config_object"] = config["config_object"]
# get the supplier slug
context["region_slug"] = self.object.hydrologic_region_slug
# get the supplier slug
context["region_name"] = self.object.hydrologic_region
# get a queryset for this hydrologic region
queryset = WaterSupplierMonthlyReport.objects.filter(hydrologic_region = context["region_name"]).order_by("supplier_name_id")
# new instance of the utility class
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
# get the most recent month of data from the most recent report
all_months_latest_report = new_queries._all_months_latest_report(queryset)
# get the most recent data for all the months in the most recent report
months_in_report = all_months_latest_report.exclude(reporting_month__isnull=True).values("reporting_month").distinct().order_by("-reporting_month")
# get the max value of calculated_rgpcd_2014 in the latest report
global_max = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
# assign it to context
context["global_max"] = global_max
# get the min value of calculated_rgpcd_2014 in the latest report
global_min = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
# assign it to context
context["global_min"] = global_min
context["target_report"] = latest_month_latest_report[0].reporting_month
context["reports_from_region"] = latest_month_latest_report
# create the hydrologic_region data for the maps
map_data = []
item = {}
item["hydrologic_slug"] = context["region_slug"]
item["hydrologic_region"] = context["region_name"]
# create the hydrologic_region data for the maps
this_month = all_months_latest_report.filter(hydrologic_region = context["region_name"]).filter(reporting_month = months_in_report[0]["reporting_month"])
this_month_avg = new_queries._get_avg_rgcpd(this_month)
item["this_month_avg"] = this_month_avg
this_month_baseline_avg = new_queries._get_last_year_avg_rgcpd(this_month)
item["this_month_baseline_avg"] = this_month_baseline_avg
last_month = all_months_latest_report.filter(hydrologic_region = context["region_name"]).filter(reporting_month = months_in_report[1]["reporting_month"])
last_month_avg = new_queries._get_avg_rgcpd(last_month)
item["last_month_avg"] = last_month_avg
item["suppliers"] = list(latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("supplier_name", "calculated_rgpcd_2014").order_by("supplier_name_id", "calculated_rgpcd_2014"))
item["count"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).count()
item["this_max"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
item["this_min"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
item["median"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values_list("calculated_rgpcd_2014", flat=True).order_by("calculated_rgpcd_2014")[int(round(item["count"]/2))]
item["min_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_min"]["calculated_rgpcd_2014__min"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["max_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_max"]["calculated_rgpcd_2014__max"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["median_range"] = new_queries.pct_value_inside_arbitrary_range(item["median"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["average_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_month_avg"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
for supplier in item["suppliers"]:
supplier["distribution_precent"] = new_queries.pct_value_inside_arbitrary_range(supplier["calculated_rgpcd_2014"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
map_data.append(item)
context["map_data"] = map_data
return context
class RegionEmbedView(BuildableDetailView):
model = HydrologicRegion
template_name = "cali_water/region_embed.html"
slug_field = "hydrologic_region_slug"
sub_directory = "share/"
def get_object(self):
object = super(RegionEmbedView, self).get_object()
return object
def get_build_path(self, obj):
"""
used to determine where to build the detail page. override this if you
would like your detail page at a different location. by default it
will be built at get_url() + "index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.sub_directory, self.get_url(obj)[1:])
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, "index.html")
def get_context_data(self, **kwargs):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# create the context object
context = super(RegionEmbedView, self).get_context_data(**kwargs)
# add the article content config to the context
context["article_content"] = config["article_content"]
# add the about this to the context
context["about_content"] = config["about_content"]
# add the javascript config to the context
context["config_object"] = config["config_object"]
# get the supplier slug
context["region_slug"] = self.object.hydrologic_region_slug
# get the supplier slug
context["region_name"] = self.object.hydrologic_region
# get a queryset for this hydrologic region
queryset = WaterSupplierMonthlyReport.objects.filter(hydrologic_region = context["region_name"]).order_by("supplier_name_id")
# new instance of the utility class
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
# get the most recent month of data from the most recent report
all_months_latest_report = new_queries._all_months_latest_report(queryset)
# get the most recent data for all the months in the most recent report
months_in_report = all_months_latest_report.exclude(reporting_month__isnull=True).values("reporting_month").distinct().order_by("-reporting_month")
# get the max value of calculated_rgpcd_2014 in the latest report
global_max = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
# assign it to context
context["global_max"] = global_max
# get the min value of calculated_rgpcd_2014 in the latest report
global_min = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
# assign it to context
context["global_min"] = global_min
context["target_report"] = latest_month_latest_report[0].reporting_month
context["reports_from_region"] = latest_month_latest_report
# create the hydrologic_region data for the maps
map_data = []
item = {}
item["hydrologic_slug"] = context["region_slug"]
item["hydrologic_region"] = context["region_name"]
# create the hydrologic_region data for the maps
this_month = all_months_latest_report.filter(hydrologic_region = context["region_name"]).filter(reporting_month = months_in_report[0]["reporting_month"])
this_month_avg = new_queries._get_avg_rgcpd(this_month)
item["this_month_avg"] = this_month_avg
this_month_baseline_avg = new_queries._get_last_year_avg_rgcpd(this_month)
item["this_month_baseline_avg"] = this_month_baseline_avg
last_month = all_months_latest_report.filter(hydrologic_region = context["region_name"]).filter(reporting_month = months_in_report[1]["reporting_month"])
last_month_avg = new_queries._get_avg_rgcpd(last_month)
item["last_month_avg"] = last_month_avg
item["suppliers"] = list(latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("supplier_name", "calculated_rgpcd_2014").order_by("supplier_name_id", "calculated_rgpcd_2014"))
item["count"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).count()
item["this_max"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
item["this_min"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
item["median"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values_list("calculated_rgpcd_2014", flat=True).order_by("calculated_rgpcd_2014")[int(round(item["count"]/2))]
item["min_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_min"]["calculated_rgpcd_2014__min"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["max_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_max"]["calculated_rgpcd_2014__max"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["median_range"] = new_queries.pct_value_inside_arbitrary_range(item["median"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["average_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_month_avg"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
for supplier in item["suppliers"]:
supplier["distribution_precent"] = new_queries.pct_value_inside_arbitrary_range(supplier["calculated_rgpcd_2014"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
map_data.append(item)
context["map_data"] = map_data
return context
class RegionReportView(BuildableDetailView):
model = HydrologicRegion
template_name = "cali_water/region_report.html"
slug_field = "hydrologic_region_slug"
sub_directory = "region/"
def get_object(self):
object = super(RegionReportView, self).get_object()
return object
def get_build_path(self, obj):
"""
used to determine where to build the detail page. override this if you
would like your detail page at a different location. by default it
will be built at get_url() + "index.html"
"""
path = os.path.join(settings.BUILD_DIR, self.sub_directory, self.get_url(obj)[1:])
path = "%sreport/" % (path)
os.path.exists(path) or os.makedirs(path)
return os.path.join(path, "index.html")
def get_context_data(self, **kwargs):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# create the context object
context = super(RegionReportView, self).get_context_data(**kwargs)
# add the article content config to the context
context["article_content"] = config["article_content"]
# add the about this to the context
context["about_content"] = config["about_content"]
# add the javascript config to the context
context["config_object"] = config["config_object"]
# get the supplier slug
context["region_slug"] = self.object.hydrologic_region_slug
# get the supplier slug
context["region_name"] = self.object.hydrologic_region
# get a queryset for this hydrologic region
queryset = WaterSupplierMonthlyReport.objects.filter(hydrologic_region = context["region_name"]).order_by("supplier_name_id")
# new instance of the utility class
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
# get the most recent month of data from the most recent report
all_months_latest_report = new_queries._all_months_latest_report(queryset)
# get the most recent data for all the months in the most recent report
months_in_report = all_months_latest_report.exclude(reporting_month__isnull=True).values("reporting_month").distinct().order_by("-reporting_month")
# get the max value of calculated_rgpcd_2014 in the latest report
global_max = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
# assign it to context
context["global_max"] = global_max
# get the min value of calculated_rgpcd_2014 in the latest report
global_min = latest_month_latest_report.values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
# assign it to context
context["global_min"] = global_min
context["target_report"] = latest_month_latest_report[0].reporting_month
context["reports_from_region"] = latest_month_latest_report
# create the hydrologic_region data for the maps
map_data = []
item = {}
item["hydrologic_slug"] = context["region_slug"]
item["hydrologic_region"] = context["region_name"]
# create the hydrologic_region data for the maps
this_month = all_months_latest_report.filter(hydrologic_region = context["region_name"]).filter(reporting_month = months_in_report[0]["reporting_month"])
this_month_avg = new_queries._get_avg_rgcpd(this_month)
item["this_month_avg"] = this_month_avg
this_month_baseline_avg = new_queries._get_last_year_avg_rgcpd(this_month)
item["this_month_baseline_avg"] = this_month_baseline_avg
last_month = all_months_latest_report.filter(hydrologic_region = context["region_name"]).filter(reporting_month = months_in_report[1]["reporting_month"])
last_month_avg = new_queries._get_avg_rgcpd(last_month)
item["last_month_avg"] = last_month_avg
item["suppliers"] = list(latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("supplier_name", "supplier_slug", "calculated_rgpcd_2013", "calculated_rgpcd_2014").order_by("supplier_name_id", "calculated_rgpcd_2013", "calculated_rgpcd_2014"))
item["count"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).count()
item["this_max"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("calculated_rgpcd_2014").aggregate(Max("calculated_rgpcd_2014"))
item["this_min"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values("calculated_rgpcd_2014").aggregate(Min("calculated_rgpcd_2014"))
item["median"] = latest_month_latest_report.filter(hydrologic_region = context["region_name"]).values_list("calculated_rgpcd_2014", flat=True).order_by("calculated_rgpcd_2014")[int(round(item["count"]/2))]
item["min_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_min"]["calculated_rgpcd_2014__min"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["max_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_max"]["calculated_rgpcd_2014__max"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["median_range"] = new_queries.pct_value_inside_arbitrary_range(item["median"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
item["average_range"] = new_queries.pct_value_inside_arbitrary_range(item["this_month_avg"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
for supplier in item["suppliers"]:
supplier["distribution_precent"] = new_queries.pct_value_inside_arbitrary_range(supplier["calculated_rgpcd_2014"], global_min["calculated_rgpcd_2014__min"], global_max["calculated_rgpcd_2014__max"])
map_data.append(item)
context["map_data"] = map_data
return context
class SupplierDetailView(BuildableDetailView):
model = WaterSupplier
template_name = "cali_water/supplier_detail.html"
slug_field = "supplier_slug"
def get_object(self):
object = super(SupplierDetailView, self).get_object()
return object
#def get_build_path(self, obj):
"""
used to determine where to build the detail page. override this if you
would like your detail page at a different location. by default it
will be built at get_url() + "index.html"
"""
#path = os.path.join(settings.BUILD_DIR, self.sub_directory, self.get_url(obj)[1:])
#os.path.exists(path) or os.makedirs(path)
#return os.path.join(path, "index.html")
def get_context_data(self, **kwargs):
# grab the yaml configuration items
config = yaml.load(open("cali_water/config.yml"))
# create the context object
context = super(SupplierDetailView, self).get_context_data(**kwargs)
# add the article content config to the context
context["article_content"] = config["article_content"]
# add the about this to the context
context["about_content"] = config["about_content"]
# add the javascript config to the context
context["config_object"] = config["config_object"]
# get the supplier slug
context["slug"] = self.object.supplier_slug
# get all the reports
queryset = WaterSupplierMonthlyReport.objects.all()
# create instance of query class
new_queries = QueryUtilities()
# get queryset of the latest month from the most recent report
latest_month_latest_report = new_queries._latest_month_latest_report(queryset)
# calculate & return the state average rgcpd for the current month
context["latest_state_avg"] = new_queries._get_avg_rgcpd(latest_month_latest_report)
# get & return queryset of all the months from the most recent report
all_months_latest_report = new_queries._all_months_latest_report(queryset)
context["results"] = all_months_latest_report.filter(supplier_name_id = self.object)
context["april_7_tier"] = {
"conservation_standard": self.object.april_7_reduction,
"conservation_placement": self.object.april_7_rgpcd,
"conservation_tier": self.object.april_7_tier,
}
context["april_18_tier"] = {
"conservation_standard": self.object.april_18_reduction,
"conservation_placement": self.object.april_18_rgpcd,
"conservation_tier": self.object.april_18_tier,
}
context["april_28_tier"] = {
"conservation_standard": self.object.april_28_reduction,
"conservation_placement": self.object.april_28_rgpcd,
"conservation_tier": self.object.april_28_tier,
}
"""
used to calculate reduction tier and has since been saved to the model
"""
#this_supplier_set = context["results"].filter(Q(reporting_month = "2014-07-01") | Q(reporting_month = "2014-08-01") | Q(reporting_month = "2014-09-01"))
#if len(this_supplier_set) > 0:
#three_month_rgcpd = new_queries._get_rolling_avg_rgcpd(this_supplier_set)
#else:
#three_month_rgcpd = None
#context["first_draft_tier"] = new_queries._get_conservation_tier(this_supplier_set)
#context["second_draft_tier"] = new_queries._get_second_draft_conservation_tier(three_month_rgcpd)
# return the conservation standard
#context["conservation"] = new_queries._get_conservation_tier(context["results"])
# return the restrictions if present
context["restrictions"] = WaterRestriction.objects.filter(supplier_name_id = self.object)
# return the incentives if present
context["incentives"] = WaterIncentive.objects.filter(supplier_name_id = self.object)
# return the conservation_methods if present
context["conservation_methods"] = WaterConservationMethod.objects.all().order_by("?")[:4]
context["target_report"] = latest_month_latest_report[0].reporting_month
# create the lists needed for the chart
context["labels"] = []
context["data_2014"] = []
context["data_2013"] = []
# append data to the lists for the chart
for result in context["results"]:
month_label = result.reporting_month.strftime("%b")
context["labels"].append(month_label)
context["data_2014"].append(result.calculated_rgpcd_2014)
context["data_2013"].append(result.calculated_rgpcd_2013)
# return the context
return context
class QueryUtilities(object):
# get me the latest south coast supplier reports
#local_supplier_reports = current_data.filter(supplier_name_id__hydrologic_region = "South Coast").extra(select = {"percent_change": "((calculated_rgpcd_2014-calculated_rgpcd_2013)/calculated_rgpcd_2013)*100"})
# show which agencies increased & decreased residential gallons per capita day
#largest_increase = local_supplier_reports.order_by("-percent_change")[:5]
#largest_decrease = local_supplier_reports.order_by("percent_change")[:5]
# misc calculations
#http://www.waterboards.ca.gov/waterrights/water_issues/programs/drought/docs/emergency_regulations/urban_water_supplier_tiers.pdf
# https://latimes-calculate.readthedocs.org/en/latest/basicfunctions.html#at-percentile
# https://latimes-calculate.readthedocs.org/en/latest/basicfunctions.html#percentile
# https://latimes-calculate.readthedocs.org/en/latest/basicfunctions.html#ordinal-rank
# http://www.nytimes.com/interactive/2015/04/01/us/water-use-in-california.html?mabReward=A4&action=click&pgtype=Homepage®ion=CColumn&module=Recommendation&src=rechp&WT.nav=RecEngine&_r=2
# http://www2.pacinst.org/gpcd/table.html#
# http://www.waterboards.ca.gov/press_room/press_releases/2015/pr030315_urbanwater.pdf
#parent_obj = WaterSupplier.objects.values_list("hydrologic_region").distinct()
#logger.debug(parent_obj)
#for item in results:
#parent_obj = WaterSupplier.objects.get(supplier_name = item.supplier_name_id)
#logger.debug(parent_obj.hydrologic_region)
# determine aggregate change in water production from baseline to latest month in latest report
#production_new = results.aggregate(Sum("calculated_production_monthly_gallons_month_2014"))
#production_new = int(production_new["calculated_production_monthly_gallons_month_2014__sum"])
#production_old = results.aggregate(Sum("calculated_production_monthly_gallons_month_2013"))
#production_old = int(production_old["calculated_production_monthly_gallons_month_2013__sum"])
#production_change = calculate.percentage_change(production_old, production_new, multiply=True)
#logger.debug(production_change)
# trying to calculate the savings for the current month
#test_savings_2014 = current_data.aggregate(Sum("calculated_production_monthly_gallons_month_2014"))
#logger.debug(test_savings_2014)
#test_savings_2013 = current_data.aggregate(Sum("calculated_production_monthly_gallons_month_2013"))
#logger.debug(test_savings_2013)
#test = calculate.percentage_change(test_savings_2013["calculated_production_monthly_gallons_month_2013__sum"], test_savings_2014["calculated_production_monthly_gallons_month_2014__sum"])
#print test
def _latest_month_latest_report(self, queryset):
# determine the most recent month from the report
latest_data = queryset.aggregate(Max("reporting_month"))
# determine the most recent report
latest_report_date = queryset.aggregate(Max("report_date"))
# determine the target report
target_report = datetime.date(latest_data["reporting_month__max"].year, latest_data["reporting_month__max"].month, latest_data["reporting_month__max"].day)
# get the latest month data from the latest report
output = queryset.filter(report_date = latest_report_date["report_date__max"]).filter(reporting_month__gte = target_report)
# return output
return output
def _all_months_latest_report(self, queryset):
# determine the most recent month from the report
latest_data = queryset.aggregate(Max("reporting_month"))
# determine the most recent report
latest_report_date = queryset.aggregate(Max("report_date"))
# determine the target report
target_report = datetime.date(latest_data["reporting_month__max"].year, latest_data["reporting_month__max"].month, latest_data["reporting_month__max"].day)
output = queryset.filter(report_date = latest_report_date["report_date__max"]).order_by("-reporting_month")
# return output
return output
def _get_second_draft_conservation_tier(self, conservation_placement):
if conservation_placement != None:
if conservation_placement < 65:
conservation_standard = .08
conservation_tier = 2
elif conservation_placement >= 65 and conservation_placement < 80:
conservation_standard = .12
conservation_tier = 3
elif conservation_placement >= 80 and conservation_placement < 95:
conservation_standard = .16
conservation_tier = 4
elif conservation_placement >= 95 and conservation_placement < 110:
conservation_standard = .20
conservation_tier = 5
elif conservation_placement >= 110 and conservation_placement < 130:
conservation_standard = .24
conservation_tier = 6
elif conservation_placement >= 130 and conservation_placement < 170:
conservation_standard = .28
conservation_tier = 7
elif conservation_placement >= 170 and conservation_placement < 215:
conservation_standard = .32
conservation_tier = 8
elif conservation_placement >= 215:
conservation_standard = .36
conservation_tier = 9
else:
conservation_placement = None
conservation_standard = None
conservation_tier = None
output = {
"conservation_standard": conservation_standard,
"conservation_tier": conservation_tier,
"conservation_placement": conservation_placement,
}
return output
def _get_conservation_tier(self, queryset):
# get the conservation_placement_threshold for an individual agency
conservation_placement = queryset.filter(reporting_month = "2014-09-01").values("calculated_rgpcd_2014")
if len(conservation_placement) > 0:
conservation_placement = conservation_placement[0]["calculated_rgpcd_2014"]
if conservation_placement < 55:
conservation_standard = .10
conservation_tier = 1
elif conservation_placement >= 55 and conservation_placement < 110:
conservation_standard = .20
conservation_tier = 2
elif conservation_placement >= 110 and conservation_placement < 165:
conservation_standard = .25
conservation_tier = 3
elif conservation_placement > 165:
conservation_standard = .35
conservation_tier = 4
else:
conservation_placement = None
conservation_standard = None
conservation_tier = None
output = {
"conservation_standard": conservation_standard,
"conservation_tier": conservation_tier,
"conservation_placement": conservation_placement
}
return output
def _get_rolling_avg_rgcpd(self, queryset):
residential_population = []
residential_gallons_used = []
average_days_in_month = []
for result in queryset:
residential_population.append(result.total_population_served)
calendar_calc = calendar.monthrange(result.reporting_month.year, result.reporting_month.month)
days_in_month = calendar_calc[1]
average_days_in_month.append(days_in_month)
if result.units.upper() == "G":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "MG":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "CCF":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "AF":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
else:
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
res_gallons = int(sum(residential_gallons_used))
total_pop = int(sum(residential_population))
days_avg = sum(average_days_in_month) / len(average_days_in_month)
output = (res_gallons / total_pop) / days_avg
return output
def _get_avg_rgcpd(self, queryset):
residential_population = []
residential_gallons_used = []
days_in_month = calendar.monthrange(queryset[0].reporting_month.year, queryset[0].reporting_month.month)
for result in queryset:
residential_population.append(result.total_population_served)
if result.units.upper() == "G":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "MG":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "CCF":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "AF":
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
else:
tmp = result.calculated_production_monthly_gallons_month_2014 * result.percent_residential_use
residential_gallons_used.append(tmp)
res_gallons = int(sum(residential_gallons_used))
total_pop = int(sum(residential_population))
output = (res_gallons / total_pop) / days_in_month[1]
return output
def _get_last_year_avg_rgcpd(self, queryset):
residential_population = []
residential_gallons_used = []
days_in_month = calendar.monthrange(queryset[0].reporting_month.year, queryset[0].reporting_month.month)
for result in queryset:
residential_population.append(result.total_population_served)
if result.units.upper() == "G":
tmp = result.calculated_production_monthly_gallons_month_2013 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "MG":
tmp = result.calculated_production_monthly_gallons_month_2013 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "CCF":
tmp = result.calculated_production_monthly_gallons_month_2013 * result.percent_residential_use
residential_gallons_used.append(tmp)
elif result.units.upper() == "AF":
tmp = result.calculated_production_monthly_gallons_month_2013 * result.percent_residential_use
residential_gallons_used.append(tmp)
else:
tmp = result.calculated_production_monthly_gallons_month_2013 * result.percent_residential_use
residential_gallons_used.append(tmp)
res_gallons = int(sum(residential_gallons_used))
total_pop = int(sum(residential_population))
output = (res_gallons / total_pop) / days_in_month[1]
return output
def calculate_values_range(self, min_value, max_value):
output = max_value - min_value
return output
def pct_value_inside_arbitrary_range(self, starting_value, min_value, max_value):