Example #1
0
def edit(request, id, form_class=NavForm, template_name="navs/edit.html"):
    nav = get_object_or_404(Nav, id=id)
    if not has_perm(request.user, 'navs.change_nav', nav):
        raise Http403

    if request.method == "POST":
        form = form_class(request.POST, instance=nav, user=request.user)
        if form.is_valid():
            nav = form.save(commit=False)
            nav = update_perms_and_save(request, form, nav)
            cache_nav(nav)

            messages.add_message(request, messages.SUCCESS,
                                 'Successfully updated %s' % nav)
            return redirect('navs.edit_items', id=nav.id)
    else:
        form = form_class(user=request.user, instance=nav)

    return render_to_response(
        template_name,
        {
            'form': form,
            'current_nav': nav
        },
        context_instance=RequestContext(request),
    )
Example #2
0
def nav(context, nav_id):
    """
    Renders the nav from cache
    if not will use the navigation tag for rendering the nav
    """
    user = AnonymousUser()

    if 'user' in context:
        if isinstance(context['user'], User):
            user = context['user']
    try:
        filters = get_query_filters(user, 'navs.view_nav')
        navs = Nav.objects.filter(filters).filter(id=nav_id)
        if user.is_authenticated():
            if not user.profile.is_superuser:
                navs = navs.distinct()

        nav_object = navs[0]
        nav = get_nav(nav_object.pk)
        if not nav:
            cache_nav(nav_object)
    except:
        return None

    context.update({
        "cached": nav,
        "nav_id": nav_id,
    })
    return context
Example #3
0
def edit_items(request, id, template_name="navs/nav_items.html"):
    nav = get_object_or_404(Nav, id=id)
    if not has_perm(request.user, 'navs.change_nav', nav):
        raise Http403

    ItemFormSet = modelformset_factory(NavItem,
                                       form=ItemForm,
                                       extra=0,
                                       can_delete=True)
    page_select = PageSelectForm()

    if request.method == "POST":
        formset = ItemFormSet(request.POST)
        if formset.is_valid():
            #delete old nav items
            nav.navitem_set.all().delete()
            items = formset.save(commit=False)
            # update or create nav items
            for item in items:
                item.nav = nav
                item.save()
            cache_nav(nav)
            messages.add_message(request, messages.SUCCESS,
                                 'Successfully updated %s' % nav)

            EventLog.objects.log(instance=nav)

            redirect_to = request.REQUEST.get('next', '')
            if redirect_to:
                return HttpResponseRedirect(redirect_to)
            else:
                return redirect('navs.detail', id=nav.id)
    else:
        formset = ItemFormSet(
            queryset=nav.navitem_set.all().order_by('ordering'))

    return render_to_response(
        template_name,
        {
            'page_select': page_select,
            'formset': formset,
            'current_nav': nav
        },
        context_instance=RequestContext(request),
    )
Example #4
0
def edit_items(request, id, template_name="navs/nav_items.html"):
    nav = get_object_or_404(Nav, id=id)
    if not has_perm(request.user, 'navs.change_nav', nav):
        raise Http403

    ItemFormSet = modelformset_factory(NavItem,
                        form=ItemForm,
                        extra=0,
                        can_delete=True)
    page_select = PageSelectForm()

    if request.method == "POST":
        formset = ItemFormSet(request.POST)
        if formset.is_valid():
            #delete old nav items
            nav.navitem_set.all().delete()
            items = formset.save(commit=False)
            # update or create nav items
            for item in items:
                item.nav = nav
                item.save()
            cache_nav(nav)
            messages.add_message(request, messages.SUCCESS, 'Successfully updated %s' % nav)
            if nav.pk == 1:  # the main nav has id 1 in the npo defaults fixture
                checklist_update('update-nav')

            EventLog.objects.log(instance=nav)

            redirect_to = request.REQUEST.get('next', '')
            if redirect_to:
                return HttpResponseRedirect(redirect_to)
            else:
                return redirect('navs.detail', id=nav.id)
    else:
        formset = ItemFormSet(queryset=nav.navitem_set.all().order_by('position'))

    return render_to_response(
        template_name,
        {'page_select':page_select, 'formset':formset, 'current_nav':nav},
        context_instance=RequestContext(request),
    )
Example #5
0
def edit(request, id, form_class=NavForm, template_name="navs/edit.html"):
    nav = get_object_or_404(Nav, id=id)
    if not has_perm(request.user, 'navs.change_nav', nav):
        raise Http403
    
    if request.method == "POST":
        form = form_class(request.POST, instance=nav, user=request.user)
        if form.is_valid():
            nav = form.save(commit=False)
            nav = update_perms_and_save(request, form, nav)
            cache_nav(nav)

            messages.add_message(request, messages.SUCCESS, 'Successfully updated %s' % nav)
            return redirect('navs.edit_items', id=nav.id)
    else:
        form = form_class(user=request.user, instance=nav)

    return render_to_response(
        template_name,
        {'form':form, 'current_nav':nav},
        context_instance=RequestContext(request),
    )
Example #6
0
def nav(context, nav_id, show_title=False, is_site_map=False):

    """
    Renders the nav from cache
    if not will use the navigation tag for rendering the nav
    """
    user = AnonymousUser()
    association_id = get_association_id(context)
    if is_site_map == 'False':
        is_site_map = False

    if 'user' in context:
        if isinstance(context['user'], User):
            user = context['user']
            if hasattr(user, 'profile'):
                association_id = user.profile.current_association_id

    try:
        nav_id = Variable(nav_id)
        nav_id = nav_id.resolve(context)
    except:
        pass

    try:
        filters = get_query_filters(user, 'navs.view_nav', association_id=association_id)
        navs = Nav.objects.filter(filters)  #.filter(id=nav_id)
        if user.is_authenticated():
            if not user.profile.is_superuser:
                navs = navs.distinct()

        nav_object = None
        nav = None
        if navs and len(navs) > 0:
            nav_object = navs[0]
            if nav_object:
                nav = get_nav(nav_object.pk, is_site_map=is_site_map, association_id=association_id)
                if not nav:
                    nav = cache_nav(nav_object, show_title, is_site_map=is_site_map, association_id=association_id)
    except:
        return None

    context.update({
        "cached": nav,
        "nav_id": nav_id,
        "show_title": show_title,
    })
    return context
Example #7
0
def nav(context, nav_id, show_title=False, is_site_map=False):

    """
    Renders the nav from cache
    if not will use the navigation tag for rendering the nav
    """
    user = AnonymousUser()
    if is_site_map == 'False':
        is_site_map = False

    if 'user' in context:
        if isinstance(context['user'], User):
            user = context['user']

    try:
        nav_id = Variable(nav_id)
        nav_id = nav_id.resolve(context)
    except:
        pass

    try:
        filters = get_query_filters(user, 'navs.view_nav')
        navs = Nav.objects.filter(filters).filter(id=nav_id)
        if user.is_authenticated():
            if not user.profile.is_superuser:
                navs = navs.distinct()

        nav_object = navs[0]
        nav = get_nav(nav_object.pk, is_site_map=is_site_map)
        if not nav:
            nav = cache_nav(nav_object, show_title, is_site_map=is_site_map)
    except:
        return None

    context.update({
        "cached": nav,
        "nav_id": nav_id,
        "show_title": show_title,
    })
    return context
Example #8
0
def bootstrap_nav(context, nav_id, show_title=False):
    """
    Bootstrap version of the nav tag
    """
    user = AnonymousUser()

    if 'user' in context:
        if isinstance(context['user'], User):
            user = context['user']

    try:
        nav_id = Variable(nav_id)
        nav_id = nav_id.resolve(context)
    except:
        pass

    try:
        filters = get_query_filters(user, 'navs.view_nav')
        navs = Nav.objects.filter(filters).filter(id=nav_id)
        if user.is_authenticated:
            if not user.profile.is_superuser:
                navs = navs.distinct()

        nav_object = navs[0]
        nav = get_nav(nav_object.pk)
        if not nav:
            nav = cache_nav(nav_object, show_title)
    except:
        return None

    context.update({
        "cached": nav,
        "nav_id": nav_id,
        "show_title": show_title,
        "is_bootstrap":
        True  # added context to enable to indicate bootstrap nav will be used
    })
    return context
Example #9
0
def bootstrap_nav(context, nav_id, show_title=False):
    """
    Bootstrap version of the nav tag
    """
    user = AnonymousUser()

    if 'user' in context:
        if isinstance(context['user'], User):
            user = context['user']

    try:
        nav_id = Variable(nav_id)
        nav_id = nav_id.resolve(context)
    except:
        pass

    try:
        filters = get_query_filters(user, 'navs.view_nav')
        navs = Nav.objects.filter(filters).filter(id=nav_id)
        if user.is_authenticated():
            if not user.profile.is_superuser:
                navs = navs.distinct()

        nav_object = navs[0]
        nav = get_nav(nav_object.pk)
        if not nav:
            nav = cache_nav(nav_object, show_title)
    except:
        return None

    context.update({
        "cached": nav,
        "nav_id": nav_id,
        "show_title": show_title,
        "is_bootstrap": True   # added context to enable to indicate bootstrap nav will be used
    })
    return context