Files
kursy-mirror/home/models.py

57 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.contrib.auth.models import Group
from django.forms import CheckboxSelectMultiple
from wagtail.admin.panels import FieldPanel
from wagtail.fields import RichTextField
from wagtail.models import Page
from wagtail.models.copying import ParentalManyToManyField
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + ["body"]
class CoursePage(Page):
body = RichTextField(blank=True)
allowed_groups = ParentalManyToManyField(
Group,
related_name="course_pages",
help_text="Select a group to restrict access to this course. Non-members will be prompted to purchase the course to view modules.",
)
def _user_has_access(self, user):
if not user.is_authenticated:
return False
user_group_ids = user.groups.values_list("id", flat=True)
return self.allowed_groups.filter(id__in=user_group_ids).exists()
def get_context(self, request):
context = super().get_context(request)
context["user_has_access"] = self._user_has_access(request.user)
return context
content_panels = Page.content_panels + [
FieldPanel("body"),
FieldPanel("allowed_groups", widget=CheckboxSelectMultiple),
]
class CourseModulePage(Page):
body = RichTextField(blank=True)
@property
def full_title(self):
if hasattr(self, "get_parent"):
parent = self.get_parent()
if parent and hasattr(parent, "specific"):
course = parent.specific
else:
course = None
if course and hasattr(course, "title"):
return f"{course.title} {self.title}"
return self.title
content_panels = Page.content_panels + ["body"]