class PagePermission(AbstractPagePermission): """Page permissions for single page """ grant_on = models.IntegerField(_("Grant on"), choices=ACCESS_CHOICES, default=ACCESS_PAGE_AND_DESCENDANTS) page = models.ForeignKey(Page, on_delete=models.CASCADE, null=True, blank=True, verbose_name=_("page")) objects = PagePermissionManager() class Meta: verbose_name = _('Page permission') verbose_name_plural = _('Page permissions') app_label = 'cms' def __str__(self): page = self.page_id and force_str(self.page) or "None" return "%s :: %s has: %s" % (page, self.audience, force_str(self.get_grant_on_display())) def clean(self): super().clean() if self.can_add and self.grant_on == ACCESS_PAGE: # this is a misconfiguration - user can add/move page to current # page but after he does this, he will not have permissions to # access this page anymore, so avoid this. message = _( "Add page permission requires also access to children, " "or descendants, otherwise added page can't be changed " "by its creator.") raise ValidationError(message) def get_page_ids(self): if self.grant_on & MASK_PAGE: yield self.page_id if self.grant_on & MASK_CHILDREN: children = self.page.get_child_pages().values_list('pk', flat=True) for child in children: yield child elif self.grant_on & MASK_DESCENDANTS: node = self.page.node if node._has_cached_hierarchy(): descendants = (node.item.pk for node in node.get_cached_descendants()) else: descendants = self.page.get_descendant_pages().values_list( 'pk', flat=True).iterator() for descendant in descendants: yield descendant
class PagePermission(AbstractPagePermission): """Page permissions for single page """ grant_on = models.IntegerField(_("Grant on"), choices=ACCESS_CHOICES, default=ACCESS_PAGE_AND_DESCENDANTS) page = models.ForeignKey(Page, null=True, blank=True, verbose_name=_("page")) objects = PagePermissionManager() class Meta: verbose_name = _('Page permission') verbose_name_plural = _('Page permissions') app_label = 'cms' def __str__(self): page = self.page_id and force_text(self.page) or "None" return "%s :: %s has: %s" % (page, self.audience, force_text(dict(ACCESS_CHOICES)[self.grant_on]))