Restrict Flatpage To Group

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
###################################
# Stick the following in a view somewhere
##################################
from django.http import HttpResponseRedirect
from django.contrib.auth.models import Group
from django.contrib.flatpages.views import flatpage

# Use this wrapper view to patrol the specific areas
def restricted_flatpage(request,group_name=None,redirect_to="/restricted/"):
    """Restricts access to flatpage to group specified by group_name and superuser"""
    if not request.user or request.user.is_anonymous() or not group_name:
        return HttpResponseRedirect(redirect_to)
    try:
        if request.user.is_superuser:
            return flatpage(request,request.path)
        g=Group.objects.get(name=group_name)
        if g.user_set.filter(id=request.user.id).count():
            return flatpage(request,request.path)
    except Group.DoesNotExist: pass
    return HttpResponseRedirect(redirect_to)

##################################
# Then stick this in the appropriate urls.py
####################################
    (r'^employee-benefits/', restricted_flatpage,{"group_name":"corporate"}),
    (r'^company', restricted_flatpage,{"group_name":"corporate"}),

More like this

  1. Row-Level, URL-based permissions for FlatPages by bradmontgomery 4 years, 10 months ago
  2. Class-based view mixin for flatpages by schwuk 1 year, 1 month ago
  3. Restrict staff access to admin pages by slink 3 years, 6 months ago
  4. Breadcrumbs for flatpages by jca 6 years, 4 months ago
  5. YUI editor for flatpages inside admin interface by nnrcschmdt 6 years ago

Comments

(Forgotten your password?)