def policies_show(request, id, title, render_callback=None): # Get request context c = get_context(request, css_class='body-policies', title='') c['policy_uid'] = id # Get the budget breakdown c['functional_breakdown'] = BudgetBreakdown(['programme']) c['economic_breakdown'] = BudgetBreakdown( ['chapter', 'article', 'heading']) c['funding_breakdown'] = BudgetBreakdown(['source', 'fund']) c['institutional_breakdown'] = BudgetBreakdown( [_year_tagged_section, _year_tagged_department]) get_budget_breakdown("fc.policy = %s and e.level = 'comunidad'", [id], [ c['functional_breakdown'], c['economic_breakdown'], c['funding_breakdown'], c['institutional_breakdown'] ]) # Additional data needed by the view show_side = 'expense' populate_stats(c) populate_descriptions(c) populate_years(c, 'functional_breakdown', 'comunidad') populate_area_descriptions(c, ['functional', 'funding', show_side]) _populate_csv_settings(c, 'policy', id) _set_show_side(c, show_side) _set_full_breakdown(c, True) c['name'] = c['descriptions']['functional'].get(c['policy_uid']) c['title_prefix'] = c['name'] return render(c, render_callback, 'policies/show.html')
def entities_compare(request, c, entity_left, entity_right): c['entity_left'] = entity_left c['entity_right'] = entity_right # Get the budget breakdowns # XXX: No good functional data at this level so far # c['functional_breakdown_left'] = BudgetBreakdown(['policy']) c['economic_breakdown_left'] = BudgetBreakdown(['chapter', 'article']) get_budget_breakdown("e.name = %s and ec.chapter <> 'X'", [entity_left.name], [c['economic_breakdown_left']]) c['economic_breakdown_right'] = BudgetBreakdown(['chapter', 'article']) get_budget_breakdown("e.name = %s and ec.chapter <> 'X'", [entity_right.name], [c['economic_breakdown_right']]) # Additional data needed by the view populate_level(c, entity_left.level) populate_entity_stats(c, entity_left, 'stats_left') populate_entity_stats(c, entity_right, 'stats_right') populate_entity_descriptions(c, entity_left) populate_area_descriptions(c, ['income', 'expense']) populate_comparison_years(c, 'economic_breakdown_left', 'economic_breakdown_right') populate_entities(c, entity_left.level) return render_to_response('entities/compare.html', c)
def entities_show(request, c, entity, render_callback=None): # Prepare the budget breakdowns c['financial_expense_breakdown'] = BudgetBreakdown() c['functional_breakdown'] = BudgetBreakdown(['policy', 'programme']) if entity.level == 'comunidad': c['economic_breakdown'] = BudgetBreakdown(['article', 'heading']) else: # Small entities have a varying level of detail: often we don't have any breakdown below # chapter, so we have to start there. Also, to be honest, the heading level doesn't add # much to what you get with articles. c['economic_breakdown'] = BudgetBreakdown(['chapter', 'article']) get_budget_breakdown("e.id = %s and fc.area <> 'X'", [entity.id], [], get_financial_breakdown_callback(c)) get_budget_breakdown("e.id = %s and ec.chapter <> 'X'", [entity.id], [c['economic_breakdown']]) # Additional data needed by the view populate_level(c, entity.level) populate_entity_stats(c, entity) populate_entity_descriptions(c, entity) populate_years(c, 'economic_breakdown', entity.level) populate_area_descriptions(c, ['functional', 'income', 'expense']) c['display_functional_view'] = True _set_full_breakdown(c, entity.level == 'comunidad') c['entity'] = entity return render(c, render_callback, 'entities/show.html')
def programmes_show(request, id, title, render_callback=None): # Retrieve the entity to display from the given slug main_entity = Entity.objects.main_entity() # Get request context c = get_context(request, css_class='body-policies', title='') c['programme_id'] = id c['programme'] = FunctionalCategory.objects.filter( budget__entity=main_entity, programme=id)[0] c['policy'] = FunctionalCategory.objects.filter( budget__entity=main_entity, policy=c['programme'].policy, function__isnull=True)[0] # Ignore if possible the descriptions for execution data, they are truncated and ugly programme_descriptions = {} def _populate_programme_descriptions(column_name, item): if not item.actual or not item.uid() in programme_descriptions: programme_descriptions[item.uid()] = getattr(item, 'description') # Get the budget breakdown. # The functional breakdown is an empty one, as we're at the deepest level, but since # we're going to be displaying this data in the policy page we send a blank one c['functional_breakdown'] = BudgetBreakdown([]) c['economic_breakdown'] = BudgetBreakdown( ['chapter', 'article', 'heading', 'uid']) c['funding_breakdown'] = BudgetBreakdown(['source', 'fund']) c['institutional_breakdown'] = BudgetBreakdown( [_year_tagged_section, _year_tagged_department]) get_budget_breakdown("fc.programme = %s and e.level = 'comunidad'", [id], [ c['economic_breakdown'], c['funding_breakdown'], c['institutional_breakdown'] ], _populate_programme_descriptions) # Note we don't modify the original descriptions, we're working on a copy (of the hash, # which is made of references to the hashes containing the descriptions themselves). c['descriptions'] = Budget.objects.get_all_descriptions( Entity.objects.region()).copy() programme_descriptions.update(c['descriptions']['expense']) c['descriptions']['expense'] = programme_descriptions c['name'] = c['descriptions']['functional'].get(c['programme_id']) c['title_prefix'] = c['name'] # Additional data needed by the view show_side = 'expense' populate_stats(c) populate_years(c, 'institutional_breakdown', 'comunidad') populate_area_descriptions(c, ['functional', 'funding', show_side]) _populate_csv_settings(c, 'programme', id) _set_show_side(c, show_side) _set_full_breakdown(c, True) return render(c, render_callback, 'policies/show.html')
def articles_show(request, id, title, show_side, render_callback=None): # Retrieve the entity to display from the given slug main_entity = Entity.objects.main_entity() # Get request context c = get_context(request, css_class='body-policies', title='') c['article_id'] = id c['article'] = EconomicCategory.objects.filter( budget__entity=main_entity, article=id, expense=(show_side == 'expense'))[0] # Ignore if possible the descriptions for execution data, they are truncated and ugly article_descriptions = {} def _populate_article_descriptions(column_name, item): if not item.actual or not item.uid() in article_descriptions: article_descriptions[item.uid()] = getattr(item, 'description') # Get the budget breakdown. # The functional one is used only when showing expenses. c['functional_breakdown'] = BudgetBreakdown(['policy', 'programme']) c['economic_breakdown'] = BudgetBreakdown(['heading', 'uid']) c['funding_breakdown'] = BudgetBreakdown(['source', 'fund']) c['institutional_breakdown'] = BudgetBreakdown( [_year_tagged_section, _year_tagged_department]) get_budget_breakdown("ec.article = %s and e.level = 'comunidad'", [id], [ c['functional_breakdown'], c['economic_breakdown'], c['funding_breakdown'], c['institutional_breakdown'] ], _populate_article_descriptions) # Note we don't modify the original descriptions, we're working on a copy (of the hash, # which is made of references to the hashes containing the descriptions themselves). c['descriptions'] = Budget.objects.get_all_descriptions( Entity.objects.region()).copy() article_descriptions.update(c['descriptions'][show_side]) c['descriptions'][show_side] = article_descriptions c['name'] = c['descriptions'][show_side].get(c['article_id']) c['title_prefix'] = c['name'] # Additional data needed by the view populate_stats(c) populate_years(c, 'institutional_breakdown', 'comunidad') populate_area_descriptions(c, ['functional', 'funding', show_side]) _populate_csv_settings(c, 'article', id) _set_show_side(c, show_side) _set_full_breakdown(c, True) return render(c, render_callback, 'policies/show.html')
def budgets(request): c = get_context(request, css_class='body-counties', title='') # Income/expense breakdown c['functional_breakdown'] = BudgetBreakdown(['policy', 'programme']) c['economic_breakdown'] = BudgetBreakdown(['article', 'heading']) for item in BudgetItem.objects.each_denormalized("e.level = %s", ['comunidad']): column_name = year_column_name(item) if not item.is_financial(): c['functional_breakdown'].add_item(column_name, item) c['economic_breakdown'].add_item(column_name, item) # Additional data needed by the view populate_stats(c) populate_descriptions(c) populate_years(c, 'functional_breakdown') return render_to_response('budgets/index.html', c)
def tax_receipt(request): c = get_context(request, css_class='body-tax-receipt', title='') # Get latest budget data populate_latest_budget(c) populate_descriptions(c) c['default_income'] = 30000 # Get the budget breakdown c['breakdown'] = BudgetBreakdown(['policy', 'programme']) for item in BudgetItem.objects.each_denormalized("b.id = %s", [c['latest_budget'].id]): c['breakdown'].add_item(c['latest_budget'].name(), item) return render_to_response('tax_receipt/index.html', c)
def welcome(request): c = get_context(request, css_class='body-welcome', title='Inicio') c['formatter'] = add_thousands_separator # Retrieve front page examples populate_latest_budget(c) c['featured_programmes'] = (FunctionalCategory.objects.filter( budget=c['latest_budget']).filter( programme__in=['24121', '24221', '35131'])) # Calculate subtotals for the selected programmes c['breakdown'] = BudgetBreakdown(['programme']) for programme in c['featured_programmes']: for item in programme.budgetitem_set.all(): c['breakdown'].add_item(c['latest_budget'].year, item) return render_to_response('welcome/index.html', c)
def entities_index(request, c, level, render_callback=None): # Get the budget breakdown c['economic_breakdown'] = BudgetBreakdown(['name']) # The top level entity has a nicely broken down budget, where each item is classified across # 4 dimensions. For smaller entities, however, we have two separate breakdowns as input, # that are loaded separately, with dummy values ('X') assigned to the three unknown dimensions. # To avoid double counting, we must calculate breakdowns along a dimension including only # those items for which we know the category (i.e. not 'X') get_budget_breakdown("e.level = %s and ec.chapter <> 'X'", [level], [c['economic_breakdown']]) # Additional data needed by the view populate_level(c, level) populate_level_stats(c, level) populate_years(c, 'economic_breakdown', level) populate_entities(c, level) # XXX: The percentage format in pages listing entities is tricky and confusing, partly because # we have many gaps in the data which vary each year, so I'm hiding the drop-down option for now. c['hide_percentage_format'] = True return render(c, render_callback, 'entities/index.html')
def entities_show_article(request, c, entity, id, title, show_side, render_callback=None): # Get request context c = get_context(request, css_class='body-policies', title='') c['article_id'] = id c['is_chapter'] = len(id) <= 1 if c['is_chapter']: c['article'] = EconomicCategory.objects.filter( budget__entity=entity, chapter=id, expense=(show_side == 'expense'))[0] else: c['article'] = EconomicCategory.objects.filter( budget__entity=entity, article=id, expense=(show_side == 'expense'))[0] # Ignore if possible the descriptions for execution data, they are truncated and ugly article_descriptions = {} def _populate_article_descriptions(column_name, item): if not item.actual or not item.uid() in article_descriptions: article_descriptions[item.uid()] = getattr(item, 'description') # Get the budget breakdown. # The functional breakdown is an empty one because our small entity data is not fully broken, # down but since we're going to be displaying this data in the policy page we send a blank one c['functional_breakdown'] = BudgetBreakdown([]) if c['is_chapter']: # XXX: Some entities combine different levels of data detail along the years. Trying # to display detailed categories (articles, headings) looks bad on the visualization, # because some years just 'disappear'. So we take the 'safe route', just visualizing # the chapter total. c['economic_breakdown'] = BudgetBreakdown(['chapter', 'article']) query = "ec.chapter = %s and e.id = %s" else: c['economic_breakdown'] = BudgetBreakdown(['heading', 'uid']) query = "ec.article = %s and e.id = %s" c['funding_breakdown'] = BudgetBreakdown(['source', 'fund']) c['institutional_breakdown'] = BudgetBreakdown( [_year_tagged_section, _year_tagged_department]) get_budget_breakdown(query, [id, entity.id], [ c['economic_breakdown'], c['funding_breakdown'], c['institutional_breakdown'] ], _populate_article_descriptions) # Note we don't modify the original descriptions, we're working on a copy (of the hash, # which is made of references to the hashes containing the descriptions themselves). c['descriptions'] = Budget.objects.get_all_descriptions(entity).copy() article_descriptions.update(c['descriptions'][show_side]) c['descriptions'][show_side] = article_descriptions c['name'] = c['descriptions'][show_side].get(c['article_id']) c['title_prefix'] = c['name'] + ' - ' + entity.name # Additional data needed by the view populate_level(c, entity.level) populate_entity_stats(c, entity) populate_years(c, 'institutional_breakdown', entity.level) populate_area_descriptions(c, ['functional', 'funding', show_side]) _populate_csv_settings(c, 'article', id) _set_show_side(c, show_side) _set_full_breakdown(c, False) c['entity'] = entity return render(c, render_callback, 'policies/show.html')