def drilldown_result( item_type: str, rollup_fn: Callable[[dc.Item], str], supply_cols: Set[AggColumn], time_range: dc.Period, ): purchases = (Purchase.active().prefetch_related("deliveries").order_by( "deliveries__delivery_date")) donations = [ d for d in purchases if rollup_fn(dc.Item(d.item)) == item_type and d.order_type == OrderType.Donation and not d.complete ] for don in donations: days_since_pledge = (date.today() - don.donation_date).days if days_since_pledge >= 7: pledge_status = "error" elif days_since_pledge > 3: pledge_status = "warning" else: pledge_status = "pending" setattr(don, "pledge_status", pledge_status) setattr(don, "days_since_pledge", days_since_pledge) purchases = [ p for p in purchases if rollup_fn(dc.Item(p.item)) == item_type and not p.complete and p.order_type != OrderType.Donation ] deliveries = [list(p.deliveries.all()) for p in purchases] # flatten deliveries = sum(deliveries, []) deliveries = sorted(deliveries, key=lambda d: d.delivery_date) inventory = [ i for i in Inventory.active() if rollup_fn(dc.Item(i.item)) == item_type ] aggregation = aggregations.asset_rollup( time_range=time_range, demand_calculation_config=DemandCalculationConfig(), supply_cols=supply_cols, ) filtered_aggregation = { item: agg for item, agg in aggregation.items() if rollup_fn(item) == item_type } return DrilldownResult(purchases, deliveries, inventory, donations, aggregation=filtered_aggregation)
def to_dataclass(self): return dc.Delivery( item=dc.Item(self.purchase.item).display(), description=self.purchase.description, delivery_date=self.delivery_date, quantity=self.quantity, vendor=self.purchase.vendor, source=self.source.display(), )
def mayoral_rollup(row: str): return dc.Item(row).to_mayoral_category()