def edit_cafe_form(cafe_id): """ Show and process form for editting cafe.""" if CURR_USER_KEY in session: user = g.user if (user.admin): cafe = Cafe.query.get_or_404(cafe_id) form = CafeForm(obj=cafe) cities = [(city.code, city.name) for city in City.query.all()] form.city_code.choices = cities if form.validate_on_submit(): cafe.name = form.name.data cafe.description = form.description.data cafe.url = form.url.data cafe.address = form.address.data cafe.city_code = form.city_code.data cafe.image_url = form.image_url.data db.session.commit() flash(f'{cafe.name} edited.') return redirect(f'/cafes/{cafe.id}') return render_template('cafe/edit-form.html', form=form, cafe=cafe) else: return Response( 'Could not verify your access level for that URL.\n' 'You have to login with proper credentials', 401) else: return redirect('/login')
def edit(id): cafe_to_edit = Cafe.query.get(id) form = CafeForm( name = cafe_to_edit.name, map_url = cafe_to_edit.map_url, img_url = cafe_to_edit.img_url, location = cafe_to_edit.location, has_sockets = check_does_have(cafe_to_edit.has_sockets), has_toilet = check_does_have(cafe_to_edit.has_toilet), has_wifi = check_does_have(cafe_to_edit.has_wifi), can_take_calls = check_does_have(cafe_to_edit.can_take_calls), seats = cafe_to_edit.seats, coffee_price = cafe_to_edit.coffee_price ) if form.validate_on_submit(): cafe_to_edit = Cafe.query.get(id) cafe_to_edit.name = form.name.data cafe_to_edit.map_url = form.map_url.data cafe_to_edit.img_url = form.img_url.data cafe_to_edit.locaiton = form.location.data cafe_to_edit.has_sockets = yes_no(form.has_sockets.data) cafe_to_edit.has_toilet = yes_no(form.has_toilet.data) cafe_to_edit.has_wifi = yes_no(form.has_wifi.data) cafe_to_edit.can_take_calls = yes_no(form.can_take_calls.data) cafe_to_edit.seats = form.seats.data cafe_to_edit.coffe_price = form.coffee_price.data db.session.commit() return redirect(url_for("cafes")) return render_template("add.html", form = form)
def add_cafe(): form = CafeForm(request.form) if request.method == "GET": return render_template("cafeform.html", form=form, formtype="Add", current_user=current_user) if request.method == "POST" and form.validate_on_submit(): # Add cafe cafe = Cafe() cafe.name = form.data["name"] cafe.location = form.data["location"] db.session.add(cafe) db.session.commit() write_to_events("created", "cafe", cafe.id) flash("Cafe added", "success") return redirect(url_for("view_cafe", cafeid=cafe.id)) else: for field, errors in form.errors.items(): flash("Error in %s: %s" % (field, "; ".join(errors)), "danger") return render_template("cafeform.html", form=form, formtype="Add", current_user=current_user) return redirect(url_for("home"))
def add_cafe_form(): """Show and process form for adding a new cafe. """ form = CafeForm() # make cities dinamic cities = [(city.code, city.name) for city in City.query.all()] form.city_code.choices = cities if form.validate_on_submit(): name = form.name.data description = form.description.data url = form.url.data address = form.address.data city_code = form.city_code.data image_url = form.image_url.data cafe = Cafe(name=name, description=description, url=url, address=address, city_code=city_code, image_url=image_url or None) db.session.add(cafe) db.session.commit() flash(f'{cafe.name} added') return redirect(f'/cafes/{cafe.id}') return render_template('cafe/add-form.html', form=form)
def add_cafe(): form = CafeForm() if form.validate_on_submit(): print(type(form.location.data)) with open('cafe-data.csv', 'a', encoding='utf-8') as cafe_data: cafe_data.write( f"\n{form.cafe.data},{form.location.data},{form.open.data},{form.close.data},{form.coffee.data}," f"{form.wifi.data}, {form.outlets.data}") return redirect(url_for('cafes')) # Exercise: # Make the form write a new row into cafe-data.csv # with if form.validate_on_submit() return render_template('add.html', form=form)
def add_cafe(): form = CafeForm() if form.validate_on_submit(): new_cafe = Cafes(name=form.cafe.data, location_link=form.location.data, open_hours=form.open.data, close_hours=form.close.data, coffee_rating=form.coffee.data, wifi_rating=form.wifi.data, power_rating=form.power.data) db.session.add(new_cafe) db.session.commit() return redirect(url_for('cafes')) return render_template('add.html', form=form)
def edit_cafe(cafeid): cafe = Cafe.query.filter(Cafe.id==cafeid).first_or_404() form = CafeForm(request.form, obj=cafe) if request.method == "GET": return render_template("cafeform.html", form=form, formtype="Edit", current_user=current_user) if request.method == "POST" and form.validate_on_submit(): form.populate_obj(cafe) db.session.commit() write_to_events("updated", "cafe", cafe.id) flash("Cafe edited", "success") return redirect(url_for("view_cafe", cafeid=cafeid)) else: for field, errors in form.errors.items(): flash("Error in %s: %s" % (field, "; ".join(errors)), "danger") return render_template("cafeform.html", form=form, formtype="Edit", current_user=current_user)
def add_cafe(): form = CafeForm() if form.validate_on_submit(): new_cafe = Cafe( name = form.name.data, map_url = form.map_url.data, image_url = form.img_url.data, location = form.location.data, has_sockets = yes_no(form.has_sockets.data), has_toilet = yes_no(form.has_toilet.data), has_wifi = yes_no(form.has_wifi.data), can_take_calls = yes_no(form.can_take_calls.data), seats = form.seats.data, coffee_price = form.coffee_price.data ) db.session.add(new_cafe) db.session.commit() return redirect(url_for("cafes")) return render_template("add.html", form = form)
def add_cafe(): cafe_form = CafeForm() return render_template('add_cafe.html', edit_form=cafe_form)