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 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 })
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 })
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))
def add_item(request, menu_pk): menu = get_object_or_404(Menu, pk=menu_pk) extension_form = None if request.method == 'POST': form = MenuItemForm(request.POST) if hasattr(settings, 'TREE_MENU_ITEM_EXTENSION_MODEL'): extension_form = get_extension_form_class()(request.POST) if form.is_valid() and (not hasattr(settings, 'TREE_MENU_ITEM_EXTENSION_MODEL') or extension_form.is_valid()): menu_item = form.save(commit=False) clean_ranks(menu_item.siblings()) # Clean ranks for new siblings menu_item.rank = menu_item.siblings().count() menu_item.menu = menu menu_item.save() if extension_form: extension = extension_form.save(commit=False) extension.menu_item = menu_item extension.save() msg = _('The menu item "%s" was added successfully.') % force_unicode(menu_item) if "_continue" in request.POST: request.user.message_set.create(message=msg + ' ' + _("You may edit it again below.")) return HttpResponseRedirect("../items/%s/" % menu_item.pk) elif "_addanother" in request.POST: request.user.message_set.create(message=msg + ' ' + _("You may add another menu item below.")) return HttpResponseRedirect(request.path) return HttpResponseRedirect("../../add_item/") else: request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse('edit_menu', kwargs={ 'menu_pk': menu.pk })) else: form_class = MenuItemForm choices = get_parent_choices(menu) form_class.base_fields['parent'].choices = choices form = form_class() if hasattr(settings, 'TREE_MENU_ITEM_EXTENSION_MODEL'): extension_form = get_extension_form_class()() return render_to_response('admin/%s/add_edit_item.html' % APP_LABEL, { 'form': form, 'extension_form': extension_form, 'menu': menu, 'title': _('Add menu item'), 'add_or_edit': 'add' }, context_instance=RequestContext(request))
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 })
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 restaurant(request, slug): restaurant = get_object_or_404(Restaurant, slug=slug) mform = MenuItemForm(prefix="menu") cform = comments.get_form()(restaurant) ajax = process_ajax(request, restaurant) if ajax: return ajax if request.method == "POST": if request.GET["type"] == "menu": mform = MenuItemForm(request.POST, prefix="menu") if mform.is_valid(): model = mform.save(commit=False) model.user = request.user model.restaurant = restaurant model.is_available = True model.save() mform = MenuItemForm(prefix="menu") return HttpResponseRedirect("./") else : cform = comments.get_form()(restaurant, data=request.POST.copy()) if cform.is_valid(): comment = cform.get_comment_object() comment.ip_address = request.META.get("REMOTE_ADDR", None) if request.user.is_authenticated(): comment.user = request.user comment.save() cform = comments.get_form()(restaurant) return HttpResponseRedirect("./") del cform.fields['honeypot'] del cform.fields['url'] referer = request.environ.get("HTTP_REFERER") if referer != None: if referer.startswith("http://stumptownvegans.com/") or referer.startswith("http://localhost:8000/") and not ReferingSite.objects.all().filter(url=referer) and not ReferingSite.objects.all().filter(restaurant=restaurant): try: ReferingSite(restaurant=restaurant, url=referer).save() except: logging.critical(traceback.format_exc()) return render_to_response("restaurants/restaurant_detail.html", {'menu_form': mform, 'comment_form': cform, 'object': restaurant, 'menu':restaurant.menuitem_set.all().order_by("category") ,'tags':getTags(restaurant)}, context_instance=RequestContext(request))
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 })
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 })
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 })