Exemplo n.º 1
0
def item_edit(request, id, library_code, menu_id=None, managed_libraries=[]):
    library = org_utils.get_library(library_code, managed_libraries)
    if not library:
        return HttpResponseForbidden(
            u'Вы должны быть сотрудником этой организации')

    menu = get_object_or_404(Menu, id=menu_id)
    item = get_object_or_404(MenuItem, id=id)

    if request.method == 'POST':
        item_form = MenuItemForm(request.POST,
                                 prefix='item_form',
                                 instance=item)
        if item_form.is_valid():
            item_form.save()
            return redirect('participant_menu:administration:menu_detail',
                            menu_id=menu_id,
                            library_code=library_code)

    else:
        item_form = MenuItemForm(prefix="item_form", instance=item)
    return render(request, 'participant_menu/administration/edit_item.html', {
        'library': library,
        'item': item,
        'item_form': item_form,
        'menu': menu
    })
Exemplo n.º 2
0
def menu_edit(request, key=None):
    if not key:
        return redirect(menu_list)
    m = MenuItem.get(key)
    if request.method == 'POST':
        formset = MenuItemForm(request.POST, instance=m)
        if formset.is_valid():
            m = formset.save()
            MenuItem.get(m.key())  # for refreshing purposes http://stackoverflow.com/questions/15773892/should-i-expect-stale-results-after-redirect-on-local-environment
            return redirect(menu_list)
    else:
        formset = MenuItemForm(instance=m)
    return render_to_response("menu_add.html", {        
                                    "formset": formset,
                                    "level"  : m.level
                              }, context_instance=RequestContext(request))     
Exemplo n.º 3
0
def menu_add(request, url_level=None):
    isInitial = False
    level = request.POST.get('level') or url_level or ""
    if not level:
        formset = SelectChoiceForm()
        isInitial = True
    elif request.POST.get('initial') == "True" or url_level:
        formset = MenuItemForm(initial={'level': level})
    elif request.method == 'POST':
        formset = MenuItemForm(request.POST)
        if formset.is_valid():
            instance = formset.save()
            MenuItem.get(instance.key()) # for refreshing purposes http://stackoverflow.com/questions/15773892/should-i-expect-stale-results-after-redirect-on-local-environment
            return redirect(menu_list)
    return render_to_response("menu_add.html", {        
                                    "formset": formset,
                                    "initial": isInitial,
                                    "level"  : level
                              }, context_instance=RequestContext(request))    
Exemplo n.º 4
0
def new_menu():
    form = MenuItemForm()

    if form.validate_on_submit():
        name = form.name.data
        price = form.price.data
        desc = form.desc.data
        photo = form.photo.data
        photo_name = secure_filename(photo.filename)
        photo_path = os.path.join('static/images/menus', photo_name)
        thumb_path = os.path.join('static/images/menus/thumbs', photo_name)
        thumb_size = 128, 128

        menu = db.engine.execute(
            text(
                "SELECT * FROM menus WHERE menu_name = :name AND menu_price = :price LIMIT 1"
            ), {
                "name": name,
                "price": price
            }).first()

        # add new staff if not exists
        if menu is None:
            # save staff photo
            photo.save(photo_path)

            # create thumbnail for staff
            try:
                im = Image.open(photo_path)
                im.thumbnail(thumb_size, Image.ANTIALIAS)
                im.save(thumb_path, "JPEG")
            except IOError:
                flash("cannot create thumbnail for this menu item")

            # save user and the thumbnail name
            insertSql = "INSERT INTO menus (menu_name, menu_price, menu_desc, menu_photo) VALUES (:mname, :mprice, :mdesc, :mphoto)"
            insertParams = {
                'mname': name,
                'mprice': price,
                'mdesc': desc,
                'mphoto': photo_name
            }
            db.engine.execute(text(insertSql), insertParams)

            flash('New menu item added successfully.')
            return redirect(url_for('menus'))

        else:
            flash('Menu item already exists.')

    return render_template('new-menu-item.html',
                           auth=current_user,
                           form=form,
                           async_mode=socketio.async_mode)
def deleteMenuItem(restaurant_id, menu_id):
    session = DBSession()
    form = MenuItemForm()
    menuItemToDelete = session.query(Menu).filter_by(id=menu_id).one()
    if form.validate_on_submit():
        session.delete(menuItemToDelete)
        session.commit()
        session.close()
        flash("Menu Item Deleted Successfully!!! ")
        return redirect(url_for('viewRestaurantDetails', restaurant_id=restaurant_id))

    return render_template('menu/deletemenu.html', item = menuItemToDelete, form=form)
Exemplo n.º 6
0
def create_item(request,
                menu_id,
                library_code,
                parent=None,
                managed_libraries=[]):
    library = org_utils.get_library(library_code, managed_libraries)
    if not library:
        return HttpResponseForbidden(
            u'Вы должны быть сотрудником этой организации')

    menu = get_object_or_404(Menu, id=menu_id)

    if not parent:
        parent = menu.root_item
    else:
        parent = get_object_or_404(MenuItem, id=parent)

    if request.method == 'POST':
        item_form = MenuItemForm(request.POST, prefix='item_form')

        if item_form.is_valid():

            item = item_form.save(commit=False)
            item.parent = parent
            item.show = parent.show
            item.save()
            return redirect('participant_menu:administration:menu_detail',
                            menu_id=menu.id,
                            library_code=library_code)
    else:
        item_form = MenuItemForm(prefix="item_form")

    return render(request, 'participant_menu/administration/create_item.html',
                  {
                      'library': library,
                      'item_form': item_form,
                      'menu': menu
                  })
def createMenuItem(restaurant_id):
    session = DBSession()
    form = MenuItemForm()
    if form.validate_on_submit():
        name = form.name.data
        course = form.course.data
        category = form.category.data
        price = form.price.data
        description = form.description.data
        menuItem = models.Menu(name=name, course =course, category=category, description = description, price= price, restaurant_id=restaurant_id)
        session.add(menuItem)
        session.commit()
        session.close()
        flash("Added {} ".format(name))
        return redirect(url_for('viewRestaurantDetails', restaurant_id = restaurant_id))

    return render_template('menu/createmenu.html', form=form)
def editMenuItem(restaurant_id, menu_id):
    session = DBSession()
    form = MenuItemForm()
    restaurant = session.query(Restaurant).filter_by(id=restaurant_id).one()
    editedMenu = session.query(Menu).filter_by(id=menu_id).one()
    if form.validate_on_submit():
        if request.form['name']:
            editedMenu.name = form.name.data
        if request.form['course']:
            editedMenu.course = form.course.data
        if request.form['category']:
            editedMenu.category = form.category.data
        if request.form['price']:
            editedMenu.price = form.price.data
        if request.form['description']:
            editedMenu.description = form.description.data
        session.add(editedMenu)
        session.commit()
        session.close()
        flash("Menu Item Successfully Edited")
        return redirect(url_for('viewRestaurantDetails', restaurant_id = restaurant_id))

    return render_template('menu/editmenu.html', restaurant_id = restaurant_id, menu_id=menu_id, item=editedMenu, form=form)
Exemplo n.º 9
0
def item_edit(request, id, menu_id=None):
    menu = get_object_or_404(Menu, id=menu_id)
    item = get_object_or_404(MenuItem, id=id)
    item_titles = MenuItemTitle.objects.filter(item=item)

    item_titles_langs = {}
    for lang in settings.LANGUAGES:
        item_titles_langs[lang] = None

    for item_title in item_titles:
        item_titles_langs[item_title.lang] = item_title

    if request.method == 'POST':
        item_form = MenuItemForm(request.POST,
                                 prefix='item_form',
                                 instance=item)
        menu_item_title_forms = []
        for lang in settings.LANGUAGES:
            if lang in item_titles_langs:
                lang = lang[0]
                if lang in item_titles_langs:
                    menu_item_title_forms.append({
                        'form':
                        MenuItemTitleForm(request.POST,
                                          prefix='menu_item_title_' + lang,
                                          instance=item_titles_langs[lang]),
                        'lang':
                        lang
                    })
                else:
                    menu_item_title_forms.append({
                        'form':
                        MenuItemTitleForm(request.POST,
                                          prefix='menu_item_title_' + lang),
                        'lang':
                        lang
                    })

        valid = False
        for menu_item_title_form in menu_item_title_forms:
            valid = menu_item_title_form['form'].is_valid()
            if not valid:
                break

        if not item_form.is_valid():
            valid = False

        if valid:
            item = item_form.save()
            for menu_item_title_form in menu_item_title_forms:
                menu_item_title = menu_item_title_form['form'].save(
                    commit=False)
                menu_item_title.item = item
                menu_item_title.lang = menu_item_title_form['lang']
                menu_item_title.save()

            if not item.is_leaf_node():
                ditems = item.get_descendants()
                for ditem in ditems:
                    ditem.show = item.show
                    ditem.save()

            return redirect('menu:administration:item_list', menu_id=menu_id)

    else:
        item_form = MenuItemForm(prefix="item_form", instance=item)
        menu_item_title_forms = []
        for lang in settings.LANGUAGES:
            lang = lang[0]
            if lang in item_titles_langs:
                menu_item_title_forms.append({
                    'form':
                    MenuItemTitleForm(prefix='menu_item_title_' + lang,
                                      instance=item_titles_langs[lang]),
                    'lang':
                    lang
                })
            else:
                menu_item_title_forms.append({
                    'form':
                    MenuItemTitleForm(prefix='menu_item_title_' + lang),
                    'lang':
                    lang
                })

    return render(
        request, 'menu/administration/edit_item.html', {
            'item': item,
            'item_form': item_form,
            'menu_item_title_forms': menu_item_title_forms,
            'menu': menu
        })
Exemplo n.º 10
0
def create_item(request, menu_id, parent=None):
    menu = get_object_or_404(Menu, id=menu_id)

    if not parent:
        parent = menu.root_item
    else:
        parent = get_object_or_404(MenuItem, id=parent)

    if request.method == 'POST':
        item_form = MenuItemForm(request.POST, prefix='item_form')

        menu_item_title_forms = []
        for lang in settings.LANGUAGES:
            menu_item_title_forms.append({
                'form':
                MenuItemTitleForm(request.POST,
                                  prefix='menu_item_title_' + lang[0]),
                'lang':
                lang[0]
            })

        if item_form.is_valid():

            item = item_form.save(commit=False)
            item.parent = parent
            item.show = parent.show
            item.save()

            valid = False
            for menu_item_title_form in menu_item_title_forms:
                valid = menu_item_title_form['form'].is_valid()
                if not valid:
                    break

            if valid:
                for menu_item_title_form in menu_item_title_forms:
                    menu_item_title = menu_item_title_form['form'].save(
                        commit=False)
                    menu_item_title.lang = menu_item_title_form['lang']
                    menu_item_title.item = item
                    menu_item_title.save()
                return redirect('menu:administration:item_list',
                                menu_id=menu.id)
    else:
        item_form = MenuItemForm(prefix="item_form")
        menu_item_title_forms = []
        for lang in settings.LANGUAGES:
            menu_item_title_forms.append({
                'form':
                MenuItemTitleForm(initial={'url': u'/' + lang[0] + u'/#'},
                                  prefix='menu_item_title_' + lang[0]),
                'lang':
                lang[0]
            })

    return render(
        request, 'menu/administration/create_item.html', {
            'item_form': item_form,
            'menu_item_title_forms': menu_item_title_forms,
            'menu': menu
        })