def cuisines(): ''' Display all cuisines ''' cuisines = DataManager.getCuisines() client_login_session = getClientLoginSession() return render_template("Cuisines.html", cuisines=cuisines, client_login_session=client_login_session)
def restaurants(): '''Serve info about all of the restaurants ''' client_login_session = getClientLoginSession() cuisines = DataManager.getCuisines() numRestaurants = 0 # get restaurants labeled with user or non-user # sectioned by cuisine cuisineToRestaurantsDict = {} for cuisine in cuisines: cuisineToRestaurantsDict[cuisine.id] = {} cuisineToRestaurantsDict[cuisine.id]['cuisine'] = cuisine restaurants = DataManager.\ getRestaurants(cuisine_id=cuisine.id) restaurantDicts = {} for restaurant in restaurants: numRestaurants += 1 restaurantDict = {} restaurantDict['restaurant'] = restaurant if (isLoggedIn() and restaurant.user_id == login_session['user_id']): restaurantDict['ownership'] = 'user' else: restaurantDict['ownership'] = 'non-user' restaurantDicts[restaurant.id] = restaurantDict cuisineToRestaurantsDict[cuisine.id]['restaurants'] = \ restaurantDicts return render_template("Restaurants.html", cuisineToRestaurantsDict=cuisineToRestaurantsDict, numRestaurants=numRestaurants, client_login_session=client_login_session)
def restaurants(): '''Serve info about all of the restaurants ''' client_login_session = getClientLoginSession() cuisines = DataManager.getCuisines() numRestaurants = 0 # get restaurants labeled with user or non-user # sectioned by cuisine cuisineToRestaurantsDict = {} for cuisine in cuisines: cuisineToRestaurantsDict[cuisine.id] = {} cuisineToRestaurantsDict[cuisine.id]['cuisine'] = cuisine restaurants = DataManager.\ getRestaurants(cuisine_id=cuisine.id) restaurantDicts = {} for restaurant in restaurants: numRestaurants += 1 restaurantDict = {} restaurantDict['restaurant'] = restaurant if (isLoggedIn() and restaurant.user_id == login_session['user_id']): restaurantDict['ownership'] = 'user' else: restaurantDict['ownership'] = 'non-user' restaurantDicts[restaurant.id] = restaurantDict cuisineToRestaurantsDict[cuisine.id]['restaurants'] = \ restaurantDicts return render_template("Restaurants.html", cuisineToRestaurantsDict=cuisineToRestaurantsDict, numRestaurants=numRestaurants, client_login_session=client_login_session)
def addRestaurant(): '''Serve form to add a restaurant ''' client_login_session = getClientLoginSession() cuisines = DataManager.getCuisines() if request.method == 'POST': if isCSRFAttack(request.form['hiddenToken']): return redirect(url_for('restaurantManagerIndex')) validCuisineIDs = {} for cuisine in cuisines: validCuisineIDs[str(cuisine.id)] = True cuisine_id = validateUserInput(request.form['cuisineID'], 'cuisine_id', 'create', 'restaurant', columnNameForMsg='cuisine', required=True, validInputs=validCuisineIDs) if cuisine_id is None: return redirect(url_for('restaurants')) name = validateUserInput(request.form['name'], 'name', 'create', 'restaurant', maxlength=100, required=True) if name is None: return redirect(url_for('restaurants')) providedPic = validateUserPicture('create', 'restaurant', file=request.files['pictureFile'], link=request.form['pictureLink'], maxlength=300, required=True) if providedPic is None: return redirect(url_for('restaurants')) picture_id = DataManager.addPicture( text=providedPic['text'], serve_type=providedPic['serve_type']) restaurant_id = DataManager.addRestaurant( name=name, cuisine_id=cuisine_id, user_id=login_session['user_id'], picture_id=picture_id) # if pic was uploaded, now that we know item id, # save actual file for serving and set the name in the database if providedPic['serve_type'] == 'upload': picfilename = 'restaurant' + str(restaurant_id) request.files['pictureFile'].save(os.path.\ join(app.config['UPLOAD_FOLDER'], picfilename)) DataManager.editPicture(picture_id=picture_id, newText=picfilename) DataManager.populateMenuWithBaseItems(restaurant_id) flash("restaurant '" + name + "' added to the database!") return redirect(url_for('restaurants')) else: return render_template('AddRestaurant.html', cuisines=cuisines, hiddenToken=login_session['state'], client_login_session=client_login_session)
def editRestaurant(restaurant_id): '''Serve form to add a restaurant menu item to a restaurant's menu ''' restaurant = DataManager.getRestaurant(restaurant_id) if restaurant.user_id != login_session['user_id']: flash("You do not have permission to edit this restaurant") return redirect(url_for('restaurant', restaurant_id=restaurant.id)) client_login_session = getClientLoginSession() restaurant = DataManager.getRestaurant(restaurant_id) cuisines = DataManager.getCuisines() picture = DataManager.getPicture(restaurant.picture_id) if request.method == 'POST': if isCSRFAttack(request.form['hiddenToken']): return redirect(url_for('restaurantManagerIndex')) oldName = restaurant.name oldCuisine = DataManager.\ getCuisine(cuisine_id=restaurant.cuisine_id) oldPicture = DataManager.getPicture(restaurant.picture_id) newName = validateUserInput(request.form['name'], 'name', 'edit', 'restaurant', maxlength=100) validCuisineIDs = {} for cuisine in cuisines: validCuisineIDs[str(cuisine.id)] = True # for 'do not change' validCuisineIDs['-2'] = True newCuisine_id = validateUserInput(request.form['cuisineID'], 'cuisine_id', 'edit', 'restaurant', columnNameForMsg='cuisine', oldInput=str(oldCuisine.id), validInputs=validCuisineIDs) if newCuisine_id == '-2': newCuisine_id = None providedPic = validateUserPicture('edit', 'restaurant', file=request.files['pictureFile'], link=request.form['pictureLink'], maxlength=300) if providedPic is not None: # delete the old pic if it was an upload and new is a link # or save the new pic if it was an upload if (providedPic['serve_type'] == 'link' and oldPicture.serve_type == 'upload'): path = app.config['UPLOAD_FOLDER'] + '/' + oldPicture.text os.remove(path) flash("deleted old uploaded pic") elif providedPic['serve_type'] == 'upload': picfilename = 'restaurant' + str(restaurant_id) request.files['pictureFile'].save(os.path.\ join(app.config['UPLOAD_FOLDER'], picfilename)) providedPic['text'] = picfilename # edit the pic DataManager.editPicture(restaurant.picture_id, newText=providedPic['text'], newServe_Type=providedPic['serve_type']) flash("updated base menu item picture") # we edited the pic directly, no need to include here DataManager.editRestaurant(restaurant.id, newName=newName, newCuisine_id=newCuisine_id) restaurant = DataManager.getRestaurant(restaurant_id) if newName is not None: flash("changed " + restaurant.name + "'s (ID " + \ str(restaurant.id) + ") name from '" + oldName + \ "' to '" + newName + "'") if newCuisine_id is not None: flash("changed " + restaurant.name + "'s (ID " + \ str(restaurant.id) + ") cuisine") return redirect(url_for('restaurant', restaurant_id=restaurant_id)) else: return render_template('EditRestaurant.html', restaurant=restaurant, cuisines=cuisines, hiddenToken=login_session['state'], picture=picture, client_login_session=client_login_session)
def cuisinesJSON(): '''JSON endpoint for all cuisines ''' cuisines = DataManager.getCuisines() return jsonify(Cuisines=[i.serialize for i in cuisines])
def addRestaurant(): '''Serve form to add a restaurant ''' client_login_session = getClientLoginSession() cuisines = DataManager.getCuisines() if request.method == 'POST': if isCSRFAttack(request.form['hiddenToken']): return redirect(url_for('restaurantManagerIndex')) validCuisineIDs = {} for cuisine in cuisines: validCuisineIDs[str(cuisine.id)] = True cuisine_id = validateUserInput(request.form['cuisineID'], 'cuisine_id', 'create', 'restaurant', columnNameForMsg='cuisine', required=True, validInputs=validCuisineIDs) if cuisine_id is None: return redirect(url_for('restaurants')) name = validateUserInput(request.form['name'], 'name', 'create', 'restaurant', maxlength=100, required=True) if name is None: return redirect(url_for('restaurants')) providedPic = validateUserPicture('create', 'restaurant', file=request.files['pictureFile'], link=request.form['pictureLink'], maxlength=300, required=True) if providedPic is None: return redirect(url_for('restaurants')) picture_id = DataManager.addPicture(text=providedPic['text'], serve_type=providedPic['serve_type']) restaurant_id = DataManager.addRestaurant( name=name, cuisine_id=cuisine_id, user_id=login_session['user_id'], picture_id=picture_id ) # if pic was uploaded, now that we know item id, # save actual file for serving and set the name in the database if providedPic['serve_type'] == 'upload': picfilename = 'restaurant' + str(restaurant_id) request.files['pictureFile'].save(os.path.\ join(app.config['UPLOAD_FOLDER'], picfilename)) DataManager.editPicture(picture_id=picture_id, newText=picfilename) DataManager.populateMenuWithBaseItems(restaurant_id) flash("restaurant '" + name + "' added to the database!") return redirect(url_for('restaurants')) else: return render_template('AddRestaurant.html', cuisines=cuisines, hiddenToken=login_session['state'], client_login_session=client_login_session)
def editRestaurant(restaurant_id): '''Serve form to add a restaurant menu item to a restaurant's menu ''' restaurant = DataManager.getRestaurant(restaurant_id) if restaurant.user_id != login_session['user_id']: flash("You do not have permission to edit this restaurant") return redirect(url_for('restaurant', restaurant_id=restaurant.id)) client_login_session = getClientLoginSession() restaurant = DataManager.getRestaurant(restaurant_id) cuisines = DataManager.getCuisines() picture = DataManager.getPicture(restaurant.picture_id) if request.method == 'POST': if isCSRFAttack(request.form['hiddenToken']): return redirect(url_for('restaurantManagerIndex')) oldName = restaurant.name oldCuisine = DataManager.\ getCuisine(cuisine_id=restaurant.cuisine_id) oldPicture = DataManager.getPicture(restaurant.picture_id) newName = validateUserInput(request.form['name'], 'name', 'edit', 'restaurant', maxlength=100) validCuisineIDs = {} for cuisine in cuisines: validCuisineIDs[str(cuisine.id)] = True # for 'do not change' validCuisineIDs['-2'] = True newCuisine_id = validateUserInput(request.form['cuisineID'], 'cuisine_id', 'edit', 'restaurant', columnNameForMsg='cuisine', oldInput=str(oldCuisine.id), validInputs=validCuisineIDs) if newCuisine_id == '-2': newCuisine_id = None providedPic = validateUserPicture('edit', 'restaurant', file=request.files['pictureFile'], link=request.form['pictureLink'], maxlength=300) if providedPic is not None: # delete the old pic if it was an upload and new is a link # or save the new pic if it was an upload if (providedPic['serve_type'] == 'link' and oldPicture.serve_type == 'upload'): path = app.config['UPLOAD_FOLDER']+'/'+oldPicture.text os.remove(path) flash("deleted old uploaded pic") elif providedPic['serve_type'] == 'upload': picfilename = 'restaurant' + str(restaurant_id) request.files['pictureFile'].save(os.path.\ join(app.config['UPLOAD_FOLDER'], picfilename)) providedPic['text'] = picfilename # edit the pic DataManager.editPicture(restaurant.picture_id, newText=providedPic['text'], newServe_Type=providedPic['serve_type']) flash("updated base menu item picture") # we edited the pic directly, no need to include here DataManager.editRestaurant(restaurant.id, newName=newName, newCuisine_id=newCuisine_id) restaurant = DataManager.getRestaurant(restaurant_id) if newName is not None: flash("changed " + restaurant.name + "'s (ID " + \ str(restaurant.id) + ") name from '" + oldName + \ "' to '" + newName + "'") if newCuisine_id is not None: flash("changed " + restaurant.name + "'s (ID " + \ str(restaurant.id) + ") cuisine") return redirect(url_for('restaurant', restaurant_id=restaurant_id)) else: return render_template('EditRestaurant.html', restaurant=restaurant, cuisines=cuisines, hiddenToken=login_session['state'], picture=picture, client_login_session=client_login_session)
def cuisinesJSON(): '''JSON endpoint for all cuisines ''' cuisines = DataManager.getCuisines() return jsonify(Cuisines=[i.serialize for i in cuisines])