Beispiel #1
0
def editCuisine(cuisine_id):
    '''Serve form to edit a cuisine
    '''
    client_login_session = getClientLoginSession()

    cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)

    if request.method == 'POST':

        if isCSRFAttack(request.form['hiddenToken']):
            return redirect(url_for('restaurantManagerIndex'))

        oldName = cuisine.name

        newName = validateUserInput(request.form['name'],
            'name', 'edit', 'cuisine', maxlength=80, unique=True, 
            oldInput=oldName, tableName='Cuisine')

        DataManager.editCuisine(cuisine_id, newName=newName)
        
        if newName is not None:
            
            flash("Changed cuisine's name from '" + oldName +\
                "' to '" + newName + "'")

        return redirect(url_for('cuisine',
                                cuisine_id=cuisine_id))
    else:
        return render_template("EditCuisine.html",
                               cuisine=cuisine,
                               hiddenToken=login_session['state'],
                               client_login_session=client_login_session)
Beispiel #2
0
def deleteBaseMenuItem(cuisine_id, baseMenuItem_id):
    '''Serve form to delete a base menu item
    '''
    client_login_session = getClientLoginSession()

    baseMenuItem = DataManager.\
                   getBaseMenuItem(baseMenuItem_id=baseMenuItem_id)

    if request.method == 'POST':

        if isCSRFAttack(request.form['hiddenToken']):
            return redirect(url_for('restaurantManagerIndex'))

        cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)
        restaurantMenuItems = DataManager.\
            getRestaurantMenuItems(baseMenuItem_id=baseMenuItem_id)
        baseForNoCuisine = DataManager.\
            getBaseMenuItem(baseMenuItem_id=-1)

        DataManager.deleteBaseMenuItem(baseMenuItem_id=baseMenuItem_id)

        flash("reassigned " + str(len(restaurantMenuItems)) + \
            " restaurant menu items' base to '" +\
            baseForNoCuisine.name + "'")
        flash("deleted " + baseMenuItem.name + " from " +\
            cuisine.name + "'s base menu and from the database")

        return redirect(url_for('cuisine',cuisine_id=cuisine_id))
    else:

        return render_template("DeleteBaseMenuItem.html",
                            baseMenuItem=baseMenuItem,
                            cuisine_id=cuisine_id,
                            hiddenToken=login_session['state'],
                            client_login_session=client_login_session)
Beispiel #3
0
def deleteCuisine(cuisine_id):
    '''Serve form to delete a cuisine
    '''
    client_login_session = getClientLoginSession()

    cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)

    if request.method == 'POST':

        if isCSRFAttack(request.form['hiddenToken']):
            return redirect(url_for('restaurantManagerIndex'))

        # all of this is for flash messaging
        cuisineName = cuisine.name
        cuisineID = cuisine.id
        restaurantMenuItems = DataManager.\
                              getRestaurantMenuItems(cuisine_id=cuisine_id)
        numItemsReassigned = len(restaurantMenuItems)
        restaurants = DataManager.\
                      getRestaurants(cuisine_id=cuisine_id)
        numRestaurantsReassigned = len(restaurants)
        baseMenuItems = DataManager.\
                        getBaseMenuItems(cuisine_id=cuisine_id)
        numItemsDeleted = len(baseMenuItems)
        itemBaseForNoCuisine = DataManager.\
            getBaseMenuItem(baseMenuItem_id=-1)

        # here is the logic
        restaurantBaseForNoCuisine = DataManager.\
                                     getCuisine(cuisine_id=-1)

        DataManager.deleteCuisine(cuisine_id)

        flash("reassigned " + str(numItemsReassigned) + \
            " restaurant menu items' base item to '" + \
            itemBaseForNoCuisine.name + "'")

        flash("reassigned " + str(numRestaurantsReassigned) + \
            " restaurants' cuisine to '" + \
            restaurantBaseForNoCuisine.name + "'")

        flash("deleted " + str(numItemsDeleted) + \
            " base menu items from the database")

        flash("deleted cuisine " + str(cuisineID) + " (" + \
            cuisineName + ") from the database")

        return redirect(url_for('cuisines'))
    else:
        return render_template("DeleteCuisine.html",
                               cuisine=cuisine,
                               hiddenToken=login_session['state'],
                               client_login_session=client_login_session)
Beispiel #4
0
def cuisineJSON(cuisine_id):
    '''JSON endpoint for a single cuisine

    Includes all restaurants with that cuisine, all base menu items
    for that cuisine, and all restaurant menu items based on that cuisine
    '''
    cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)
    baseMenuItems = DataManager.getBaseMenuItems(cuisine_id=cuisine_id)
    restaurants = DataManager.getRestaurants(cuisine_id=cuisine_id)
    restaurantMenuItems = DataManager.\
                          getRestaurantMenuItems(cuisine_id=cuisine_id)

    return jsonify(Cuisine=cuisine.serialize,
                   BaseMenuItems=[i.serialize for i in baseMenuItems],
                   Restaurants=[i.serialize for i in restaurants],
                   RestaurantMenuItems=\
                    [i.serialize for i in restaurantMenuItems])
Beispiel #5
0
def cuisineJSON(cuisine_id):
    '''JSON endpoint for a single cuisine

    Includes all restaurants with that cuisine, all base menu items
    for that cuisine, and all restaurant menu items based on that cuisine
    '''
    cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)
    baseMenuItems = DataManager.getBaseMenuItems(cuisine_id=cuisine_id)
    restaurants = DataManager.getRestaurants(cuisine_id=cuisine_id)
    restaurantMenuItems = DataManager.\
                          getRestaurantMenuItems(cuisine_id=cuisine_id)

    return jsonify(Cuisine=cuisine.serialize,
                   BaseMenuItems=[i.serialize for i in baseMenuItems],
                   Restaurants=[i.serialize for i in restaurants],
                   RestaurantMenuItems=\
                    [i.serialize for i in restaurantMenuItems])
Beispiel #6
0
def restaurant(restaurant_id):
    '''Serve info about a restaurant
    '''
    client_login_session = getClientLoginSession()

    restaurant = DataManager.getRestaurant(restaurant_id)
    owner = DataManager.getUser(restaurant.user_id)
    restaurantMenuItems = DataManager.\
                          getRestaurantMenuItems(restaurant_id=restaurant_id)
    cuisine = DataManager.getCuisine(cuisine_id=restaurant.cuisine_id)

    picture = DataManager.getPicture(restaurant.picture_id)

    numMenuItems = len(restaurantMenuItems)

    if numMenuItems > 0:
        mostExpensiveItem = restaurantMenuItems[0]
        for item in restaurantMenuItems:
            if item.price > mostExpensiveItem.price:
                mostExpensiveItem = item
                mostExpensiveItem.price =\
                    Decimal(mostExpensiveItem.price).\
                    quantize(Decimal('0.01'))
        mostExpensiveItem.price =\
            Decimal(mostExpensiveItem.price).\
            quantize(Decimal('0.01'))
    else:
        mostExpensiveItem = DataManager.\
                            getBaseMenuItem(baseMenuItem_id=-1)
        mostExpensiveItem.name = 'N/A'
        mostExpensiveItem.price = 'N/A'

    return render_template('Restaurant.html',
                           restaurant=restaurant,
                           numMenuItems=numMenuItems,
                           mostExpensiveItem=mostExpensiveItem,
                           cuisine=cuisine,
                           picture=picture,
                           owner=owner,
                           client_login_session=client_login_session)
Beispiel #7
0
def restaurant(restaurant_id):
    '''Serve info about a restaurant
    '''
    client_login_session = getClientLoginSession()

    restaurant = DataManager.getRestaurant(restaurant_id)
    owner = DataManager.getUser(restaurant.user_id)
    restaurantMenuItems = DataManager.\
                          getRestaurantMenuItems(restaurant_id=restaurant_id)
    cuisine = DataManager.getCuisine(cuisine_id=restaurant.cuisine_id)

    picture = DataManager.getPicture(restaurant.picture_id)

    numMenuItems = len(restaurantMenuItems)

    if numMenuItems > 0:
        mostExpensiveItem = restaurantMenuItems[0]
        for item in restaurantMenuItems:
            if item.price > mostExpensiveItem.price:
                mostExpensiveItem = item
                mostExpensiveItem.price =\
                    Decimal(mostExpensiveItem.price).\
                    quantize(Decimal('0.01'))
        mostExpensiveItem.price =\
            Decimal(mostExpensiveItem.price).\
            quantize(Decimal('0.01'))
    else:
        mostExpensiveItem = DataManager.\
                            getBaseMenuItem(baseMenuItem_id=-1)
        mostExpensiveItem.name = 'N/A'
        mostExpensiveItem.price = 'N/A'

    return render_template('Restaurant.html', restaurant=restaurant, 
                           numMenuItems=numMenuItems,
                           mostExpensiveItem=mostExpensiveItem,
                           cuisine=cuisine, picture=picture, owner=owner,
                           client_login_session=client_login_session)
Beispiel #8
0
def cuisine(cuisine_id):
    '''Serve cuisine info page
    '''
    cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)
    restaurants = DataManager.\
                  getRestaurants(cuisine_id=cuisine_id)
    baseMenuItems = DataManager.\
                    getBaseMenuItems(cuisine_id=cuisine_id)
    restaurantMenuItems = DataManager.\
        getRestaurantMenuItems(cuisine_id=cuisine_id)
    sectionedBaseMenuItems = DataManager.\
                             getBaseMenuItems(cuisine_id=cuisine_id,
                                              byMenuSection=True)

    client_login_session = getClientLoginSession()

    # get restaurants labeled with user or non-user
    restaurantDicts = {}
    for restaurant in restaurants:
        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

    # get the base items with their children 
    # in format that plays nice with jinja
    # and labels things user or non-user
    # and also calculate some data about the items
    mostExpensiveBaseMenuItem = DataManager.\
    getBaseMenuItem(baseMenuItem_id=-1)

    mostExpensiveRestaurantMenuItem = DataManager.\
        getBaseMenuItem(baseMenuItem_id=-1)

    sectionedBaseItemsWithChildren = {}

    for section, baseItemList in sectionedBaseMenuItems.iteritems():

        sectionedBaseItemsWithChildren[section] = {}

        for baseItem in baseItemList:

            baseItemID = baseItem.id

            if baseItem.price > mostExpensiveBaseMenuItem.price:
                mostExpensiveBaseMenuItem = baseItem

            childrenItems = DataManager.\
                getRestaurantMenuItems(baseMenuItem_id=baseItem.id)
            children = {}

            for item in childrenItems:

                if item.price > mostExpensiveRestaurantMenuItem.price:
                    mostExpensiveRestaurantMenuItem = item

                itemRestaurant = DataManager.\
                                 getRestaurant(item.restaurant_id)
                itemUserID = itemRestaurant.user_id
                child = {}
                child['item'] = item

                if (isLoggedIn() and
                    itemUserID == login_session['user_id']):
                    child['ownership'] = 'user'
                else:
                    child['ownership'] = 'non-user'

                children[item.id] = child

            itemWithChildren = {'item':baseItem, 'children':children}
            sectionedBaseItemsWithChildren[section][baseItem.id] = \
                itemWithChildren

    # this means there were no items, so display N/A
    if mostExpensiveRestaurantMenuItem.id == -1:
        mostExpensiveRestaurantMenuItem.name = "N/A"
        mostExpensiveRestaurantMenuItem.price = "N/A"
        mostExpensiveRestaurantMenuItem.restaurant_id = "N/A"
    else:
        # display nicely
        mostExpensiveRestaurantMenuItem.price = \
            Decimal(mostExpensiveRestaurantMenuItem.price).\
            quantize(Decimal('0.01'))

    if mostExpensiveBaseMenuItem.id == -1:
        mostExpensiveBaseMenuItem.name = "N/A"
        mostExpensiveBaseMenuItem.price = "N/A"
    else:
        mostExpensiveBaseMenuItem.price = \
            Decimal(mostExpensiveBaseMenuItem.price).\
            quantize(Decimal('0.01'))

    return render_template("Cuisine.html",
        cuisine=cuisine,
        mostExpensiveBaseMenuItem=mostExpensiveBaseMenuItem,
        mostExpensiveRestaurantMenuItem=mostExpensiveRestaurantMenuItem,
        restaurantDicts=restaurantDicts,
        sectionedBaseItemsWithChildren=sectionedBaseItemsWithChildren,
        client_login_session=client_login_session)
Beispiel #9
0
def editBaseMenuItem(cuisine_id, baseMenuItem_id):
    '''Serve form to edit a base menu item
    '''
    client_login_session = getClientLoginSession()

    baseMenuItem = DataManager.\
                   getBaseMenuItem(baseMenuItem_id=baseMenuItem_id)
    cuisine = DataManager.getCuisine(cuisine_id=cuisine_id)

    baseMenuItem.price = Decimal(baseMenuItem.price).quantize(Decimal('0.01'))

    picture = DataManager.getPicture(baseMenuItem.picture_id)

    menuSections = DataManager.getMenuSections()

    if request.method == 'POST':

        if isCSRFAttack(request.form['hiddenToken']):
            return redirect(url_for('restaurantManagerIndex'))

        oldName = baseMenuItem.name
        oldDescription = baseMenuItem.description
        oldPrice = baseMenuItem.price
        oldPicture = picture
        oldMenuSection_id = baseMenuItem.menuSection_id
        
        newName = validateUserInput(request.form['name'], 'name', 
            'edit', 'base menu item', maxlength=80, 
            unique=True, oldInput=oldName)
            
        newDescription = validateUserInput(request.form['description'],
            'description', 'edit', 'base menu item', maxlength=250,
            oldInput=oldDescription)

        newPrice = validateUserInput(request.form['price'], 'price',
            'edit', 'base menu item', maxlength=20,
            priceFormat=True, oldInput=str(oldPrice))

        validMenuSectionIDs = {}
        for menuSection in menuSections:
            validMenuSectionIDs[str(menuSection.id)] = True

        # for 'do not change'
        validMenuSectionIDs['-1'] = True

        newMenuSection_id = validateUserInput(request.form['menuSection'],
                'menuSection_id', 'edit', 'base menu item',
                columnNameForMsg='menu section',
                oldInput=str(oldMenuSection_id),
                validInputs=validMenuSectionIDs)

        if newMenuSection_id == '-1':
            newMenuSection_id = None

        providedPic = validateUserPicture('edit', 'base menu item',
            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 = 'baseMenuItem' + str(baseMenuItem_id)
                request.files['pictureFile'].save(os.path.\
                    join(app.config['UPLOAD_FOLDER'], picfilename))
                providedPic['text'] = picfilename

            # edit the pic
            DataManager.editPicture(baseMenuItem.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.editBaseMenuItem(baseMenuItem.id,
            newName=newName, newDescription=newDescription, 
            newPrice=newPrice, newMenuSection_id=newMenuSection_id)

        if newName is not None:
            flash("changed name from '"+oldName+"' to '"+newName+"'")

        if newDescription is not None:
            flash("changed description from '"+ oldDescription + "' to '" + \
                newDescription + "'")

        if newPrice is not None:
            flash("changed price from '" + str(oldPrice) + "' to '" + \
                str(newPrice) + "'")

        if newMenuSection_id is not None:
            flash("changed menu section")

        return redirect(url_for('baseMenuItem',
                                cuisine_id=cuisine_id,
                                baseMenuItem_id=baseMenuItem_id))
    else:
        return render_template("EditBaseMenuItem.html",
                               baseMenuItem=baseMenuItem,
                               cuisine=cuisine,
                               hiddenToken=login_session['state'],
                               picture=picture,
                               menuSections=menuSections,
                               client_login_session=client_login_session)
Beispiel #10
0
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)