Пример #1
0
def edit_menu_item(restaurant_id, menu_id):
    """Created route and function to edit each menu item in a restaurant"""
    if Restaurant.user_creator(login_session['user_id'], restaurant_id):
        menu = db_session.query(MenuItem).filter_by(
            restaurant_id=restaurant_id, id=menu_id).first()
        form = MenuItems()
        if form.validate_on_submit():
            menu.name = form.name.data
            menu.price = form.price.data
            menu.course = form.course.data
            menu.description = form.description.data
            db_session.add(menu)
            db_session.commit()
            return redirect(
                url_for('restaurant_b.restaurant_detail',
                        restaurant_id=restaurant_id))
        else:
            form.description.data = menu.description
            return render_template("menu_item/editMenuItem.html",
                                   form=form,
                                   menu=menu,
                                   restaurant_id=restaurant_id,
                                   menu_id=menu_id)
    else:
        flash(
            "You cannot made any changes, make your own restaurant and try again"
        )
        return redirect(url_for('restaurant_b.show_restaurants'))
Пример #2
0
def create_restaurant():
    form = RestaurantForm()

    if form.validate_on_submit():
        add_values = Restaurant(name=form.name.data,
                                phone=form.phone.data,
                                food_type=form.course.data,
                                email=form.email.data,
                                website=form.website.data,
                                description=form.description.data,
                                user_id=login_session['user_id'])
        db_session.add(add_values)
        db_session.commit()
        db_session.refresh(add_values)
        add_address = RestaurantAddress(street=form.street.data,
                                        city=form.city.data,
                                        state=form.state.data,
                                        zip_code=form.zip_code.data,
                                        restaurant_id=add_values.id)
        db_session.add(add_address)
        db_session.commit()
        flash("New restaurant Created")
        return redirect(url_for('.show_restaurants'))

    else:
        return render_template("restaurant/newRestaurant.html", form=form)
Пример #3
0
def create_menu_restaurant(restaurant_id):
    """Created route and function to create new menu items
        for each restaurant"""
    if Restaurant.user_creator(login_session['user_id'], restaurant_id):
        form = MenuItems()
        if form.validate_on_submit():
            new_item = MenuItem(name=form.name.data,
                                price=form.price.data,
                                course=form.course.data,
                                description=form.description.data,
                                restaurant_id=restaurant_id,
                                user_id=login_session['user_id'])
            db_session.add(new_item)
            db_session.commit()
            return redirect(
                url_for('restaurant_b.restaurant_detail',
                        restaurant_id=restaurant_id))
        else:
            return render_template('menu_item/newMenuItem.html',
                                   form=form,
                                   restaurant_id=restaurant_id)
    else:
        flash(
            "You cannot made any changes, make your own restaurant and try again"
        )
        return redirect(url_for('restaurant_b.show_restaurants'))
Пример #4
0
def newCategory():
    """Allows a logged in user to create a new category"""
    form = CategoryForm(request.form)
    if request.method == 'POST' and form.validate():
        new_category = Category(name=request.form['name'],
                                user_id=login_session['user_id'])
        db_session.add(new_category)
        db_session.commit()
        flash('New Category %s Successfully Created' % new_category.name)
        return redirect(url_for('homePage.index'))
    return render_template('/newcategory.html', form=form)
Пример #5
0
def create_user(login_session):
    """
    Creates new user in the db based on OAuth profile info.
    login_session is passed in, and this info is used to create and persist
    a user in the db.
    Output is the user.id of the newly created user
    """
    new_user = User(name=login_session['username'],
                    email=login_session['email'],
                    picture=login_session['picture'])
    db_session.add(new_user)
    db_session.commit()
    user = db_session.query(User).filter_by(email=login_session['email']).one()
    return user.id
Пример #6
0
def deleteBook(category_id, book_id):
    """Manages book deletion. Local Permissions:
    Must be logged in and user that created the book"""
    deletedBook = db_session.query(Book).filter_by(id=book_id).one()
    form = BookForm(request.form)
    if deletedBook.user_id != login_session['user_id']:
        flash('You are not authorised to delete this book.')
        return redirect(url_for('category_admin.showCategory',
                                category_id=category_id))
    if request.method == 'POST':
        db_session.delete(deletedBook)
        db_session.commit()
        flash('Book %s by %s successfully deleted!' %
              (deletedBook.name, deletedBook.author))
        return redirect(url_for('homePage.index'))
    else:
        return render_template('/deletebook.html', category_id=category_id,
                               book_id=book_id, book=deletedBook, form=form)
Пример #7
0
def delete_menu_item(restaurant_id, menu_id):
    """Created route and function to delete a menu item"""
    if Restaurant.user_creator(login_session['user_id'], restaurant_id):
        menu = db_session.query(MenuItem).filter_by(
            restaurant_id=restaurant_id, id=menu_id).first()
        if request.method == 'POST':
            db_session.delete(menu)
            db_session.commit()
            return redirect(url_for('restaurant_b.show_restaurants'))

        return render_template("menu_item/deleteMenuItem.html",
                               menu=menu,
                               restaurant_id=restaurant_id)

    else:
        flash(
            "You cannot made any changes, make your own restaurant and try again"
        )
        return redirect(url_for('restaurant_b.show_restaurants'))
Пример #8
0
def deleteCategory(category_id):
    """Allows a category to be deleted, with local permissions:
    user must be logged in and original creator of the category
    """
    deletedCategory = db_session.query(Category).filter_by(
        id=category_id).one()
    form = CategoryForm(request.form)
    if deletedCategory.user_id != login_session['user_id']:
        flash('You are not authorised to delete this category.')
        return redirect(
            url_for('category_admin.showCategory', category_id=category_id))
    if request.method == 'POST':
        db_session.delete(deletedCategory)
        db_session.commit()
        flash('Category %s successfully deleted!' % deletedCategory.name)
        return redirect(url_for('homePage.index'))
    else:
        return render_template('/deletecategory.html',
                               category=deletedCategory,
                               form=form)
Пример #9
0
def editCategory(category_id):
    """Allows a category to be edited, with local permissions:
    user must be logged in and original creator of the category
    """
    editedCategory = db_session.query(Category).filter_by(id=category_id).one()
    form = CategoryForm(request.form)
    if editedCategory.user_id != login_session['user_id']:
        flash('You are not authorised to edit this category.')
        return redirect(
            url_for('category_admin.showCategory', category_id=category_id))
    if request.method == 'POST' and form.validate():
        editedCategory.name = request.form['name']
        db_session.add(editedCategory)
        db_session.commit()
        flash('Category %s Successfully Edited' % editedCategory.name)
        return redirect(url_for('homePage.index'))
    else:
        return render_template('/editcategory.html',
                               category=editedCategory,
                               form=form)
Пример #10
0
def delete_restaurant(restaurant_id):
    """Created route and function to delete restaurants individually"""
    if Restaurant.user_creator(login_session['user_id'], restaurant_id):
        restaurant = db_session.query(Restaurant).filter_by(
            id=restaurant_id).one()
        address = db_session.query(RestaurantAddress).filter_by(
            restaurant_id=restaurant.id).one()
        if request.method == 'POST':
            db_session.delete(address)
            db_session.delete(restaurant)
            db_session.commit()
            flash("Your Restaurant have been deleted")
            return redirect(url_for('.show_restaurants'))

        return render_template("restaurant/deleteRestaurant.html",
                               restaurant=restaurant)
    else:
        flash(
            "You cannot made any changes, make your own restaurant and try again"
        )
        return redirect(url_for('.show_restaurants'))
Пример #11
0
def edit_restaurant(restaurant_id):
    """Created route and function to edit restaurants"""
    if Restaurant.user_creator(login_session['user_id'], restaurant_id):
        restaurant = db_session.query(Restaurant).filter_by(
            id=restaurant_id).one()
        address = db_session.query(RestaurantAddress).filter_by(
            restaurant_id=restaurant.id).one()
        form = RestaurantForm()
        if form.validate_on_submit():
            restaurant.name = form.name.data
            restaurant.phone = form.phone.data
            restaurant.email = form.email.data
            restaurant.course = form.course.data
            restaurant.description = form.description.data
            restaurant.website = form.website.data
            db_session.add(restaurant)

            db_session.commit()
            db_session.refresh(restaurant)
            address.street = form.street.data
            address.city = form.city.data
            address.state = form.state.data
            address.zip_code = form.zip_code.data
            db_session.add(address)
            db_session.commit()
            flash("Your restaurant have been edited successfully")
            return redirect(url_for('.show_restaurants'))
        else:
            form.description.data = restaurant.description
            return render_template("restaurant/editrestaurant.html",
                                   form=form,
                                   restaurant=restaurant,
                                   address=address)
    else:
        flash(
            "You cannot made any changes, make your own restaurant and try again"
        )
        return redirect(url_for('.show_restaurants'))
Пример #12
0
def newBook():
    """Create a new book, with control that user must be logged in

    """
    categories = db_session.query(Category).order_by(asc(Category.name))
    form = BookForm(request.form)
    if request.method == 'POST' and form.validate():
        c = request.form['category']
        c_submitted = db_session.query(Category).filter(
            Category.name == c).first()
        newBook = Book(name=request.form['name'],
                       description=request.form['description'],
                       price=request.form['price'],
                       author=request.form['author'],
                       category=c_submitted,
                       user_id=login_session['user_id'])
        db_session.add(newBook)
        db_session.commit()
        flash('New Book %s by %s Successfully Created' %
              (newBook.name, newBook.author))

        return redirect(url_for('homePage.index'))
    return render_template('/newbook.html', categories=categories, form=form)
Пример #13
0
def editBook(category_id, book_id):
    """Edit a book, with local permissions:
    User must be logged in and created of the original book entry
    """
    editedBook = db_session.query(Book).filter_by(id=book_id).one()
    categories = db_session.query(Category).order_by(asc(Category.name))
    form = BookForm(request.form)
    if editedBook.user_id != login_session['user_id']:
        flash('You are not authorised to edit this book.')
        return redirect(url_for('category_admin.showCategory',
                                category_id=category_id))
    if request.method == 'POST' and form.validate():

        if request.form['name']:
            editedBook.name = request.form['name']
        if request.form['author']:
            editedBook.author = request.form['author']
        if request.form['price']:
            editedBook.price = request.form['price']
        if request.form['description']:
            editedBook.description = request.form['description']
        if request.form['category']:
            c = request.form['category']
            c_submitted = db_session.query(Category).filter(
                Category.name == c).first()
            editedBook.category = c_submitted

        db_session.add(editedBook)
        db_session.commit()

        flash('Book %s by %s Edited Successfully!' %
              (editedBook.name, editedBook.author))
        return redirect(url_for('homePage.index'))
    else:
        return render_template('/editbook.html', category_id=category_id,
                               book_id=book_id, book=editedBook,
                               categories=categories, form=form)