Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. from wagtail.core.models import Page, PageViewRestriction
  2.  
  3. def filter_pages(user):
  4. pages = Page.objects.live()
  5.  
  6. # Unauthenticated users can only see public pages
  7. if not user.is_authenticated:
  8. pages = pages.public()
  9. # Superusers can implicitly view all pages. No further filtering required
  10. elif not user.is_superuser:
  11. # Get all page ids where the user's groups do NOT have access to
  12. disallowed_ids = PageViewRestriction.objects.exclude(groups__id=user.groups.all()).values_list("page", flat=True)
  13. # Exclude all pages with disallowed ids
  14. pages = pages.exclude(id__in=disallowed_ids)
  15.  
  16. return pages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement