def editCatalogItem(category_name, catalog_name): '''Edit a catalog item''' if 'username' not in login_session: return redirect('/login') categories = session.query(Category).all() category = session.query(Category).filter_by(name=category_name).one() itemToEdit = session.query(CatalogItem).filter_by(category_id=category.id, title=catalog_name).one() if login_session['user_id'] != itemToEdit.user_id: return """<script>function myFunction() { alert('You are not authorized to edit this item. Please ' + 'create your own restaurant in order to add items.');} </script><body onload='myFunction()'>""" if request.method == 'POST': if request.form['title']: itemToEdit.title = request.form['title'] if request.form['description']: itemToEdit.description = request.form['description'] if request.form['categorylist']: targetCategoryName = request.form['categorylist'] targetCategoryID = session.query(Category).filter_by( name=targetCategoryName).one().id itemToEdit.category_id = targetCategoryID session.add(itemToEdit) session.commit() flash('%s Successfully Updated' % (itemToEdit.title)) return redirect(url_for('showCatalogItems', category_name=category.name)) else: return render_template('editCatalogItem.html', item=itemToEdit, categories=categories, category_name=category.name)
def showCatalogItems(category_name): '''Show all items under a category''' categories = session.query(Category).order_by(asc(Category.name)) category = session.query(Category).filter_by(name=category_name).one() items = session.query(CatalogItem).filter_by(category_id=category.id).all() return render_template('catalogItems.html', items=items, category=category, categories=categories)
def categoryItemJSON(category_name, catalog_name): category = session.query(Category).filter_by(name=category_name).one() item = session.query(CatalogItem).filter_by(category_id=category.id, title=catalog_name).one() item = item.serialize del item['cat_id'] del item['id'] return jsonify(ItemInfo=item)
def allCategoriesItemsJSON(): categories = session.query(Category).all() res = [] for r in categories: dct = r.serialize category = session.query(Category).filter_by(name=dct['name']).one() items = session.query(CatalogItem).filter_by( category_id=category.id).all() dct['items'] = [i.serialize for i in items] res.append(dct) return jsonify(categories=[r for r in res])
def showOneCatalogItem(category_name, catalog_name): '''Show detailed information about one catalog item under a category''' category = session.query(Category).filter_by(name=category_name).one() item = session.query(CatalogItem).filter_by( category_id=category.id, title=catalog_name).one() creator = getUserInfo(item.user_id) if ('username' not in login_session or creator.id != login_session['user_id']): return render_template('publicOneCatalogItem.html', item=item, category_name=category.name) else: return render_template('oneCatalogItem.html', item=item, category_name=category.name)
def showCategories(): ''' display all categories''' categories = session.query(Category).order_by(asc(Category.name)) catalogCategory = session.query( CatalogItem, Category).filter(CatalogItem.category_id == Category.id).all() catalogCategory = sorted(catalogCategory, key=lambda x: x[0].id, reverse=True)[:10] if 'username' not in login_session: return render_template('publicCategories.html', categories=categories, items=catalogCategory) else: return render_template('categories.html', categories=categories, items=catalogCategory)
def deleteCatalogItem(category_name, catalog_name): '''Delete a new catalog Item''' if 'username' not in login_session: return redirect('/login') category = session.query(Category).filter_by(name=category_name).one() itemToDelete = session.query(CatalogItem).filter_by( category_id=category.id, title=catalog_name).one() if login_session['user_id'] != itemToDelete.user_id: return """<script>function myFunction() { alert('You are not authorized to delete this item. You ' + 'can only delete items that you created.');} </script><body onload='myFunction()'>""" if request.method == 'POST': session.delete(itemToDelete) session.commit() flash('%s have been deleted.' % (itemToDelete.title)) return redirect(url_for('showCatalogItems', category_name=category.name)) else: return render_template('deleteCatalogItem.html', item=itemToDelete, category_name=category.name)
def newCatalogItem(): '''Add a new catalog item''' if 'username' not in login_session: return redirect('/login') categories = session.query(Category).all() if request.method == 'POST': cat_name = request.form['categorylist'] cat_id = session.query(Category).filter_by(name=cat_name).one().id newItem = CatalogItem(title=request.form['title'], description=request.form['description'], category_id=cat_id, user_id=login_session['user_id']) existingItems = session.query(CatalogItem).filter_by( category_id=newItem.category_id).all() existingTitles = set([item.title for item in existingItems]) if newItem.title not in existingTitles: session.add(newItem) session.commit() flash('New Catalog %s Item Successfully Created' % (newItem.title)) else: flash('Same item exists!') return redirect(url_for('showCategories')) else: return render_template('newCatalogItem.html', categories=categories)
def categoriesJSON(): categories = session.query(Category).all() return jsonify(categories=[r.serialize for r in categories])