Sith/trombi/models.py

229 lines
6.8 KiB
Python
Raw Normal View History

2023-04-06 11:08:42 +00:00
# -*- coding:utf-8 -*-
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# Copyright 2023 © AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
# All contributors are listed in the CONTRIBUTORS file.
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# You can find the whole source code at https://github.com/ae-utbm/sith3
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
2017-05-09 21:42:01 +00:00
#
2023-04-06 11:08:42 +00:00
# PREVIOUSLY LICENSED UNDER THE MIT LICENSE,
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE.old
# OR WITHIN THE LOCAL FILE "LICENSE.old"
2017-05-09 21:42:01 +00:00
#
2024-06-24 11:07:36 +00:00
from datetime import date
2017-05-09 21:42:01 +00:00
from django.conf import settings
from django.core.exceptions import ValidationError
2024-06-24 11:07:36 +00:00
from django.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
2017-05-09 21:42:01 +00:00
from club.models import Club
2024-06-24 11:07:36 +00:00
from core.models import User
from core.utils import get_semester_code
2017-05-09 21:42:01 +00:00
2017-06-12 08:10:52 +00:00
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-06-12 08:10:52 +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):
2018-10-04 19:29:19 +00:00
return (
super(AvailableTrombiManager, self)
.get_queryset()
.filter(subscription_deadline__gte=date.today())
)
2017-06-12 08:10:52 +00:00
2017-05-09 21:42:01 +00:00
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
"""
2018-10-04 19:29:19 +00:00
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", on_delete=models.CASCADE)
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:
2018-10-04 19:29:19 +00:00
raise ValidationError(
_(
"Closing the subscriptions after the "
"comments is definitively not a good idea."
)
)
2017-05-09 21:42:01 +00:00
def get_absolute_url(self):
2018-10-04 19:29:19 +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.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-06-12 08:10:52 +00:00
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
"""
2018-10-04 19:29:19 +00:00
user = models.OneToOneField(
User,
verbose_name=_("trombi user"),
related_name="trombi_user",
on_delete=models.CASCADE,
2018-10-04 19:29:19 +00:00
)
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)
def is_owned_by(self, user):
return user.is_owner(self.trombi)
def make_memberships(self):
self.memberships.all().delete()
2018-10-04 19:29:19 +00:00
for m in self.user.memberships.filter(
role__gt=settings.SITH_MAXIMUM_FREE_ROLE
).order_by("end_date"):
role = str(settings.SITH_CLUB_ROLES[m.role])
if m.description:
role += " (%s)" % m.description
if m.end_date:
end_date = get_semester_code(m.end_date)
else:
end_date = ""
TrombiClubMembership(
2017-06-13 08:48:04 +00:00
user=self,
club=str(m.club),
role=role[:64],
start=get_semester_code(m.start_date),
2017-06-13 08:48:04 +00:00
end=end_date,
).save()
2017-06-12 08:10:52 +00:00
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.
"""
2018-10-04 19:29:19 +00:00
author = models.ForeignKey(
TrombiUser,
verbose_name=_("author"),
related_name="given_comments",
on_delete=models.CASCADE,
2018-10-04 19:29:19 +00:00
)
target = models.ForeignKey(
TrombiUser,
verbose_name=_("target"),
related_name="received_comments",
on_delete=models.CASCADE,
2018-10-04 19:29:19 +00:00
)
content = models.TextField(_("content"), default="")
2017-05-12 16:30:06 +00:00
is_moderated = models.BooleanField(_("is the comment moderated"), default=False)
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)
2017-06-13 08:48:04 +00:00
class TrombiClubMembership(models.Model):
"""
This represent a membership to a club
"""
2018-10-04 19:29:19 +00:00
user = models.ForeignKey(
TrombiUser,
verbose_name=_("user"),
related_name="memberships",
on_delete=models.CASCADE,
2018-10-04 19:29:19 +00:00
)
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:
2018-10-04 19:29:19 +00:00
ordering = ["id"]
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):
2018-10-04 19:29:19 +00:00
return reverse("trombi:profile")