Login

Restrict Flatpage To Group

Author:
nikolaj
Posted:
March 26, 2008
Language:
Python
Version:
.96
Tags:
flatpages groups restricted
Score:
0 (after 0 ratings)

Simple little flatpage wrapper view that lets you easily block off certain areas of flatpages to only a certain user group. Allows superuser in as well.

 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 6 years ago
  2. app based group permissions pre load without foreign keys by ZaherSarieddine 1 month ago
  3. Class-based view mixin for flatpages by schwuk 2 years, 4 months ago
  4. Restrict staff access to admin pages by slink 4 years, 9 months ago
  5. Breadcrumbs for flatpages by jca 7 years, 6 months ago

Comments

Please login first before commenting.