society-self-service/sspromotions/models.py

123 lines
4.0 KiB
Python
Raw Normal View History

2018-12-07 15:53:46 +11:00
# Society Self-Service
2019-01-11 15:45:11 +11:00
# Copyright © 2018-2019 Yingtong Li (RunasSudo)
2018-12-07 15:53:46 +11:00
#
# 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
2018-12-07 15:53:46 +11:00
from django.contrib.auth.models import User
from django.conf import settings
2018-12-07 15:53:46 +11:00
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)
managers = JSONField(default=[], blank=True)
2018-12-07 15:53:46 +11:00
class Meta:
ordering = ['order', 'id']
2018-12-07 15:53:46 +11:00
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)
2019-02-09 23:13:03 +11:00
if hasattr(manager, 'userprofile') and user.email in manager.userprofile.delegates:
2018-12-07 15:53:46 +11:00
return True
return False
def contains_member(self, member):
if self.subscribable:
return GroupSubscription.objects.filter(member=member, group=self).count() > 0
2019-02-13 15:24:14 +11:00
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)
return groups
2018-12-07 15:53:46 +11:00
class BulletinItem(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
2018-12-07 15:53:46 +11:00
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
2019-02-09 23:13:03 +11:00
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)