forked from sc3/cook-convictions-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export community areas with the most DUIs
Add queries and a management command
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
convictions_data/management/commands/export_dui_convictions_by_geo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import csv | ||
from optparse import make_option | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
import convictions_data.models | ||
|
||
class Command(BaseCommand): | ||
help = ("Export CSV table showing most common conviction statute by geography") | ||
option_list = BaseCommand.option_list + ( | ||
make_option('--count', | ||
action='store', | ||
type='int', | ||
default=20, | ||
dest='count', | ||
help="Show this many statutes"), | ||
make_option('--model', | ||
action='store', | ||
default='CommunityArea', | ||
dest='model', | ||
help="Get most common statutes for this model"), | ||
) | ||
|
||
def handle(self, *args, **options): | ||
model_cls = getattr(convictions_data.models, options['model']) | ||
qs = model_cls.objects.all().with_dui_annotations().order_by('-num_dui') | ||
|
||
fieldnames = ['name', 'count', 'pct'] | ||
writer = csv.DictWriter(self.stdout, | ||
fieldnames=fieldnames) | ||
|
||
writer.writeheader() | ||
|
||
for geo in qs[:options['count']]: | ||
row = { | ||
'name': geo.name, | ||
'count': geo.num_dui, | ||
'pct': geo.pct_dui, | ||
} | ||
writer.writerow(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters