def addBaseMenuItem(cuisine_id): '''Serve form to add a base menu item ''' client_login_session = getClientLoginSession() cuisine = DataManager.getCuisine(cuisine_id=cuisine_id) menuSections = DataManager.getMenuSections() if request.method == 'POST': if isCSRFAttack(request.form['hiddenToken']): return redirect(url_for('restaurantManagerIndex')) name = validateUserInput(request.form['name'], 'name', 'create', 'base menu item', maxlength=80, required=True, unique=True, tableName='BaseMenuItem') if name is None: return redirect(url_for('cuisine', cuisine_id=cuisine.id)) description = \ validateUserInput(request.form['description'], 'description', 'create', 'base menu item', maxlength=250, required=True) if description is None: return redirect(url_for('cuisine', cuisine_id=cuisine.id)) price = validateUserInput(request.form['price'], 'price', 'create', 'base menu item', maxlength=20, required=True, priceFormat=True) if price is None: return redirect(url_for('cuisine', cuisine_id=cuisine.id)) validMenuSectionIDs = {} for menuSection in menuSections: validMenuSectionIDs[str(menuSection.id)] = True menuSection_id = validateUserInput(request.form['menuSection'], 'menuSection_id', 'create', 'base menu item', columnNameForMsg='menu section', required=True, validInputs=validMenuSectionIDs) if menuSection_id is None: return redirect(url_for('cuisine', cuisine_id=cuisine.id)) providedPic = validateUserPicture('create', 'base menu item', file=request.files['pictureFile'], link=request.form['pictureLink'], maxlength=300, required=True) if providedPic is None: return redirect(url_for('cuisine', cuisine_id=cuisine.id)) picture_id = DataManager.addPicture(text=providedPic['text'], serve_type=providedPic['serve_type']) baseMenuItem_id = DataManager.\ addBaseMenuItem(name, cuisine_id, description=description, price=price, menuSection_id=menuSection_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 = 'baseMenuItem' + str(baseMenuItem_id) request.files['pictureFile'].save(os.path.\ join(app.config['UPLOAD_FOLDER'], picfilename)) DataManager.editPicture(picture_id=picture_id, newText=picfilename) flash("added '" + name + "' to " + cuisine.name + \ "'s base menu") return redirect(url_for('cuisine', cuisine_id=cuisine.id)) else: return render_template('AddBaseMenuItem.html', cuisine=cuisine, menuSections=menuSections, hiddenToken=login_session['state'], 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 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)