127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
# Society Self-Service
|
|
# Copyright © 2018-2019 Yingtong Li (RunasSudo)
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
from ssmembership.models import Member
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.conf import settings
|
|
from django.db import models
|
|
from jsonfield import JSONField
|
|
|
|
class Group(models.Model):
|
|
name = models.CharField(max_length=100)
|
|
subscribable = models.BooleanField()
|
|
order = models.IntegerField(null=True, blank=True)
|
|
hidden = models.BooleanField()
|
|
|
|
managers = JSONField(default=[], blank=True)
|
|
|
|
class Meta:
|
|
ordering = ['order', 'id']
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def can_user_access(self, user):
|
|
if user.is_superuser:
|
|
return True
|
|
if user.email in self.managers:
|
|
return True
|
|
for email in self.managers:
|
|
manager = User.objects.get(email=email)
|
|
if hasattr(manager, 'userprofile') and user.email in manager.userprofile.delegates:
|
|
return True
|
|
return False
|
|
|
|
def contains_member(self, member):
|
|
if self.subscribable:
|
|
return GroupSubscription.objects.filter(member=member, group=self).count() > 0
|
|
|
|
if self.name in settings.PROMO_GROUPS_MANDATORY:
|
|
return True
|
|
if next(v for k, v in Member.YEARS if k == member.year) in self.name.split('/'):
|
|
# Year level group
|
|
return True
|
|
return False
|
|
|
|
def subscribe_member(self, member, subscribe):
|
|
if not self.subscribable:
|
|
raise Exception('Cannot subscribe to an unsubscribable group')
|
|
|
|
if subscribe:
|
|
if GroupSubscription.objects.filter(member=member, group=self).count() == 0:
|
|
subscription = GroupSubscription(member=member, group=self)
|
|
subscription.save()
|
|
else:
|
|
GroupSubscription.objects.filter(member=member, group=self).delete()
|
|
|
|
@staticmethod
|
|
def get_member_groups(member):
|
|
# Subscribable groups
|
|
subscriptions = GroupSubscription.objects.filter(member=member)
|
|
groups = set(subscription.group for subscription in subscriptions.all())
|
|
|
|
# Non-subscribable groups
|
|
for group in Group.objects.filter(subscribable=False):
|
|
if group.contains_member(member):
|
|
groups.add(group)
|
|
|
|
groups = list(groups)
|
|
groups.sort(key=lambda group: group.order)
|
|
|
|
return groups
|
|
|
|
class BulletinItem(models.Model):
|
|
author = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|
|
also_limit = JSONField(default=[])
|
|
title = models.CharField(max_length=100)
|
|
link = models.CharField(max_length=100, null=True)
|
|
image = models.ImageField(upload_to='promo_uploads/%Y/%m/%d/', null=True)
|
|
content = models.TextField()
|
|
date = models.DateField()
|
|
|
|
def can_user_access(self, user):
|
|
if self.group.can_user_access(user):
|
|
return True
|
|
if user == self.author:
|
|
return True
|
|
if hasattr(self.author, 'userprofile') and user.email in self.author.userprofile.delegates:
|
|
return True
|
|
return False
|
|
|
|
class BulletinSubscription(models.Model):
|
|
member = models.ForeignKey(Member, on_delete=models.CASCADE)
|
|
subscribed = models.BooleanField()
|
|
|
|
@staticmethod
|
|
def is_member_subscribed(member):
|
|
return BulletinSubscription.objects.filter(member=member, subscribed=True).count() > 0
|
|
|
|
@staticmethod
|
|
def set_member_subscribed(member, subscribed):
|
|
if BulletinSubscription.objects.filter(member=member).count() > 0:
|
|
subscription = BulletinSubscription.objects.get(member=member)
|
|
else:
|
|
subscription = BulletinSubscription(member=member)
|
|
subscription.subscribed = subscribed
|
|
subscription.save()
|
|
|
|
class GroupSubscription(models.Model):
|
|
member = models.ForeignKey(Member, on_delete=models.CASCADE)
|
|
group = models.ForeignKey(Group, on_delete=models.CASCADE)
|