2021-08-07 15:20:30 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
|
|
|
|
"""Evenements et logs divers
|
|
|
|
"""
|
2022-04-12 17:12:51 +02:00
|
|
|
import datetime
|
|
|
|
import re
|
|
|
|
|
|
|
|
from flask import g, url_for
|
|
|
|
from flask_login import current_user
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
from app import db
|
2022-04-12 17:12:51 +02:00
|
|
|
from app import email
|
|
|
|
from app import log
|
|
|
|
from app.auth.models import User
|
2024-07-08 23:13:45 +02:00
|
|
|
from app.models import ScoDocModel, SHORT_STR_LEN
|
2022-04-12 17:12:51 +02:00
|
|
|
import app.scodoc.sco_utils as scu
|
|
|
|
from app.scodoc import sco_preferences
|
2021-08-07 15:20:30 +02:00
|
|
|
|
|
|
|
|
2024-07-08 23:13:45 +02:00
|
|
|
class Scolog(ScoDocModel):
|
2021-08-07 15:20:30 +02:00
|
|
|
"""Log des actions (journal modif etudiants)"""
|
|
|
|
|
|
|
|
__tablename__ = "scolog"
|
|
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
date = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
|
2021-08-10 12:57:38 +02:00
|
|
|
method = db.Column(db.Text)
|
|
|
|
msg = db.Column(db.Text)
|
2021-08-07 15:20:30 +02:00
|
|
|
etudid = db.Column(db.Integer) # sans contrainte pour garder logs après suppression
|
2024-06-21 16:34:23 +02:00
|
|
|
authenticated_user = db.Column(db.Text) # user_name login, sans contrainte
|
2021-08-07 15:20:30 +02:00
|
|
|
# zope_remote_addr suppressed
|
|
|
|
|
2022-06-22 11:44:03 +02:00
|
|
|
@classmethod
|
|
|
|
def logdb(
|
|
|
|
cls, method: str = None, etudid: int = None, msg: str = None, commit=False
|
|
|
|
):
|
2024-07-08 23:13:45 +02:00
|
|
|
"""Add entry in student's log (replacement for old scolog.logdb).
|
|
|
|
Par défaut ne commite pas."""
|
2022-06-22 11:44:03 +02:00
|
|
|
entry = Scolog(
|
|
|
|
method=method,
|
|
|
|
msg=msg,
|
|
|
|
etudid=etudid,
|
|
|
|
authenticated_user=current_user.user_name,
|
|
|
|
)
|
|
|
|
db.session.add(entry)
|
|
|
|
if commit:
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-07-08 23:13:45 +02:00
|
|
|
def to_dict(self, convert_date=False) -> dict:
|
|
|
|
"convert to dict"
|
|
|
|
return {
|
|
|
|
"etudid": self.etudid,
|
|
|
|
"date": (
|
|
|
|
(self.date.strftime(scu.DATETIME_FMT) if convert_date else self.date)
|
|
|
|
if self.date
|
|
|
|
else ""
|
|
|
|
),
|
|
|
|
"_date_order": self.date.isoformat() if self.date else "",
|
|
|
|
"authenticated_user": self.authenticated_user or "",
|
|
|
|
"msg": self.msg or "",
|
|
|
|
"method": self.method or "",
|
|
|
|
}
|
|
|
|
|
2021-08-07 15:20:30 +02:00
|
|
|
|
2024-10-29 19:18:36 +01:00
|
|
|
class ScolarNews(ScoDocModel):
|
2021-08-07 15:20:30 +02:00
|
|
|
"""Nouvelles pour page d'accueil"""
|
|
|
|
|
2022-04-20 22:55:40 +02:00
|
|
|
NEWS_ABS = "ABS" # saisie absence
|
|
|
|
NEWS_APO = "APO" # changements de codes APO
|
|
|
|
NEWS_FORM = "FORM" # modification formation (object=formation_id)
|
2022-04-12 17:12:51 +02:00
|
|
|
NEWS_INSCR = "INSCR" # inscription d'étudiants (object=None ou formsemestre_id)
|
2023-06-23 10:37:32 +02:00
|
|
|
NEWS_JURY = "JURY" # saisie jury
|
2022-04-20 22:55:40 +02:00
|
|
|
NEWS_MISC = "MISC" # unused
|
2022-04-12 17:12:51 +02:00
|
|
|
NEWS_NOTE = "NOTES" # saisie note (object=moduleimpl_id)
|
|
|
|
NEWS_SEM = "SEM" # creation semestre (object=None)
|
2023-06-23 10:37:32 +02:00
|
|
|
|
2022-04-12 17:12:51 +02:00
|
|
|
NEWS_MAP = {
|
2022-04-20 22:55:40 +02:00
|
|
|
NEWS_ABS: "saisie absence",
|
|
|
|
NEWS_APO: "modif. code Apogée",
|
|
|
|
NEWS_FORM: "modification formation",
|
2022-04-12 17:12:51 +02:00
|
|
|
NEWS_INSCR: "inscription d'étudiants",
|
2023-06-23 10:37:32 +02:00
|
|
|
NEWS_JURY: "saisie jury",
|
2022-04-20 22:55:40 +02:00
|
|
|
NEWS_MISC: "opération", # unused
|
2022-04-12 17:12:51 +02:00
|
|
|
NEWS_NOTE: "saisie note",
|
|
|
|
NEWS_SEM: "création semestre",
|
|
|
|
}
|
|
|
|
NEWS_TYPES = list(NEWS_MAP.keys())
|
|
|
|
|
2021-08-07 15:20:30 +02:00
|
|
|
__tablename__ = "scolar_news"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2021-08-13 11:26:22 +02:00
|
|
|
dept_id = db.Column(db.Integer, db.ForeignKey("departement.id"), index=True)
|
2022-04-12 17:12:51 +02:00
|
|
|
date = db.Column(
|
|
|
|
db.DateTime(timezone=True), server_default=db.func.now(), index=True
|
|
|
|
)
|
2024-06-21 16:34:23 +02:00
|
|
|
authenticated_user = db.Column(
|
|
|
|
db.Text, index=True
|
|
|
|
) # user_name login, sans contrainte
|
2021-08-07 15:20:30 +02:00
|
|
|
# type in 'INSCR', 'NOTES', 'FORM', 'SEM', 'MISC'
|
2022-04-12 17:12:51 +02:00
|
|
|
type = db.Column(db.String(SHORT_STR_LEN), index=True)
|
|
|
|
object = db.Column(
|
|
|
|
db.Integer, index=True
|
|
|
|
) # moduleimpl_id, formation_id, formsemestre_id
|
2021-08-07 15:20:30 +02:00
|
|
|
text = db.Column(db.Text)
|
|
|
|
url = db.Column(db.Text)
|
2022-04-12 17:12:51 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
f"<{self.__class__.__name__}(id={self.id}, date='{self.date.isoformat()}')>"
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2024-07-22 16:53:50 +02:00
|
|
|
"exemple: 'Notes dans Stage (S3 FI) par Aurélie Dupont'"
|
2022-04-12 17:12:51 +02:00
|
|
|
formsemestre = self.get_news_formsemestre()
|
|
|
|
user = User.query.filter_by(user_name=self.authenticated_user).first()
|
|
|
|
|
|
|
|
sem_text = (
|
|
|
|
f"""(<a href="{url_for('notes.formsemestre_status', scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre.id)
|
|
|
|
}">{formsemestre.sem_modalite()}</a>)"""
|
|
|
|
if formsemestre
|
|
|
|
else ""
|
|
|
|
)
|
|
|
|
author = f"par {user.get_nomcomplet()}" if user else ""
|
|
|
|
return f"{self.text} {sem_text} {author}"
|
|
|
|
|
|
|
|
def formatted_date(self) -> str:
|
|
|
|
"06 Avr 14h23"
|
|
|
|
mois = scu.MONTH_NAMES_ABBREV[self.date.month - 1]
|
|
|
|
return f"{self.date.day} {mois} {self.date.hour:02d}h{self.date.minute:02d}"
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
"date": {
|
|
|
|
"display": self.date.strftime("%d/%m/%Y %H:%M"),
|
|
|
|
"timestamp": self.date.timestamp(),
|
|
|
|
},
|
|
|
|
"type": self.NEWS_MAP.get(self.type, "?"),
|
|
|
|
"authenticated_user": self.authenticated_user,
|
|
|
|
"text": self.text,
|
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
2022-04-13 13:53:53 +02:00
|
|
|
def last_news(cls, n=1, dept_id=None, filter_dept=True) -> list:
|
2022-04-12 17:12:51 +02:00
|
|
|
"The most recent n news. Returns list of ScolarNews instances."
|
2022-04-13 13:53:53 +02:00
|
|
|
query = cls.query
|
|
|
|
if filter_dept:
|
|
|
|
if dept_id is None:
|
|
|
|
dept_id = g.scodoc_dept_id
|
|
|
|
query = query.filter_by(dept_id=dept_id)
|
|
|
|
|
|
|
|
return query.order_by(cls.date.desc()).limit(n).all()
|
2022-04-12 17:12:51 +02:00
|
|
|
|
|
|
|
@classmethod
|
2023-11-22 23:31:16 +01:00
|
|
|
def add(cls, typ, obj=None, text="", url=None, max_frequency=600, dept_id=None):
|
2022-04-12 17:12:51 +02:00
|
|
|
"""Enregistre une nouvelle
|
|
|
|
Si max_frequency, ne génère pas 2 nouvelles "identiques"
|
2023-06-23 10:37:32 +02:00
|
|
|
à moins de max_frequency secondes d'intervalle (10 minutes par défaut).
|
2022-04-12 17:12:51 +02:00
|
|
|
Deux nouvelles sont considérées comme "identiques" si elles ont
|
|
|
|
même (obj, typ, user).
|
|
|
|
La nouvelle enregistrée est aussi envoyée par mail.
|
|
|
|
"""
|
2023-11-22 23:31:16 +01:00
|
|
|
dept_id = dept_id if dept_id is not None else g.scodoc_dept_id
|
2022-04-12 17:12:51 +02:00
|
|
|
if max_frequency:
|
|
|
|
last_news = (
|
|
|
|
cls.query.filter_by(
|
2023-11-22 23:31:16 +01:00
|
|
|
dept_id=dept_id,
|
2022-04-12 17:12:51 +02:00
|
|
|
authenticated_user=current_user.user_name,
|
|
|
|
type=typ,
|
|
|
|
object=obj,
|
|
|
|
)
|
|
|
|
.order_by(cls.date.desc())
|
|
|
|
.limit(1)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if last_news:
|
|
|
|
now = datetime.datetime.now(tz=last_news.date.tzinfo)
|
|
|
|
if (now - last_news.date) < datetime.timedelta(seconds=max_frequency):
|
2023-06-23 10:37:32 +02:00
|
|
|
# pas de nouvel event, mais met à jour l'heure
|
|
|
|
last_news.date = datetime.datetime.now()
|
|
|
|
db.session.add(last_news)
|
|
|
|
db.session.commit()
|
2022-04-12 17:12:51 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
news = ScolarNews(
|
2023-11-22 23:31:16 +01:00
|
|
|
dept_id=dept_id,
|
2022-04-12 17:12:51 +02:00
|
|
|
authenticated_user=current_user.user_name,
|
|
|
|
type=typ,
|
|
|
|
object=obj,
|
|
|
|
text=text,
|
|
|
|
url=url,
|
|
|
|
)
|
|
|
|
db.session.add(news)
|
|
|
|
db.session.commit()
|
|
|
|
log(f"news: {news}")
|
|
|
|
news.notify_by_mail()
|
|
|
|
|
2022-10-01 10:39:46 +02:00
|
|
|
def get_news_formsemestre(self) -> "FormSemestre":
|
2022-04-12 17:12:51 +02:00
|
|
|
"""formsemestre concerné par la nouvelle
|
|
|
|
None si inexistant
|
|
|
|
"""
|
2022-10-01 10:39:46 +02:00
|
|
|
from app.models.formsemestre import FormSemestre
|
2023-12-10 20:59:32 +01:00
|
|
|
from app.models.moduleimpls import ModuleImpl
|
2022-10-01 10:39:46 +02:00
|
|
|
|
2022-04-12 17:12:51 +02:00
|
|
|
formsemestre_id = None
|
|
|
|
if self.type == self.NEWS_INSCR:
|
|
|
|
formsemestre_id = self.object
|
|
|
|
elif self.type == self.NEWS_NOTE:
|
|
|
|
moduleimpl_id = self.object
|
|
|
|
if moduleimpl_id:
|
2023-07-11 06:57:38 +02:00
|
|
|
modimpl = db.session.get(ModuleImpl, moduleimpl_id)
|
2022-04-12 17:12:51 +02:00
|
|
|
if modimpl is None:
|
|
|
|
return None # module does not exists anymore
|
|
|
|
formsemestre_id = modimpl.formsemestre_id
|
|
|
|
|
|
|
|
if not formsemestre_id:
|
|
|
|
return None
|
2023-07-11 06:57:38 +02:00
|
|
|
formsemestre = db.session.get(FormSemestre, formsemestre_id)
|
2022-04-12 17:12:51 +02:00
|
|
|
return formsemestre
|
|
|
|
|
|
|
|
def notify_by_mail(self):
|
|
|
|
"""Notify by email"""
|
|
|
|
formsemestre = self.get_news_formsemestre()
|
|
|
|
|
|
|
|
prefs = sco_preferences.SemPreferences(
|
|
|
|
formsemestre_id=formsemestre.id if formsemestre else None
|
|
|
|
)
|
|
|
|
destinations = prefs["emails_notifications"] or ""
|
|
|
|
destinations = [x.strip() for x in destinations.split(",")]
|
|
|
|
destinations = [x for x in destinations if x]
|
|
|
|
if not destinations:
|
|
|
|
return
|
|
|
|
#
|
|
|
|
txt = self.text
|
|
|
|
if formsemestre:
|
|
|
|
txt += f"""\n\nSemestre {formsemestre.titre_mois()}\n\n"""
|
|
|
|
txt += f"""<a href="{url_for("notes.formsemestre_status", _external=True,
|
|
|
|
scodoc_dept=g.scodoc_dept, formsemestre_id=formsemestre.id)
|
|
|
|
}">{formsemestre.sem_modalite()}</a>
|
|
|
|
"""
|
|
|
|
user = User.query.filter_by(user_name=self.authenticated_user).first()
|
|
|
|
if user:
|
|
|
|
txt += f"\n\nEffectué par: {user.get_nomcomplet()}\n"
|
|
|
|
|
|
|
|
txt = (
|
|
|
|
"\n"
|
|
|
|
+ txt
|
|
|
|
+ """\n
|
|
|
|
--- Ceci est un message de notification automatique issu de ScoDoc
|
|
|
|
--- vous recevez ce message car votre adresse est indiquée dans les paramètres de ScoDoc.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
# Transforme les URL en URL absolues
|
2024-04-08 18:56:43 +02:00
|
|
|
base = url_for("scolar.index_html", scodoc_dept=g.scodoc_dept)[
|
|
|
|
: -len("/index_html")
|
|
|
|
]
|
2022-04-12 17:12:51 +02:00
|
|
|
txt = re.sub('href=/.*?"', 'href="' + base + "/", txt)
|
|
|
|
|
|
|
|
# Transforme les liens HTML en texte brut: '<a href="url">texte</a>' devient 'texte: url'
|
|
|
|
# (si on veut des messages non html)
|
|
|
|
txt = re.sub(r'<a.*?href\s*=\s*"(.*?)".*?>(.*?)</a>', r"\2: \1", txt)
|
|
|
|
|
|
|
|
subject = "[ScoDoc] " + self.NEWS_MAP.get(self.type, "?")
|
2023-02-28 19:43:48 +01:00
|
|
|
sender = email.get_from_addr()
|
2022-04-12 17:12:51 +02:00
|
|
|
email.send_email(subject, sender, destinations, txt)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def scolar_news_summary_html(cls, n=5) -> str:
|
|
|
|
"""News summary, formated in HTML"""
|
|
|
|
news_list = cls.last_news(n=n)
|
|
|
|
if not news_list:
|
|
|
|
return ""
|
2024-03-21 12:06:34 +01:00
|
|
|
dept_news_url = url_for("scolar.dept_news", scodoc_dept=g.scodoc_dept)
|
2022-04-12 17:12:51 +02:00
|
|
|
H = [
|
2024-08-23 08:39:53 +02:00
|
|
|
f"""<div class="scobox news" desktop="true"><div class="scobox-title" desktop="true"><a href="{
|
2024-03-21 12:06:34 +01:00
|
|
|
dept_news_url
|
2022-04-12 17:12:51 +02:00
|
|
|
}">Dernières opérations</a>
|
2024-08-23 08:39:53 +02:00
|
|
|
</div><ul class="newslist">"""
|
2022-04-12 17:12:51 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
for news in news_list:
|
|
|
|
H.append(
|
|
|
|
f"""<li class="newslist"><span class="newsdate">{news.formatted_date()}</span><span
|
|
|
|
class="newstext">{news}</span></li>"""
|
|
|
|
)
|
2024-03-21 12:06:34 +01:00
|
|
|
H.append(
|
|
|
|
f"""<li class="newslist">
|
|
|
|
<span class="newstext"><a href="{dept_news_url}" class="stdlink">...</a>
|
|
|
|
</span>
|
2024-08-23 08:39:53 +02:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<ul class="newslist" mobile="true" style="margin-bottom: 0px;">
|
|
|
|
<li><a href="{dept_news_url}" class="stdlink">Dernières opérations</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2024-08-16 01:11:06 +02:00
|
|
|
"""
|
|
|
|
)
|
2022-04-12 17:12:51 +02:00
|
|
|
|
|
|
|
# Informations générales
|
|
|
|
H.append(
|
2024-08-16 01:11:06 +02:00
|
|
|
f"""<div desktop="true">
|
2024-03-21 12:06:34 +01:00
|
|
|
Pour en savoir plus sur ScoDoc voir
|
|
|
|
<a class="stdlink" href="{scu.SCO_ANNONCES_WEBSITE}">scodoc.org</a>
|
2022-04-12 17:12:51 +02:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
return "\n".join(H)
|