def _add_eco_benefits_to_context_dict(instance, feature, context): FeatureClass = feature.__class__ benefits, basis, failure_code = FeatureClass.benefits.benefits_for_object(instance, feature) if failure_code in ECOBENEFIT_FAILURE_CODES_AND_PATTERNS: context[failure_code] = True elif benefits: context.update(format_benefits(instance, benefits, basis))
def _add_eco_benefits_to_context_dict(instance, feature, context): FeatureClass = feature.__class__ benefits, basis, failure_code = FeatureClass.benefits\ .benefits_for_object( instance, feature) if failure_code in ECOBENEFIT_FAILURE_CODES_AND_PATTERNS: context[failure_code] = True elif benefits: context.update(format_benefits(instance, benefits, basis))
def _add_eco_benefits_to_context_dict(instance, feature, context): FeatureClass = feature.__class__ if not hasattr(FeatureClass, "benefits"): return benefits, basis, error = FeatureClass.benefits.benefits_for_object(instance, feature) if error == BAD_CODE_PAIR: context["invalid_eco_pair"] = True elif benefits: context.update(format_benefits(instance, benefits, basis))
def _run_model(instance, growth_model, scenario, region_code): n_years = scenario['years'] yearly_counts, planted_trees = growth_model.run(scenario) eco_trees = _model_trees_to_eco_trees(planted_trees, region_code) eco_input = { 'region': region_code, 'instance_id': str(instance.id), 'years': n_years, 'scenario_trees': eco_trees } eco, err = ecobackend.json_benefits_call('eco_scenario.json', eco_input, post=True, convert_params=False) if err: raise Exception(err) total_eco = compute_currency_and_transform_units(instance, eco['Total']) total_eco = format_benefits(instance, total_eco, None, digits=0) currency_symbol = total_eco['currency_symbol'] total_eco = _list_for_display(total_eco['benefits']) yearly_eco = [ compute_currency_and_transform_units(instance, benefits) for benefits in eco['Years'] ] yearly_eco = [_list_for_display(benefits) for benefits in yearly_eco] yearly_eco_condensed = yearly_eco[0] for i in range(0, len(yearly_eco_condensed)): yearly_eco_condensed[i] = { 'label': yearly_eco_condensed[i]['label'], 'unit': yearly_eco_condensed[i]['unit'], 'values': [benefits[i]['value'] for benefits in yearly_eco], 'currencies': [benefits[i]['currency'] for benefits in yearly_eco] } year_headers = [_('Year %(n)s' % {'n': i}) for i in range(1, n_years + 1)] growth_csv_data = _growth_csv_data(instance, year_headers, planted_trees) return { 'years': n_years, 'yearly_counts': yearly_counts, 'total_eco': total_eco, 'yearly_eco': yearly_eco_condensed, 'currency_symbol': currency_symbol, 'currency_axis_label': "%s %s" % (currency_symbol, _('saved')), 'eco_csv_header': _eco_csv_header(year_headers), 'growth_csv_data': growth_csv_data, }
def _add_eco_benefits_to_context_dict(instance, feature, context): FeatureClass = feature.__class__ benefits, basis, error = FeatureClass.benefits\ .benefits_for_object( instance, feature) if error in ECOBENEFIT_ERRORS: context[error] = True elif benefits: context.update(format_benefits(instance, benefits, basis))
def _run_model(instance, growth_model, scenario, region_code): n_years = scenario['years'] yearly_counts, planted_trees = growth_model.run(scenario) eco_trees = _model_trees_to_eco_trees(planted_trees, region_code) eco_input = { 'region': region_code, 'instance_id': str(instance.id), 'years': n_years, 'scenario_trees': eco_trees } eco, err = ecobackend.json_benefits_call('eco_scenario.json', eco_input, post=True, convert_params=False) if err: raise Exception(err) total_eco = compute_currency_and_transform_units(instance, eco['Total']) total_eco = format_benefits(instance, total_eco, None, digits=0) currency_symbol = total_eco['currency_symbol'] total_eco = _list_for_display(total_eco['benefits']) yearly_eco = [compute_currency_and_transform_units(instance, benefits) for benefits in eco['Years']] yearly_eco = [_list_for_display(benefits) for benefits in yearly_eco] yearly_eco_condensed = yearly_eco[0] for i in range(0, len(yearly_eco_condensed)): yearly_eco_condensed[i] = { 'label': yearly_eco_condensed[i]['label'], 'unit': yearly_eco_condensed[i]['unit'], 'values': [benefits[i]['value'] for benefits in yearly_eco], 'currencies': [benefits[i]['currency'] for benefits in yearly_eco] } year_headers = [_('Year %(n)s' % {'n': i}) for i in range(1, n_years + 1)] growth_csv_data = _growth_csv_data(instance, year_headers, planted_trees) return { 'years': n_years, 'yearly_counts': yearly_counts, 'total_eco': total_eco, 'yearly_eco': yearly_eco_condensed, 'currency_symbol': currency_symbol, 'currency_axis_label': "%s %s" % (currency_symbol, _('saved')), 'eco_csv_header': _eco_csv_header(year_headers), 'growth_csv_data': growth_csv_data, }
def search_tree_benefits(request, instance): filter_str = request.REQUEST.get('q', '') display_str = request.REQUEST.get('show', '') hide_summary_text = request.REQUEST.get('hide_summary', 'false') hide_summary = hide_summary_text.lower() == 'true' filter = Filter(filter_str, display_str, instance) total_plots = get_cached_plot_count( filter, lambda: filter.get_object_count(Plot)) benefits, basis = get_benefits_for_filter(filter) # Inject the plot count as a basis for tree benefit calcs basis.get('plot', {})['n_plots'] = total_plots # We also want to inject the total currency amount saved # for plot-based items except CO2 stored total_currency_saved = 0 for benefit_name, benefit in benefits.get('plot', {}).iteritems(): if benefit_name != BenefitCategory.CO2STORAGE: currency = benefit.get('currency', 0.0) if currency: total_currency_saved += currency # save it as if it were a normal benefit so we get formatting # and currency conversion benefits.get('plot', {})['totals'] = { 'value': None, 'currency': total_currency_saved, 'label': _('Total annual benefits') } formatted = format_benefits(instance, benefits, basis, digits=0) formatted['hide_summary'] = hide_summary formatted['tree_count_label'] = ( 'tree,' if basis['plot']['n_total'] == 1 else 'trees,') formatted['plot_count_label'] = ( 'planting site' if basis['plot']['n_plots'] == 1 else 'planting sites') if instance.has_resources and 'resource' in basis: formatted['plot_count_label'] += ',' return formatted
def search_tree_benefits(request, instance): filter_str = request.REQUEST.get('q', '') display_str = request.REQUEST.get('show', '') hide_summary_text = request.REQUEST.get('hide_summary', 'false') hide_summary = hide_summary_text.lower() == 'true' filter = Filter(filter_str, display_str, instance) total_plots = get_cached_plot_count(filter) benefits, basis = get_benefits_for_filter(filter) # Inject the plot count as a basis for tree benefit calcs basis.get('plot', {})['n_plots'] = total_plots # We also want to inject the total currency amount saved # for plot-based items except CO2 stored total_currency_saved = 0 for benefit_name, benefit in benefits.get('plot', {}).iteritems(): if benefit_name != BenefitCategory.CO2STORAGE: currency = benefit.get('currency', 0.0) if currency: total_currency_saved += currency # save it as if it were a normal benefit so we get formatting # and currency conversion benefits.get('plot', {})['totals'] = { 'value': None, 'currency': total_currency_saved, 'label': _('Total annual benefits') } formatted = format_benefits(instance, benefits, basis, digits=0) formatted['hide_summary'] = hide_summary formatted['tree_count_label'] = ( 'tree,' if basis['plot']['n_total'] == 1 else 'trees,') formatted['plot_count_label'] = ( 'planting site' if basis['plot']['n_plots'] == 1 else 'planting sites') if instance.has_resources and 'resource' in basis: formatted['plot_count_label'] += ',' return formatted
def test_all_ecobenefits(self): p = self._center_as_3857() plot = Plot(geom=p, instance=self.instance) plot.save_with_user(self.user) tree = Tree(plot=plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) tree.save_with_user(self.user) p.x += 1.1 p.y += 1.1 box = self._box_around_point(p) bioswale = Bioswale(instance=self.instance, geom=p, polygon=box, feature_type='Bioswale', drainage_area=100.0) bioswale.save_with_user(self.user) filter = Filter('', '', self.instance) benefits, basis = get_benefits_for_filter(filter) self.assertIn('plot', benefits) plot_benefits = benefits['plot'] plot_categories = set(plot_benefits.keys()) self.assertSetEqual(plot_categories, set(BenefitCategory.GROUPS)) plot_currencies = { cat: benefit.get('currency', None) for cat, benefit in plot_benefits.items() } self.assertIsNotNone(min(plot_currencies.values())) expected_total_currency = sum( [benefit['currency'] for benefit in plot_benefits.values()]) - \ plot_benefits[BenefitCategory.CO2STORAGE]['currency'] + \ benefits['resource'][BenefitCategory.STORMWATER]['currency'] formatted = format_benefits(self.instance, benefits, basis, digits=0) self.assertAlmostEqual(formatted['benefits_total_currency'], expected_total_currency, 3)
def search_tree_benefits(request, instance): filter_str = request.GET.get('q', '') display_str = request.GET.get('show', '') hide_summary_text = request.GET.get('hide_summary', 'false') hide_summary = hide_summary_text.lower() == 'true' filter = Filter(filter_str, display_str, instance) total_plots = get_cached_plot_count(filter) benefits, basis = get_benefits_for_filter(filter) # Inject the plot count as a basis for tree benefit calcs basis.get('plot', {})['n_plots'] = total_plots formatted = format_benefits(instance, benefits, basis, digits=0) n_trees = basis['plot']['n_total'] n_plots = basis['plot']['n_plots'] n_empty_plots = n_plots - n_trees n_resources = 0 tree_count_label = ungettext('tree', 'trees', n_trees) + ',' empty_plot_count_label = ungettext( 'empty planting site', 'empty planting sites', n_empty_plots) has_resources = instance.has_resources and 'resource' in basis if has_resources: n_resources = basis['resource']['n_total'] empty_plot_count_label += ',' context = { 'n_trees': n_trees, 'n_empty_plots': n_empty_plots, 'n_resources': n_resources, 'tree_count_label': tree_count_label, 'empty_plot_count_label': empty_plot_count_label, 'has_resources': has_resources, 'hide_summary': hide_summary, 'single_result': _single_result_context(instance, n_plots, n_resources, filter) } context.update(formatted) return context
def test_all_ecobenefits(self): p = self._center_as_3857() plot = Plot(geom=p, instance=self.instance) plot.save_with_user(self.user) tree = Tree(plot=plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) tree.save_with_user(self.user) p.x += 1.1 p.y += 1.1 box = self._box_around_point(p) bioswale = Bioswale(instance=self.instance, geom=p, polygon=box, feature_type='Bioswale', drainage_area=100.0) bioswale.save_with_user(self.user) filter = Filter('', '', self.instance) benefits, basis = get_benefits_for_filter(filter) self.assertIn('plot', benefits) plot_benefits = benefits['plot'] plot_categories = set(plot_benefits.keys()) self.assertSetEqual(plot_categories, set(BenefitCategory.GROUPS)) plot_currencies = { cat: benefit.get('currency', None) for cat, benefit in plot_benefits.items()} self.assertIsNotNone(min(plot_currencies.values())) expected_total_currency = sum( [benefit['currency'] for benefit in plot_benefits.values()]) - \ plot_benefits[BenefitCategory.CO2STORAGE]['currency'] + \ benefits['resource'][BenefitCategory.STORMWATER]['currency'] formatted = format_benefits(self.instance, benefits, basis, digits=0) self.assertAlmostEqual(formatted['benefits_total_currency'], expected_total_currency, 3)
def search_tree_benefits(request, instance): filter_str = request.REQUEST.get("q", "") display_str = request.REQUEST.get("show", "") hide_summary_text = request.REQUEST.get("hide_summary", "false") hide_summary = hide_summary_text.lower() == "true" filter = Filter(filter_str, display_str, instance) total_plots = filter.get_object_count(Plot) benefits, basis = get_benefits_for_filter(filter) # Inject the plot count as a basis for tree benefit calcs basis.get("plot", {})["n_plots"] = total_plots # We also want to inject the total currency amount saved # for plot-based items except CO2 stored total_currency_saved = 0 for benefit_name, benefit in benefits.get("plot", {}).iteritems(): if benefit_name != "co2storage": currency = benefit.get("currency", 0.0) if currency: total_currency_saved += currency # save it as if it were a normal benefit so we get formatting # and currency conversion benefits.get("plot", {})["totals"] = { "value": None, "currency": total_currency_saved, "label": trans("Total annual benefits"), } formatted = format_benefits(instance, benefits, basis) formatted["hide_summary"] = hide_summary formatted["tree_count_label"] = "tree," if basis["plot"]["n_total"] == 1 else "trees," formatted["plot_count_label"] = "planting site" if basis["plot"]["n_plots"] == 1 else "planting sites" if instance.supports_resources and "resource" in benefits: formatted["plot_count_label"] += "," return formatted
def search_tree_benefits(request, instance): filter_str = request.REQUEST.get('q', '') display_str = request.REQUEST.get('show', '') hide_summary_text = request.REQUEST.get('hide_summary', 'false') hide_summary = hide_summary_text.lower() == 'true' filter = Filter(filter_str, display_str, instance) total_plots = get_cached_plot_count(filter) benefits, basis = get_benefits_for_filter(filter) # Inject the plot count as a basis for tree benefit calcs basis.get('plot', {})['n_plots'] = total_plots # We also want to inject the total currency amount saved # for plot-based items except CO2 stored total_currency_saved = 0 for benefit_name, benefit in benefits.get('plot', {}).iteritems(): if benefit_name != BenefitCategory.CO2STORAGE: currency = benefit.get('currency', 0.0) if currency: total_currency_saved += currency # save it as if it were a normal benefit so we get formatting # and currency conversion benefits.get('plot', {})['totals'] = { 'value': None, 'currency': total_currency_saved, 'label': _('Total annual benefits') } formatted = format_benefits(instance, benefits, basis, digits=0) n_trees = basis['plot']['n_total'] n_plots = basis['plot']['n_plots'] n_empty_plots = n_plots - n_trees n_resources = 0 tree_count_label = ungettext('tree', 'trees', n_trees) + ',' empty_plot_count_label = ungettext('empty planting site', 'empty planting sites', n_empty_plots) has_resources = instance.has_resources and 'resource' in basis if has_resources: n_resources = basis['resource']['n_total'] empty_plot_count_label += ',' context = { 'n_trees': n_trees, 'n_empty_plots': n_empty_plots, 'n_resources': n_resources, 'tree_count_label': tree_count_label, 'empty_plot_count_label': empty_plot_count_label, 'has_resources': has_resources, 'hide_summary': hide_summary, 'single_result': _single_result_context(instance, n_plots, n_resources, filter) } context.update(formatted) return context
def search_tree_benefits(request, instance): filter_str = request.REQUEST.get('q', '') display_str = request.REQUEST.get('show', '') hide_summary_text = request.REQUEST.get('hide_summary', 'false') hide_summary = hide_summary_text.lower() == 'true' filter = Filter(filter_str, display_str, instance) total_plots = get_cached_plot_count(filter) benefits, basis = get_benefits_for_filter(filter) # Inject the plot count as a basis for tree benefit calcs basis.get('plot', {})['n_plots'] = total_plots # We also want to inject the total currency amount saved # for plot-based items except CO2 stored total_currency_saved = 0 for benefit_name, benefit in benefits.get('plot', {}).iteritems(): if benefit_name != BenefitCategory.CO2STORAGE: currency = benefit.get('currency', 0.0) if currency: total_currency_saved += currency # save it as if it were a normal benefit so we get formatting # and currency conversion benefits.get('plot', {})['totals'] = { 'value': None, 'currency': total_currency_saved, 'label': _('Total annual benefits') } formatted = format_benefits(instance, benefits, basis, digits=0) n_trees = basis['plot']['n_total'] n_plots = basis['plot']['n_plots'] n_empty_plots = n_plots - n_trees n_resources = 0 tree_count_label = ungettext('tree', 'trees', n_trees) + ',' empty_plot_count_label = ungettext( 'empty planting site', 'empty planting sites', n_empty_plots) has_resources = instance.has_resources and 'resource' in basis if has_resources: n_resources = basis['resource']['n_total'] empty_plot_count_label += ',' context = { 'n_trees': n_trees, 'n_empty_plots': n_empty_plots, 'n_resources': n_resources, 'tree_count_label': tree_count_label, 'empty_plot_count_label': empty_plot_count_label, 'has_resources': has_resources, 'hide_summary': hide_summary, 'single_result': _single_result_context(instance, n_plots, n_resources, filter) } context.update(formatted) return context