Sith/trombi/models.py

118 lines
4.7 KiB
Python
Raw Normal View History

2017-05-09 21:42:01 +00:00
# -*- coding:utf-8 -*
#
# Copyright 2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils import timezone
from django.core.exceptions import ValidationError
from datetime import timedelta, date
from core.models import User
from club.models import Club
2017-05-10 20:17:05 +00:00
class TrombiManager(models.Manager):
2017-05-09 21:42:01 +00:00
def get_queryset(self):
2017-05-10 20:17:05 +00:00
return super(TrombiManager, self).get_queryset()
2017-05-09 21:42:01 +00:00
2017-05-10 20:17:05 +00:00
class AvailableTrombiManager(models.Manager):
2017-05-09 21:42:01 +00:00
def get_queryset(self):
2017-05-10 20:17:05 +00:00
return super(AvailableTrombiManager,
2017-05-09 21:42:01 +00:00
self).get_queryset().filter(subscription_deadline__gte=date.today())
2017-05-10 20:17:05 +00:00
class Trombi(models.Model):
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
This is the main class, the Trombi itself.
2017-05-09 21:42:01 +00:00
It contains the deadlines for the users, and the link to the club that makes
2017-05-10 20:17:05 +00:00
its Trombi.
2017-05-09 21:42:01 +00:00
"""
subscription_deadline = models.DateField(_('subscription deadline'),
default=timezone.now, help_text=_("Before this date, users are "
2017-05-10 20:17:05 +00:00
"allowed to subscribe to this Trombi. "
2017-05-09 21:42:01 +00:00
"After this date, users subscribed will be allowed to comment on each other."))
comments_deadline = models.DateField(_('comments deadline'),
default=timezone.now, help_text=_("After this date, users won't be "
"able to make comments anymore."))
max_chars = models.IntegerField(_('maximum characters'), default=400,
help_text=_('Maximum number of characters allowed in a comment.'))
2017-05-10 20:17:05 +00:00
club = models.OneToOneField(Club, related_name='trombi')
2017-05-09 21:42:01 +00:00
2017-05-10 20:17:05 +00:00
objects = TrombiManager()
availables = AvailableTrombiManager()
2017-05-09 21:42:01 +00:00
def __str__(self):
return str(self.club.name)
def clean(self):
if self.subscription_deadline > self.comments_deadline:
raise ValidationError(_("Closing the subscriptions after the "
"comments is definitively not a good idea."))
def get_absolute_url(self):
2017-05-10 20:17:05 +00:00
return reverse('trombi:detail', kwargs={'trombi_id': self.id})
2017-05-09 21:42:01 +00:00
def is_owned_by(self, user):
return user.is_owner(self.club)
def can_be_edited_by(self, user):
2017-05-09 21:42:01 +00:00
return user.can_edit(self.club)
def can_be_viewed_by(self, user):
return user.id in [u.user.id for u in self.users.all()]
2017-05-10 20:17:05 +00:00
class TrombiUser(models.Model):
2017-05-09 21:42:01 +00:00
"""
This class is only here to avoid cross references between the core, club,
2017-05-10 20:17:05 +00:00
and trombi modules. It binds a User to a Trombi without needing to import
Trombi into the core.
It also adds the pictures to the profile without needing all the security
like the other SithFiles.
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
user = models.OneToOneField(User, verbose_name=_("trombi user"), related_name='trombi_user')
trombi = models.ForeignKey(Trombi, verbose_name=_("trombi"), related_name='users', blank=True, null=True, on_delete=models.SET_NULL)
profile_pict = models.ImageField(upload_to='trombi', verbose_name=_("profile pict"), null=True, blank=True,
help_text=_("The profile picture you want in the trombi (warning: this picture may be published)"))
scrub_pict = models.ImageField(upload_to='trombi', verbose_name=_("scrub pict"), null=True, blank=True,
help_text=_("The scrub picture you want in the trombi (warning: this picture may be published)"))
2017-05-09 21:42:01 +00:00
def __str__(self):
return str(self.user)
2017-05-10 20:17:05 +00:00
class TrombiComment(models.Model):
2017-05-09 21:42:01 +00:00
"""
2017-05-10 20:17:05 +00:00
This represent a comment given by someone to someone else in the same Trombi
2017-05-09 21:42:01 +00:00
instance.
"""
2017-05-10 20:17:05 +00:00
author = models.ForeignKey(TrombiUser, verbose_name=_("author"), related_name='given_comments')
target = models.ForeignKey(TrombiUser, verbose_name=_("target"), related_name='received_comments')
content = models.TextField(_("content"), default="")
2017-05-09 21:42:01 +00:00
def can_be_viewed_by(self, user):
if user.id == self.target.user.id:
return False
2017-05-10 20:17:05 +00:00
return user.id == self.author.user.id or user.can_edit(self.author.trombi)