2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
2017-12-16 19:48:18 +00:00
|
|
|
# - Sli <antoine@bartuccio.fr>
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-01-10 17:30:27 +00:00
|
|
|
from django.shortcuts import render
|
2017-01-03 15:50:53 +00:00
|
|
|
from django.db import models, transaction
|
2017-09-06 11:16:28 +00:00
|
|
|
from django.db.models import Q
|
2022-08-03 22:26:43 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2017-09-06 11:16:28 +00:00
|
|
|
from django.utils import timezone
|
2019-10-05 17:05:56 +00:00
|
|
|
from django.urls import reverse
|
2016-12-21 01:38:21 +00:00
|
|
|
from django.conf import settings
|
2022-08-03 22:26:43 +00:00
|
|
|
from django.templatetags.static import static
|
2017-01-11 00:34:16 +00:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2017-12-16 19:48:18 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-12-21 01:38:21 +00:00
|
|
|
|
2017-10-17 13:13:29 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2023-03-30 12:38:40 +00:00
|
|
|
from core import utils
|
2017-10-17 13:13:29 +00:00
|
|
|
from core.models import User, Preferences, RealGroup, Notification, SithFile
|
2016-12-23 02:02:46 +00:00
|
|
|
from club.models import Club
|
2017-06-12 06:59:03 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
|
2016-12-21 01:38:21 +00:00
|
|
|
class Sith(models.Model):
|
2016-12-23 02:02:46 +00:00
|
|
|
"""A one instance class storing all the modifiable infos"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-12-21 01:38:21 +00:00
|
|
|
alert_msg = models.TextField(_("alert message"), default="", blank=True)
|
|
|
|
info_msg = models.TextField(_("info message"), default="", blank=True)
|
2017-01-02 23:07:45 +00:00
|
|
|
weekmail_destinations = models.TextField(_("weekmail destinations"), default="")
|
2023-03-30 12:38:40 +00:00
|
|
|
version = utils.get_git_revision_short_hash()
|
2016-12-21 01:38:21 +00:00
|
|
|
|
|
|
|
def is_owned_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
|
|
|
return user.is_com_admin
|
2016-12-21 01:38:21 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "⛩ Sith ⛩"
|
|
|
|
|
2017-06-12 06:59:03 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
NEWS_TYPES = [
|
2018-10-04 19:29:19 +00:00
|
|
|
("NOTICE", _("Notice")),
|
|
|
|
("EVENT", _("Event")),
|
|
|
|
("WEEKLY", _("Weekly")),
|
|
|
|
("CALL", _("Call")),
|
2017-06-12 06:59:03 +00:00
|
|
|
]
|
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
|
|
|
|
class News(models.Model):
|
|
|
|
"""The news class"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
title = models.CharField(_("title"), max_length=64)
|
|
|
|
summary = models.TextField(_("summary"))
|
|
|
|
content = models.TextField(_("content"))
|
2018-10-04 19:29:19 +00:00
|
|
|
type = models.CharField(
|
|
|
|
_("type"), max_length=16, choices=NEWS_TYPES, default="EVENT"
|
|
|
|
)
|
2019-10-05 17:05:56 +00:00
|
|
|
club = models.ForeignKey(
|
|
|
|
Club, related_name="news", verbose_name=_("club"), on_delete=models.CASCADE
|
|
|
|
)
|
2018-10-04 19:29:19 +00:00
|
|
|
author = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
related_name="owned_news",
|
|
|
|
verbose_name=_("author"),
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2016-12-23 02:02:46 +00:00
|
|
|
is_moderated = models.BooleanField(_("is moderated"), default=False)
|
2018-10-04 19:29:19 +00:00
|
|
|
moderator = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
related_name="moderated_news",
|
|
|
|
verbose_name=_("moderator"),
|
|
|
|
null=True,
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2016-12-23 17:40:12 +00:00
|
|
|
|
|
|
|
def is_owned_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
|
|
|
return user.is_com_admin or user == self.author
|
2016-12-23 17:40:12 +00:00
|
|
|
|
|
|
|
def can_be_edited_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
return user.is_com_admin
|
2016-12-23 17:40:12 +00:00
|
|
|
|
|
|
|
def can_be_viewed_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
return self.is_moderated or user.is_com_admin
|
2016-12-23 02:02:46 +00:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("com:news_detail", kwargs={"news_id": self.id})
|
2016-12-23 02:02:46 +00:00
|
|
|
|
2017-09-12 14:40:28 +00:00
|
|
|
def get_full_url(self):
|
2017-09-17 13:04:30 +00:00
|
|
|
return "https://%s%s" % (settings.SITH_URL, self.get_absolute_url())
|
2017-09-12 14:40:28 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s: %s" % (self.type, self.title)
|
|
|
|
|
2017-09-06 11:16:28 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super(News, self).save(*args, **kwargs)
|
2018-10-04 19:29:19 +00:00
|
|
|
for u in (
|
|
|
|
RealGroup.objects.filter(id=settings.SITH_GROUP_COM_ADMIN_ID)
|
|
|
|
.first()
|
|
|
|
.users.all()
|
|
|
|
):
|
|
|
|
Notification(
|
|
|
|
user=u,
|
|
|
|
url=reverse("com:news_admin_list"),
|
|
|
|
type="NEWS_MODERATION",
|
|
|
|
param="1",
|
|
|
|
).save()
|
|
|
|
|
2017-09-06 11:16:28 +00:00
|
|
|
|
|
|
|
def news_notification_callback(notif):
|
2018-10-04 19:29:19 +00:00
|
|
|
count = (
|
|
|
|
News.objects.filter(
|
|
|
|
Q(dates__start_date__gt=timezone.now(), is_moderated=False)
|
|
|
|
| Q(type="NOTICE", is_moderated=False)
|
|
|
|
)
|
|
|
|
.distinct()
|
|
|
|
.count()
|
|
|
|
)
|
2017-09-06 11:16:28 +00:00
|
|
|
if count:
|
|
|
|
notif.viewed = False
|
|
|
|
notif.param = "%s" % count
|
|
|
|
notif.date = timezone.now()
|
|
|
|
else:
|
|
|
|
notif.viewed = True
|
2017-06-12 06:59:03 +00:00
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2016-12-23 02:02:46 +00:00
|
|
|
class NewsDate(models.Model):
|
|
|
|
"""
|
|
|
|
A date class, useful for weekly events, or for events that just have no date
|
|
|
|
|
|
|
|
This class allows more flexibilty managing the dates related to a news, particularly when this news is weekly, since
|
|
|
|
we don't have to make copies
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2019-10-05 17:05:56 +00:00
|
|
|
news = models.ForeignKey(
|
|
|
|
News,
|
|
|
|
related_name="dates",
|
|
|
|
verbose_name=_("news_date"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2018-10-04 19:29:19 +00:00
|
|
|
start_date = models.DateTimeField(_("start_date"), null=True, blank=True)
|
|
|
|
end_date = models.DateTimeField(_("end_date"), null=True, blank=True)
|
2016-12-23 02:02:46 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s: %s - %s" % (self.news.title, self.start_date, self.end_date)
|
2017-01-02 23:07:45 +00:00
|
|
|
|
2017-06-12 06:59:03 +00:00
|
|
|
|
2017-01-02 23:07:45 +00:00
|
|
|
class Weekmail(models.Model):
|
|
|
|
"""
|
|
|
|
The weekmail class
|
2019-08-26 18:30:05 +00:00
|
|
|
|
|
|
|
:ivar title: Title of the weekmail
|
|
|
|
:ivar intro: Introduction of the weekmail
|
|
|
|
:ivar joke: Joke of the week
|
|
|
|
:ivar protip: Tip of the week
|
|
|
|
:ivar conclusion: Conclusion of the weekmail
|
|
|
|
:ivar sent: Track if the weekmail has been sent
|
2017-01-02 23:07:45 +00:00
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2017-01-03 15:50:53 +00:00
|
|
|
title = models.CharField(_("title"), max_length=64, blank=True)
|
2017-01-02 23:07:45 +00:00
|
|
|
intro = models.TextField(_("intro"), blank=True)
|
|
|
|
joke = models.TextField(_("joke"), blank=True)
|
|
|
|
protip = models.TextField(_("protip"), blank=True)
|
|
|
|
conclusion = models.TextField(_("conclusion"), blank=True)
|
|
|
|
sent = models.BooleanField(_("sent"), default=False)
|
|
|
|
|
|
|
|
class Meta:
|
2018-10-04 19:29:19 +00:00
|
|
|
ordering = ["-id"]
|
2017-01-02 23:07:45 +00:00
|
|
|
|
|
|
|
def send(self):
|
2019-08-26 18:30:05 +00:00
|
|
|
"""
|
|
|
|
Send the weekmail to all users with the receive weekmail option opt-in.
|
|
|
|
Also send the weekmail to the mailing list in settings.SITH_COM_EMAIL.
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
dest = [
|
|
|
|
i[0]
|
|
|
|
for i in Preferences.objects.filter(receive_weekmail=True).values_list(
|
|
|
|
"user__email"
|
|
|
|
)
|
|
|
|
]
|
2017-01-03 15:50:53 +00:00
|
|
|
with transaction.atomic():
|
2017-01-11 00:34:16 +00:00
|
|
|
email = EmailMultiAlternatives(
|
2017-06-12 06:59:03 +00:00
|
|
|
subject=self.title,
|
|
|
|
body=self.render_text(),
|
|
|
|
from_email=settings.SITH_COM_EMAIL,
|
2018-10-04 19:29:19 +00:00
|
|
|
to=Sith.objects.first().weekmail_destinations.split(" "),
|
2017-06-12 06:59:03 +00:00
|
|
|
bcc=dest,
|
|
|
|
)
|
2017-01-11 00:34:16 +00:00
|
|
|
email.attach_alternative(self.render_html(), "text/html")
|
2017-01-10 17:30:27 +00:00
|
|
|
email.send()
|
2017-01-03 15:50:53 +00:00
|
|
|
self.sent = True
|
|
|
|
self.save()
|
|
|
|
Weekmail().save()
|
2017-01-02 23:07:45 +00:00
|
|
|
|
2017-01-11 00:34:16 +00:00
|
|
|
def render_text(self):
|
2019-08-26 18:30:05 +00:00
|
|
|
"""
|
|
|
|
Renders a pure text version of the mail for readers without HTML support.
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
return render(
|
|
|
|
None, "com/weekmail_renderer_text.jinja", context={"weekmail": self}
|
|
|
|
).content.decode("utf-8")
|
2017-01-10 17:30:27 +00:00
|
|
|
|
2017-01-11 00:34:16 +00:00
|
|
|
def render_html(self):
|
2019-08-26 18:30:05 +00:00
|
|
|
"""
|
|
|
|
Renders an HTML version of the mail with images and fancy CSS.
|
|
|
|
"""
|
2018-10-04 19:29:19 +00:00
|
|
|
return render(
|
|
|
|
None, "com/weekmail_renderer_html.jinja", context={"weekmail": self}
|
|
|
|
).content.decode("utf-8")
|
2017-01-11 00:34:16 +00:00
|
|
|
|
2017-04-27 12:39:25 +00:00
|
|
|
def get_banner(self):
|
2019-08-26 18:30:05 +00:00
|
|
|
"""
|
|
|
|
Return an absolute link to the banner.
|
|
|
|
"""
|
2022-06-01 20:46:12 +00:00
|
|
|
return (
|
2022-09-25 19:44:56 +00:00
|
|
|
"http://" + settings.SITH_URL + static("com/img/weekmail_bannerV2P22.png")
|
2022-06-01 20:46:12 +00:00
|
|
|
)
|
2018-03-23 21:44:18 +00:00
|
|
|
|
|
|
|
def get_footer(self):
|
2019-08-26 18:30:05 +00:00
|
|
|
"""
|
|
|
|
Return an absolute link to the footer.
|
|
|
|
"""
|
2022-06-01 20:40:52 +00:00
|
|
|
return "http://" + settings.SITH_URL + static("com/img/weekmail_footerP22.png")
|
2017-04-27 12:39:25 +00:00
|
|
|
|
2017-01-11 00:34:16 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "Weekmail %s (sent: %s) - %s" % (self.id, self.sent, self.title)
|
|
|
|
|
2017-01-15 22:09:30 +00:00
|
|
|
def is_owned_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
|
|
|
return user.is_com_admin
|
2017-01-15 22:09:30 +00:00
|
|
|
|
2017-06-12 06:59:03 +00:00
|
|
|
|
2017-01-02 23:07:45 +00:00
|
|
|
class WeekmailArticle(models.Model):
|
2018-10-04 19:29:19 +00:00
|
|
|
weekmail = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
Weekmail,
|
|
|
|
related_name="articles",
|
|
|
|
verbose_name=_("weekmail"),
|
|
|
|
null=True,
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2017-01-02 23:07:45 +00:00
|
|
|
title = models.CharField(_("title"), max_length=64)
|
|
|
|
content = models.TextField(_("content"))
|
2018-10-04 19:29:19 +00:00
|
|
|
author = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
related_name="owned_weekmail_articles",
|
|
|
|
verbose_name=_("author"),
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
club = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
Club,
|
|
|
|
related_name="weekmail_articles",
|
|
|
|
verbose_name=_("club"),
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
|
|
|
rank = models.IntegerField(_("rank"), default=-1)
|
2017-01-02 23:07:45 +00:00
|
|
|
|
2017-01-15 22:09:30 +00:00
|
|
|
def is_owned_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
|
|
|
return user.is_com_admin
|
2017-01-15 22:09:30 +00:00
|
|
|
|
2017-03-24 08:19:15 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s - %s (%s)" % (self.title, self.author, self.club)
|
2017-10-17 13:13:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Screen(models.Model):
|
|
|
|
name = models.CharField(_("name"), max_length=128)
|
|
|
|
|
|
|
|
def active_posters(self):
|
|
|
|
now = timezone.now()
|
2018-10-04 19:29:19 +00:00
|
|
|
return self.posters.filter(is_moderated=True, date_begin__lte=now).filter(
|
|
|
|
Q(date_end__isnull=True) | Q(date_end__gte=now)
|
|
|
|
)
|
2017-10-17 13:13:29 +00:00
|
|
|
|
|
|
|
def is_owned_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
|
|
|
return user.is_com_admin
|
2017-10-17 13:13:29 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-11-01 17:12:33 +00:00
|
|
|
return "%s" % (self.name)
|
2017-10-17 13:13:29 +00:00
|
|
|
|
2017-12-16 19:48:18 +00:00
|
|
|
|
2017-10-17 13:13:29 +00:00
|
|
|
class Poster(models.Model):
|
2018-10-04 19:29:19 +00:00
|
|
|
name = models.CharField(
|
|
|
|
_("name"), blank=False, null=False, max_length=128, default=""
|
|
|
|
)
|
2017-10-17 13:13:29 +00:00
|
|
|
file = models.ImageField(_("file"), null=False, upload_to="com/posters")
|
2018-10-04 19:29:19 +00:00
|
|
|
club = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
Club,
|
|
|
|
related_name="posters",
|
|
|
|
verbose_name=_("club"),
|
|
|
|
null=False,
|
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2017-10-17 13:13:29 +00:00
|
|
|
screens = models.ManyToManyField(Screen, related_name="posters")
|
|
|
|
date_begin = models.DateTimeField(blank=False, null=False, default=timezone.now)
|
|
|
|
date_end = models.DateTimeField(blank=True, null=True)
|
2018-10-04 19:29:19 +00:00
|
|
|
display_time = models.IntegerField(
|
|
|
|
_("display time"), blank=False, null=False, default=15
|
|
|
|
)
|
2017-10-17 13:13:29 +00:00
|
|
|
is_moderated = models.BooleanField(_("is moderated"), default=False)
|
2018-10-04 19:29:19 +00:00
|
|
|
moderator = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="moderated_posters",
|
|
|
|
verbose_name=_("moderator"),
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
2019-10-05 17:05:56 +00:00
|
|
|
on_delete=models.CASCADE,
|
2018-10-04 19:29:19 +00:00
|
|
|
)
|
2017-10-17 13:13:29 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2017-12-16 19:48:18 +00:00
|
|
|
if not self.is_moderated:
|
2018-10-04 19:29:19 +00:00
|
|
|
for u in (
|
|
|
|
RealGroup.objects.filter(id=settings.SITH_GROUP_COM_ADMIN_ID)
|
|
|
|
.first()
|
|
|
|
.users.all()
|
|
|
|
):
|
|
|
|
Notification(
|
|
|
|
user=u,
|
|
|
|
url=reverse("com:poster_moderate_list"),
|
|
|
|
type="POSTER_MODERATION",
|
|
|
|
).save()
|
2017-10-17 13:13:29 +00:00
|
|
|
return super(Poster, self).save(*args, **kwargs)
|
|
|
|
|
2017-12-22 11:53:43 +00:00
|
|
|
def clean(self, *args, **kwargs):
|
|
|
|
if self.date_end and self.date_begin > self.date_end:
|
|
|
|
raise ValidationError(_("Begin date should be before end date"))
|
|
|
|
|
2017-10-17 13:13:29 +00:00
|
|
|
def is_owned_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
if user.is_anonymous:
|
|
|
|
return False
|
|
|
|
return user.is_com_admin or len(user.clubs_with_rights) > 0
|
2017-10-17 13:13:29 +00:00
|
|
|
|
2017-10-23 07:30:26 +00:00
|
|
|
def can_be_moderated_by(self, user):
|
2023-05-02 10:36:59 +00:00
|
|
|
return user.is_com_admin
|
2017-10-17 13:13:29 +00:00
|
|
|
|
2017-12-16 19:48:18 +00:00
|
|
|
def get_display_name(self):
|
|
|
|
return self.club.get_display_name()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def page(self):
|
|
|
|
return self.club.page
|
|
|
|
|
2017-10-17 13:13:29 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|