models.py 6.79 KB
Newer Older
Skia's avatar
Skia committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# -*- 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.core.exceptions import ValidationError

from datetime import timedelta, date

from core.models import User
Skia's avatar
Skia committed
34
from core.utils import get_start_of_semester, get_semester
Skia's avatar
Skia committed
35 36
from club.models import Club

Krophil's avatar
Krophil committed
37

Skia's avatar
Skia committed
38
class TrombiManager(models.Manager):
Skia's avatar
Skia committed
39
    def get_queryset(self):
Skia's avatar
Skia committed
40
        return super(TrombiManager, self).get_queryset()
Skia's avatar
Skia committed
41

Krophil's avatar
Krophil committed
42

Skia's avatar
Skia committed
43
class AvailableTrombiManager(models.Manager):
Skia's avatar
Skia committed
44
    def get_queryset(self):
Sli's avatar
Sli committed
45 46 47 48 49
        return (
            super(AvailableTrombiManager, self)
            .get_queryset()
            .filter(subscription_deadline__gte=date.today())
        )
Krophil's avatar
Krophil committed
50

Skia's avatar
Skia committed
51

Skia's avatar
Skia committed
52
class Trombi(models.Model):
Skia's avatar
Skia committed
53
    """
Skia's avatar
Skia committed
54
    This is the main class, the Trombi itself.
Skia's avatar
Skia committed
55
    It contains the deadlines for the users, and the link to the club that makes
Skia's avatar
Skia committed
56
    its Trombi.
Skia's avatar
Skia committed
57
    """
Sli's avatar
Sli committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    subscription_deadline = models.DateField(
        _("subscription deadline"),
        default=date.today,
        help_text=_(
            "Before this date, users are "
            "allowed to subscribe to this Trombi. "
            "After this date, users subscribed will be allowed to comment on each other."
        ),
    )
    comments_deadline = models.DateField(
        _("comments deadline"),
        default=date.today,
        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."),
    )
    show_profiles = models.BooleanField(
        _("show users profiles to each other"), default=True
    )
    club = models.OneToOneField(Club, related_name="trombi")
Skia's avatar
Skia committed
84

Skia's avatar
Skia committed
85 86
    objects = TrombiManager()
    availables = AvailableTrombiManager()
Skia's avatar
Skia committed
87 88 89 90 91 92

    def __str__(self):
        return str(self.club.name)

    def clean(self):
        if self.subscription_deadline > self.comments_deadline:
Sli's avatar
Sli committed
93 94 95 96 97 98
            raise ValidationError(
                _(
                    "Closing the subscriptions after the "
                    "comments is definitively not a good idea."
                )
            )
Skia's avatar
Skia committed
99 100

    def get_absolute_url(self):
Sli's avatar
Sli committed
101
        return reverse("trombi:detail", kwargs={"trombi_id": self.id})
Skia's avatar
Skia committed
102 103 104 105

    def is_owned_by(self, user):
        return user.can_edit(self.club)

106 107 108
    def can_be_viewed_by(self, user):
        return user.id in [u.user.id for u in self.users.all()]

Krophil's avatar
Krophil committed
109

Skia's avatar
Skia committed
110
class TrombiUser(models.Model):
Skia's avatar
Skia committed
111 112
    """
    This class is only here to avoid cross references between the core, club,
Skia's avatar
Skia committed
113 114
    and trombi modules. It binds a User to a Trombi without needing to import
    Trombi into the core.
115 116
    It also adds the pictures to the profile without needing all the security
    like the other SithFiles.
Skia's avatar
Skia committed
117
    """
Sli's avatar
Sli committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    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)"
        ),
    )
Skia's avatar
Skia committed
148

149 150 151
    def __str__(self):
        return str(self.user)

152 153 154
    def is_owned_by(self, user):
        return user.is_owner(self.trombi)

Skia's avatar
Skia committed
155 156
    def make_memberships(self):
        self.memberships.all().delete()
Sli's avatar
Sli committed
157 158 159
        for m in self.user.memberships.filter(
            role__gt=settings.SITH_MAXIMUM_FREE_ROLE
        ).order_by("end_date"):
Skia's avatar
Skia committed
160 161 162 163 164 165 166 167
            role = str(settings.SITH_CLUB_ROLES[m.role])
            if m.description:
                role += " (%s)" % m.description
            if m.end_date:
                end_date = get_semester(m.end_date)
            else:
                end_date = ""
            TrombiClubMembership(
Krophil's avatar
Krophil committed
168 169 170 171 172 173
                user=self,
                club=str(m.club),
                role=role[:64],
                start=get_semester(m.start_date),
                end=end_date,
            ).save()
Skia's avatar
Skia committed
174

Krophil's avatar
Krophil committed
175

Skia's avatar
Skia committed
176
class TrombiComment(models.Model):
Skia's avatar
Skia committed
177
    """
Skia's avatar
Skia committed
178
    This represent a comment given by someone to someone else in the same Trombi
Skia's avatar
Skia committed
179 180
    instance.
    """
Sli's avatar
Sli committed
181 182 183 184 185 186 187

    author = models.ForeignKey(
        TrombiUser, verbose_name=_("author"), related_name="given_comments"
    )
    target = models.ForeignKey(
        TrombiUser, verbose_name=_("target"), related_name="received_comments"
    )
188
    content = models.TextField(_("content"), default="")
Skia's avatar
Skia committed
189
    is_moderated = models.BooleanField(_("is the comment moderated"), default=False)
Skia's avatar
Skia committed
190

191 192 193
    def can_be_viewed_by(self, user):
        if user.id == self.target.user.id:
            return False
Skia's avatar
Skia committed
194
        return user.id == self.author.user.id or user.can_edit(self.author.trombi)
195

Krophil's avatar
Krophil committed
196

Skia's avatar
Skia committed
197 198 199 200
class TrombiClubMembership(models.Model):
    """
    This represent a membership to a club
    """
Sli's avatar
Sli committed
201 202 203 204

    user = models.ForeignKey(
        TrombiUser, verbose_name=_("user"), related_name="memberships"
    )
Skia's avatar
Skia committed
205 206 207 208 209 210
    club = models.CharField(_("club"), max_length=32, default="")
    role = models.CharField(_("role"), max_length=64, default="")
    start = models.CharField(_("start"), max_length=16, default="")
    end = models.CharField(_("end"), max_length=16, default="")

    class Meta:
Sli's avatar
Sli committed
211
        ordering = ["id"]
Skia's avatar
Skia committed
212 213 214 215 216 217 218 219

    def __str__(self):
        return "%s - %s - %s (%s)" % (self.user, self.club, self.role, self.start)

    def can_be_edited_by(self, user):
        return user.id == self.user.user.id or user.can_edit(self.user.trombi)

    def get_absolute_url(self):
Sli's avatar
Sli committed
220
        return reverse("trombi:profile")