2009-06-08 10:03:49 +00:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
2009-06-11 23:12:51 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2009-06-08 10:03:49 +00:00
|
|
|
|
2009-07-09 23:47:29 +00:00
|
|
|
import authority
|
|
|
|
|
from authority.permissions import BasePermission
|
2009-06-11 23:12:51 +00:00
|
|
|
|
2020-02-07 09:50:47 +00:00
|
|
|
|
2009-07-09 23:47:29 +00:00
|
|
|
class FlatPagePermission(BasePermission):
|
2009-06-11 23:22:43 +00:00
|
|
|
"""
|
2009-06-21 23:07:26 +00:00
|
|
|
This class contains a bunch of checks:
|
2018-01-19 11:37:15 +00:00
|
|
|
|
2009-06-21 23:07:26 +00:00
|
|
|
1. the default checks 'add_flatpage', 'browse_flatpage',
|
|
|
|
|
'change_flatpage' and 'delete_flatpage'
|
|
|
|
|
2. the custom checks:
|
|
|
|
|
a) 'review_flatpage', which is similar to the default checks
|
|
|
|
|
b) 'top_secret', which is represented by the top_secret method
|
|
|
|
|
|
2009-07-06 22:44:14 +00:00
|
|
|
You can use those checks in your views directly like::
|
2009-06-21 23:07:26 +00:00
|
|
|
|
|
|
|
|
def review_flatpage(request, url):
|
|
|
|
|
flatpage = get_object_or_404(url__contains=url)
|
|
|
|
|
check = FlatPagePermission(request.user)
|
|
|
|
|
if check.review_flatpage(obj=flatpage):
|
|
|
|
|
print "yay, you can review this flatpage!"
|
|
|
|
|
return flatpage(request, url)
|
|
|
|
|
|
2009-07-06 22:44:14 +00:00
|
|
|
Or the same view using the decorator permission_required::
|
2009-06-22 14:09:06 +00:00
|
|
|
|
2009-07-06 22:44:14 +00:00
|
|
|
@permission_required('flatpage_permission.review_flatpage',
|
|
|
|
|
('flatpages.flatpage', 'url__contains', 'url'))
|
2009-06-22 14:09:06 +00:00
|
|
|
def review_flatpage(request, url):
|
|
|
|
|
print "yay, you can review this flatpage!"
|
|
|
|
|
return flatpage(request, url)
|
|
|
|
|
|
2009-07-06 22:44:14 +00:00
|
|
|
Or you can use this permission in your templates like this::
|
2009-06-21 23:07:26 +00:00
|
|
|
|
|
|
|
|
{% ifhasperm "flatpage_permission.review_flatpage" request.user flatpage %}
|
|
|
|
|
Yes, you are allowed to review flatpage '{{ flatpage }}', aren't you?
|
|
|
|
|
{% else %}
|
|
|
|
|
Nope, sorry. You aren't allowed to review this flatpage.
|
|
|
|
|
{% endifhasperm %}
|
2009-07-06 22:44:14 +00:00
|
|
|
|
2009-06-11 23:22:43 +00:00
|
|
|
"""
|
2020-02-07 09:50:47 +00:00
|
|
|
|
|
|
|
|
label = "flatpage_permission"
|
|
|
|
|
checks = ("review", "top_secret")
|
2009-06-11 23:12:51 +00:00
|
|
|
|
2009-07-10 13:55:25 +00:00
|
|
|
def top_secret(self, flatpage=None, lala=None):
|
2009-06-11 23:12:51 +00:00
|
|
|
if flatpage and flatpage.registration_required:
|
2009-06-21 23:07:26 +00:00
|
|
|
return self.browse_flatpage(obj=flatpage)
|
2009-06-11 23:12:51 +00:00
|
|
|
return False
|
2020-02-07 09:50:47 +00:00
|
|
|
|
|
|
|
|
top_secret.short_description = _("Is allowed to see top secret flatpages")
|
|
|
|
|
|
2009-07-09 23:07:20 +00:00
|
|
|
|
2018-01-19 11:37:15 +00:00
|
|
|
authority.sites.register(FlatPage, FlatPagePermission)
|