Пример #1
0
def add_menu(request):
    if request.method == 'POST':
        validate_menu = MenuForm(request.POST)
        if validate_menu.is_valid():
            new_menu = validate_menu.save(commit=False)
            if request.POST.get('status', ''):
                new_menu.status = 'on'

            menu_count = Menu.objects.filter(parent=new_menu.parent).count()
            new_menu.lvl = menu_count + 1
            if new_menu.url[-1] != '/':
                new_menu.url = new_menu.url + '/'

            new_menu.save()
            data = {"error": False, 'response': 'Menu created successfully'}
        else:
            data = {"error": True, 'response': validate_menu.errors}
        return HttpResponse(json.dumps(data),
                            content_type='application/json; charset=utf-8')

    if request.user.is_superuser:
        c = {}
        c.update(csrf(request))
        parent = Menu.objects.filter(parent=None).order_by('lvl')
        return render(request, 'admin/content/menu/new-menu-item.html', {
            'parent': parent,
            'csrf_token': c['csrf_token']
        })
    else:
        return render_to_response('admin/accessdenied.html')
Пример #2
0
def add_menu(request):
    if request.method == 'POST':
        validate_menu = MenuForm(request.POST)
        if validate_menu.is_valid():
            new_menu = validate_menu.save(commit=False)
            if request.POST.get('status', ''):
                new_menu.status = 'on'

            menu_count = Menu.objects.filter(parent=new_menu.parent).count()
            new_menu.lvl = menu_count + 1
            if new_menu.url[-1] != '/':
                new_menu.url = new_menu.url+'/'

            new_menu.save()
            data = {"error": False, 'response': 'Menu created successfully'}
        else:
            data = {"error": True, 'response': validate_menu.errors}
        return HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8')

    if request.user.is_superuser:
        c = {}
        c.update(csrf(request))
        parent = Menu.objects.filter(parent=None).order_by('lvl')
        return render(request, 'admin/content/menu/new-menu-item.html',
                      {'parent': parent, 'csrf_token': c['csrf_token']})
    else:
        return render_to_response('admin/accessdenied.html')
Пример #3
0
def add_menu(request):
    if request.method == "POST":
        validate_menu = MenuForm(request.POST)
        if validate_menu.is_valid():
            new_menu = validate_menu.save(commit=False)
            if request.POST.get("status", ""):
                new_menu.status = "on"

            menu_count = Menu.objects.filter(parent=new_menu.parent).count()
            new_menu.lvl = menu_count + 1
            if new_menu.url[-1] != "/":
                new_menu.url = new_menu.url + "/"

            new_menu.save()
            data = {"error": False, "response": "Menu created successfully"}
        else:
            data = {"error": True, "response": validate_menu.errors}
        return HttpResponse(json.dumps(data), content_type="application/json; charset=utf-8")

    if request.user.is_superuser:
        c = {}
        c.update(csrf(request))
        parent = Menu.objects.filter(parent=None).order_by("lvl")
        return render(
            request, "admin/content/menu/new-menu-item.html", {"parent": parent, "csrf_token": c["csrf_token"]}
        )
    else:
        return render_to_response("admin/accessdenied.html")
Пример #4
0
 def test_Menuform(self):
     self.client = Client()
     form = MenuForm(data={
         'title': 'main',
         'url': 'micro.in',
         'status': 'on'
     })
     self.assertTrue(form.is_valid())
Пример #5
0
def edit_menu(request, pk):
    if request.method == 'POST':
        menu_instance = get_object_or_404(Menu, pk=pk)
        current_parent = menu_instance.parent
        current_lvl = menu_instance.lvl
        validate_menu = MenuForm(request.POST, instance=menu_instance)

        if validate_menu.is_valid():
            updated_menu = validate_menu.save(commit=False)
            if updated_menu.parent != current_parent:
                try:
                    if updated_menu.parent.id == updated_menu.id:
                        data = {'error': True, 'response': {
                            'parent': 'you can not choose the same as parent'}}
                        return HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8')
                except Exception:
                    pass

                lnk_count = Menu.objects.filter(
                    parent=updated_menu.parent).count()
                updated_menu.lvl = lnk_count + 1
                lvlmax = Menu.objects.filter(
                    parent=current_parent).aggregate(Max('lvl'))['lvl__max']
                if lvlmax != 1:
                    for i in Menu.objects.filter(parent=current_parent, lvl__gt=current_lvl, lvl__lte=lvlmax):
                        i.lvl = i.lvl-1
                        i.save()
            if request.POST.get('url'):
                updated_menu.url = request.POST.get('url').rstrip('/')
            else:
                updated_menu.url = 'none'

            if request.POST.get('status', ''):
                updated_menu.status = 'on'

            updated_menu.save()

            data = {'error': False, 'response': 'updated successfully'}
        else:
            data = {'error': True, 'response': validate_menu.errors}
        return HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8')

    if request.user.is_superuser:
        parent = Menu.objects.filter(parent=None).order_by('lvl')
        current_menu = get_object_or_404(Menu, pk=pk)
        c = {}
        c.update(csrf(request))
        return render(request, 'admin/content/menu/edit-menu-item.html',
                      {'csrf_token': c['csrf_token'], 'current_menu': current_menu, 'parent': parent})
    else:
        return render_to_response('admin/accessdenied.html')
Пример #6
0
def edit_menu(request, pk):
    if request.method == "POST":
        menu_instance = get_object_or_404(Menu, pk=pk)
        current_parent = menu_instance.parent
        current_lvl = menu_instance.lvl
        validate_menu = MenuForm(request.POST, instance=menu_instance)

        if validate_menu.is_valid():
            updated_menu = validate_menu.save(commit=False)
            if updated_menu.parent != current_parent:
                try:
                    if updated_menu.parent.id == updated_menu.id:
                        data = {"error": True, "response": {"parent": "you can not choose the same as parent"}}
                        return HttpResponse(json.dumps(data), content_type="application/json; charset=utf-8")
                except Exception:
                    pass

                lnk_count = Menu.objects.filter(parent=updated_menu.parent).count()
                updated_menu.lvl = lnk_count + 1
                lvlmax = Menu.objects.filter(parent=current_parent).aggregate(Max("lvl"))["lvl__max"]
                if lvlmax != 1:
                    for i in Menu.objects.filter(parent=current_parent, lvl__gt=current_lvl, lvl__lte=lvlmax):
                        i.lvl = i.lvl - 1
                        i.save()
            if updated_menu.url[-1] != "/":
                updated_menu.url = updated_menu.url + "/"

            if request.POST.get("status", ""):
                updated_menu.status = "on"

            updated_menu.save()

            data = {"error": False, "response": "updated successfully"}
        else:
            data = {"error": True, "response": validate_menu.errors}
        return HttpResponse(json.dumps(data), content_type="application/json; charset=utf-8")

    if request.user.is_superuser:
        parent = Menu.objects.filter(parent=None).order_by("lvl")
        current_menu = get_object_or_404(Menu, pk=pk)
        c = {}
        c.update(csrf(request))
        return render(
            request,
            "admin/content/menu/edit-menu-item.html",
            {"csrf_token": c["csrf_token"], "current_menu": current_menu, "parent": parent},
        )
    else:
        return render_to_response("admin/accessdenied.html")
Пример #7
0
 def test_Menuform(self):
     self.client = Client()
     form = MenuForm(
         data={'title': 'main', 'url': 'micro.in', 'status': 'on'})
     self.assertTrue(form.is_valid())
Пример #8
0
def edit_menu(request, pk):
    if request.method == 'POST':
        menu_instance = get_object_or_404(Menu, pk=pk)
        current_parent = menu_instance.parent
        current_lvl = menu_instance.lvl
        validate_menu = MenuForm(request.POST, instance=menu_instance)

        if validate_menu.is_valid():
            updated_menu = validate_menu.save(commit=False)
            if updated_menu.parent != current_parent:
                try:
                    if updated_menu.parent.id == updated_menu.id:
                        data = {
                            'error': True,
                            'response': {
                                'parent':
                                'you can not choose the same as parent'
                            }
                        }
                        return HttpResponse(
                            json.dumps(data),
                            content_type='application/json; charset=utf-8')
                except Exception:
                    pass

                lnk_count = Menu.objects.filter(
                    parent=updated_menu.parent).count()
                updated_menu.lvl = lnk_count + 1
                lvlmax = Menu.objects.filter(parent=current_parent).aggregate(
                    Max('lvl'))['lvl__max']
                if lvlmax != 1:
                    for i in Menu.objects.filter(parent=current_parent,
                                                 lvl__gt=current_lvl,
                                                 lvl__lte=lvlmax):
                        i.lvl = i.lvl - 1
                        i.save()
            if updated_menu.url[-1] != '/':
                updated_menu.url = updated_menu.url + '/'

            if request.POST.get('status', ''):
                updated_menu.status = 'on'

            updated_menu.save()

            data = {'error': False, 'response': 'updated successfully'}
        else:
            data = {'error': True, 'response': validate_menu.errors}
        return HttpResponse(json.dumps(data),
                            content_type='application/json; charset=utf-8')

    if request.user.is_superuser:
        parent = Menu.objects.filter(parent=None).order_by('lvl')
        current_menu = get_object_or_404(Menu, pk=pk)
        c = {}
        c.update(csrf(request))
        return render(
            request, 'admin/content/menu/edit-menu-item.html', {
                'csrf_token': c['csrf_token'],
                'current_menu': current_menu,
                'parent': parent
            })
    else:
        return render_to_response('admin/accessdenied.html')
Пример #9
0
def edit_menu(request, pk):
    country = request.GET.get('country') if request.GET.get(
        'country') else request.POST.get('country_id')
    menu_instance = Menu.objects.filter(pk=pk, country__slug=country).first()
    if not menu_instance:
        each = Menu.objects.filter(pk=pk).first()
        if each:
            menu_instance = Menu.objects.filter(title=each.title,
                                                country__slug=country).first()
    if menu_instance:
        if request.method == 'POST':
            current_parent = menu_instance.parent
            current_lvl = menu_instance.lvl
            if menu_instance:
                validate_menu = MenuForm(request.POST, instance=menu_instance)
            else:
                validate_menu = MenuForm(request.POST)
            if validate_menu.is_valid():
                updated_menu = validate_menu.save(commit=False)
                country = Country.objects.get(slug=country)
                if country:
                    updated_menu.country = country
                if updated_menu.parent != current_parent:
                    try:
                        if updated_menu.parent.id == updated_menu.id:
                            data = {
                                'error': True,
                                'response': {
                                    'parent':
                                    'you can not choose the same as parent'
                                }
                            }
                            return HttpResponse(
                                json.dumps(data),
                                content_type='application/json; charset=utf-8')
                    except Exception:
                        pass

                    lnk_count = Menu.objects.filter(
                        parent=updated_menu.parent).count()
                    updated_menu.lvl = lnk_count + 1
                    lvlmax = Menu.objects.filter(
                        parent=current_parent).aggregate(
                            Max('lvl'))['lvl__max']
                    if lvlmax != 1:
                        for i in Menu.objects.filter(parent=current_parent,
                                                     lvl__gt=current_lvl,
                                                     lvl__lte=lvlmax):
                            i.lvl = i.lvl - 1
                            i.save()
                if request.POST.get('url'):
                    updated_menu.url = request.POST.get('url').rstrip('/')
                else:
                    updated_menu.url = 'none'

                if request.POST.get('status', ''):
                    updated_menu.status = 'on'

                updated_menu.save()

                data = {'error': False, 'response': 'updated successfully'}
            else:
                data = {'error': True, 'response': validate_menu.errors}
            return HttpResponse(json.dumps(data),
                                content_type='application/json; charset=utf-8')

        if request.user.is_superuser:
            parent = Menu.objects.filter(parent=None,
                                         country__slug=country).order_by('lvl')
            countries = Country.objects.all()
            c = {}
            c.update(csrf(request))
            return render(
                request, 'admin/content/menu/edit-menu-item.html', {
                    'csrf_token': c['csrf_token'],
                    'current_menu': menu_instance,
                    'parent': parent,
                    'countries': countries
                })
    else:
        return render_to_response('admin/accessdenied.html')