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 django.contrib.auth.models import User
|
|
|
|
|
2019-01-12 15:31:53 +11:00
|
|
|
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)
|
|
|
|
|
2019-01-25 10:30:55 +11:00
|
|
|
managers = JSONField(default=[], blank=True)
|
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)
|
|
|
|
if user.email in manager.delegates:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['order', 'id']
|
|
|
|
|
|
|
|
class BulletinItem(models.Model):
|
2019-01-25 10:30:55 +11:00
|
|
|
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()
|
2019-01-25 10:30:55 +11:00
|
|
|
|
|
|
|
def can_user_access(self, user):
|
|
|
|
if self.group.can_user_access(user):
|
|
|
|
return True
|
|
|
|
if user == self.author:
|
|
|
|
return True
|
|
|
|
if user.email in self.author.delegates:
|
|
|
|
return True
|
|
|
|
return False
|
2019-01-11 15:45:11 +11:00
|
|
|
|
|
|
|
class CalendarItem(models.Model):
|
|
|
|
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)
|
|
|
|
date = models.DateField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['date', 'title', 'id']
|