Beispiel #1
0
def restaurants(invalid=None):
    form = RestaurantForm(request.form)
    print(form.validate())

    con = sql.connect("database.db")
    con.row_factory = sql.Row

    if request.method == 'POST':
        try:
            email = request.form['email'].strip()
            restaurant = request.form['restaurant'].strip()
            address = request.form['address'].strip()

            if len(email) != 0 and len(restaurant) != 0 and len(address) != 0:
                with sql.connect("database.db") as con:
                    cur = con.cursor()
                    cur.execute(
                        "INSERT INTO restaurants(email, restaurant, address) VALUES (?,?, ?)",
                        (email, restaurant, address))
                    con.commit()
                    return render_template("submitted.html",
                                           link=url_for("restaurants"),
                                           location="restaurants")
            else:
                print("Not added")
                return render_template("restaurants.html", invalid=True)
        except:
            con.rollback()

    con.close()

    return render_template("restaurants.html")
def deleteRestaurant(restaurant_id):
    session = DBSession()
    form = RestaurantForm()
    restaurantToDelete = session.query(Restaurant).filter_by(id=restaurant_id).one()
    if form.validate_on_submit():
        session.delete(restaurantToDelete)
        session.commit()
        session.close()
        flash("Restaurant Deleted Successfully!!! ")
        return redirect(url_for('showRestaurants', restaurant_id=restaurant_id))

    return render_template('restaurant/deleterestaurant.html', item = restaurantToDelete, form=form)
def createRestaurant():
    session = DBSession()
    form = RestaurantForm()
    if form.validate_on_submit():
        name = form.name.data
        location = form.location.data
        description = form.description.data
        restaurant = models.Restaurant(name=name, location =location, description = description)
        session.add(restaurant)
        session.commit()
        session.close()
        flash("Added '{}'".format(name))
        return redirect(url_for('showRestaurants'))

    return render_template('restaurant/createrestaurant.html', form=form)
def editRestaurant(restaurant_id):
    session = DBSession()
    form = RestaurantForm()
    editedrestaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    if form.validate_on_submit():
        if request.form['name']:
            editedrestaurant.name = form.name.data
        if request.form['location']:
            editedrestaurant.location = form.location.data
        if request.form['description']:
            editedrestaurant.description = form.description.data
        session.add(editedrestaurant)
        session.commit()
        session.close()
        flash("Restaurant Edited Successfully!!! ")
        return redirect(url_for('showRestaurants', restaurant_id=restaurant_id))

    return render_template('restaurant/editrestaurant.html', restaurant_id=restaurant_id, item=editedrestaurant, form=form)