Add organization private pages

This commit is contained in:
cuom1999 2020-12-27 23:45:58 -06:00
parent 4c3441b197
commit 4502dbf6b9
14 changed files with 369 additions and 156 deletions

View file

@ -8,7 +8,7 @@ from django.utils.translation import gettext_lazy as _
from mptt.fields import TreeForeignKey
from mptt.models import MPTTModel
from judge.models.profile import Profile
from judge.models.profile import Organization, Profile
__all__ = ['MiscConfig', 'validate_regex', 'NavigationBar', 'BlogPost']
@ -72,6 +72,9 @@ class BlogPost(models.Model):
content = models.TextField(verbose_name=_('post content'))
summary = models.TextField(verbose_name=_('post summary'), blank=True)
og_image = models.CharField(verbose_name=_('openGraph image'), default='', max_length=150, blank=True)
organizations = models.ManyToManyField(Organization, blank=True, verbose_name=_('organizations'),
help_text=_('If private, only these organizations may see the blog post.'))
is_organization_private = models.BooleanField(verbose_name=_('private to organizations'), default=False)
def __str__(self):
return self.title
@ -81,11 +84,22 @@ class BlogPost(models.Model):
def can_see(self, user):
if self.visible and self.publish_on <= timezone.now():
return True
if not self.is_organization_private:
return True
if user.is_authenticated and \
self.organizations.filter(id__in=user.profile.organizations.all()).exists():
return True
if user.has_perm('judge.edit_all_post'):
return True
return user.is_authenticated and self.authors.filter(id=user.profile.id).exists()
def is_editable_by(self, user):
if not user.is_authenticated:
return False
if user.has_perm('judge.edit_all_post'):
return True
return user.has_perm('judge.change_blogpost') and self.authors.filter(id=user.profile.id).exists()
class Meta:
permissions = (
('edit_all_post', _('Edit all posts')),