Ejemplo n.º 1
0
def add_plots():
    """Explains what plots are and shows form to add new plots. POST adds new plot to user"""
    form = PlotAddForm()
    form.projects.choices = [(
        project.id,
        project.name,
    ) for project in g.user.projects]

    form.plantlists.choices = [(
        plantlist.id,
        plantlist.name,
    ) for plantlist in g.user.plantlists]

    if form.validate_on_submit():

        try:
            plot = Plot.add(
                name=form.name.data,
                description=form.description.data,
                width=form.width.data,
                length=form.length.data,
                # is_public=form.is_public.data,
            )
            db.session.commit()
            g.user.plots.append(plot)

            # Append plot to selected projects
            for project in form.projects.data:
                project = Project.query.get(project)
                plot.projects.append(project)
            # Append selected plant list to the project
            for plantlist in form.plantlists.data:
                plantlist = PlantList.query.get(plantlist)
                plot.plantlists.append(plantlist)

            db.session.commit()

        except IntegrityError:
            flash("Failed to create plot.", "danger")
            return render_template("plots/add.html", form=form)

        flash("Successfully created plot!", "success")

        return redirect(url_for("show_plot", plot_id=plot.id))

    return render_template("plots/add.html", form=form)
Ejemplo n.º 2
0
    def test_project_add_plot(self):
        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id

            plot = Plot.add(name="AddPlot", width=1, length=1)
            project = Project.query.get(self.testproject_id)

            resp = c.post(
                f"/projects/{project.id}/add/plot/{plot.id}", follow_redirects=True,
            )

            self.assertIn(plot, project.plots)
            self.assertEqual(resp.status_code, 200)
            self.assertIn(
                f"Plot {plot.id} connected to Project {project.id} successfully.",
                str(resp.data),
            )
Ejemplo n.º 3
0
    def test_query_connections(self):
        with self.client as c:
            with c.session_transaction() as sess:
                sess[CURR_USER_KEY] = self.testuser_id

            plot2 = Plot.add(name="Plot2", width=1, length=1)
            db.session.commit()
            user = User.query.get(self.testuser_id)
            user.plots.append(plot2)

            resp = c.get(
                f"/query/project/{self.testproject_id}/plots", follow_redirects=True,
            )

            plot = Plot.query.get(self.testplot_id)

            self.assertIn([plot.id, plot.name], resp.json["list_items"])
            self.assertNotIn([plot.id, plot.name], resp.json["options"])
            self.assertIn([plot2.id, plot2.name], resp.json["options"])
            self.assertNotIn([plot2.id, plot2.name], resp.json["list_items"])
Ejemplo n.º 4
0
    def save(self,request):
        from django.contrib.gis.geos import Point

        plot = Plot()
        plot.data_owner = request.user

        address = self.cleaned_data.get('edit_address_street')
        if address:
            plot.address_street = address
            plot.geocoded_address = address
        city = self.cleaned_data.get('edit_address_city')
        geo_address = self.cleaned_data.get('geocode_address')
        if geo_address:
            plot.geocoded_address = geo_address
        if city:
            plot.address_city = city
        zip_ = self.cleaned_data.get('edit_address_zip')
        if zip_:
            plot.address_zip = zip_
        
        plot_width = self.cleaned_data.get('plot_width')
        plot_width_in = self.cleaned_data.get('plot_width_in')
        if plot_width:
            plot.width = float(plot_width)
        if plot_width_in:
            plot.width = plot.width + (float(plot_width_in) / 12)
        plot_length = self.cleaned_data.get('plot_length')
        plot_length_in = self.cleaned_data.get('plot_length_in')
        if plot_length:
            plot.length = float(plot_length)
        if plot_length_in:
            plot.length = plot.length + (float(plot_length_in) / 12)
        plot_type = self.cleaned_data.get('plot_type')
        if plot_type:
            plot.type = plot_type
        power_lines = self.cleaned_data.get('power_lines')
        if power_lines != "":
            plot.powerline_conflict_potential = power_lines
        sidewalk_damage = self.cleaned_data.get('sidewalk_damage')
        if sidewalk_damage:
            plot.sidewalk_damage = sidewalk_damage

        import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',)
        plot.import_event = import_event

        pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326)
        plot.geometry = pnt
        plot.last_updated_by = request.user
        plot.save()

        species = self.cleaned_data.get('species_id')
        height = self.cleaned_data.get('height')
        canopy_height = self.cleaned_data.get('canopy_height')
        dbh = self.cleaned_data.get('dbh')
        crown_width = self.cleaned_data.get('crown_width')
        dbh_type = self.cleaned_data.get('dbh_type')
        condition = self.cleaned_data.get('condition')
        canopy_condition = self.cleaned_data.get('canopy_condition')

        new_tree = Tree()
        if species:
            spp = Species.objects.filter(symbol=species)
            if spp:
              new_tree.species=spp[0]

        if crown_width:
            new_tree.crown_width = crown_width
        if height:
            new_tree.height = height
        if canopy_height:
            new_tree.canopy_height = canopy_height
        if dbh:
            if dbh_type == 'circumference':
                dbh = dbh / math.pi
            new_tree.dbh = dbh
        if condition:
            new_tree.condition = condition
        if canopy_condition:
            new_tree.canopy_condition = canopy_condition
        
        new_tree.import_event = import_event            
        new_tree.last_updated_by = request.user
        new_tree.plot = plot
        new_tree.save()
        #print new_tree.__dict__
        fauna = self.cleaned_data.get('fauna')
        if fauna:
            print 'fauna',fauna
            fauna_dict = dict(Choices().get_field_choices('fauna'))
            for f in fauna:
              fauna = TreeFauna()
              fauna.reported_by = request.user
              fauna.key = f
              fauna.value = datetime.now()
              fauna.fauna = fauna_dict[f] # or random string
              fauna.tree = new_tree
              fauna.save()
        
        return plot
Ejemplo n.º 5
0
from database import db_session
from models import Plot

p1 = Plot(title='Plot 1',
          xlabel='xvar',
          ylabel='yvar',
          query='select a,b,c from data',
          num_series=2,
          page='home',
          graph_type='lineChart',
          xvar='a',
          yvars='b,c')
p2 = Plot(title='Plot 2',
          xlabel='xvar',
          ylabel='yvar',
          query='select a,x,y from data',
          num_series=2,
          page='home',
          graph_type='lineChart',
          xvar='a',
          yvars='x,y')
p3 = Plot(title='Plot 3',
          xlabel='xvar',
          ylabel='yvar',
          query='select a,z from data',
          num_series=1,
          page='home',
          graph_type='lineChart',
          xvar='a',
          yvars='z')
p4 = Plot(title='Plot 4',
Ejemplo n.º 6
0
    def save(self,request):
        from django.contrib.gis.geos import Point

        plot = Plot()
        plot.data_owner = request.user

        address = self.cleaned_data.get('edit_address_street')
        if address:
            plot.address_street = address
            plot.geocoded_address = address
        city = self.cleaned_data.get('edit_address_city')
        geo_address = self.cleaned_data.get('geocode_address')
        if geo_address:
            plot.geocoded_address = geo_address
        if city:
            plot.address_city = city
        zip_ = self.cleaned_data.get('edit_address_zip')
        if zip_:
            plot.address_zip = zip_
        
        plot_width = self.cleaned_data.get('plot_width')
        plot_width_in = self.cleaned_data.get('plot_width_in')
        if plot_width:
            plot.width = float(plot_width)
        if plot_width_in:
            plot.width = plot.width + (float(plot_width_in) / 12)
        plot_length = self.cleaned_data.get('plot_length')
        plot_length_in = self.cleaned_data.get('plot_length_in')
        if plot_length:
            plot.length = float(plot_length)
        if plot_length_in:
            plot.length = plot.length + (float(plot_length_in) / 12)
        plot_type = self.cleaned_data.get('plot_type')
        if plot_type:
            plot.type = plot_type
        power_lines = self.cleaned_data.get('power_lines')
        if power_lines != "":
            plot.powerline_conflict_potential = power_lines
        sidewalk_damage = self.cleaned_data.get('sidewalk_damage')
        if sidewalk_damage:
            plot.sidewalk_damage = sidewalk_damage
        owner_additional_id = self.cleaned_data.get('owner_additional_id')
        if owner_additional_id:
            plot.owner_additional_id = owner_additional_id

        import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',)
        plot.import_event = import_event

        pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326)
        plot.geometry = pnt
        plot.last_updated_by = request.user
        plot.save()

        species = self.cleaned_data.get('species_id')
        species_other1 = self.cleaned_data.get('species_other1')
        species_other2 = self.cleaned_data.get('species_other2')
        height = self.cleaned_data.get('height')
        canopy_height = self.cleaned_data.get('canopy_height')
        dbh = self.cleaned_data.get('dbh')
        dbh_type = self.cleaned_data.get('dbh_type')
        condition = self.cleaned_data.get('condition')
        canopy_condition = self.cleaned_data.get('canopy_condition')

        #TODO: fix this
        pests = self.cleaned_data.get('pests')
        if species or height or canopy_height or dbh or \
           condition or canopy_condition or pests:
           # print species, height, canopy_height, dbh, condition, canopy_condition
            if species:
                spp = Species.objects.filter(id=species)
                if spp:
                    new_tree = Tree(species=spp[0])
                else:
                    new_tree = Tree()
            else:
                new_tree = Tree()

            new_tree.pests = pests

            if species_other1:
                new_tree.species_other1 = species_other1
            if species_other2:
                new_tree.species_other2 = species_other2
            if height:
                new_tree.height = height
            if canopy_height:
                new_tree.canopy_height = canopy_height
            if dbh:
                if dbh_type == 'circumference':
                    new_tree.dbh = dbh / math.pi
                else:
                    new_tree.dbh = dbh
            if condition:
                new_tree.condition = condition
            if canopy_condition:
                new_tree.canopy_condition = canopy_condition
            
            new_tree.import_event = import_event            
            new_tree.last_updated_by = request.user
            new_tree.plot = plot
            new_tree.save()
            #print new_tree.__dict__
        
        return plot
Ejemplo n.º 7
0
    def save(self, request):
        from django.contrib.gis.geos import Point

        plot = Plot()
        plot.data_owner = request.user

        address = self.cleaned_data.get('edit_address_street')
        if address:
            plot.address_street = address
            plot.geocoded_address = address
        city = self.cleaned_data.get('edit_address_city')
        geo_address = self.cleaned_data.get('geocode_address')
        if geo_address:
            plot.geocoded_address = geo_address
        if city:
            plot.address_city = city
        zip_ = self.cleaned_data.get('edit_address_zip')
        if zip_:
            plot.address_zip = zip_

        plot_width = self.cleaned_data.get('plot_width')
        plot_width_in = self.cleaned_data.get('plot_width_in')
        if plot_width:
            plot.width = float(plot_width)
        if plot_width_in:
            plot.width = plot.width + (float(plot_width_in) / 12)
        plot_length = self.cleaned_data.get('plot_length')
        plot_length_in = self.cleaned_data.get('plot_length_in')
        if plot_length:
            plot.length = float(plot_length)
        if plot_length_in:
            plot.length = plot.length + (float(plot_length_in) / 12)
        plot_type = self.cleaned_data.get('plot_type')
        if plot_type:
            plot.type = plot_type
        power_lines = self.cleaned_data.get('power_lines')
        if power_lines != "":
            plot.powerline_conflict_potential = power_lines
        sidewalk_damage = self.cleaned_data.get('sidewalk_damage')
        if sidewalk_damage:
            plot.sidewalk_damage = sidewalk_damage
        owner_additional_id = self.cleaned_data.get('owner_additional_id')
        if owner_additional_id:
            plot.owner_additional_id = owner_additional_id

        import_event, created = ImportEvent.objects.get_or_create(
            file_name='site_add', )
        plot.import_event = import_event

        pnt = Point(self.cleaned_data.get('lon'),
                    self.cleaned_data.get('lat'),
                    srid=4326)
        plot.geometry = pnt
        plot.last_updated_by = request.user
        plot.save()

        species = self.cleaned_data.get('species_id')
        species_other1 = self.cleaned_data.get('species_other1')
        species_other2 = self.cleaned_data.get('species_other2')
        height = self.cleaned_data.get('height')
        canopy_height = self.cleaned_data.get('canopy_height')
        dbh = self.cleaned_data.get('dbh')
        dbh_type = self.cleaned_data.get('dbh_type')
        condition = self.cleaned_data.get('condition')
        canopy_condition = self.cleaned_data.get('canopy_condition')

        #TODO: fix this
        pests = self.cleaned_data.get('pests')
        if species or height or canopy_height or dbh or \
           condition or canopy_condition or pests:
            # print species, height, canopy_height, dbh, condition, canopy_condition
            if species:
                spp = Species.objects.filter(id=species)
                if spp:
                    new_tree = Tree(species=spp[0])
                else:
                    new_tree = Tree()
            else:
                new_tree = Tree()

            new_tree.pests = pests

            if species_other1:
                new_tree.species_other1 = species_other1
            if species_other2:
                new_tree.species_other2 = species_other2
            if height:
                new_tree.height = height
            if canopy_height:
                new_tree.canopy_height = canopy_height
            if dbh:
                if dbh_type == 'circumference':
                    new_tree.dbh = dbh / math.pi
                else:
                    new_tree.dbh = dbh
            if condition:
                new_tree.condition = condition
            if canopy_condition:
                new_tree.canopy_condition = canopy_condition

            new_tree.import_event = import_event
            new_tree.last_updated_by = request.user
            new_tree.plot = plot
            new_tree.save()
            #print new_tree.__dict__

        return plot
Ejemplo n.º 8
0
    def setUp(self):
        """Create test client, add sample data"""

        db.drop_all()
        db.create_all()

        self.client = app.test_client()

        self.testuser = User.signup(
            username="******", email="*****@*****.**", password="******",
        )
        self.testuser_id = 100
        self.testuser.id = self.testuser_id

        self.otheruser = User.signup(
            username="******", email="*****@*****.**", password="******",
        )

        self.otheruser_id = 200
        self.otheruser.id = self.otheruser_id

        self.testproject = Project(
            name="Project_Test", description="Project_Test test description.",
        )
        self.testproject_name = self.testproject.name
        self.testproject_id = 110
        self.testproject.id = self.testproject_id
        db.session.add(self.testproject)

        self.testplot = Plot(
            name="Plot_Test",
            width=5,
            length=10,
            description="Plot_Test test description.",
        )
        self.testplot_name = self.testplot.name
        self.testplot_id = 120
        self.testplot.id = self.testplot_id
        db.session.add(self.testplot)

        self.testplantlist = PlantList(
            name="Plantlist_Test", description="Plantlist_Test test description.",
        )
        self.testplantlist_name = self.testplantlist.name
        self.testplantlist_id = 130
        self.testplantlist.id = self.testplantlist_id
        db.session.add(self.testplantlist)

        self.testplant = Plant(
            trefle_id=1231,
            slug="plantus-slugs1",
            common_name="common plant1",
            scientific_name="plantus testus1",
            family="Plantaceae1",
            family_common_name="Plant Family1",
        )
        self.testplant_common_name = self.testplant.common_name
        self.testplant_id = 140
        self.testplant.id = self.testplant_id

        db.session.add(self.testplant)

        self.testsymbol = Symbol(
            symbol="<i class='symbol fas fa-seedling' style='color:#228B22;'></i>"
        )

        self.testsymbol_id = 1
        self.testsymbol.id = self.testsymbol_id
        db.session.add(self.testsymbol)

        # Connections
        self.testuser.projects.append(self.testproject)
        self.testuser.plots.append(self.testplot)
        self.testuser.plantlists.append(self.testplantlist)
        self.testproject.plots.append(self.testplot)
        self.testproject.plantlists.append(self.testplantlist)
        self.testplot.plantlists.append(self.testplantlist)
        self.testplantlist.plants.append(self.testplant)

        db.session.commit()