Exemplo n.º 1
0
def get_menu_by_position(position):
    """
    Return all menu items based on position ID.
    """
    menu_title = ''
    all_items = []
    #get all menu items

    def get_menu_parent(obj):
        if obj.parent:
            return obj.parent.id
        return 0

    try:
        menu = Menuz.objects.get(position__exact=position)
        menu_title = menu.title
        menu_items = menu.menuzitem_set.all()
        #get all menu data, and return title and links only

        for obj in menu_items:
            if obj.content_type == 'custom' or obj.content_type == 'innerlink':
                all_items.append({
                    'id': obj.id,
                    'url': obj.url,
                    'title': obj.title,
                    'parent_id': get_menu_parent(obj)})
            else:
                menu_model = get_menuz_object_model(obj.content_type)
                if menu_model:
                    try:
                        item = menu_model.objects.get(pk=obj.content_id)
                        all_items.append({'id': obj.id,
                                         'obj_id': item.pk,
                                         'url': item.get_absolute_url(),
                                         'title': obj.title,
                                         'parent_id': get_menu_parent(obj)})
                    except Exception as err:
                        pass
    except Exception as err:
        pass
    #return a tuple of title and its items
    return (menu_title, all_items)
Exemplo n.º 2
0
def get_menu_by_position(position):
    """
    Return all menu items based on position ID.
    """
    menu_title = ''
    all_items = []

    #get all menu items

    def has_parent(obj):
        if obj.parent:
            return obj.parent.id
        return 0

    try:
        menu = Menuz.objects.get(position__exact=position)
        menu_title = menu.title
        menu_contents = menu.menuzitem_set.all()
        #get all menu data, and return title and links only

        for obj in menu_contents:
            if obj.content_type == 'custom' or obj.content_type == 'innerlink':
                all_items.append({
                    'id': obj.id,
                    'url': obj.url,
                    'title': obj.title,
                    'parent_id': has_parent(obj)
                })
            else:
                if get_menuz_object_model(obj.content_type):
                    model = get_menuz_object_model(obj.content_type)
                    try:
                        item = model.objects.get(pk=obj.content_id)

                        # works with egocms
                        # this will check if current page setted as homepage_id
                        # if yes, then display site base url
                        if egosetting_available:
                            homepage_id = get_setting('site_homepage')
                            if homepage_id == item.id:
                                all_items.append({
                                    'id': obj.id,
                                    'url': reverse('index'),
                                    'title': obj.title,
                                    'parent_id': has_parent(obj)
                                })
                            else:
                                all_items.append({
                                    'id': obj.id,
                                    'url': item.get_absolute_url(),
                                    'title': obj.title,
                                    'parent_id': has_parent(obj)
                                })
                        else:
                            all_items.append({
                                'id': obj.id,
                                'url': item.get_absolute_url(),
                                'title': obj.title,
                                'parent_id': has_parent(obj)
                            })

                    except Exception, err:
                        print err
    except Exception, err:
        print err
Exemplo n.º 3
0
def add_menuz(request):
    """
    Ajax add menu.
    """
    if request.is_ajax() and request.method == 'POST':
        mtype = request.POST.get('mtype', '')
        menu_id = request.POST.get('menu_id', 0)
        menu = Menuz.objects.get(pk=menu_id)

        #if menu type custom
        if mtype == 'custom':
            customform = CustomMenuForm(request.POST)
            if customform.is_valid():

                menuitem = MenuzItem()
                menuitem.menu = menu
                menuitem.content_type = mtype
                menuitem.title = strip_tags(customform.cleaned_data['custom_title'])
                menuitem.url = customform.cleaned_data['custom_url']
                menuitem.save()

                data = {
                    'id': menuitem.id,
                    'title': menuitem.title,
                    'url': menuitem.url,
                    'content_type': menuitem.content_type,
                    'content_id': menuitem.content_id
                }
                menu_data = [data]
                return HttpResponse(json.dumps({'status': 'success', 'menu_data': menu_data}),
                                    content_type='application/javascript; charset=utf-8;')
            else:
                fields = [field for field in customform.errors]
                return HttpResponse(json.dumps({'status': 'failed', 'fields': fields}),
                                    content_type='application/javascript; charset=utf-8;')

        #if menu type Innerlink, similar to custom but different treatment
        elif mtype == 'innerlink':

            links = request.POST.getlist(mtype)

            if links and menu:
                menu_data = []
                for link in links:
                    link_split = link.split('$')

                    menuitem = MenuzItem()
                    menuitem.menu = menu
                    menuitem.content_type = mtype
                    menuitem.title = strip_tags(link_split[1])
                    menuitem.url = strip_tags(link_split[0])
                    menuitem.save()

                    data = {
                        'id': menuitem.id,
                        'title': menuitem.title,
                        'url': menuitem.url,
                        'content_type': menuitem.content_type,
                        'content_id': menuitem.content_id
                    }
                    menu_data.append(data)
                return HttpResponse(json.dumps({'status': 'success', 'menu_data': menu_data}),
                                    content_type='application/javascript; charset=utf-8;')

        #if menu type Model Menu
        else:
            obj_ids = request.POST.getlist(mtype)
            model = get_menuz_object_model(mtype)

            if obj_ids and menu and model:
                queryset = model.objects.filter(pk__in=obj_ids)
                menu_data = []
                for obj in queryset:

                    menuitem = MenuzItem()
                    menuitem.menu = menu
                    menuitem.content_type = mtype
                    menuitem.content_id = obj.pk
                    menuitem.title = obj.__unicode__()
                    menuitem.save()

                    data = {
                        'id': menuitem.id,
                        'title': menuitem.title,
                        'url': menuitem.url,
                        'content_type': menuitem.content_type,
                        'content_id': menuitem.content_id
                    }
                    menu_data.append(data)
                return HttpResponse(json.dumps({'status': 'success', 'menu_data': menu_data}),
                                    content_type='application/javascript; charset=utf-8;')

        return HttpResponse(json.dumps({'status': 'failed'}),
                                content_type='application/javascript; charset=utf-8;')
    raise Http404
Exemplo n.º 4
0
def add_menuz(request):
    if request.is_ajax():
        if request.method == 'POST':
            mtype = request.POST.get('mtype','')
            menu_id = request.POST.get('menu_id',0)
            menu = Menuz.objects.get(pk=menu_id)

            #if menu type custom
            if mtype == 'custom':
                customform = CustomMenuForm(request.POST)
                if customform.is_valid():

                    menuitem = MenuzItem()
                    menuitem.menu = menu
                    menuitem.content_type = mtype
                    menuitem.title = strip_tags(customform.cleaned_data['custom_title'])
                    menuitem.url = customform.cleaned_data['custom_url']
                    menuitem.save()
                    data = {
                        'id': menuitem.id,
                        'title': menuitem.title,
                        'url': menuitem.url,
                        'content_type': menuitem.content_type,
                        'content_id': menuitem.content_id
                    }
                    menu_data = [data]
                    return HttpResponse(simplejson.dumps({'status':'success', 'menu_data': menu_data}),
                                        content_type='application/javascript; charset=utf-8;')
                else:
                    fields = [field for field in customform.errors]
                    return HttpResponse(simplejson.dumps({'status':'failed', 'fields':fields}),
                                        content_type='application/javascript; charset=utf-8;')

            #if menu type Innerlink, similar to custom but different treatment
            elif mtype == 'innerlink':

                links = request.POST.getlist(mtype)

                if links and menu:
                    menu_data = []
                    for link in links:
                        link_split = link.split('$')

                        menuitem = MenuzItem()
                        menuitem.menu = menu
                        menuitem.content_type = mtype
                        menuitem.title = strip_tags(link_split[1])
                        menuitem.url = strip_tags(link_split[0])
                        menuitem.save()

                        data = {
                            'id': menuitem.id,
                            'title': menuitem.title,
                            'url': menuitem.url,
                            'content_type': menuitem.content_type,
                            'content_id': menuitem.content_id
                        }
                        menu_data.append(data)
                    return HttpResponse(simplejson.dumps({'status':'success', 'menu_data': menu_data}),
                                        content_type='application/javascript; charset=utf-8;')

            #if menu type Model Menu
            else:
                obj_ids = request.POST.getlist(mtype)
                model = get_menuz_object_model(mtype)

                if obj_ids and menu and model:
                    queryset = model.objects.filter(pk__in=obj_ids)
                    menu_data = []
                    for obj in queryset:

                        menuitem = MenuzItem()
                        menuitem.menu = menu
                        menuitem.content_type = mtype
                        menuitem.content_id = obj.pk
                        menuitem.title = obj.__unicode__()
                        menuitem.save()

                        data = {
                            'id': menuitem.id,
                            'title': menuitem.title,
                            'url': menuitem.url,
                            'content_type': menuitem.content_type,
                            'content_id': menuitem.content_id
                        }
                        menu_data.append(data)
                    return HttpResponse(simplejson.dumps({'status':'success', 'menu_data': menu_data}),
                                        content_type='application/javascript; charset=utf-8;')

            return HttpResponse(simplejson.dumps({'status':'failed'}),
                                    content_type='application/javascript; charset=utf-8;')