Restrict Flatpage To Group

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##################################fromdjango.httpimportHttpResponseRedirectfromdjango.contrib.auth.modelsimportGroupfromdjango.contrib.flatpages.viewsimportflatpage# Use this wrapper view to patrol the specific areasdefrestricted_flatpage(request,group_name=None,redirect_to="/restricted/"):"""Restricts access to flatpage to group specified by group_name and superuser"""ifnotrequest.userorrequest.user.is_anonymous()ornotgroup_name:returnHttpResponseRedirect(redirect_to)try:ifrequest.user.is_superuser:returnflatpage(request,request.path)g=Group.objects.get(name=group_name)ifg.user_set.filter(id=request.user.id).count():returnflatpage(request,request.path)exceptGroup.DoesNotExist:passreturnHttpResponseRedirect(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"}),