Ejemplo n.º 1
0
def cafe_edit(cafe_id):
    """ show form for editting cafe and handle form"""
    cafe = Cafe.query.get(cafe_id)
    form = AddEditCafeForm(obj=cafe)

    #get city_code choice from current cities in db
    form.city_code.choices = City.get_all_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)

    #######################################
    # users

    @app.route('/signup', methods=["GET", "POST"])
    def user_signup():
        """return sign up form and handle sign up """
        form = SignupForm()

        if form.validate_on_submit():
            new_user = User.register(username=form.username.data,
                                     email=form.email.data,
                                     first_name=form.first_name.data,
                                     last_name=form.last_name.data,
                                     description=form.description.data,
                                     image_url=form.image_url.data,
                                     password=form.password.data)

        db.session.add(new_user)
        db.session.commit()

        do_login(new_user)

        flash(f"You are signed up and logged in")
        return redirect("/cafes")

    return render_template("auth/signup-form.html", form=form)
Ejemplo n.º 2
0
def cafe_add():
    """ show form for adding cafe and handle form"""
    form = AddEditCafeForm()

    #get city_code choice from current cities in db
    form.city_code.choices = City.get_all_cities()

    if form.validate_on_submit():
        new_cafe = Cafe(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 or None)

        db.session.add(new_cafe)
        db.session.commit()

        flash(f'{new_cafe.name} added')
        return redirect(f"/cafes/{new_cafe.id}")

    return render_template("cafe/add-form.html", form=form)