private pages based on django authentication

208 views
Skip to first unread message

Vic Hunter

unread,
Sep 3, 2015, 6:09:47 AM9/3/15
to Wagtail support
I'm in the process of learning wagtail for our project site and run into something I don't know how to tackle.

This is what I want:
I have a list of 'projects', each 'project' is a wagtail page similar to the blog example in gettingstarted.
Each project belongs to one or more groups from the django auth.

I did that with the following approach. Indeed is show the groups available in the django auth

from django.contrib.auth.models import Group

class ProjectGroup(Orderable, models.Model):
   
group = models.ForeignKey(Group, related_name="+")
    page
= ParentalKey('home.
ProjectPage', related_name='group')
    panels
= [
       
FieldPanel('group'),
   
]

class
ProjectPage(Page):
    body
= RichTextField()
   
#....some more stuff here
    tags
= ClusterTaggableManager(through=ProjectPageTag, blank=True)
    postdate
= models.DateField("Post date")
    startdate = models.DateField("Start date")
     enddate = models.DateField("End date")    

     search_fields
= Page.search_fields + (
        index
.SearchField('body'),
   
)

   
@property
   
def project_index(self):
       
# Find closest ancestor which is a project index
       
return self.get_ancestors().type(ProjectIndexPage).last()

ProjectPage.content_panels = [
   
FieldPanel('title', classname="full title"),
    FieldPanel('postdate'),
    FieldPanel('startdate'),
    FieldPanel('enddate'),
   
FieldPanel('body', classname="full"),
   
InlinePanel(ProjectPage, 'related_links', label="Related links"),
   
InlinePanel(ProjectPage, 'group', label="group"),
]
.

The index list will show all the projects available but not everyone may view the contents of each project.
Now, where would I need to insert the code to check if a user is a) logged in (with redirect if not) and b) belongs to the group set in the project page?
Hope I'm clear enough.
Vic







Matthew Westcott

unread,
Sep 3, 2015, 9:39:31 AM9/3/15
to wag...@googlegroups.com
Hi Vic,
Wagtail will provide group-based access restrictions as a built-in feature in the near future (it hasn't made it into 1.1, but will hopefully be in 1.2 in a month or so's time). For now, you can implement a custom 'serve' method on ProjectPage <http://docs.wagtail.io/en/v1.0/reference/pages/model_recipes.html#overriding-the-serve-method>, which works in a similar way to a Django view function:

from django.shortcuts import redirect

class ProjectPage(Page):
...
def serve(self, request):
project_groups = [pg.group for pg in self.group.all()]
if any([(user_group in project_groups) for user_group in request.user.groups.all()]):
# User is in the required group - proceed to serve the page as normal
return super(ProjectPage, self).serve(request)
else:
# User is not in the required group - redirect them away
return redirect('/path/to/login')

Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages