On Sun, Jan 31, 2010 at 11:46 PM, esatterwh...@wi.rr.com < esatterwh...@wi.rr.com> wrote:
> if if you want to change the menu based on the user, you could > probably just use the user permissions from the auth context processor > > if the user has the permissions ( access ) to the option - show it > > else - don't show it. > > > or if you want to use the model you have listed here, you could use > the {% ifequal tag %} > > {% ifequal myuser.access 1000 %} > show some stuff... > {% else %} > show something else... > {% endifequal %} > The problem with that is if the user has permission 1000 for superuser and there is a menu item for staff requiring a permission of 100 the if check would fail. My programmers brain says, no problem just use a bit vector, but django templates don't allow that either. In the development version it's possible to do; {% if myuser.access >= 100 %} But i'm looking at deploying the site so i don't really want to use it. > > I personally think using putting users in groups and assigning > permissions then checking in the template for who has what would be > the easiest solution. Django does most of the work for you > That may be a better option, i'm still learning django and may have cheated with access permissions. I'll look into it but i want to avoid a mess of if checks. Since the menu is dynamically generated i can't assume a menu item with any particular permission is going to arrive. The solution i did use seems to be working quite well. In the template the "access" tag works like this. {% for item in menu.objects.all %} {% access item %} #Do stuff {% endaccess %} {% endfor %} The "access" tag checks for the existence of user in the context and calculates the users access level, or failing that assumes an access of 1 (Anonymous). Then it checks for an access property on item and renders the block only if user.access >= item.access . You see why the formerly mentioned if check is way simpler? > On Jan 30, 9:28 pm, Dylan Evans <dy...@contentfree.info> wrote: > > No i have processors, i have all the data i need in the template, i just > > don't know an elegant way to condition out menu items. > > > > This is how my model works > > > > ACCESS_CHOICES = ( > > (1, "Public"), > > (10, "Private"), > > (100, "Staff"), > > (1000, "System") > > ) > > > > class Menu(models.Model): > > name = models.CharField(max_length=16) > > > > class MenuItem(models.Model): > > menu = models.ForeignKey(Menu) > > access = models.IntegerField(choices=ACCESS_CHOICES) > > > > class MenuSubItem(models.Model): > > item = models.ForeignKey(MenuItem) > > access = models.ForeignKey(MenuSubItem) > > > > > > > > > > > > On Sun, Jan 31, 2010 at 5:31 AM, Shawn Milochik <sh...@milochik.com> > wrote: > > > I think you've missed context processors, which is easy to do. I'm > assuming > > > that your issue is that you want to have something passed in the > context on > > > every page load to do something like decide which menu items are > available > > > based upon whether the user is logged in, their privileges, or > whatever. > > > > > Context processors allow you to define a dictionary that gets appended > to > > > the context of every response you send, so you can have that common > stuff > > > there. > > > > > 1. Write code to get the values appropriate for the current user or > > > whatever. Put these in a Python file in your app. > > > 2. Add that file to the TEMPLATE_CONTEXT_PROCESSORS in your > settings.py. > > > 3. Replace Context() with RequestContext() in your render_to_response > > > calls. > > > > > If I've missed your actual point, please give more detail. I think this > > > simplifies what you're trying to do. > > > > > Shawn > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Django users" group. > > > To post to this group, send email to django-us...@googlegroups.com. > > > To unsubscribe from this group, send email to > > > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > <django-users%2bunsubscr...@google groups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/django-users?hl=en. > > > > -- > > "The UNIX system has a command, nice ... in order to be nice to the other > > users. Nobody ever uses it." - Andrew S. Tanenbaum > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- "The UNIX system has a command, nice ... in order to be nice to the other users. Nobody ever uses it." - Andrew S. Tanenbaum -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.