def manage_menu(request, id): menu = get_object_or_404(Menu, id=id) get_role(request.user, menu.restaurant.id, UserRole.MANAGER) if request.POST: menu_form = MenuForm(request.POST, instance=menu) if menu_form.is_valid(): menu_form.save() else: menu_form = MenuForm(instance=menu) categories = CategoryI18n.objects.filter(category__menu=menu, language=menu.language) categories = categories.order_by("category__ordering") return render( request, "manager/manage-menu.html", { "restaurant": menu. restaurant, # needed for displaying restaurant's name in the header "form": menu_form, "menu": menu_form.instance, "categories": categories, "ordering": menu.category_ordering_str(), "add_cat_form": AddCategoryForm(), "add_translation_form": AddTranslationForm(menu), })
def delete_item(request, id): item = get_object_or_404(Item, id=id) category = item.category get_role(request.user, category.menu.restaurant.id, UserRole.MANAGER) # guard lang = category.menu.language item18n = ItemI18n.objects.filter(language=lang, item=item).first() # XXX assert item18n # XXX add a delete method to item, use here and in category delete if "_delete" in request.POST: ItemI18n.objects.filter(item=item).delete() item.delete() return redirect("manager:manage_category", category.id) return render( request, "manager/delete-item.html", { "item18n": item18n, # restaurant and cat18n are needed for the header "restaurant": category.menu.restaurant, "cat18n": CategoryI18n.get_for_language(lang, category), })
def add_translation(request, menu_id): menu = Menu.objects.get(id=menu_id) get_role(request.user, menu.restaurant.id, UserRole.MANAGER) # guard if request.POST: form = AddTranslationForm(menu, request.POST) if form.is_valid(): lang = form.cleaned_data.get("language") menu.translations.add(lang) return redirect("manager:manage_menu", menu.id)
def checkout(request, id): order = get_object_or_404(Order, id=id) if order.checkout: raise Http404("Order (%d) already checked out", order.id) restaurant_id = order.table.restaurant.id get_role(request.user, restaurant_id, UserRole.WAITER) # guard # do the checkout order.checkout = timezone.now() order.save() return redirect("waiter:for_restaurant", restaurant_id)
def manage_category(request, id): cat = get_object_or_404(Category, id=id) get_role(request.user, cat.menu.restaurant.id, UserRole.MANAGER) language = request.GET.get("l") if language: language = Language.objects.filter(abbr=language).first() if not language: language = cat.menu.language cat18n = CategoryI18n.objects.filter(category=cat, language=language).first() # XXX if there is no translation? get master language translation if cat18n is None: cat18n = CategoryI18n.objects.filter( category=cat, language=cat.menu.language).first() cat18n.translated = False items = cat.items_for_language(language) # languages (language, is_current_language, has_translation) languages = [] for lang in cat.menu.all_languages(): languages.append(( lang, lang == language, CategoryI18n.objects.filter(language=lang, category=cat).first(), )) # view change_view_form = ChangeViewForm(cat) # set next in case of renaming category request.session["next"] = request.build_absolute_uri() # XXX use a form here with the ability to change the view return render( request, "manager/manage-category.html", { "cat": cat, "restaurant": cat.menu. restaurant, # needed for displaying restaurant's name in the header "language": language, "languages": languages, "cat18n": cat18n, "items": items, "change_view_form": change_view_form, "ordering": cat.item_ordering_str(), })
def choose_table(request): data = request.GET.get("data") if not data: raise Http404("Data not specified") user_id, restaurant_id = data.split(":") restaurant = get_object_or_404(Restaurant, id=restaurant_id) get_role(request.user, restaurant.id, UserRole.WAITER) # just a guard # choose table tables = Table.objects.filter(restaurant_id=restaurant.id) return render(request, "waiter/choose-table.html", { "tables": tables, "user_id": user_id, "restaurant_id": restaurant.id, })
def rename_category(request, id): cat = get_object_or_404(Category, id=id) get_role(request.user, cat.menu.restaurant.id, UserRole.MANAGER) # guard if request.POST: lang = Language.objects.filter(abbr=request.POST["language"]).first() cat18n = CategoryI18n.objects.get_or_create(category=cat, language=lang)[0] cat18n.name = request.POST["name"] cat18n.save() redir = util.get_next_page(request) if redir: return redir return redirect("manager:manage_category", cat.id)
def manage_item(request, id): item = get_object_or_404(Item, id=id) get_role(request.user, item.category.menu.restaurant.id, UserRole.MANAGER) # guard # default language (first from GET, if not, then default menu language) # XXX factorize into helper language = request.GET.get("l") if language: language = Language.objects.filter(abbr=language).first() if not language: language = item.category.menu.language item18n = ItemI18n.get_for_language(language, item) cat18n = CategoryI18n.get_for_language(language, item.category) form = ManageItemForm(item18n) if request.method == 'POST': ManageItemForm.save_allergens(request.POST, item) form = ManageItemForm(item18n, request.POST, request.FILES) if form.is_valid(): item18n = form.save(language) form = ManageItemForm( item18n) # needed or the image won't show up in the form. XXX # languages (language, is_current_language, translation) languages = [] for lang in item.category.menu.all_languages(): tr = ItemI18n.objects.filter(language=lang, item=item).first() languages.append(( lang, lang == language, tr, )) return render( request, "manager/manage-item.html", { "restaurant": item.category.menu.restaurant, "cat18n": cat18n, "item18n": item18n, "form": form, "allergens": AllergenI18n.for_item(item, language), "languages": languages, "language": language, })
def delete_translation(request, menu_id, lang_id): menu = Menu.objects.get(id=menu_id) get_role(request.user, menu.restaurant.id, UserRole.MANAGER) # guard language = Language.objects.get(id=lang_id) if "_delete" in request.POST: categories = CategoryI18n.objects.filter(language=language, category__menu=menu) for cat in categories: ItemI18n.objects.filter(language=language, item__category=cat.category).delete() categories.delete() menu.translations.remove(language) return redirect("manager:manage_menu", menu.id) return render(request, "manager/delete-translation.html", { "language": language, })
def for_restaurant(request, id): role = get_role(request.user, id, UserRole.MANAGER) restaurant = role.restaurant roles = UserRole.objects.filter(user=request.user, role__name__icontains=UserRole.MANAGER) return render(request, "manager/index.html", { "restaurant": restaurant, "show_change_restaurant": len(roles) > 1, })
def manage_restaurant(request, id): restaurant = get_object_or_404(Restaurant, id=id) get_role(request.user, id, UserRole.MANAGER) if request.POST: restaurant_form = RestaurantForm(request.POST, instance=restaurant) address_form = AddressForm(request.POST, instance=restaurant.address) if restaurant_form.is_valid(): restaurant_form.save() if address_form.is_valid(): address_form.save() return redirect("manager:for_restaurant", restaurant.id) else: restaurant_form = RestaurantForm(instance=restaurant) address_form = AddressForm(instance=restaurant.address) return render( request, "manager/manage-restaurant.html", { "restaurant": restaurant, "restaurant_form": restaurant_form, "address_form": address_form, })
def for_restaurant(request, id): role = get_role(request.user, id, UserRole.WAITER) restaurant = role.restaurant # display orders for the last two days. # XXX bulgarian constant 2 start = timezone.now() - relativedelta(days=2) orders = Order.objects.filter(table__restaurant=restaurant, checkin__gt=start) orders = orders.order_by("-checkin") return render(request, "waiter/index.html", { "restaurant": restaurant, "orders": orders })
def delete_category(request, id): cat = get_object_or_404(Category, id=id) get_role(request.user, cat.menu.restaurant.id, UserRole.MANAGER) # guard # XXX factorise guard into an annotation (@require_role=UserRole.MANAGER) language = cat.menu.language menu = cat.menu cat18n = CategoryI18n.objects.filter(language=language, category=cat).first() if "_delete" in request.POST: # delete items for i in Item.objects.filter(category=cat).all(): ItemI18n.objects.filter(item=i).delete() i.delete() cat.delete() return redirect("manager:manage_menu", menu.id) return render(request, "manager/delete-category.html", { "restaurant": cat18n.category.menu.restaurant, "cat18n": cat18n, })
def add_category(request, id): menu = get_object_or_404(Menu, id=id) get_role(request.user, menu.restaurant.id, UserRole.MANAGER) if request.method == 'POST': # unorthodox form use XXX language = menu.language form = AddCategoryForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] view = form.cleaned_data["view"] cat = Category() cat.menu = menu cat.view = view cat.save() cat18n = CategoryI18n() cat18n.category = cat cat18n.language = language cat18n.name = name cat18n.save() return redirect("manager:manage_menu", id)
def add_item(request, id): cat = get_object_or_404(Category, id=id) get_role(request.user, cat.menu.restaurant.id, UserRole.MANAGER) # guard language = cat.menu.language form = AddItemForm(cat.menu) if request.method == "POST": form = AddItemForm(cat.menu, request.POST, request.FILES) if form.is_valid(): item18n = form.save(cat, language) AddItemForm.save_allergens(request.POST, item18n.item) return redirect("manager:manage_item", item18n.item.id) return render( request, "manager/add-item.html", { "form": form, "language": language, "currency": cat.menu.currency, "restaurant": cat.menu.restaurant, "cat18n": CategoryI18n.get_for_language(language, cat), "allergens": AllergenI18n.for_item(None, language), })