Login

Restrict Flatpage To Group

Author:
nikolaj
Posted:
March 26, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.