def mkPlot(u, geom=Point(50, 50)): p = Plot(geometry=geom, last_updated_by=u, import_event=ImportEvent.objects.all()[0], present=True, data_owner=u) p.save() return p
def mkPlot(u, geom=Point(50, 50)): p = Plot( geometry=geom, last_updated_by=u, import_event=ImportEvent.objects.all()[0], present=True, data_owner=u) p.save() return p
def test_result_map(self): ################################################################## # Test main result map page # Note -> This page does not depend at all on the request # p1 = Plot(geometry=Point(50, 50), last_updated_by=self.u, import_event=self.ie, present=True, width=100, length=100, data_owner=self.u) p2 = Plot(geometry=Point(60, 50), last_updated_by=self.u, import_event=self.ie, present=True, width=90, length=110, data_owner=self.u) p1.save() p2.save() # For max/min plot size p3 = Plot(geometry=Point(50, 50), last_updated_by=self.u, import_event=self.ie, present=True, width=80, length=120, data_owner=self.u) p4 = Plot(geometry=Point(60, 50), last_updated_by=self.u, import_event=self.ie, present=True, width=70, length=130, data_owner=self.u) p5 = Plot(geometry=Point(60, 50), last_updated_by=self.u, import_event=self.ie, present=True, width=60, length=70, data_owner=self.u) p3.save() p4.save() p5.save() t3 = Tree(plot=p3, species=None, last_updated_by=self.u, import_event=self.ie, present=True) t3.save() t4 = Tree(plot=p4, species=None, last_updated_by=self.u, import_event=self.ie, present=True) t4.save() t5 = Tree(plot=p5, species=None, last_updated_by=self.u, import_event=self.ie, present=True) t5.save() t1 = Tree(plot=p1, species=None, last_updated_by=self.u, import_event=self.ie) t1.present = True current_year = datetime.now().year t1.date_planted = date(1999, 9, 9) t2 = Tree(plot=p2, species=None, last_updated_by=self.u, import_event=self.ie) t1.present = True t1.save() t2.save() set_auto_now(t1, "last_updated", False) t1.last_updated = date(1999, 9, 9) t1.save() response = self.client.get("/map/") req = response.context set_auto_now(t1, "last_updated", True) # t1 and t2 should not be in the latest trees/plots because it excludes superuser edits exp = set([]) got = set([t.pk for t in req['latest_trees']]) self.assertTrue(exp <= got) got = set([t.pk for t in req['latest_plots']]) self.assertTrue(exp <= got) # Check to verify platting dates self.assertEquals(int(req['min_year']), 1999) self.assertEquals(int(req['current_year']), current_year) # Correct min/max plot sizes self.assertEqual(int(req['min_plot']), 60) self.assertEqual(int(req['max_plot']), 130) min_updated = mktime(t1.last_updated.timetuple()) max_updated = mktime(t2.last_updated.timetuple()) self.assertEqual(req['min_updated'], min_updated) self.assertEqual(req['max_updated'], max_updated)
def setUp(self): ###### # Request/Render mock ###### def local_render_to_response(*args, **kwargs): from django.template import loader, RequestContext from django.http import HttpResponse httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} hr = HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) if hasattr(args[1], 'dicts'): hr.request_context = args[1].dicts return hr django.shortcuts.render_to_response = local_render_to_response ###### # Content types ###### r1 = ReputationAction(name="edit verified", description="blah") r2 = ReputationAction(name="edit tree", description="blah") r3 = ReputationAction(name="Administrative Action", description="blah") r4 = ReputationAction(name="add tree", description="blah") r5 = ReputationAction(name="edit plot", description="blah") r6 = ReputationAction(name="add plot", description="blah") r7 = ReputationAction(name="add stewardship", description="blah") r8 = ReputationAction(name="remove stewardship", description="blah") self.ra = [r1, r2, r3, r4, r5, r6, r7, r8] for r in self.ra: r.save() ###### # Set up benefit values ###### bv = BenefitValues(co2=0.02, pm10=9.41, area="InlandValleys", electricity=0.1166, voc=4.69, ozone=5.0032, natural_gas=1.25278, nox=12.79, stormwater=0.0078, sox=3.72, bvoc=4.96) bv.save() self.bv = bv dbh = "[1.0, 2.0, 3.0]" rsrc = Resource(meta_species="BDM_OTHER", electricity_dbh=dbh, co2_avoided_dbh=dbh, aq_pm10_dep_dbh=dbh, region="Sim City", aq_voc_avoided_dbh=dbh, aq_pm10_avoided_dbh=dbh, aq_ozone_dep_dbh=dbh, aq_nox_avoided_dbh=dbh, co2_storage_dbh=dbh, aq_sox_avoided_dbh=dbh, aq_sox_dep_dbh=dbh, bvoc_dbh=dbh, co2_sequestered_dbh=dbh, aq_nox_dep_dbh=dbh, hydro_interception_dbh=dbh, natural_gas_dbh=dbh) rsrc.save() self.rsrc = rsrc ###### # Users ###### u = User.objects.filter(username="******") if u: u = u[0] else: u = User.objects.create_user("jim", "*****@*****.**", "jim") u.is_staff = True u.is_superuser = True u.save() up = UserProfile(user=u) u.reputation = Reputation(user=u) u.reputation.save() self.u = u ####### # Setup geometries -> Two stacked 100x100 squares ####### n1geom = MultiPolygon( Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) n2geom = MultiPolygon( Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101)))) n1 = Neighborhood(name="n1", region_id=2, city="c1", state="PA", county="PAC", geometry=n1geom) n2 = Neighborhood(name="n2", region_id=2, city="c2", state="NY", county="NYC", geometry=n2geom) n1.save() n2.save() z1geom = MultiPolygon( Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) z2geom = MultiPolygon( Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100)))) z1 = ZipCode(zip="19107", geometry=z1geom) z2 = ZipCode(zip="10001", geometry=z2geom) z1.save() z2.save() exgeom1 = MultiPolygon( Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0)))) ex1 = ExclusionMask(geometry=exgeom1, type="building") ex1.save() agn1 = AggregateNeighborhood(annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location=n1) agn2 = AggregateNeighborhood(annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location=n2) agn1.save() agn2.save() self.agn1 = agn1 self.agn2 = agn2 self.z1 = z1 self.z2 = z2 self.n1 = n1 self.n2 = n2 ###### # And we could use a few species... ###### s1 = Species(symbol="s1", genus="testus1", species="specieius1") s2 = Species(symbol="s2", genus="testus2", species="specieius2") s1.save() s2.save() self.s1 = s1 self.s2 = s2 ####### # Create some basic plots ####### ie = ImportEvent(file_name='site_add') ie.save() self.ie = ie p1_no_tree = Plot(geometry=Point(50, 50), last_updated_by=u, import_event=ie, present=True, data_owner=u) p1_no_tree.save() p2_tree = Plot(geometry=Point(51, 51), last_updated_by=u, import_event=ie, present=True, data_owner=u) p2_tree.save() p3_tree_species1 = Plot(geometry=Point(50, 100), last_updated_by=u, import_event=ie, present=True, data_owner=u) p3_tree_species1.save() p4_tree_species2 = Plot(geometry=Point(50, 150), last_updated_by=u, import_event=ie, present=True, data_owner=u) p4_tree_species2.save() t1 = Tree(plot=p2_tree, species=None, last_updated_by=u, import_event=ie) t1.present = True t1.save() t2 = Tree(plot=p3_tree_species1, species=s1, last_updated_by=u, import_event=ie) t2.present = True t2.save() t3 = Tree(plot=p4_tree_species2, species=s2, last_updated_by=u, import_event=ie) t3.present = True t3.save() self.p1_no_tree = p1_no_tree self.p2_tree = p2_tree self.p3_tree_species1 = p3_tree_species1 self.p4_tree_species2 = p4_tree_species2 self.plots = [p1_no_tree, p2_tree, p3_tree_species1, p4_tree_species2] self.t1 = t1 self.t2 = t2 self.t3 = t3
def test_result_map(self): ################################################################## # Test main result map page # Note -> This page does not depend at all on the request # p1 = Plot(geometry=Point(50,50), last_updated_by=self.u, import_event=self.ie,present=True, width=100, length=100, data_owner=self.u) p2 = Plot(geometry=Point(60,50), last_updated_by=self.u, import_event=self.ie,present=True, width=90, length=110, data_owner=self.u) p1.save() p2.save() # For max/min plot size p3 = Plot(geometry=Point(50,50), last_updated_by=self.u, import_event=self.ie,present=True, width=80, length=120, data_owner=self.u) p4 = Plot(geometry=Point(60,50), last_updated_by=self.u, import_event=self.ie,present=True, width=70, length=130, data_owner=self.u) p5 = Plot(geometry=Point(60,50), last_updated_by=self.u, import_event=self.ie,present=True, width=60, length=70, data_owner=self.u) p3.save() p4.save() p5.save() t3 = Tree(plot=p3, species=None, last_updated_by=self.u, import_event=self.ie,present=True) t3.save() t4 = Tree(plot=p4, species=None, last_updated_by=self.u, import_event=self.ie,present=True) t4.save() t5 = Tree(plot=p5, species=None, last_updated_by=self.u, import_event=self.ie,present=True) t5.save() t1 = Tree(plot=p1, species=None, last_updated_by=self.u, import_event=self.ie) t1.present = True current_year = datetime.now().year t1.date_planted = date(1999,9,9) t2 = Tree(plot=p2, species=None, last_updated_by=self.u, import_event=self.ie) t1.present = True t1.save() t2.save() set_auto_now(t1, "last_updated", False) t1.last_updated = date(1999,9,9) t1.save() response = self.client.get("/map/") req = response.context set_auto_now(t1, "last_updated", True) # t1 and t2 should not be in the latest trees/plots because it excludes superuser edits exp = set([]) got = set([t.pk for t in req['latest_trees']]) self.assertTrue(exp <= got) got = set([t.pk for t in req['latest_plots']]) self.assertTrue(exp <= got) # Check to verify platting dates self.assertEquals(int(req['min_year']), 1999) self.assertEquals(int(req['current_year']), current_year) # Correct min/max plot sizes self.assertEqual(int(req['min_plot']), 60) self.assertEqual(int(req['max_plot']), 130) min_updated = mktime(t1.last_updated.timetuple()) max_updated = mktime(t2.last_updated.timetuple()) self.assertEqual(req['min_updated'], min_updated) self.assertEqual(req['max_updated'], max_updated)
def setUp(self): ###### # Request/Render mock ###### def local_render_to_response(*args, **kwargs): from django.template import loader, RequestContext from django.http import HttpResponse httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} hr = HttpResponse( loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) if hasattr(args[1], 'dicts'): hr.request_context = args[1].dicts return hr django.shortcuts.render_to_response = local_render_to_response ###### # Content types ###### r1 = ReputationAction(name="edit verified", description="blah") r2 = ReputationAction(name="edit tree", description="blah") r3 = ReputationAction(name="Administrative Action", description="blah") r4 = ReputationAction(name="add tree", description="blah") r5 = ReputationAction(name="edit plot", description="blah") r6 = ReputationAction(name="add plot", description="blah") r7 = ReputationAction(name="add stewardship", description="blah") r8 = ReputationAction(name="remove stewardship", description="blah") self.ra = [r1,r2,r3,r4,r5,r6,r7,r8] for r in self.ra: r.save() ###### # Set up benefit values ###### bv = BenefitValues(co2=0.02, pm10=9.41, area="InlandValleys", electricity=0.1166,voc=4.69,ozone=5.0032,natural_gas=1.25278, nox=12.79,stormwater=0.0078,sox=3.72,bvoc=4.96) bv.save() self.bv = bv dbh = "[1.0, 2.0, 3.0]" rsrc = Resource(meta_species="BDM_OTHER", electricity_dbh=dbh, co2_avoided_dbh=dbh, aq_pm10_dep_dbh=dbh, region="Sim City", aq_voc_avoided_dbh=dbh, aq_pm10_avoided_dbh=dbh, aq_ozone_dep_dbh=dbh, aq_nox_avoided_dbh=dbh, co2_storage_dbh=dbh,aq_sox_avoided_dbh=dbh, aq_sox_dep_dbh=dbh, bvoc_dbh=dbh, co2_sequestered_dbh=dbh, aq_nox_dep_dbh=dbh, hydro_interception_dbh=dbh, natural_gas_dbh=dbh) rsrc.save() self.rsrc = rsrc ###### # Users ###### u = User.objects.filter(username="******") if u: u = u[0] else: u = User.objects.create_user("jim","*****@*****.**","jim") u.is_staff = True u.is_superuser = True u.save() up = UserProfile(user=u) u.reputation = Reputation(user=u) u.reputation.save() self.u = u ####### # Setup geometries -> Two stacked 100x100 squares ####### n1geom = MultiPolygon(Polygon(((0,0),(100,0),(100,100),(0,100),(0,0)))) n2geom = MultiPolygon(Polygon(((0,101),(101,101),(101,200),(0,200),(0,101)))) n1 = Neighborhood(name="n1", region_id=2, city="c1", state="PA", county="PAC", geometry=n1geom) n2 = Neighborhood(name="n2", region_id=2, city="c2", state="NY", county="NYC", geometry=n2geom) n1.save() n2.save() z1geom = MultiPolygon(Polygon(((0,0),(100,0),(100,100),(0,100),(0,0)))) z2geom = MultiPolygon(Polygon(((0,100),(100,100),(100,200),(0,200),(0,100)))) z1 = ZipCode(zip="19107",geometry=z1geom) z2 = ZipCode(zip="10001",geometry=z2geom) z1.save() z2.save() exgeom1 = MultiPolygon(Polygon(((0,0),(25,0),(25,25),(0,25),(0,0)))) ex1 = ExclusionMask(geometry=exgeom1, type="building") ex1.save() agn1 = AggregateNeighborhood( annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location = n1) agn2 = AggregateNeighborhood( annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location = n2) agn1.save() agn2.save() self.agn1 = agn1 self.agn2 = agn2 self.z1 = z1 self.z2 = z2 self.n1 = n1 self.n2 = n2 ###### # And we could use a few species... ###### s1 = Species(symbol="s1",genus="testus1",species="specieius1") s2 = Species(symbol="s2",genus="testus2",species="specieius2") s1.save() s2.save() self.s1 = s1 self.s2 = s2 ####### # Create some basic plots ####### ie = ImportEvent(file_name='site_add') ie.save() self.ie = ie p1_no_tree = Plot(geometry=Point(50,50), last_updated_by=u, import_event=ie,present=True, data_owner=u) p1_no_tree.save() p2_tree = Plot(geometry=Point(51,51), last_updated_by=u, import_event=ie,present=True, data_owner=u) p2_tree.save() p3_tree_species1 = Plot(geometry=Point(50,100), last_updated_by=u, import_event=ie,present=True, data_owner=u) p3_tree_species1.save() p4_tree_species2 = Plot(geometry=Point(50,150), last_updated_by=u, import_event=ie,present=True, data_owner=u) p4_tree_species2.save() t1 = Tree(plot=p2_tree, species=None, last_updated_by=u, import_event=ie) t1.present = True t1.save() t2 = Tree(plot=p3_tree_species1, species=s1, last_updated_by=u, import_event=ie) t2.present = True t2.save() t3 = Tree(plot=p4_tree_species2, species=s2, last_updated_by=u, import_event=ie) t3.present = True t3.save() self.p1_no_tree = p1_no_tree self.p2_tree = p2_tree self.p3_tree_species1 = p3_tree_species1; self.p4_tree_species2 = p4_tree_species2; self.plots = [p1_no_tree, p2_tree, p3_tree_species1, p4_tree_species2] self.t1 = t1 self.t2 = t2 self.t3 = t3
def commit_row(self): # If this row was already commit... abort if self.plot: self.status = TreeImportRow.SUCCESS self.save() # First validate if not self.validate_row(): return False # Get our data data = self.cleaned self.convert_units(data, { fields.trees.PLOT_WIDTH: self.import_event.plot_width_conversion_factor, fields.trees.PLOT_LENGTH: self.import_event.plot_length_conversion_factor, fields.trees.DIAMETER: self.import_event.diameter_conversion_factor, fields.trees.TREE_HEIGHT: self.import_event.tree_height_conversion_factor, fields.trees.CANOPY_HEIGHT: self.import_event.canopy_height_conversion_factor }) # We need the import event from treemap.models # the names of things are a bit odd here but # self.import_event -> # TreeImportEvent (importer) -> # ImportEvent (treemap) # base_treemap_import_event = self.import_event.base_import_event plot_edited = False tree_edited = False # Initially grab plot from row if it exists plot = self.plot if plot is None: plot = Plot(present=True) # Event if TREE_PRESENT is None, a tree # can still be spawned here if there is # any tree data later tree = plot.current_tree() # Check for an existing tree: if self.model_fields.OPENTREEMAP_ID_NUMBER in data: plot = Plot.objects.get( pk=data[self.model_fields.OPENTREEMAP_ID_NUMBER]) tree = plot.current_tree() else: if data.get(self.model_fields.TREE_PRESENT, False): tree_edited = True if tree is None: tree = Tree(present=True) data_owner = self.import_event.owner for modelkey, importdatakey in TreeImportRow.PLOT_MAP.iteritems(): importdata = data.get(importdatakey, None) if importdata: plot_edited = True setattr(plot, modelkey, importdata) if plot_edited: plot.last_updated_by = data_owner plot.import_event = base_treemap_import_event plot.save() for modelkey, importdatakey in TreeImportRow.TREE_MAP.iteritems(): importdata = data.get(importdatakey, None) if importdata: tree_edited = True if tree is None: tree = Tree(present=True) setattr(tree, modelkey, importdata) if tree_edited: tree.last_updated_by = data_owner tree.import_event = base_treemap_import_event tree.plot = plot tree.save() self.plot = plot self.status = TreeImportRow.SUCCESS self.save() return True
def commit_row(self): # If this row was already commit... abort if self.plot: self.status = TreeImportRow.SUCCESS self.save() # First validate if not self.validate_row(): return False # Get our data data = self.cleaned self.convert_units( data, { fields.trees.PLOT_WIDTH: self.import_event.plot_width_conversion_factor, fields.trees.PLOT_LENGTH: self.import_event.plot_length_conversion_factor, fields.trees.DIAMETER: self.import_event.diameter_conversion_factor, fields.trees.TREE_HEIGHT: self.import_event.tree_height_conversion_factor, fields.trees.CANOPY_HEIGHT: self.import_event.canopy_height_conversion_factor }) # We need the import event from treemap.models # the names of things are a bit odd here but # self.import_event -> # TreeImportEvent (importer) -> # ImportEvent (treemap) # base_treemap_import_event = self.import_event.base_import_event plot_edited = False tree_edited = False # Initially grab plot from row if it exists plot = self.plot if plot is None: plot = Plot(present=True) # Event if TREE_PRESENT is None, a tree # can still be spawned here if there is # any tree data later tree = plot.current_tree() # Check for an existing tree: if self.model_fields.OPENTREEMAP_ID_NUMBER in data: plot = Plot.objects.get( pk=data[self.model_fields.OPENTREEMAP_ID_NUMBER]) tree = plot.current_tree() else: if data.get(self.model_fields.TREE_PRESENT, False): tree_edited = True if tree is None: tree = Tree(present=True) data_owner = self.import_event.owner for modelkey, importdatakey in TreeImportRow.PLOT_MAP.iteritems(): importdata = data.get(importdatakey, None) if importdata: plot_edited = True setattr(plot, modelkey, importdata) if plot_edited: plot.last_updated_by = data_owner plot.import_event = base_treemap_import_event plot.save() for modelkey, importdatakey in TreeImportRow.TREE_MAP.iteritems(): importdata = data.get(importdatakey, None) if importdata: tree_edited = True if tree is None: tree = Tree(present=True) setattr(tree, modelkey, importdata) if tree_edited: tree.last_updated_by = data_owner tree.import_event = base_treemap_import_event tree.plot = plot tree.save() self.plot = plot self.status = TreeImportRow.SUCCESS self.save() return True