# Path: countyapi/models.py
# class CountyInmate(models.Model):
#     """
#     Model that represents a Cook County Jail inmate.
#     """
#     jail_id = models.CharField(max_length=15, primary_key=True)
#     person_id = models.CharField(max_length=64, null=True)
#     race = models.CharField(max_length=4, null=True, blank=True)
#     last_seen_date = models.DateTimeField(auto_now=True)
#     booking_date = models.DateField(null=True)
#     discharge_date_earliest = models.DateTimeField(null=True)
#     discharge_date_latest = models.DateTimeField(null=True)
#     gender = models.CharField(max_length=1, null=True, blank=True)
#     height = models.IntegerField(null=True, blank=True)
#     weight = models.IntegerField(null=True, blank=True)
#     age_at_booking = models.IntegerField(null=True, blank=True)
#     bail_status = models.CharField(max_length=50, null=True)
#     bail_amount = models.IntegerField(null=True, blank=True)
#     in_jail = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.jail_id
# 
#     class Meta:
#         ordering = ['-jail_id']
# 
# class DailyPopulationCounts(models.Model):
#     """
#     Population counts by day.
#     """
#     booking_date = models.DateField(null=True)
#     total = models.IntegerField(default=0)
#     female_as = models.IntegerField(default=0)
#     female_b = models.IntegerField(default=0)
#     female_bk = models.IntegerField(default=0)
#     female_in = models.IntegerField(default=0)
#     female_lb = models.IntegerField(default=0)
#     female_lw = models.IntegerField(default=0)
#     female_lt = models.IntegerField(default=0)
#     female_w = models.IntegerField(default=0)
#     female_wh = models.IntegerField(default=0)
#     male_as = models.IntegerField(default=0)
#     male_b = models.IntegerField(default=0)
#     male_bk = models.IntegerField(default=0)
#     male_in = models.IntegerField(default=0)
#     male_lb = models.IntegerField(default=0)
#     male_lw = models.IntegerField(default=0)
#     male_lt = models.IntegerField(default=0)
#     male_w = models.IntegerField(default=0)
#     male_wh = models.IntegerField(default=0)
# 
#     class Meta:
#         ordering = ['booking_date']
# 
# class DailyBookingsCounts(models.Model):
#     """
#     Bookings counts by day.
#     """
#     booking_date = models.DateField(null=True)
#     total = models.IntegerField(default=0)
#     female_as = models.IntegerField(default=0)
#     female_b = models.IntegerField(default=0)
#     female_bk = models.IntegerField(default=0)
#     female_in = models.IntegerField(default=0)
#     female_lb = models.IntegerField(default=0)
#     female_lw = models.IntegerField(default=0)
#     female_lt = models.IntegerField(default=0)
#     female_w = models.IntegerField(default=0)
#     female_wh = models.IntegerField(default=0)
#     female_minors = models.IntegerField(default=0)
#     male_as = models.IntegerField(default=0)
#     male_b = models.IntegerField(default=0)
#     male_bk = models.IntegerField(default=0)
#     male_in = models.IntegerField(default=0)
#     male_lb = models.IntegerField(default=0)
#     male_lw = models.IntegerField(default=0)
#     male_lt = models.IntegerField(default=0)
#     male_w = models.IntegerField(default=0)
#     male_wh = models.IntegerField(default=0)
#     male_minors = models.IntegerField(default=0)
# 
#     class Meta:
#         ordering = ['booking_date']


from datetime import datetime, timedelta
from django.core.management.base import BaseCommand
from countyapi.models import CountyInmate, DailyPopulationCounts, DailyBookingsCounts
from django.db.models import Max, Min, Q
from copy import copy
            'male_in': 0,
            'male_lb': 0,
            'male_lw': 0,
            'male_lt': 0,
            'male_w': 0,
            'male_wh': 0,
            'male_minors': 0,
        }

        counts = {}
        booking_counts = {}

        min_date = MIN_DATE
        max_date = CountyInmate.objects.all().aggregate(
            Max('booking_date'))['booking_date__max'] + timedelta(days=1)

        for day in self.daterange(min_date, max_date):
            print("Processing %s-%s-%s" % (day.year, day.month, day.day))
            inmates = CountyInmate.objects.filter(booking_date__lte=day)\
                .filter(Q(discharge_date_earliest__gt=day) | Q(discharge_date_earliest__isnull=True))
                
            inmates_booked = CountyInmate.objects.filter(booking_date=day)
            
            daily_pop_row = self.count_dictionary(inmates, template)
            booking_row = self.count_dictionary(inmates_booked, booking_template, track_minors=True)
            
            counts[day.strftime('%Y-%m-%d')] = daily_pop_row
            booking_counts[day.strftime('%Y-%m-%d')] = booking_row

        self.save_count(counts, DailyPopulationCounts)

# Path: countyapi/models.py
# class CountyInmate(models.Model):
#     """
#     Model that represents a Cook County Jail inmate.
#     """
#     jail_id = models.CharField(max_length=15, primary_key=True)
#     person_id = models.CharField(max_length=64, null=True)
#     race = models.CharField(max_length=4, null=True, blank=True)
#     last_seen_date = models.DateTimeField(auto_now=True)
#     booking_date = models.DateField(null=True)
#     discharge_date_earliest = models.DateTimeField(null=True)
#     discharge_date_latest = models.DateTimeField(null=True)
#     gender = models.CharField(max_length=1, null=True, blank=True)
#     height = models.IntegerField(null=True, blank=True)
#     weight = models.IntegerField(null=True, blank=True)
#     age_at_booking = models.IntegerField(null=True, blank=True)
#     bail_status = models.CharField(max_length=50, null=True)
#     bail_amount = models.IntegerField(null=True, blank=True)
#     in_jail = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.jail_id
# 
#     class Meta:
#         ordering = ['-jail_id']
# 
# class DailyPopulationCounts(models.Model):
#     """
#     Population counts by day.
#     """
#     booking_date = models.DateField(null=True)
#     total = models.IntegerField(default=0)
#     female_as = models.IntegerField(default=0)
#     female_b = models.IntegerField(default=0)
#     female_bk = models.IntegerField(default=0)
#     female_in = models.IntegerField(default=0)
#     female_lb = models.IntegerField(default=0)
#     female_lw = models.IntegerField(default=0)
#     female_lt = models.IntegerField(default=0)
#     female_w = models.IntegerField(default=0)
#     female_wh = models.IntegerField(default=0)
#     male_as = models.IntegerField(default=0)
#     male_b = models.IntegerField(default=0)
#     male_bk = models.IntegerField(default=0)
#     male_in = models.IntegerField(default=0)
#     male_lb = models.IntegerField(default=0)
#     male_lw = models.IntegerField(default=0)
#     male_lt = models.IntegerField(default=0)
#     male_w = models.IntegerField(default=0)
#     male_wh = models.IntegerField(default=0)
# 
#     class Meta:
#         ordering = ['booking_date']
# 
# class DailyBookingsCounts(models.Model):
#     """
#     Bookings counts by day.
#     """
#     booking_date = models.DateField(null=True)
#     total = models.IntegerField(default=0)
#     female_as = models.IntegerField(default=0)
#     female_b = models.IntegerField(default=0)
#     female_bk = models.IntegerField(default=0)
#     female_in = models.IntegerField(default=0)
#     female_lb = models.IntegerField(default=0)
#     female_lw = models.IntegerField(default=0)
#     female_lt = models.IntegerField(default=0)
#     female_w = models.IntegerField(default=0)
#     female_wh = models.IntegerField(default=0)
#     female_minors = models.IntegerField(default=0)
#     male_as = models.IntegerField(default=0)
#     male_b = models.IntegerField(default=0)
#     male_bk = models.IntegerField(default=0)
#     male_in = models.IntegerField(default=0)
#     male_lb = models.IntegerField(default=0)
#     male_lw = models.IntegerField(default=0)
#     male_lt = models.IntegerField(default=0)
#     male_w = models.IntegerField(default=0)
#     male_wh = models.IntegerField(default=0)
#     male_minors = models.IntegerField(default=0)
# 
#     class Meta:
#         ordering = ['booking_date']


# Path: countyapi/management/commands/generate_summaries.py
from datetime import datetime, timedelta
from django.core.management.base import BaseCommand
from countyapi.models import CountyInmate, DailyPopulationCounts, DailyBookingsCounts
from django.db.models import Max, Min, Q
from copy import copy

            'male_in': 0,
            'male_lb': 0,
            'male_lw': 0,
            'male_lt': 0,
            'male_w': 0,
            'male_wh': 0,
            'male_minors': 0,
        }

        counts = {}
        booking_counts = {}

        min_date = MIN_DATE
        max_date = CountyInmate.objects.all().aggregate(
            Max('booking_date'))['booking_date__max'] + timedelta(days=1)

        for day in self.daterange(min_date, max_date):
            print("Processing %s-%s-%s" % (day.year, day.month, day.day))
            inmates = CountyInmate.objects.filter(booking_date__lte=day)\
                .filter(Q(discharge_date_earliest__gt=day) | Q(discharge_date_earliest__isnull=True))
                
            inmates_booked = CountyInmate.objects.filter(booking_date=day)
            
            daily_pop_row = self.count_dictionary(inmates, template)
            booking_row = self.count_dictionary(inmates_booked, booking_template, track_minors=True)
            
            counts[day.strftime('%Y-%m-%d')] = daily_pop_row
            booking_counts[day.strftime('%Y-%m-%d')] = booking_row

        self.save_count(counts, DailyPopulationCounts)

        self.save_count(booking_counts, DailyBookingsCounts)