feat(home/models.py): add CoursePage page with group restriction
This commit is contained in:
@@ -2,9 +2,39 @@ from django.db import models
|
|||||||
|
|
||||||
from wagtail.models import Page
|
from wagtail.models import Page
|
||||||
from wagtail.fields import RichTextField
|
from wagtail.fields import RichTextField
|
||||||
|
from django.contrib.auth.models import Group
|
||||||
|
|
||||||
|
|
||||||
class HomePage(Page):
|
class HomePage(Page):
|
||||||
body = RichTextField(blank=True)
|
body = RichTextField(blank=True)
|
||||||
|
|
||||||
content_panels = Page.content_panels + ["body"]
|
content_panels = Page.content_panels + ["body"]
|
||||||
|
|
||||||
|
|
||||||
|
class CoursePage(Page):
|
||||||
|
body = RichTextField(blank=True)
|
||||||
|
group = models.ForeignKey(
|
||||||
|
Group,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
help_text="Only members of this group can access this course.",
|
||||||
|
)
|
||||||
|
|
||||||
|
content_panels = Page.content_panels + [
|
||||||
|
"body",
|
||||||
|
"group",
|
||||||
|
]
|
||||||
|
|
||||||
|
def serve(self, request):
|
||||||
|
if self.group:
|
||||||
|
if (
|
||||||
|
not request.user.is_authenticated
|
||||||
|
or self.group not in request.user.groups.all()
|
||||||
|
):
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# render 403.html
|
||||||
|
return render(request, "403.html", status=403)
|
||||||
|
|
||||||
|
return super().serve(request)
|
||||||
|
|||||||
Reference in New Issue
Block a user