def add_plants(user_id): """Adds a plant to this user's account""" if not g.user or (session[LOGGED_IN_USER] != user_id): flash('Access unauthorized', 'danger') return redirect('/') which_user = User.query.get_or_404(user_id) form = AddPlantForm() if form.validate_on_submit(): new_plant = Plants( plant_name = form.plant_name.data, plant_birthday = form.plant_birthday.data, last_watered = form.last_watered.data or form.last_watered.default, last_trimmed = form.last_trimmed.data or form.last_trimmed.default, last_repotted = form.last_repotted.data or form.last_repotted.default, indoor = form.indoor.data or False, user_id = which_user.id ) db.session.add(new_plant) db.session.commit() flash('Plant added successfully!', 'success') return redirect(f'/{g.user.id}/plants') else: return render_template('/plants/add_plant.html', form=form) return render_template('/plants/add_plant.html', form=form)
def add_plant(): from models import Plants name = request.form.get('name') imageurl = request.form.get('imageurl') plant = Plants(name, imageurl) db.session.add(plant) db.session.commit() plants = Plants.query.all() return render_template('plant_list.html', plants=plants)
def add_plant(): form = add_plant_form() if form.validate_on_submit(): print ("duh") try: new_plant = Plants(name=form.name.data, german_name=form.german_name.data, latin_name=form.latin_name.data, plant_information=form.plant_information.data, light=form.light.data, watering=form.watering.data, placement=form.placement.data, insect_friendly=form.insect_friendly.data, other_information=form.other_information.data) db_session.add(new_plant) db_session.commit() print ("New plant added") return redirect(url_for('plants')) except: flash('plant could not be added') print ("could not be added. Error!") return render_template ('add_plant.jinja', form=form)
def edit(id): plant_edit = Plants.query.filter_by(id=id).first() form = edit_form(obj=plant_edit) if form.validate_on_submit(): form.populate_obj(plant_edit) edited_plant = Plants(name=form.name.data, german_name=form.german_name.data, latin_name=form.latin_name.data, plant_information=form.plant_information.data, light=form.light.data, watering=form.watering.data, placement=form.placement.data, insect_friendly=form.insect_friendly.data, other_information=form.other_information.data) try: db_session.add(plant_edit) db_session.commit() print ("plant edited") return redirect(url_for('plants')) except: print ("error") return redirect(url_for('plants')) return render_template('edit.jinja', form=form, plant_edit=plant_edit)
def add_plant_to_user(): """Add plant to user account""" query = request.json new_plant = Plants(plant_name = query['hiddenData'], user_id = g.user.id) db.session.add(new_plant) try: db.session.commit() succesful = {'message': 'Added to account!'} return jsonify(succesful) except: unsuccesful = {'message': 'Error while adding'} return jsonify(unsuccesful)
def add_new_plant(user_id, room_id): body = request.get_json() if body is None: raise APIException("You need to specify the request body as a json object", status_code=400) if 'id_room' not in body: raise APIException('You need to specify the id room', status_code=400) if 'name_plant' not in body: raise APIException('You need to specify the name of the plant', status_code=400) if 'type_plant' not in body: raise APIException('You need to specify the type of plant', status_code=400) if 'grow_phase' not in body: raise APIException('You need to specify the grow phase', status_code=400) new_plant = Plants(id_room=body['id_room'], name_plant=body["name_plant"], type_plant=body["type_plant"], grow_phase=body["grow_phase"], sensor_number=body["sensor_number"]) new_plant.create() return ({'status': 'OK', 'message': 'Plant Added succesfully'}), 200
def addNewPlant(form): # FETCH DATA FROM FORM latitude = form.latitude.data longitude = form.longitude.data status = form.status.data comments = form.comments.data techs = { ("pv", form.pv.data), ("csp", form.csp.data), ("windon", form.windon.data), ("windoff", form.windoff.data), ("wave", form.wave.data), ("tidal", form.tidal.data), ("storage", form.storage.data), } type_forms = form.types.entries # CHECK DATA form_errors = False name_check = Plants.query.filter_by(name=form.name.data).all() if name_check: form_errors = True form.name.errors = {'Please enter a unique plant name'} db_country = Countries.query.filter_by(id=form.country.data).first() # Check at least one of the capacities was added if form.capacity_ac.data == None and form.capacity_dc.data == None: form_errors = True # For some reason, decimalField's error is a tuple instead of a list.. form.capacity_ac.errors = { 'Please enter the plant capacity (can be estimated then corrected once known)' } form.capacity_dc.errors = {''} # If both are added, check DC is greater than AC elif form.capacity_ac.data != None and form.capacity_dc.data != None and form.capacity_ac.data > form.capacity_dc.data: form_errors = True form.capacity_ac.errors = { 'Please ensure AC capacity is equal or lower to DC capacity' } form.capacity_dc.errors = {''} # Check at tech has been selected & find tech ID selected_a_technology = False techs_selected = {} for tech, boolean in techs: if boolean == True: technology = Technologies.query.filter_by(short=tech).first() if technology != None: selected_a_technology = True techs_selected[tech] = technology if selected_a_technology == False: form_errors = True form.csp.errors = {'Please select a technology'} # If errors have been found, return form errors if form_errors == True: result = {"errors": form} return result # CREATE PLANT IN DATABASE # Create new plant db_plant = Plants( name=form.name.data, country_id=db_country.id, capacity_ac=form.capacity_ac.data, capacity_dc=form.capacity_dc.data, latitude=latitude, longitude=longitude, status=status, comments=comments, ) for technology in techs_selected: print(techs_selected[technology]) db_plant.technologies.append(techs_selected[technology]) # Add plant to database db.session.add(db_plant) try: db.session.commit() except IntegrityError: db.session.rollback() result = FALSE return result # CREATE PLANT COMPONENT CONFIGURATION (if filled in) # Creates and attachs all components and their respective types to the plant for entry in type_forms[1:]: # Skips template - entry.data['component'] is set automatically when cloning template type form, along with any component # Should be able to remove due to type_forms[1:] if entry.data['component'] == None or entry.data['component'] == '': continue # Quantity if entry.data['quantity'] != None: quantity = entry.data['quantity'] db_configuration = Configurations(plant_id=db_plant.id, quantity=quantity) else: db_configuration = Configurations(plant_id=db_plant.id) # Add component configuration to database db.session.add(db_configuration) # Component: db_configuration.component_id = entry.data['component'] # Type1: db_configuration.type1_id = entry.data['type1'] # Type2: if entry.data['type2'] != None: db_configuration.type2_id = entry.data['type2'] # Supplier db_configuration.supplier_id = entry.data['supplier'] # Model model = entry.data['model'] print(model) if model != "": db_model = Models(name=entry.data['model'], component_id=entry.data['component'], supplier_id=entry.data['supplier'], type1_id=entry.data['type1']) if entry.data['type2'] != None: db_model.type2_id = entry.data['type2'] db.session.add(db_model) db_configuration.model_id = db_model.id db.session.commit() result = db_plant return result