def show_plot(plot_id): """Show specific plot details""" plot = Plot.query.get_or_404(plot_id) if g.user not in plot.users: flash("Not authorized to view this page.", "danger") return redirect(url_for("homepage")) form_plantlist = AddPlantListForm() form_plantlist.plantlists.choices = [( plantlist.id, plantlist.name, ) for plantlist in g.user.plantlists if plantlist not in plot.plantlists] form_project = AddProjectForm() form_project.projects.choices = [( project.id, project.name, ) for project in g.user.projects if project not in plot.projects] return render_template( "plots/show.html", form_plantlist=form_plantlist, form_project=form_project, plot=plot, )
def user_content(user_id): """Shows user content page, which is an overview of projects, plots, and plant lists saved to user's profile""" user = User.query.get_or_404(user_id) if g.user != user: flash("Not authorized to view this page.", "danger") return redirect(url_for("homepage")) form_plot = AddPlotForm() # Load in choices based on users currently connected components form_plot.plots.choices = [(plot.id, plot.name,) for plot in g.user.plots] form_plantlist = AddPlantListForm() form_plantlist.plantlists.choices = [ (plantlist.id, plantlist.name,) for plantlist in g.user.plantlists ] form_project = AddProjectForm() form_project.projects.choices = [ (project.id, project.name,) for project in g.user.projects ] return render_template( "users/content.html", user=user, form_plot=form_plot, form_plantlist=form_plantlist, form_project=form_project, )
def plant_profile(plant_slug): """Shows specific plant profile page""" payload = { "token": TREFLE_API_KEY, } trefle_plant = requests.get(f"{API_BASE_URL}/plants/{plant_slug}", params=payload).json()["data"] # Some responses have data nested in "main_species" if "main_species" in trefle_plant: main_species = trefle_plant["main_species"] else: main_species = trefle_plant form = AddPlantListForm() # If user is logged in, they can add this plant to their plantlists # Otherwise ask them to signup/login. if g.user: plant = Plant.query.filter( Plant.trefle_id == main_species["id"]).one_or_none() if request.method == "POST": if not plant: try: plant = Plant.add( trefle_id=main_species["id"], slug=main_species["slug"], common_name=main_species["common_name"], scientific_name=main_species["scientific_name"], family=main_species["family"], family_common_name=main_species["family_common_name"], image_url=main_species["image_url"], ) db.session.commit() except IntegrityError: flash("Failed to create plant.", "danger") return render_template("plants/profile.html", main_species=main_species, form=form) # Append selected plantlists to the plant for plantlist in form.plantlists.data: plantlist = PlantList.query.get(plantlist) plant.plantlists.append(plantlist) db.session.commit() if plant: form.plantlists.choices = [( plantlist.id, plantlist.name, ) for plantlist in g.user.plantlists if plantlist not in plant.plantlists] else: form.plantlists.choices = [( plantlist.id, plantlist.name, ) for plantlist in g.user.plantlists] return render_template("plants/profile.html", main_species=main_species, form=form)