def create_category(request, venue_id, category_id):
    def general_render(form):
        values = {
            'form': form,
            'venue_id': venue_id
        }
        values.update(csrf(request))
        return render(request, 'web/menu/category_form.html', values)

    try:
        venue = Venue.objects.get(id=venue_id)
        category = Category.objects.get(id=category_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()

        if request.method == 'GET':
            return general_render(CategoryForm(initial={
                'parent_category_id': category.id
            }))
        elif request.method == 'POST':
            form = CategoryForm(request.POST, request.FILES)
            if form.is_valid():
                save_category(form, venue)
                return redirect('/web/%s/menu/%s/category' % (venue_id, category_id))
            else:
                return general_render(form)
    except Category.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()
def create_group_modifier_item(request, venue_id):
    def general_render(form):
        values = {
            'form': form
        }
        values.update(csrf(request))
        return render(request, 'web/menu/modifier_form.html', values)

    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()

        if request.method == 'GET':
            return general_render(GroupModifierItemForm(initial={
                'group_modifier_id': request.GET.get('group_modifier_id')
            }))
        elif request.method == 'POST':
            form = GroupModifierItemForm(request.POST, request.FILES)
            if form.is_valid():
                save_group_modifier_item(form, venue)
                return redirect('/web/%s/menu/modifiers' % venue.id)
            else:
                return general_render(form)
    except Company.DoesNotExist:
        return HttpResponseBadRequest()
def select_products_for_group_modifier(request, venue_id, modifier_id):
    def general_render():
        values = {
            'products': products,
            'modifier': modifier
        }
        values.update(csrf(request))
        return render(request, 'web/menu/select_products.html', values)

    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()
        modifier = GroupModifier.objects.get(id=modifier_id)
        bindings = GroupModifierBinding.objects.filter(modifier=modifier)
        binding_products = [binding.product for binding in bindings]
        if request.method == 'GET':
            products = venue.get_products_from_menu(venue_product=False)
            for product in products:
                product.has_modifier = product in binding_products
            return general_render()
        elif request.method == 'POST':
            for product in venue.get_products_from_menu(venue_product=False):
                confirmed = bool(request.POST.get(str(product.id)))
                if product in binding_products:
                    if not confirmed:
                        bindings[binding_products.index(product)].delete()
                else:
                    if confirmed:
                        GroupModifierBinding(product=product, modifier=modifier).save()
            return redirect('/web/%s/menu/modifiers' % venue.id)
    except Company.DoesNotExist:
        return HttpResponseBadRequest()
def change_single_modifier(request, venue_id, modifier_id):
    def general_render(form):
        values = {
            'form': form
        }
        values.update(csrf(request))
        return render(request, 'web/menu/modifier_form.html', values)

    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()

        modifier = SingleModifier.objects.get(id=modifier_id)

        if request.method == 'GET':
            form = SingleModifierForm(initial={
                'single_modifier_id': modifier.id,
                'name': modifier.name,
                'min_price': modifier.min_price
            })
            form.hide_min_price()
            return general_render(form)
        elif request.method == 'POST':
            form = SingleModifierForm(request.POST, request.FILES)
            form.hide_min_price()
            if form.is_valid():
                save_single_modifier(form, venue)
                return redirect('/web/%s/menu/modifiers' % venue.id)
            else:
                return general_render(form)
    except Category.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()
def menu(request, venue_id):
    company = Company.get_by_username(request.user.username)
    if not company:
        return HttpResponseForbidden()
    try:
        venue = Venue.objects.get(id=venue_id)
        if venue.company != company:
            return HttpResponseForbidden()
    except Venue.DoesNotExist:
        return HttpResponseBadRequest()
    return redirect('/web/%s/menu/%s/category' % (venue_id, venue.first_category.id))
Example #6
0
def main_page(request):
    user = request.user
    company = Company.get_by_username(user.username)
    venues = Venue.objects.filter(company=company)
    for venue in venues:
        venue.address_name = venue.address.to_str()
        venue.status = u'Открыто'
    return render(request, 'web/company/main.html', {
        'user': user,
        'venues': venues
    })
def category(request, venue_id, category_id):
    try:
        venue = Venue.objects.get(id=venue_id)
        category = Category.objects.get(id=category_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()
        request_category = category
        while category.parent:
            category = category.parent
        if venue.first_category != category:
            return HttpResponseBadRequest()
    except Category.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()
def delete_category(request, venue_id):
    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()
        if request.method == 'POST':
            category_id = request.POST.get('category_id')
            category = Category.objects.get(id=category_id)
            category.get_first_category().save()
            category.delete_category()
            return HttpResponse(json.dumps({
                'category_id': category_id
            }))
    except Category.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()
def modifiers(request, venue_id):
    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()
        group_modifiers = venue.group_modifiers.all()
        for modifier in group_modifiers:
            modifier.choices = modifier.group_modifier_item.all()
        return render(request, 'web/menu/modifiers.html', {
            'single_modifiers': venue.single_modifiers.all(),
            'group_modifiers': group_modifiers,
            'venue_id': venue.id,
            'create': request.user.has_perm('pizza_delivery_app.update_single_modifiers'),
            'update': request.user.has_perm('pizza_delivery_app.update_single_modifiers'),
            'delete': request.user.has_perm('pizza_delivery_app.update_single_modifiers')
        })
    except Company.DoesNotExist:
        return HttpResponseBadRequest()
Example #10
0
def change_price(request, venue_id):
    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()
        if request.method == 'POST':
            venue_product_id = request.POST.get('venue_product_id')
            product = VenueProduct.objects.get(id=venue_product_id)
            change = int(request.POST.get('change_in_price'))
            product.change_price(change)

            return HttpResponse(json.dumps({
                'price': product.price,
                'product_id': venue_product_id
            }))
        else:
            HttpResponseNotAllowed(permitted_methods='POST')
    except VenueProduct.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()
Example #11
0
def change_status(request, venue_id):
    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()

        if request.method == 'POST':
            venue_product_id = request.POST.get('venue_product_id')
            product = VenueProduct.objects.get(id=venue_product_id)
            product.change_status()

            return HttpResponse(json.dumps({
                'status': dict(VenueProduct.STATUS_CHOICES)[product.status],
                'product_id': venue_product_id
            }))
        else:
            HttpResponseNotAllowed(permitted_methods='POST')
    except VenueProduct.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()
Example #12
0
def change_product(request, venue_id, product_id):
    def general_render(form):
        values = {
            'form': form,
            'venue_id': venue_id
        }
        values.update(csrf(request))
        return render(request, 'web/menu/product_form.html', values)

    try:
        venue = Venue.objects.get(id=venue_id)
        user_company = Company.get_by_username(request.user.username)
        if venue.company != user_company:
            return HttpResponseForbidden()

        product = Product.objects.get(id=product_id)

        if request.method == 'GET':
            form = ProductForm(initial={
                'category_id': product.category.id,
                'product_id': product.id,
                'name': product.name,
                'description': product.description
            })
            form.hide_min_price()
            return general_render(form)
        elif request.method == 'POST':
            form = ProductForm(request.POST, request.FILES)
            form.hide_min_price()
            if form.is_valid():
                save_product(form, venue)
                return redirect('/web/%s/menu/%s/category' % (venue_id, product.category.id))
            else:
                return general_render(form)
    except Category.DoesNotExist, Company.DoesNotExist:
        return HttpResponseBadRequest()