Sith/forum/models.py

445 lines
14 KiB
Python
Raw Normal View History

#
# Copyright 2016,2017,2018
# - 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 __future__ import annotations
2024-06-24 11:07:36 +00:00
from datetime import datetime
2024-07-18 15:33:14 +00:00
from datetime import timezone as tz
2024-06-24 11:07:36 +00:00
from itertools import chain
from typing import Self
2024-06-24 11:07:36 +00:00
2017-01-21 02:42:06 +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
2017-01-21 02:42:06 +00:00
from django.utils import timezone
2017-02-24 00:50:09 +00:00
from django.utils.functional import cached_property
2024-06-24 11:07:36 +00:00
from django.utils.translation import gettext_lazy as _
2017-01-21 02:42:06 +00:00
from club.models import Club
2024-06-24 11:07:36 +00:00
from core.models import Group, User
2017-01-21 02:42:06 +00:00
2017-06-12 07:58:24 +00:00
# Those functions prevent generating migration upon settings changes
def get_default_edit_group():
return [settings.SITH_GROUP_OLD_SUBSCRIBERS_ID]
def get_default_view_group():
return [settings.SITH_GROUP_PUBLIC_ID]
2017-01-21 02:42:06 +00:00
class Forum(models.Model):
2024-07-12 07:34:16 +00:00
"""The Forum class, made as a tree to allow nice tidy organization.
2017-02-24 00:50:09 +00:00
owner_club allows club members to moderate there own topics
edit_groups allows to put any group as a forum admin
view_groups allows some groups to view a forum
2017-01-21 02:42:06 +00:00
"""
2018-10-04 19:29:19 +00:00
id = models.AutoField(primary_key=True, db_index=True)
2018-10-04 19:29:19 +00:00
name = models.CharField(_("name"), max_length=64)
description = models.CharField(_("description"), max_length=512, default="")
is_category = models.BooleanField(_("is a category"), default=False)
parent = models.ForeignKey(
"Forum",
related_name="children",
null=True,
blank=True,
on_delete=models.CASCADE,
)
2018-10-04 19:29:19 +00:00
owner_club = models.ForeignKey(
Club,
related_name="owned_forums",
verbose_name=_("owner club"),
default=settings.SITH_MAIN_CLUB_ID,
on_delete=models.CASCADE,
2018-10-04 19:29:19 +00:00
)
edit_groups = models.ManyToManyField(
Group,
related_name="editable_forums",
blank=True,
default=get_default_edit_group,
)
view_groups = models.ManyToManyField(
Group,
related_name="viewable_forums",
blank=True,
default=get_default_view_group,
)
number = models.IntegerField(
_("number to choose a specific forum ordering"), default=1
)
_last_message = models.ForeignKey(
"ForumMessage",
related_name="forums_where_its_last",
verbose_name=_("the last message"),
null=True,
on_delete=models.SET_NULL,
)
_topic_number = models.IntegerField(_("number of topics"), default=0)
2017-03-12 17:06:01 +00:00
class Meta:
2018-10-04 19:29:19 +00:00
ordering = ["number"]
def __str__(self):
return self.name
def save(self, *args, **kwargs):
copy_rights = False
if self.id is None:
copy_rights = True
2024-06-27 12:46:43 +00:00
super().save(*args, **kwargs)
if copy_rights:
self.copy_rights()
def get_absolute_url(self):
return reverse("forum:view_forum", kwargs={"forum_id": self.id})
def clean(self):
self.check_loop()
def set_topic_number(self):
self._topic_number = self.get_topic_number()
self.save()
if self.parent:
self.parent.set_topic_number()
def set_last_message(self):
2018-10-04 19:29:19 +00:00
topic = (
ForumTopic.objects.filter(forum__id=self.id)
.exclude(_last_message=None)
.order_by("-_last_message__id")
.first()
)
forum = (
Forum.objects.filter(parent__id=self.id)
.exclude(_last_message=None)
.order_by("-_last_message__id")
.first()
)
if topic and forum:
if topic._last_message_id < forum._last_message_id:
self._last_message_id = forum._last_message_id
else:
self._last_message_id = topic._last_message_id
elif topic:
self._last_message_id = topic._last_message_id
elif forum:
self._last_message_id = forum._last_message_id
self.save()
if self.parent:
self.parent.set_last_message()
def apply_rights_recursively(self):
children = self.children.all()
for c in children:
c.copy_rights()
c.apply_rights_recursively()
def copy_rights(self):
2024-07-12 07:34:16 +00:00
"""Copy, if possible, the rights of the parent folder."""
if self.parent is not None:
self.owner_club = self.parent.owner_club
self.edit_groups.set(self.parent.edit_groups.all())
self.view_groups.set(self.parent.view_groups.all())
self.save()
2017-06-12 07:58:24 +00:00
_club_memberships = {} # This cache is particularly efficient:
2018-10-04 19:29:19 +00:00
# divided by 3 the number of requests on the main forum page
# after the first load
def is_owned_by(self, user):
if user.is_anonymous:
return False
2024-08-16 20:43:54 +00:00
if user.is_root or user.is_in_group(pk=settings.SITH_GROUP_FORUM_ADMIN_ID):
return True
2017-06-12 07:58:24 +00:00
try:
m = Forum._club_memberships[self.id][user.id]
except KeyError:
m = self.owner_club.get_membership_for(user)
2017-06-12 07:58:24 +00:00
try:
Forum._club_memberships[self.id][user.id] = m
except KeyError:
Forum._club_memberships[self.id] = {}
Forum._club_memberships[self.id][user.id] = m
if m:
return m.role > settings.SITH_MAXIMUM_FREE_ROLE
return False
2017-01-21 02:42:06 +00:00
def check_loop(self):
2024-07-12 07:34:16 +00:00
"""Raise a validation error when a loop is found within the parent list."""
2017-01-21 02:42:06 +00:00
objs = []
cur = self
while cur.parent is not None:
if cur in objs:
2018-10-04 19:29:19 +00:00
raise ValidationError(_("You can not make loops in forums"))
2017-01-21 02:42:06 +00:00
objs.append(cur)
cur = cur.parent
2018-04-26 13:20:45 +00:00
def get_full_name(self):
2018-10-04 19:29:19 +00:00
return "/".join(
chain.from_iterable(
[[parent.name for parent in self.get_parent_list()], [self.name]]
)
)
2018-04-26 13:20:45 +00:00
2017-02-24 00:50:09 +00:00
@cached_property
def parent_list(self):
return self.get_parent_list()
2017-01-21 02:42:06 +00:00
def get_parent_list(self):
parents = []
current = self.parent
while current is not None:
parents.append(current)
current = current.parent
return parents
2017-01-21 02:42:06 +00:00
@property
2017-02-24 00:50:09 +00:00
def topic_number(self):
return self._topic_number
2017-02-24 00:50:09 +00:00
2017-01-21 21:47:30 +00:00
def get_topic_number(self):
number = self.topics.all().count()
for c in self.children.all():
2017-02-24 00:50:09 +00:00
number += c.topic_number
2017-01-21 21:47:30 +00:00
return number
2017-02-24 00:50:09 +00:00
@cached_property
def last_message(self):
return self._last_message
def get_children_list(self) -> list[Self]:
children = [self.id]
for c in self.children.all():
children.append(c.id)
children.extend(c.get_children_list())
return children
2017-01-21 21:47:30 +00:00
2017-06-12 07:58:24 +00:00
2017-01-21 02:42:06 +00:00
class ForumTopic(models.Model):
forum = models.ForeignKey(Forum, related_name="topics", on_delete=models.CASCADE)
author = models.ForeignKey(
User, related_name="forum_topics", on_delete=models.CASCADE
)
2018-10-04 19:29:19 +00:00
description = models.CharField(_("description"), max_length=256, default="")
subscribed_users = models.ManyToManyField(
User, related_name="favorite_topics", verbose_name=_("subscribed users")
)
_last_message = models.ForeignKey(
"ForumMessage",
related_name="+",
verbose_name=_("the last message"),
null=True,
on_delete=models.SET_NULL,
)
_title = models.CharField(_("title"), max_length=64, blank=True)
_message_number = models.IntegerField(_("number of messages"), default=0)
2017-01-21 02:42:06 +00:00
class Meta:
2018-10-04 19:29:19 +00:00
ordering = ["-_last_message__date"]
def __str__(self):
return self.title
def save(self, *args, **kwargs):
2024-06-27 12:46:43 +00:00
super().save(*args, **kwargs)
2017-06-12 07:58:24 +00:00
self.forum.set_topic_number() # Recompute the cached value
self.forum.set_last_message()
2017-01-21 03:19:15 +00:00
def get_absolute_url(self):
return reverse("forum:view_topic", kwargs={"topic_id": self.id})
def is_owned_by(self, user):
return self.forum.is_owned_by(user)
def can_be_edited_by(self, user):
2024-08-16 20:43:54 +00:00
return user.is_root or user.can_edit(self.forum)
def can_be_viewed_by(self, user):
2024-08-16 20:43:54 +00:00
return user.is_root or user.can_view(self.forum)
def get_first_unread_message(self, user: User) -> ForumMessage | None:
if not hasattr(user, "forum_infos"):
2017-03-12 19:24:16 +00:00
return None
return (
self.messages.exclude(readers=user)
.filter(date__gte=user.forum_infos.last_read_date)
.order_by("id")
.first()
)
2017-03-12 19:24:16 +00:00
@cached_property
def last_message(self):
return self._last_message
@cached_property
def title(self):
return self._title
2017-06-12 07:58:24 +00:00
2017-01-21 02:42:06 +00:00
class ForumMessage(models.Model):
2024-07-12 07:34:16 +00:00
"""A message in the forum (thx Cpt. Obvious.)."""
2018-10-04 19:29:19 +00:00
topic = models.ForeignKey(
ForumTopic, related_name="messages", on_delete=models.CASCADE
)
author = models.ForeignKey(
User, related_name="forum_messages", on_delete=models.CASCADE
)
2017-01-21 02:42:06 +00:00
title = models.CharField(_("title"), default="", max_length=64, blank=True)
message = models.TextField(_("message"), default="")
2018-10-04 19:29:19 +00:00
date = models.DateTimeField(_("date"), default=timezone.now)
readers = models.ManyToManyField(
User, related_name="read_messages", verbose_name=_("readers")
)
_deleted = models.BooleanField(_("is deleted"), default=False)
2017-01-21 02:42:06 +00:00
class Meta:
2018-10-04 19:29:19 +00:00
ordering = ["-date"]
2017-01-21 02:42:06 +00:00
2017-01-28 23:16:41 +00:00
def __str__(self):
return "%s (%s) - %s" % (self.id, self.author, self.title)
def save(self, *args, **kwargs):
2017-06-12 07:58:24 +00:00
self._deleted = self.is_deleted() # Recompute the cached value
2024-06-27 12:46:43 +00:00
super().save(*args, **kwargs)
if self.is_last_in_topic():
self.topic._last_message_id = self.id
if self.is_first_in_topic() and self.title:
self.topic._title = self.title
self.topic._message_number = self.topic.messages.count()
self.topic.save()
def get_absolute_url(self):
return reverse("forum:view_message", kwargs={"message_id": self.id})
def is_first_in_topic(self):
2018-10-04 19:29:19 +00:00
return bool(self.id == self.topic.messages.order_by("date").first().id)
def is_last_in_topic(self):
2018-10-04 19:29:19 +00:00
return bool(self.id == self.topic.messages.order_by("date").last().id)
2017-01-28 23:16:41 +00:00
def is_owned_by(self, user):
if user.is_anonymous:
return False
# Anyone can create a topic: it's better to
2018-10-04 19:29:19 +00:00
# check the rights at the forum level, since it's more controlled
2017-02-24 00:50:09 +00:00
return self.topic.forum.is_owned_by(user) or user.id == self.author.id
def can_be_edited_by(self, user):
2017-02-24 00:50:09 +00:00
return user.can_edit(self.topic.forum)
def can_be_viewed_by(self, user):
# No need to check the real rights since it's already done by the Topic view
# and it impacts performances too much
return not self._deleted
2017-02-24 00:50:05 +00:00
def can_be_moderated_by(self, user):
2024-08-16 20:43:54 +00:00
return self.topic.forum.is_owned_by(user) or user.id == self.author_id
2017-02-24 00:50:05 +00:00
def get_url(self):
2018-10-04 19:29:19 +00:00
return (
self.topic.get_absolute_url()
+ "?page="
+ str(self.get_page())
+ "#msg_"
+ str(self.id)
)
2017-03-12 19:24:16 +00:00
def get_page(self):
2018-10-04 19:29:19 +00:00
return (
int(
self.topic.messages.filter(id__lt=self.id).count()
/ settings.SITH_FORUM_PAGE_LENGTH
)
+ 1
)
2017-01-28 23:16:41 +00:00
def mark_as_read(self, user):
if user.is_anonymous:
return
if not self.is_read(user):
self.readers.add(user)
2017-01-28 23:16:41 +00:00
2017-02-24 00:50:09 +00:00
def is_read(self, user):
2018-10-04 19:29:19 +00:00
return (self.date < user.forum_infos.last_read_date) or (
user in self.readers.all()
)
2017-02-24 00:50:09 +00:00
2017-02-24 00:50:05 +00:00
def is_deleted(self):
2024-07-10 08:17:08 +00:00
if self._state.adding:
2024-07-09 14:55:07 +00:00
return False
2018-10-04 19:29:19 +00:00
meta = self.metas.exclude(action="EDIT").order_by("-date").first()
2017-02-24 00:50:05 +00:00
if meta:
return meta.action == "DELETE"
return False
2017-06-12 07:58:24 +00:00
2017-02-24 00:50:05 +00:00
MESSAGE_META_ACTIONS = [
2018-10-04 19:29:19 +00:00
("EDIT", _("Message edited by")),
("DELETE", _("Message deleted by")),
("UNDELETE", _("Message undeleted by")),
2017-06-12 07:58:24 +00:00
]
2017-02-24 00:50:05 +00:00
class ForumMessageMeta(models.Model):
user = models.ForeignKey(
User, related_name="forum_message_metas", on_delete=models.CASCADE
)
message = models.ForeignKey(
ForumMessage, related_name="metas", on_delete=models.CASCADE
)
2018-10-04 19:29:19 +00:00
date = models.DateTimeField(_("date"), default=timezone.now)
2017-02-24 00:50:05 +00:00
action = models.CharField(_("action"), choices=MESSAGE_META_ACTIONS, max_length=16)
def __str__(self):
return f"{self.user.nick_name} ({self.date})"
def save(self, *args, **kwargs):
2024-06-27 12:46:43 +00:00
super().save(*args, **kwargs)
self.message._deleted = self.message.is_deleted()
self.message.save()
2017-01-28 23:16:41 +00:00
class ForumUserInfo(models.Model):
2024-07-12 07:34:16 +00:00
"""The forum infos of a user.
2017-02-24 00:50:09 +00:00
This currently stores only the last date a user clicked "Mark all as read".
However, this can be extended with lot of user preferences dedicated to a
user, such as the favourite topics, the signature, and so on...
"""
2018-10-04 19:29:19 +00:00
user = models.OneToOneField(
User, related_name="_forum_infos", on_delete=models.CASCADE
)
2018-10-04 19:29:19 +00:00
last_read_date = models.DateTimeField(
_("last read date"),
default=datetime(
2024-07-18 15:33:14 +00:00
year=settings.SITH_SCHOOL_START_YEAR, month=1, day=1, tzinfo=tz.utc
2018-10-04 19:29:19 +00:00
),
)
2017-01-21 02:42:06 +00:00
def __str__(self):
return str(self.user)