2019-05-16 14:51:30 +00:00
|
|
|
#
|
|
|
|
# Copyright 2019
|
|
|
|
# - Sli <antoine@bartuccio.fr>
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import validators
|
2019-05-16 14:51:30 +00:00
|
|
|
from django.db import models
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.urls import reverse
|
2019-06-18 19:55:12 +00:00
|
|
|
from django.utils import timezone
|
2019-06-20 13:03:51 +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 _
|
2019-07-05 18:11:33 +00:00
|
|
|
from rest_framework import serializers
|
2019-06-15 12:17:39 +00:00
|
|
|
|
|
|
|
from core.models import User
|
2019-05-16 14:51:30 +00:00
|
|
|
|
|
|
|
# Create your models here.
|
|
|
|
|
|
|
|
|
|
|
|
class UV(models.Model):
|
|
|
|
"""
|
|
|
|
Contains infos about an UV (course)
|
|
|
|
"""
|
|
|
|
|
2019-06-15 12:17:39 +00:00
|
|
|
code = models.CharField(
|
|
|
|
_("code"),
|
|
|
|
max_length=10,
|
|
|
|
unique=True,
|
|
|
|
validators=[
|
|
|
|
validators.RegexValidator(
|
|
|
|
regex="([A-Z0-9]+)",
|
|
|
|
message=_(
|
|
|
|
"The code of an UV must only contains uppercase characters without accent and numbers"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
author = models.ForeignKey(
|
|
|
|
User,
|
2019-06-16 13:37:25 +00:00
|
|
|
related_name="uv_created",
|
2019-06-15 15:01:25 +00:00
|
|
|
verbose_name=_("author"),
|
2019-06-15 12:17:39 +00:00
|
|
|
null=False,
|
|
|
|
blank=False,
|
2019-10-05 17:05:56 +00:00
|
|
|
on_delete=models.CASCADE,
|
2019-06-15 12:17:39 +00:00
|
|
|
)
|
|
|
|
credit_type = models.CharField(
|
|
|
|
_("credit type"),
|
|
|
|
max_length=10,
|
|
|
|
choices=settings.SITH_PEDAGOGY_UV_TYPE,
|
|
|
|
default=settings.SITH_PEDAGOGY_UV_TYPE[0][0],
|
|
|
|
)
|
|
|
|
manager = models.CharField(_("uv manager"), max_length=300)
|
|
|
|
semester = models.CharField(
|
|
|
|
_("semester"),
|
2019-06-15 15:01:25 +00:00
|
|
|
max_length=20,
|
2019-06-15 12:17:39 +00:00
|
|
|
choices=settings.SITH_PEDAGOGY_UV_SEMESTER,
|
|
|
|
default=settings.SITH_PEDAGOGY_UV_SEMESTER[0][0],
|
|
|
|
)
|
|
|
|
language = models.CharField(
|
|
|
|
_("language"),
|
|
|
|
max_length=10,
|
|
|
|
choices=settings.SITH_PEDAGOGY_UV_LANGUAGE,
|
|
|
|
default=settings.SITH_PEDAGOGY_UV_LANGUAGE[0][0],
|
|
|
|
)
|
|
|
|
credits = models.IntegerField(
|
|
|
|
_("credits"),
|
|
|
|
validators=[validators.MinValueValidator(0)],
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
)
|
|
|
|
# Double star type not implemented yet
|
2019-06-18 08:56:05 +00:00
|
|
|
|
|
|
|
department = models.CharField(
|
|
|
|
_("departmenmt"),
|
|
|
|
max_length=10,
|
|
|
|
choices=settings.SITH_PROFILE_DEPARTMENTS,
|
|
|
|
default=settings.SITH_PROFILE_DEPARTMENTS[-1][0],
|
|
|
|
)
|
2019-06-15 12:17:39 +00:00
|
|
|
|
|
|
|
# All texts about the UV
|
|
|
|
title = models.CharField(_("title"), max_length=300)
|
|
|
|
manager = models.CharField(_("uv manager"), max_length=300)
|
|
|
|
objectives = models.TextField(_("objectives"))
|
|
|
|
program = models.TextField(_("program"))
|
|
|
|
skills = models.TextField(_("skills"))
|
2019-06-15 15:01:25 +00:00
|
|
|
key_concepts = models.TextField(_("key concepts"))
|
2019-06-15 12:17:39 +00:00
|
|
|
|
|
|
|
# Hours types CM, TD, TP, THE and TE
|
|
|
|
# Kind of dirty but I have nothing else in mind for now
|
|
|
|
hours_CM = models.IntegerField(
|
|
|
|
_("hours CM"),
|
|
|
|
validators=[validators.MinValueValidator(0)],
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
2019-06-15 15:01:25 +00:00
|
|
|
default=0,
|
2019-06-15 12:17:39 +00:00
|
|
|
)
|
|
|
|
hours_TD = models.IntegerField(
|
|
|
|
_("hours TD"),
|
|
|
|
validators=[validators.MinValueValidator(0)],
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
2019-06-15 15:01:25 +00:00
|
|
|
default=0,
|
2019-06-15 12:17:39 +00:00
|
|
|
)
|
|
|
|
hours_TP = models.IntegerField(
|
|
|
|
_("hours TP"),
|
|
|
|
validators=[validators.MinValueValidator(0)],
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
2019-06-15 15:01:25 +00:00
|
|
|
default=0,
|
2019-06-15 12:17:39 +00:00
|
|
|
)
|
|
|
|
hours_THE = models.IntegerField(
|
|
|
|
_("hours THE"),
|
|
|
|
validators=[validators.MinValueValidator(0)],
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
2019-06-15 15:01:25 +00:00
|
|
|
default=0,
|
2019-06-15 12:17:39 +00:00
|
|
|
)
|
|
|
|
hours_TE = models.IntegerField(
|
|
|
|
_("hours TE"),
|
|
|
|
validators=[validators.MinValueValidator(0)],
|
|
|
|
blank=False,
|
|
|
|
null=False,
|
2019-06-15 15:01:25 +00:00
|
|
|
default=0,
|
2019-06-15 12:17:39 +00:00
|
|
|
)
|
2019-05-16 14:51:30 +00:00
|
|
|
|
2024-06-27 13:48:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.code
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.id})
|
|
|
|
|
2019-06-16 15:02:45 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
2019-07-09 14:59:59 +00:00
|
|
|
Can be created by superuser, root or pedagogy admin user
|
2019-06-16 15:02:45 +00:00
|
|
|
"""
|
2023-05-02 10:36:59 +00:00
|
|
|
return user.is_in_group(pk=settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)
|
2019-06-16 15:02:45 +00:00
|
|
|
|
2019-07-09 14:59:59 +00:00
|
|
|
def can_be_viewed_by(self, user):
|
2019-06-20 13:03:51 +00:00
|
|
|
"""
|
2019-07-09 14:59:59 +00:00
|
|
|
Only visible by subscribers
|
2019-06-20 13:03:51 +00:00
|
|
|
"""
|
2019-07-09 14:59:59 +00:00
|
|
|
return user.is_subscribed
|
|
|
|
|
|
|
|
def __grade_average_generic(self, field):
|
|
|
|
comments = self.comments.filter(**{field + "__gte": 0})
|
|
|
|
if not comments.exists():
|
|
|
|
return -1
|
|
|
|
|
|
|
|
return int(sum(comments.values_list(field, flat=True)) / comments.count())
|
|
|
|
|
2019-09-04 18:49:17 +00:00
|
|
|
def has_user_already_commented(self, user):
|
|
|
|
"""
|
|
|
|
Help prevent multiples comments from the same user
|
|
|
|
This function checks that no other comment has been posted by a specified user
|
|
|
|
|
|
|
|
:param user: core.models.User
|
|
|
|
:return: if the user has already posted a comment on this UV
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
|
|
return self.comments.filter(author=user).exists()
|
|
|
|
|
2019-07-09 14:59:59 +00:00
|
|
|
@cached_property
|
|
|
|
def grade_global_average(self):
|
|
|
|
return self.__grade_average_generic("grade_global")
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def grade_utility_average(self):
|
|
|
|
return self.__grade_average_generic("grade_utility")
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def grade_interest_average(self):
|
|
|
|
return self.__grade_average_generic("grade_interest")
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def grade_teaching_average(self):
|
|
|
|
return self.__grade_average_generic("grade_teaching")
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def grade_work_load_average(self):
|
|
|
|
return self.__grade_average_generic("grade_work_load")
|
2019-06-20 13:03:51 +00:00
|
|
|
|
2019-07-09 14:59:59 +00:00
|
|
|
|
|
|
|
class UVComment(models.Model):
|
|
|
|
"""
|
|
|
|
A comment about an UV
|
|
|
|
"""
|
2019-06-16 16:26:30 +00:00
|
|
|
|
2019-06-16 13:37:25 +00:00
|
|
|
author = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="uv_comments",
|
|
|
|
verbose_name=_("author"),
|
|
|
|
null=False,
|
|
|
|
blank=False,
|
2019-10-05 17:05:56 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
uv = models.ForeignKey(
|
|
|
|
UV, related_name="comments", verbose_name=_("uv"), on_delete=models.CASCADE
|
2019-06-16 13:37:25 +00:00
|
|
|
)
|
2019-06-19 23:29:12 +00:00
|
|
|
comment = models.TextField(_("comment"), blank=True)
|
2019-06-16 13:37:25 +00:00
|
|
|
grade_global = models.IntegerField(
|
|
|
|
_("global grade"),
|
2019-06-17 16:12:21 +00:00
|
|
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
2019-06-16 13:37:25 +00:00
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
default=-1,
|
|
|
|
)
|
|
|
|
grade_utility = models.IntegerField(
|
|
|
|
_("utility grade"),
|
2019-06-17 16:12:21 +00:00
|
|
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
2019-06-16 13:37:25 +00:00
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
default=-1,
|
|
|
|
)
|
|
|
|
grade_interest = models.IntegerField(
|
|
|
|
_("interest grade"),
|
2019-06-17 16:12:21 +00:00
|
|
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
2019-06-16 13:37:25 +00:00
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
default=-1,
|
|
|
|
)
|
|
|
|
grade_teaching = models.IntegerField(
|
|
|
|
_("teaching grade"),
|
2019-06-17 16:12:21 +00:00
|
|
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
2019-06-16 13:37:25 +00:00
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
default=-1,
|
|
|
|
)
|
|
|
|
grade_work_load = models.IntegerField(
|
|
|
|
_("work load grade"),
|
2019-06-17 16:12:21 +00:00
|
|
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
2019-06-16 13:37:25 +00:00
|
|
|
blank=False,
|
|
|
|
null=False,
|
|
|
|
default=-1,
|
|
|
|
)
|
2019-06-18 19:55:12 +00:00
|
|
|
publish_date = models.DateTimeField(_("publish date"), blank=True)
|
|
|
|
|
2024-06-27 13:48:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.uv} - {self.author}"
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.publish_date is None:
|
|
|
|
self.publish_date = timezone.now()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2019-07-09 14:59:59 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Is owned by a pedagogy admin, a superuser or the author himself
|
|
|
|
"""
|
|
|
|
return self.author == user or user.is_owner(self.uv)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def is_reported(self):
|
|
|
|
"""
|
|
|
|
Return True if someone reported this UV
|
|
|
|
"""
|
|
|
|
return self.reports.exists()
|
|
|
|
|
2019-06-16 13:37:25 +00:00
|
|
|
|
|
|
|
class UVResult(models.Model):
|
|
|
|
"""
|
|
|
|
Results got to an UV
|
|
|
|
Views will be implemented after the first release
|
|
|
|
Will list every UV done by an user
|
|
|
|
Linked to user
|
|
|
|
uv
|
|
|
|
Contains a grade settings.SITH_PEDAGOGY_UV_RESULT_GRADE
|
|
|
|
a semester (P/A)20xx
|
|
|
|
"""
|
|
|
|
|
2019-10-05 17:05:56 +00:00
|
|
|
uv = models.ForeignKey(
|
|
|
|
UV, related_name="results", verbose_name=_("uv"), on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
user = models.ForeignKey(
|
|
|
|
User, related_name="uv_results", verbose_name=("user"), on_delete=models.CASCADE
|
|
|
|
)
|
2019-06-16 13:37:25 +00:00
|
|
|
grade = models.CharField(
|
|
|
|
_("grade"),
|
|
|
|
max_length=10,
|
|
|
|
choices=settings.SITH_PEDAGOGY_UV_RESULT_GRADE,
|
|
|
|
default=settings.SITH_PEDAGOGY_UV_RESULT_GRADE[0][0],
|
|
|
|
)
|
|
|
|
semester = models.CharField(
|
|
|
|
_("semester"),
|
|
|
|
max_length=5,
|
|
|
|
validators=[validators.RegexValidator("[AP][0-9]{3}")],
|
|
|
|
)
|
2019-05-16 14:51:30 +00:00
|
|
|
|
2024-06-27 13:48:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.user.username} ; {self.uv.code} ; {self.grade}"
|
|
|
|
|
2019-05-16 14:51:30 +00:00
|
|
|
|
|
|
|
class UVCommentReport(models.Model):
|
|
|
|
"""
|
|
|
|
Report an inapropriate comment
|
|
|
|
"""
|
|
|
|
|
2019-06-19 23:29:12 +00:00
|
|
|
comment = models.ForeignKey(
|
|
|
|
UVComment,
|
|
|
|
related_name="reports",
|
|
|
|
verbose_name=_("report"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
reporter = models.ForeignKey(
|
2019-10-05 17:05:56 +00:00
|
|
|
User,
|
|
|
|
related_name="reported_uv_comment",
|
|
|
|
verbose_name=_("reporter"),
|
|
|
|
on_delete=models.CASCADE,
|
2019-06-19 23:29:12 +00:00
|
|
|
)
|
|
|
|
reason = models.TextField(_("reason"))
|
2019-07-05 18:11:33 +00:00
|
|
|
|
2024-06-27 13:48:07 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.reporter.username} : {self.reason}"
|
|
|
|
|
2022-12-19 19:55:33 +00:00
|
|
|
@cached_property
|
|
|
|
def uv(self):
|
|
|
|
return self.comment.uv
|
|
|
|
|
2019-07-09 14:59:59 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Can be created by a pedagogy admin, a superuser or a subscriber
|
|
|
|
"""
|
|
|
|
return user.is_subscribed or user.is_owner(self.comment.uv)
|
|
|
|
|
2019-07-05 18:11:33 +00:00
|
|
|
|
|
|
|
# Custom serializers
|
|
|
|
|
|
|
|
|
|
|
|
class UVSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
Custom seralizer for UVs
|
|
|
|
Allow adding more informations like absolute_url
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UV
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
absolute_url = serializers.SerializerMethodField()
|
|
|
|
update_url = serializers.SerializerMethodField()
|
|
|
|
delete_url = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
def get_absolute_url(self, obj):
|
|
|
|
return obj.get_absolute_url()
|
|
|
|
|
|
|
|
def get_update_url(self, obj):
|
|
|
|
return reverse("pedagogy:uv_update", kwargs={"uv_id": obj.id})
|
|
|
|
|
|
|
|
def get_delete_url(self, obj):
|
|
|
|
return reverse("pedagogy:uv_delete", kwargs={"uv_id": obj.id})
|