def setUp(self): # Example url for # CEAT, 1630 dbh, NoEastXXX # eco.json?otmcode=CEAT&diameter=1630®ion=NoEastXXX def mockbenefits(*args, **kwargs): benefits = { "Benefits": { "aq_nox_avoided": 0.6792, "aq_nox_dep": 0.371, "aq_ozone_dep": 0.775, "aq_pm10_avoided": 0.0436, "aq_pm10_dep": 0.491, "aq_sox_avoided": 0.372, "aq_sox_dep": 0.21, "aq_voc_avoided": 0.0254, "bvoc": -0.077, "co2_avoided": 255.5, "co2_sequestered": 0, "co2_storage": 6575, "electricity": 187, "hydro_interception": 12.06, "natural_gas": 5834.1 } } return (benefits, None) region = ITreeRegion.objects.get(code='NoEastXXX') p = region.geometry.point_on_surface converter = BenefitCurrencyConversion(currency_symbol='$', electricity_kwh_to_currency=1.0, natural_gas_kbtu_to_currency=1.0, co2_lb_to_currency=1.0, o3_lb_to_currency=1.0, nox_lb_to_currency=1.0, pm10_lb_to_currency=1.0, sox_lb_to_currency=1.0, voc_lb_to_currency=1.0, h20_gal_to_currency=1.0) converter.save() self.instance = make_instance(is_public=True, point=p) self.instance.eco_benefits_conversion = converter self.instance.save() self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_diameter=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) self.origBenefitFn = ecobackend.json_benefits_call ecobackend.json_benefits_call = mockbenefits
def setUp(self): self.instance = make_instance() self.conversions =\ BenefitCurrencyConversion.get_default_for_region('PiedmtCLT') self.conversions.save() self.instance.eco_benefits_conversion = self.conversions self.instance.save() self.commander = make_commander_user(self.instance)
def forwards(self, orm): # Logic for getting a BenefitCurrencyConversion by region is on the model # But we need to save it using an orm['...'] model from treemap.models import BenefitCurrencyConversion as CurrentCurrencyConversion FrozenCurrencyConversion = orm['treemap.BenefitCurrencyConversion'] instances = orm['treemap.Instance'].objects.all() for instance in instances: current_conversion =\ CurrentCurrencyConversion.get_default_for_point(instance.bounds.centroid) if current_conversion: frozen_conversion = FrozenCurrencyConversion() for field, value in current_conversion.__dict__.iteritems(): setattr(frozen_conversion, field, value) frozen_conversion.save() instance.eco_benefits_conversion = frozen_conversion instance.save()
def handle(self, *args, **options): if len(args) != 1: raise Exception( 'Expected instance name as the first argument') name = args[0] if not options['user']: raise Exception('An admin user must be specified. with "--user"') if options.get('center', None) and options.get('geojson', None): raise Exception('You must specifiy only one of ' '"center" and "geojson"') elif (not options.get('center', None) and not options.get('geojson', None)): raise Exception('You must specifiy at least one of ' '"center" and "geojson"') if options['center']: center = options['center'].split(',') if len(center) != 2: raise Exception('Center should be a lon,lat pair') center_pt = Point(float(center[0]), float(center[1]), srid=4326) # Bounding box built in web mercator to have units in meters center_pt.transform(3857) x = center_pt.x y = center_pt.y instance_bounds = InstanceBounds.create_from_point(x, y) else: geom = GEOSGeometry(open(options['geojson'], srid=4326).read()) instance_bounds = InstanceBounds.objects.create(geom=geom) if not options.get('url_name', None): raise Exception('You must specify a "url_name" starting with a ' 'lowercase letter and containing lowercase ' 'letters, numbers, and dashes ("-")') url_name = options.get('url_name') instance = Instance( config={}, name=name, bounds=instance_bounds, is_public=True, url_name=url_name) instance.seed_with_dummy_default_role() instance.full_clean() instance.save() instance.boundaries = Boundary.objects.filter( geom__intersects=instance_bounds.geom) role = Role.objects.create( name='user', instance=instance, rep_thresh=0, default_permission=FieldPermission.WRITE_DIRECTLY) create_stewardship_udfs(instance) add_species_to_instance(instance) add_default_permissions(instance, roles=[role]) eco_benefits_conversion = \ BenefitCurrencyConversion.get_default_for_point(Point(x, y)) if eco_benefits_conversion: eco_benefits_conversion.save() instance.eco_benefits_conversion = eco_benefits_conversion instance.default_role = role instance.save() user = User.objects.get(username=options['user']) InstanceUser( instance=instance, user=user, role=role, admin=True).save_with_user(user)
def _get_default_conversions(): currency_symbol = locale.localeconv()['currency_symbol'] or '$' return BenefitCurrencyConversion(currency_symbol=currency_symbol)
def handle(self, *args, **options): if len(args) != 1: raise Exception('Expected instance name as the first argument') name = args[0] if not options['user']: raise Exception('An admin user must be specified. with "--user"') if options.get('center', None) and options.get('geojson', None): raise Exception('You must specifiy only one of ' '"center" and "geojson"') elif (not options.get('center', None) and not options.get('geojson', None)): raise Exception('You must specifiy at least one of ' '"center" and "geojson"') if options['center']: center = options['center'].split(',') if len(center) != 2: raise Exception('Center should be a lon,lat pair') center_pt = Point(float(center[0]), float(center[1]), srid=4326) # Bounding box built in web mercator to have units in meters center_pt.transform(3857) x = center_pt.x y = center_pt.y instance_bounds = InstanceBounds.create_from_point(x, y) else: geom = GEOSGeometry(open(options['geojson'], srid=4326).read()) instance_bounds = InstanceBounds.objects.create(geom=geom) if not options.get('url_name', None): raise Exception('You must specify a "url_name" starting with a ' 'lowercase letter and containing lowercase ' 'letters, numbers, and dashes ("-")') url_name = options.get('url_name') instance = Instance(config={}, name=name, bounds=instance_bounds, is_public=True, url_name=url_name) instance.seed_with_dummy_default_role() instance.full_clean() instance.save() instance.boundaries = Boundary.objects.filter( geom__intersects=instance_bounds.geom) role = Role.objects.create( name='user', instance=instance, rep_thresh=0, default_permission_level=FieldPermission.WRITE_DIRECTLY) create_stewardship_udfs(instance) add_species_to_instance(instance) add_default_permissions(instance, roles=[role]) add_instance_permissions([role]) eco_benefits_conversion = \ BenefitCurrencyConversion.get_default_for_instance(instance) if eco_benefits_conversion: eco_benefits_conversion.save() instance.eco_benefits_conversion = eco_benefits_conversion instance.default_role = role instance.save() user = User.objects.get(username=options['user']) InstanceUser(instance=instance, user=user, role=role, admin=True).save_with_user(user)