society-self-service/sspromotions/models.py
Yingtong Li 4638a71cfb
Tweak how managers work
Anyone can add to any group, and can edit their own items. Managers of a group can edit any items in that group.
2019-01-25 10:32:48 +11:00

75 lines
2.3 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 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)
managers = JSONField(default=[], blank=True)
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):
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 user.email in self.author.delegates:
return True
return False
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']