Exemplo n.º 1
0
def index():
    """Displays the index i.e. home page, with db queries made to return
    all categories in ascending order i.e. alphabetical order
    all books in descending order by created date
    """
    categories = db_session.query(Category).order_by(asc(Category.name))
    books = db_session.query(Book).order_by(desc(Book.created_at))
    return render_template('index.html', categories=categories, books=books)
Exemplo n.º 2
0
def showCategory(category_id):
    """Displays the category with all books within the category"""
    category = db_session.query(Category).filter_by(id=category_id).one()
    books = db_session.query(Book).filter_by(category_id=category_id).all()
    return render_template('categorybooks.html',
                           category_id=category_id,
                           category=category,
                           books=books)
Exemplo n.º 3
0
def categoryWithBooksJSON(category_id):
    """
    Generates JSON for a category with all books in the category
    Specified by the category_id
    """
    books = db_session.query(Book).filter_by(category_id=category_id).all()
    return jsonify(Category=[b.serialize for b in books])
Exemplo n.º 4
0
def get_user_info(user_id):
    """
    If a user_id is passed into this method, it returns the
    user object associated with the id
    """
    user = db_session.query(User).filter_by(id=user_id).one()
    return user
Exemplo n.º 5
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'))
Exemplo n.º 6
0
def restaurant_menu_json(restaurant_id):
    """Created route and function for api endpoint,
        it will be use to display every menu for each restaurant"""
    try:
        items = db_session.query(MenuItem).filter_by(restaurant_id=restaurant_id).all()
        return jsonify(menu_items=[i.serialize for i in items])
    except:
        return "Info not found"
Exemplo n.º 7
0
def restaurant_info(restaurant_id):
    """reated route and function for api endpoint,
        it will be use to display restaurants info"""
    try:
        restaurants = db_session.query(Restaurant).filter_by(id=restaurant_id).one()
        restaurants_info = jsonify(restaurant_info=restaurants.serialize)
        return restaurants_info
    except:
        return "Info not found"
Exemplo n.º 8
0
def restaurant_detail(restaurant_id):
    """Created route and function to display the
        restaurant main page and menu individually"""
    try:
        restaurant = db_session.query(Restaurant).filter_by(
            id=restaurant_id).one()
        address = db_session.query(RestaurantAddress).filter_by(
            id=restaurant_id).one()
        items = db_session.query(MenuItem).filter_by(
            restaurant_id=restaurant.id)
        return render_template("restaurant/restaurantownpage.html",
                               restaurant=restaurant,
                               items=items,
                               address=address)

    except:
        return render_template("restaurant/restaurants.html",
                               error="Restaurant Not Found")
Exemplo n.º 9
0
def each_menu_json(restaurant_id, menu_id):
    """Created route and function for api endpoint,
        it will be use to display each menu individually"""
    try:
        item = db_session.query(MenuItem).filter_by(id=menu_id, restaurant_id=restaurant_id).one()
        return jsonify(MenuItem=item.serialize)

    except:
        return "Info not found"
Exemplo n.º 10
0
def get_user_id(email):
    """
    Takes in an email address, and if this matches a value stored in
    db, it returns the associated user.id
    """
    try:
        user = db_session.query(User).filter_by(email=email).one()
        return user.id
    except:
        return None
Exemplo n.º 11
0
def restaurant_location(restaurant_id):
    """reated route and function for api endpoint,
        it will be use to display restaurants info"""
    try:
        address = db_session.query(RestaurantAddress).filter_by(restaurant_id=restaurant_id).one()
        restaurant_address = jsonify(location=address.serialize)
        return restaurant_address

    except:
        return "Info not found"
Exemplo n.º 12
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'))
Exemplo n.º 13
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'))
Exemplo n.º 14
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)
Exemplo n.º 15
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
Exemplo n.º 16
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)
Exemplo n.º 17
0
def show_restaurants():
    """Created route and function to display the
       restaurant list """
    restaurants = db_session.query(Restaurant).order_by(desc(Restaurant.name))
    if restaurants:
        if 'user_id' in login_session:
            print('user_id in login_session')
            return render_template('restaurant/restaurants.html',
                                   restaurants=restaurants,
                                   hello="database found")
        else:
            print('user_id not in login_session')
            return render_template('restaurant/restaurants.html',
                                   restaurants=restaurants)
    else:
        return render_template('restaurant/restaurants.html',
                               restaurants=restaurants,
                               hello="no database")
Exemplo n.º 18
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)
Exemplo n.º 19
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'))
Exemplo n.º 20
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)
Exemplo n.º 21
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)
Exemplo n.º 22
0
def bookJSON(book_id):
    """
    Generates JSON for a book specified by the book_id
    """
    book = db_session.query(Book).filter_by(id=book_id).one()
    return jsonify(book=book.serialize)
Exemplo n.º 23
0
def theBook(category_id, book_id):
    """Displays page showing one book including all the book's info"""
    book = db_session.query(Book).filter_by(id=book_id).one()
    return render_template('book.html', category_id=category_id, book=book)
Exemplo n.º 24
0
def allCategoriesJSON():
    """
    Generates JSON for all categories
    """
    categories = db_session.query(Category).all()
    return jsonify(categories=[c.serialize for c in categories])