Question

My situation is as follows.

I have a django app that is a CMS, and this one app generates page content and menus.

There is 'default' view that generates all the page content. One of the fields of the main model is designed to limit the visibility of pages depending on the setting:

  • 'internal' (limited to users on our network),
  • 'worldwide' (viewable by the www) and now, I would like to add an extra value
  • 'secure', which would limit viewing only to those who are logged in to the site.

I dont think @login_required would work, as it only works on whole functions.

We want the author or editor to be able to set this flag, rather than code a special function; and there will be instances where a page migrates from 'internal' to 'secure' then to 'worldwide' - so ideally the urls should stay the same.

What is the best way to go about this? thanks in advance... patrick

Was it helpful?

Solution

As you want the flag set on your object, I'm assuming that flag can't be read until you're already within the view (i.e. you won't be storing it in something accessible by the request object, like a session), so that precludes custom decorators. You could choose to go with something a bit crude but workable like this:

if (val=="internal" and inrange(request.META['REMOTE_ADDR'])) or (val=="secure" and  request.user.is_authenticated()) or val=="worldwide":
     return render_to_response .... etc.
else:
     return HttpResponseForbidden()

Substituting your model values, and writing the inrange function, naturally.

The more sophisticated approach, though, would be to write custom middleware and use the process_view() function. See the docs for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top