mirror of
https://github.com/ae-utbm/sith.git
synced 2025-12-21 07:13:21 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7279dfd8a | ||
|
|
5b6f5c828a | ||
|
|
26f2b532fc | ||
|
|
3cb3af7553 |
@@ -203,7 +203,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<i class="fa-solid fa-graduation-cap fa-xl"></i>
|
<i class="fa-solid fa-graduation-cap fa-xl"></i>
|
||||||
<a href="{{ url("pedagogy:guide") }}">{% trans %}UE Guide{% endtrans %}</a>
|
<a href="{{ url("pedagogy:guide") }}">{% trans %}UV Guide{% endtrans %}</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<i class="fa-solid fa-calendar-days fa-xl"></i>
|
<i class="fa-solid fa-calendar-days fa-xl"></i>
|
||||||
|
|||||||
@@ -98,9 +98,9 @@ class PageAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
@admin.register(SithFile)
|
@admin.register(SithFile)
|
||||||
class SithFileAdmin(admin.ModelAdmin):
|
class SithFileAdmin(admin.ModelAdmin):
|
||||||
list_display = ("name", "owner", "size", "date", "is_in_sas")
|
list_display = ("name", "owner", "size", "date")
|
||||||
autocomplete_fields = ("parent", "owner", "moderator")
|
autocomplete_fields = ("parent", "owner", "moderator")
|
||||||
search_fields = ("name", "parent__name")
|
search_fields = ("name",)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(OperationLog)
|
@admin.register(OperationLog)
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ class SithFileController(ControllerBase):
|
|||||||
)
|
)
|
||||||
@paginate(PageNumberPaginationExtra, page_size=50)
|
@paginate(PageNumberPaginationExtra, page_size=50)
|
||||||
def search_files(self, search: Annotated[str, MinLen(1)]):
|
def search_files(self, search: Annotated[str, MinLen(1)]):
|
||||||
return SithFile.objects.filter(is_in_sas=False).filter(name__icontains=search)
|
return SithFile.objects.filter(name__icontains=search)
|
||||||
|
|
||||||
|
|
||||||
@api_controller("/group")
|
@api_controller("/group")
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ from core.utils import resize_image
|
|||||||
from counter.models import Counter, Product, ProductType, ReturnableProduct, StudentCard
|
from counter.models import Counter, Product, ProductType, ReturnableProduct, StudentCard
|
||||||
from election.models import Candidature, Election, ElectionList, Role
|
from election.models import Candidature, Election, ElectionList, Role
|
||||||
from forum.models import Forum
|
from forum.models import Forum
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
from sas.models import Album, PeoplePictureRelation, Picture
|
from sas.models import Album, PeoplePictureRelation, Picture
|
||||||
from subscription.models import Subscription
|
from subscription.models import Subscription
|
||||||
|
|
||||||
@@ -110,7 +110,6 @@ class Command(BaseCommand):
|
|||||||
p.save(force_lock=True)
|
p.save(force_lock=True)
|
||||||
|
|
||||||
club_root = SithFile.objects.create(name="clubs", owner=root)
|
club_root = SithFile.objects.create(name="clubs", owner=root)
|
||||||
sas = SithFile.objects.create(name="SAS", owner=root)
|
|
||||||
main_club = Club.objects.create(
|
main_club = Club.objects.create(
|
||||||
id=1, name="AE", address="6 Boulevard Anatole France, 90000 Belfort"
|
id=1, name="AE", address="6 Boulevard Anatole France, 90000 Belfort"
|
||||||
)
|
)
|
||||||
@@ -661,20 +660,20 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
# Create some data for pedagogy
|
# Create some data for pedagogy
|
||||||
|
|
||||||
UE(
|
UV(
|
||||||
code="PA00",
|
code="PA00",
|
||||||
author=User.objects.get(id=0),
|
author=User.objects.get(id=0),
|
||||||
credit_type=settings.SITH_PEDAGOGY_UE_TYPE[3][0],
|
credit_type=settings.SITH_PEDAGOGY_UV_TYPE[3][0],
|
||||||
manager="Laurent HEYBERGER",
|
manager="Laurent HEYBERGER",
|
||||||
semester=settings.SITH_PEDAGOGY_UE_SEMESTER[3][0],
|
semester=settings.SITH_PEDAGOGY_UV_SEMESTER[3][0],
|
||||||
language=settings.SITH_PEDAGOGY_UE_LANGUAGE[0][0],
|
language=settings.SITH_PEDAGOGY_UV_LANGUAGE[0][0],
|
||||||
department=settings.SITH_PROFILE_DEPARTMENTS[-2][0],
|
department=settings.SITH_PROFILE_DEPARTMENTS[-2][0],
|
||||||
credits=5,
|
credits=5,
|
||||||
title="Participation dans une association étudiante",
|
title="Participation dans une association étudiante",
|
||||||
objectives="* Permettre aux étudiants de réaliser, pendant un semestre, un projet culturel ou associatif et de le valoriser.",
|
objectives="* Permettre aux étudiants de réaliser, pendant un semestre, un projet culturel ou associatif et de le valoriser.",
|
||||||
program="""* Semestre précédent proposition d'un projet et d'un cahier des charges
|
program="""* Semestre précédent proposition d'un projet et d'un cahier des charges
|
||||||
* Evaluation par un jury de six membres
|
* Evaluation par un jury de six membres
|
||||||
* Si accord réalisation dans le cadre de l'UE
|
* Si accord réalisation dans le cadre de l'UV
|
||||||
* Compte-rendu de l'expérience
|
* Compte-rendu de l'expérience
|
||||||
* Présentation""",
|
* Présentation""",
|
||||||
skills="""* Gérer un projet associatif ou une action éducative en autonomie:
|
skills="""* Gérer un projet associatif ou une action éducative en autonomie:
|
||||||
@@ -694,33 +693,21 @@ class Command(BaseCommand):
|
|||||||
# SAS
|
# SAS
|
||||||
for f in self.SAS_FIXTURE_PATH.glob("*"):
|
for f in self.SAS_FIXTURE_PATH.glob("*"):
|
||||||
if f.is_dir():
|
if f.is_dir():
|
||||||
album = Album(
|
album = Album.objects.create(name=f.name, is_moderated=True)
|
||||||
parent=sas,
|
|
||||||
name=f.name,
|
|
||||||
owner=root,
|
|
||||||
is_folder=True,
|
|
||||||
is_in_sas=True,
|
|
||||||
is_moderated=True,
|
|
||||||
)
|
|
||||||
album.clean()
|
|
||||||
album.save()
|
|
||||||
for p in f.iterdir():
|
for p in f.iterdir():
|
||||||
file = resize_image(Image.open(p), 1000, "WEBP")
|
file = resize_image(Image.open(p), 1000, "WEBP")
|
||||||
pict = Picture(
|
pict = Picture(
|
||||||
parent=album,
|
parent=album,
|
||||||
name=p.name,
|
name=p.name,
|
||||||
file=file,
|
original=file,
|
||||||
owner=root,
|
owner=root,
|
||||||
is_folder=False,
|
|
||||||
is_in_sas=True,
|
|
||||||
is_moderated=True,
|
is_moderated=True,
|
||||||
mime_type="image/webp",
|
|
||||||
size=file.size,
|
|
||||||
)
|
)
|
||||||
pict.file.name = p.name
|
pict.original.name = pict.name
|
||||||
pict.full_clean()
|
|
||||||
pict.generate_thumbnails()
|
pict.generate_thumbnails()
|
||||||
|
pict.full_clean()
|
||||||
pict.save()
|
pict.save()
|
||||||
|
album.generate_thumbnail()
|
||||||
|
|
||||||
img_skia = Picture.objects.get(name="skia.jpg")
|
img_skia = Picture.objects.get(name="skia.jpg")
|
||||||
img_sli = Picture.objects.get(name="sli.jpg")
|
img_sli = Picture.objects.get(name="sli.jpg")
|
||||||
@@ -790,16 +777,16 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
subscribers = Group.objects.create(name="Cotisants")
|
subscribers = Group.objects.create(name="Cotisants")
|
||||||
subscribers.permissions.add(
|
subscribers.permissions.add(
|
||||||
*list(perms.filter(codename__in=["add_news", "add_uecomment"]))
|
*list(perms.filter(codename__in=["add_news", "add_uvcomment"]))
|
||||||
)
|
)
|
||||||
old_subscribers = Group.objects.create(name="Anciens cotisants")
|
old_subscribers = Group.objects.create(name="Anciens cotisants")
|
||||||
old_subscribers.permissions.add(
|
old_subscribers.permissions.add(
|
||||||
*list(
|
*list(
|
||||||
perms.filter(
|
perms.filter(
|
||||||
codename__in=[
|
codename__in=[
|
||||||
"view_ue",
|
"view_uv",
|
||||||
"view_uecomment",
|
"view_uvcomment",
|
||||||
"add_uecommentreport",
|
"add_uvcommentreport",
|
||||||
"view_user",
|
"view_user",
|
||||||
"view_picture",
|
"view_picture",
|
||||||
"view_album",
|
"view_album",
|
||||||
@@ -875,7 +862,7 @@ class Command(BaseCommand):
|
|||||||
pedagogy_admin.permissions.add(
|
pedagogy_admin.permissions.add(
|
||||||
*list(
|
*list(
|
||||||
perms.filter(content_type__app_label="pedagogy")
|
perms.filter(content_type__app_label="pedagogy")
|
||||||
.exclude(codename__in=["change_uecomment"])
|
.exclude(codename__in=["change_uvcomment"])
|
||||||
.values_list("pk", flat=True)
|
.values_list("pk", flat=True)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ from counter.models import (
|
|||||||
Selling,
|
Selling,
|
||||||
)
|
)
|
||||||
from forum.models import Forum, ForumMessage, ForumTopic
|
from forum.models import Forum, ForumMessage, ForumTopic
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
from subscription.models import Subscription
|
from subscription.models import Subscription
|
||||||
|
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ class Command(BaseCommand):
|
|||||||
random.sample(old_subscribers, k=min(80, len(old_subscribers))),
|
random.sample(old_subscribers, k=min(80, len(old_subscribers))),
|
||||||
)
|
)
|
||||||
self.stdout.write("Creating uvs...")
|
self.stdout.write("Creating uvs...")
|
||||||
self.create_ues()
|
self.create_uvs()
|
||||||
self.stdout.write("Creating products...")
|
self.stdout.write("Creating products...")
|
||||||
self.create_products()
|
self.create_products()
|
||||||
self.stdout.write("Creating sales and refills...")
|
self.stdout.write("Creating sales and refills...")
|
||||||
@@ -192,7 +192,7 @@ class Command(BaseCommand):
|
|||||||
memberships = Membership.objects.bulk_create(memberships)
|
memberships = Membership.objects.bulk_create(memberships)
|
||||||
Membership._add_club_groups(memberships)
|
Membership._add_club_groups(memberships)
|
||||||
|
|
||||||
def create_ues(self):
|
def create_uvs(self):
|
||||||
root = User.objects.get(username="root")
|
root = User.objects.get(username="root")
|
||||||
categories = ["CS", "TM", "OM", "QC", "EC"]
|
categories = ["CS", "TM", "OM", "QC", "EC"]
|
||||||
branches = ["TC", "GMC", "GI", "EDIM", "E", "IMSI", "HUMA"]
|
branches = ["TC", "GMC", "GI", "EDIM", "E", "IMSI", "HUMA"]
|
||||||
@@ -207,7 +207,7 @@ class Command(BaseCommand):
|
|||||||
+ str(random.randint(10, 90))
|
+ str(random.randint(10, 90))
|
||||||
)
|
)
|
||||||
uvs.append(
|
uvs.append(
|
||||||
UE(
|
UV(
|
||||||
code=code,
|
code=code,
|
||||||
author=root,
|
author=root,
|
||||||
manager=random.choice(teachers),
|
manager=random.choice(teachers),
|
||||||
@@ -229,7 +229,7 @@ class Command(BaseCommand):
|
|||||||
hours_TE=random.randint(15, 40),
|
hours_TE=random.randint(15, 40),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
UE.objects.bulk_create(uvs, ignore_conflicts=True)
|
UV.objects.bulk_create(uvs, ignore_conflicts=True)
|
||||||
|
|
||||||
def create_products(self):
|
def create_products(self):
|
||||||
categories = [
|
categories = [
|
||||||
|
|||||||
27
core/migrations/0048_remove_sithfiles.py
Normal file
27
core/migrations/0048_remove_sithfiles.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Generated by Django 4.2.17 on 2025-01-26 15:01
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
from django.db.migrations.state import StateApps
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import core.models
|
||||||
|
|
||||||
|
|
||||||
|
def remove_sas_sithfiles(apps: StateApps, schema_editor):
|
||||||
|
SithFile: type[core.models.SithFile] = apps.get_model("core", "SithFile")
|
||||||
|
SithFile.objects.filter(is_in_sas=True).delete()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
("core", "0047_alter_notification_date_alter_notification_type"),
|
||||||
|
("sas", "0007_alter_peoplepicturerelation_picture_and_more"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(
|
||||||
|
remove_sas_sithfiles, reverse_code=migrations.RunPython.noop, elidable=True
|
||||||
|
)
|
||||||
|
]
|
||||||
9
core/migrations/0049_remove_sithfile_is_in_sas.py
Normal file
9
core/migrations/0049_remove_sithfile_is_in_sas.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Generated by Django 4.2.17 on 2025-02-14 11:58
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [("core", "0048_remove_sithfiles")]
|
||||||
|
|
||||||
|
operations = [migrations.RemoveField(model_name="sithfile", name="is_in_sas")]
|
||||||
@@ -833,9 +833,6 @@ class SithFile(models.Model):
|
|||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
)
|
)
|
||||||
asked_for_removal = models.BooleanField(_("asked for removal"), default=False)
|
asked_for_removal = models.BooleanField(_("asked for removal"), default=False)
|
||||||
is_in_sas = models.BooleanField(
|
|
||||||
_("is in the SAS"), default=False, db_index=True
|
|
||||||
) # Allows to query this flag, updated at each call to save()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("file")
|
verbose_name = _("file")
|
||||||
@@ -844,22 +841,10 @@ class SithFile(models.Model):
|
|||||||
return self.get_parent_path() + "/" + self.name
|
return self.get_parent_path() + "/" + self.name
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
sas = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first()
|
|
||||||
self.is_in_sas = sas in self.get_parent_list() or self == sas
|
|
||||||
adding = self._state.adding
|
adding = self._state.adding
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
if adding:
|
if adding:
|
||||||
self.copy_rights()
|
self.copy_rights()
|
||||||
if self.is_in_sas:
|
|
||||||
for user in User.objects.filter(
|
|
||||||
groups__id__in=[settings.SITH_GROUP_SAS_ADMIN_ID]
|
|
||||||
):
|
|
||||||
Notification(
|
|
||||||
user=user,
|
|
||||||
url=reverse("sas:moderation"),
|
|
||||||
type="SAS_MODERATION",
|
|
||||||
param="1",
|
|
||||||
).save()
|
|
||||||
|
|
||||||
def is_owned_by(self, user: User) -> bool:
|
def is_owned_by(self, user: User) -> bool:
|
||||||
if user.is_anonymous:
|
if user.is_anonymous:
|
||||||
@@ -872,8 +857,6 @@ class SithFile(models.Model):
|
|||||||
return user.is_board_member
|
return user.is_board_member
|
||||||
if user.is_com_admin:
|
if user.is_com_admin:
|
||||||
return True
|
return True
|
||||||
if self.is_in_sas and user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
|
|
||||||
return True
|
|
||||||
return user.id == self.owner_id
|
return user.id == self.owner_id
|
||||||
|
|
||||||
def can_be_viewed_by(self, user: User) -> bool:
|
def can_be_viewed_by(self, user: User) -> bool:
|
||||||
@@ -900,8 +883,6 @@ class SithFile(models.Model):
|
|||||||
super().clean()
|
super().clean()
|
||||||
if "/" in self.name:
|
if "/" in self.name:
|
||||||
raise ValidationError(_("Character '/' not authorized in name"))
|
raise ValidationError(_("Character '/' not authorized in name"))
|
||||||
if self == self.parent:
|
|
||||||
raise ValidationError(_("Loop in folder tree"), code="loop")
|
|
||||||
if self == self.parent or (
|
if self == self.parent or (
|
||||||
self.parent is not None and self in self.get_parent_list()
|
self.parent is not None and self in self.get_parent_list()
|
||||||
):
|
):
|
||||||
@@ -982,18 +963,6 @@ class SithFile(models.Model):
|
|||||||
def is_file(self):
|
def is_file(self):
|
||||||
return not self.is_folder
|
return not self.is_folder
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def as_picture(self):
|
|
||||||
from sas.models import Picture
|
|
||||||
|
|
||||||
return Picture.objects.filter(id=self.id).first()
|
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def as_album(self):
|
|
||||||
from sas.models import Album
|
|
||||||
|
|
||||||
return Album.objects.filter(id=self.id).first()
|
|
||||||
|
|
||||||
def get_parent_list(self):
|
def get_parent_list(self):
|
||||||
parents = []
|
parents = []
|
||||||
current = self.parent
|
current = self.parent
|
||||||
|
|||||||
@@ -184,18 +184,18 @@
|
|||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if user.has_perm("pedagogy.add_ue") or user.has_perm("pedagogy.delete_uecomment") %}
|
{% if user.has_perm("pedagogy.add_uv") or user.has_perm("pedagogy.delete_uvcomment") %}
|
||||||
<div>
|
<div>
|
||||||
<h4>{% trans %}Pedagogy{% endtrans %}</h4>
|
<h4>{% trans %}Pedagogy{% endtrans %}</h4>
|
||||||
<ul>
|
<ul>
|
||||||
{% if user.has_perm("pedagogy.add_ue") %}
|
{% if user.has_perm("pedagogy.add_uv") %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ url("pedagogy:ue_create") }}">
|
<a href="{{ url("pedagogy:uv_create") }}">
|
||||||
{% trans %}Create UE{% endtrans %}
|
{% trans %}Create UV{% endtrans %}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if user.has_perm("pedagogy.delete_uecomment") %}
|
{% if user.has_perm("pedagogy.delete_uvcomment") %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ url("pedagogy:moderation") }}">
|
<a href="{{ url("pedagogy:moderation") }}">
|
||||||
{% trans %}Moderate comments{% endtrans %}
|
{% trans %}Moderate comments{% endtrans %}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from typing import Callable
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase
|
||||||
@@ -17,8 +18,8 @@ from pytest_django.asserts import assertNumQueries
|
|||||||
from core.baker_recipes import board_user, old_subscriber_user, subscriber_user
|
from core.baker_recipes import board_user, old_subscriber_user, subscriber_user
|
||||||
from core.models import Group, QuickUploadImage, SithFile, User
|
from core.models import Group, QuickUploadImage, SithFile, User
|
||||||
from core.utils import RED_PIXEL_PNG
|
from core.utils import RED_PIXEL_PNG
|
||||||
|
from sas.baker_recipes import picture_recipe
|
||||||
from sas.models import Picture
|
from sas.models import Picture
|
||||||
from sith import settings
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@@ -30,24 +31,19 @@ class TestImageAccess:
|
|||||||
lambda: baker.make(
|
lambda: baker.make(
|
||||||
User, groups=[Group.objects.get(pk=settings.SITH_GROUP_SAS_ADMIN_ID)]
|
User, groups=[Group.objects.get(pk=settings.SITH_GROUP_SAS_ADMIN_ID)]
|
||||||
),
|
),
|
||||||
lambda: baker.make(
|
|
||||||
User, groups=[Group.objects.get(pk=settings.SITH_GROUP_COM_ADMIN_ID)]
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_sas_image_access(self, user_factory: Callable[[], User]):
|
def test_sas_image_access(self, user_factory: Callable[[], User]):
|
||||||
"""Test that only authorized users can access the sas image."""
|
"""Test that only authorized users can access the sas image."""
|
||||||
user = user_factory()
|
user = user_factory()
|
||||||
picture: SithFile = baker.make(
|
picture = picture_recipe.make()
|
||||||
Picture, parent=SithFile.objects.get(pk=settings.SITH_SAS_ROOT_DIR_ID)
|
assert user.can_edit(picture)
|
||||||
)
|
|
||||||
assert picture.is_owned_by(user)
|
|
||||||
|
|
||||||
def test_sas_image_access_owner(self):
|
def test_sas_image_access_owner(self):
|
||||||
"""Test that the owner of the image can access it."""
|
"""Test that the owner of the image can access it."""
|
||||||
user = baker.make(User)
|
user = baker.make(User)
|
||||||
picture: Picture = baker.make(Picture, owner=user)
|
picture = picture_recipe.make(owner=user)
|
||||||
assert picture.is_owned_by(user)
|
assert user.can_edit(picture)
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"user_factory",
|
"user_factory",
|
||||||
@@ -63,7 +59,41 @@ class TestImageAccess:
|
|||||||
user = user_factory()
|
user = user_factory()
|
||||||
owner = baker.make(User)
|
owner = baker.make(User)
|
||||||
picture: Picture = baker.make(Picture, owner=owner)
|
picture: Picture = baker.make(Picture, owner=owner)
|
||||||
assert not picture.is_owned_by(user)
|
assert not user.can_edit(picture)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
class TestUserPicture:
|
||||||
|
def test_anonymous_user_unauthorized(self, client):
|
||||||
|
"""An anonymous user shouldn't have access to an user's photo page."""
|
||||||
|
response = client.get(
|
||||||
|
reverse(
|
||||||
|
"core:user_pictures",
|
||||||
|
kwargs={"user_id": User.objects.get(username="sli").pk},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("username", "status"),
|
||||||
|
[
|
||||||
|
("guy", 403),
|
||||||
|
("root", 200),
|
||||||
|
("skia", 200),
|
||||||
|
("sli", 200),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_page_is_working(self, client, username, status):
|
||||||
|
"""Only user that subscribed (or admins) should be able to see the page."""
|
||||||
|
# Test for simple user
|
||||||
|
client.force_login(User.objects.get(username=username))
|
||||||
|
response = client.get(
|
||||||
|
reverse(
|
||||||
|
"core:user_pictures",
|
||||||
|
kwargs={"user_id": User.objects.get(username="sli").pk},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert response.status_code == status
|
||||||
|
|
||||||
|
|
||||||
# TODO: many tests on the pages:
|
# TODO: many tests on the pages:
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from counter.baker_recipes import sale_recipe
|
|||||||
from counter.models import Counter, Customer, Permanency, Refilling, Selling
|
from counter.models import Counter, Customer, Permanency, Refilling, Selling
|
||||||
from counter.utils import is_logged_in_counter
|
from counter.utils import is_logged_in_counter
|
||||||
from eboutic.models import Invoice, InvoiceItem
|
from eboutic.models import Invoice, InvoiceItem
|
||||||
|
from sas.models import Picture
|
||||||
|
|
||||||
|
|
||||||
class TestSearchUsers(TestCase):
|
class TestSearchUsers(TestCase):
|
||||||
@@ -34,6 +35,7 @@ class TestSearchUsers(TestCase):
|
|||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
# News.author has on_delete=PROTECT, so news must be deleted beforehand
|
# News.author has on_delete=PROTECT, so news must be deleted beforehand
|
||||||
News.objects.all().delete()
|
News.objects.all().delete()
|
||||||
|
Picture.objects.all().delete() # same for pictures
|
||||||
User.objects.all().delete()
|
User.objects.all().delete()
|
||||||
user_recipe = Recipe(
|
user_recipe = Recipe(
|
||||||
User,
|
User,
|
||||||
|
|||||||
@@ -12,18 +12,23 @@
|
|||||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
from dataclasses import dataclass
|
||||||
from datetime import date, timedelta
|
from datetime import date, timedelta
|
||||||
|
|
||||||
# Image utils
|
# Image utils
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Final
|
from typing import Any, Final, Unpack
|
||||||
|
|
||||||
import PIL
|
import PIL
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.core.files.uploadedfile import UploadedFile
|
from django.core.files.uploadedfile import UploadedFile
|
||||||
from django.http import HttpRequest
|
from django.db import models
|
||||||
|
from django.forms import BaseForm
|
||||||
|
from django.http import Http404, HttpRequest
|
||||||
|
from django.shortcuts import get_list_or_404
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
from django.utils.safestring import SafeString
|
||||||
from django.utils.timezone import localdate
|
from django.utils.timezone import localdate
|
||||||
from PIL import ExifTags
|
from PIL import ExifTags
|
||||||
from PIL.Image import Image, Resampling
|
from PIL.Image import Image, Resampling
|
||||||
@@ -42,6 +47,21 @@ to generate a dummy image that is considered valid nonetheless
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FormFragmentTemplateData[T: BaseForm]:
|
||||||
|
"""Dataclass used to pre-render form fragments"""
|
||||||
|
|
||||||
|
form: T
|
||||||
|
template: str
|
||||||
|
context: dict[str, Any]
|
||||||
|
|
||||||
|
def render(self, request: HttpRequest) -> SafeString:
|
||||||
|
# Request is needed for csrf_tokens
|
||||||
|
return render_to_string(
|
||||||
|
self.template, context={"form": self.form, **self.context}, request=request
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_start_of_semester(today: date | None = None) -> date:
|
def get_start_of_semester(today: date | None = None) -> date:
|
||||||
"""Return the date of the start of the semester of the given date.
|
"""Return the date of the start of the semester of the given date.
|
||||||
If no date is given, return the start date of the current semester.
|
If no date is given, return the start date of the current semester.
|
||||||
@@ -205,3 +225,56 @@ def get_client_ip(request: HttpRequest) -> str | None:
|
|||||||
return ip
|
return ip
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
Filterable = models.Model | models.QuerySet | models.Manager
|
||||||
|
ListFilter = dict[str, list | tuple | set]
|
||||||
|
|
||||||
|
|
||||||
|
def get_list_exact_or_404(klass: Filterable, **kwargs: Unpack[ListFilter]) -> list:
|
||||||
|
"""Use filter() to return a list of objects from a list of unique keys (like ids)
|
||||||
|
or raises Http404 if the list has not the same length as the given one.
|
||||||
|
|
||||||
|
Work like `get_object_or_404()` but for lists of objects, with some caveats :
|
||||||
|
|
||||||
|
- The filter must be a list, a tuple or a set.
|
||||||
|
- There can't be more than exactly one filter.
|
||||||
|
- There must be no duplicate in the filter.
|
||||||
|
- The filter should consist in unique keys (like ids), or it could fail randomly.
|
||||||
|
|
||||||
|
klass may be a Model, Manager, or QuerySet object. All other passed
|
||||||
|
arguments and keyword arguments are used in the filter() query.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
Http404: If the list is empty or doesn't have as many elements as the keys list.
|
||||||
|
ValueError: If the first argument is not a Model, Manager, or QuerySet object.
|
||||||
|
ValueError: If more than one filter is passed.
|
||||||
|
TypeError: If the given filter is not a list, a tuple or a set.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
Get all the products with ids 1, 2, 3: ::
|
||||||
|
|
||||||
|
products = get_list_exact_or_404(Product, id__in=[1, 2, 3])
|
||||||
|
|
||||||
|
Don't work with duplicate ids: ::
|
||||||
|
|
||||||
|
products = get_list_exact_or_404(Product, id__in=[1, 2, 3, 3])
|
||||||
|
# Raises Http404: "The list of keys must contain no duplicates."
|
||||||
|
"""
|
||||||
|
if len(kwargs) > 1:
|
||||||
|
raise ValueError("get_list_exact_or_404() only accepts one filter.")
|
||||||
|
key, list_filter = next(iter(kwargs.items()))
|
||||||
|
if not isinstance(list_filter, (list, tuple, set)):
|
||||||
|
raise TypeError(
|
||||||
|
f"The given filter must be a list, a tuple or a set, not {type(list_filter)}"
|
||||||
|
)
|
||||||
|
if len(list_filter) != len(set(list_filter)):
|
||||||
|
raise ValueError("The list of keys must contain no duplicates.")
|
||||||
|
kwargs = {key: list_filter}
|
||||||
|
obj_list = get_list_or_404(klass, **kwargs)
|
||||||
|
if len(obj_list) != len(list_filter):
|
||||||
|
raise Http404(
|
||||||
|
"The given list of keys doesn't match the number of objects found."
|
||||||
|
f"Expected {len(list_filter)} items, got {len(obj_list)}."
|
||||||
|
)
|
||||||
|
return obj_list
|
||||||
|
|||||||
@@ -374,7 +374,7 @@ class FileDeleteView(AllowFragment, CanEditPropMixin, DeleteView):
|
|||||||
class FileModerationView(AllowFragment, ListView):
|
class FileModerationView(AllowFragment, ListView):
|
||||||
model = SithFile
|
model = SithFile
|
||||||
template_name = "core/file_moderation.jinja"
|
template_name = "core/file_moderation.jinja"
|
||||||
queryset = SithFile.objects.filter(is_moderated=False, is_in_sas=False)
|
queryset = SithFile.objects.filter(is_moderated=False)
|
||||||
ordering = "id"
|
ordering = "id"
|
||||||
paginate_by = 100
|
paginate_by = 100
|
||||||
|
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ from django.conf import settings
|
|||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
dependencies = [
|
dependencies = [
|
||||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
("pedagogy", "0003_alter_ue_language"),
|
("pedagogy", "0003_alter_uv_language"),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
@@ -215,12 +215,11 @@ On modifie donc le modèle :
|
|||||||
```python
|
```python
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from core.models import User
|
from core.models import User
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
|
|
||||||
|
|
||||||
class UserUe(models.Model):
|
class UserUe(models.Model):
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
ue = models.ForeignKey(UE, on_delete=models.CASCADE)
|
ue = models.ForeignKey(UV, on_delete=models.CASCADE)
|
||||||
```
|
```
|
||||||
|
|
||||||
On refait la commande `makemigrations` et on obtient :
|
On refait la commande `makemigrations` et on obtient :
|
||||||
@@ -238,7 +237,7 @@ class Migration(migrations.Migration):
|
|||||||
model_name="userue",
|
model_name="userue",
|
||||||
name="ue",
|
name="ue",
|
||||||
field=models.ForeignKey(
|
field=models.ForeignKey(
|
||||||
on_delete=models.deletion.CASCADE, to="pedagogy.ue"
|
on_delete=models.deletion.CASCADE, to="pedagogy.uv"
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
@@ -281,7 +280,7 @@ python ./manage.py squasmigrations <app> <migration de début (incluse)> <migrat
|
|||||||
Par exemple, dans notre cas, ça donnera :
|
Par exemple, dans notre cas, ça donnera :
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python ./manage.py squashmigrations pedagogy 0004 0005
|
python ./manage.py squasmigrations pedagogy 0004 0005
|
||||||
```
|
```
|
||||||
|
|
||||||
La commande vous donnera ceci :
|
La commande vous donnera ceci :
|
||||||
@@ -293,7 +292,7 @@ class Migration(migrations.Migration):
|
|||||||
replaces = [("pedagogy", "0004_userue"), ("pedagogy", "0005_alter_userue_ue")]
|
replaces = [("pedagogy", "0004_userue"), ("pedagogy", "0005_alter_userue_ue")]
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("pedagogy", "0003_alter_ue_language"),
|
("pedagogy", "0003_alter_uv_language"),
|
||||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -313,7 +312,7 @@ class Migration(migrations.Migration):
|
|||||||
(
|
(
|
||||||
"ue",
|
"ue",
|
||||||
models.ForeignKey(
|
models.ForeignKey(
|
||||||
on_delete=models.deletion.CASCADE, to="pedagogy.ue"
|
on_delete=models.deletion.CASCADE, to="pedagogy.uv"
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -263,3 +263,35 @@ avec un unique champ permettant de sélectionner des groupes.
|
|||||||
Par défaut, seuls les utilisateurs avec la permission
|
Par défaut, seuls les utilisateurs avec la permission
|
||||||
`auth.change_permission` auront accès à ce formulaire
|
`auth.change_permission` auront accès à ce formulaire
|
||||||
(donc, normalement, uniquement les utilisateurs Root).
|
(donc, normalement, uniquement les utilisateurs Root).
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant A as Utilisateur
|
||||||
|
participant B as ReverseProxy
|
||||||
|
participant C as MarkdownImage
|
||||||
|
participant D as Model
|
||||||
|
|
||||||
|
A->>B: GET /page/foo
|
||||||
|
B->>C: GET /page/foo
|
||||||
|
C-->>B: La page, avec les urls
|
||||||
|
B-->>A: La page, avec les urls
|
||||||
|
alt image publique
|
||||||
|
A->>B: GET markdown/public/2025/img.webp
|
||||||
|
B-->>A: img.webp
|
||||||
|
end
|
||||||
|
alt image privée
|
||||||
|
A->>B: GET markdown_image/{id}
|
||||||
|
B->>C: GET markdown_image/{id}
|
||||||
|
C->>D: user.can_view(image)
|
||||||
|
alt l'utilisateur a le droit de voir l'image
|
||||||
|
D-->>C: True
|
||||||
|
C-->>B: 200 (avec le X-Accel-Redirect)
|
||||||
|
B-->>A: img.webp
|
||||||
|
end
|
||||||
|
alt l'utilisateur n'a pas le droit de l'image
|
||||||
|
D-->>C: False
|
||||||
|
C-->>B: 403
|
||||||
|
B-->>A: 403
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|||||||
@@ -25,13 +25,12 @@ import warnings
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Final, Optional
|
from typing import Final, Optional
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from club.models import Club, Membership
|
from club.models import Club, Membership
|
||||||
from core.models import Group, Page, SithFile, User
|
from core.models import Group, Page, User
|
||||||
from core.utils import RED_PIXEL_PNG
|
from core.utils import RED_PIXEL_PNG
|
||||||
from sas.models import Album, PeoplePictureRelation, Picture
|
from sas.models import Album, PeoplePictureRelation, Picture
|
||||||
from subscription.models import Subscription
|
from subscription.models import Subscription
|
||||||
@@ -91,13 +90,8 @@ class Command(BaseCommand):
|
|||||||
self.NB_CLUBS = options["club_count"]
|
self.NB_CLUBS = options["club_count"]
|
||||||
|
|
||||||
root = User.objects.filter(username="root").first()
|
root = User.objects.filter(username="root").first()
|
||||||
sas = SithFile.objects.get(id=settings.SITH_SAS_ROOT_DIR_ID)
|
|
||||||
self.galaxy_album = Album.objects.create(
|
self.galaxy_album = Album.objects.create(
|
||||||
name="galaxy-register-file",
|
name="galaxy-register-file", owner=root, is_moderated=True
|
||||||
owner=root,
|
|
||||||
is_moderated=True,
|
|
||||||
is_in_sas=True,
|
|
||||||
parent=sas,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.make_clubs()
|
self.make_clubs()
|
||||||
@@ -285,14 +279,10 @@ class Command(BaseCommand):
|
|||||||
owner=u,
|
owner=u,
|
||||||
name=f"galaxy-picture {u} {i // self.NB_USERS}",
|
name=f"galaxy-picture {u} {i // self.NB_USERS}",
|
||||||
is_moderated=True,
|
is_moderated=True,
|
||||||
is_folder=False,
|
|
||||||
parent=self.galaxy_album,
|
parent=self.galaxy_album,
|
||||||
is_in_sas=True,
|
original=ContentFile(RED_PIXEL_PNG),
|
||||||
file=ContentFile(RED_PIXEL_PNG),
|
|
||||||
compressed=ContentFile(RED_PIXEL_PNG),
|
compressed=ContentFile(RED_PIXEL_PNG),
|
||||||
thumbnail=ContentFile(RED_PIXEL_PNG),
|
thumbnail=ContentFile(RED_PIXEL_PNG),
|
||||||
mime_type="image/png",
|
|
||||||
size=len(RED_PIXEL_PNG),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.picts[i].file.name = self.picts[i].name
|
self.picts[i].file.name = self.picts[i].name
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -23,35 +23,35 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from haystack.admin import SearchModelAdmin
|
from haystack.admin import SearchModelAdmin
|
||||||
|
|
||||||
from pedagogy.models import UE, UEComment, UECommentReport
|
from pedagogy.models import UV, UVComment, UVCommentReport
|
||||||
|
|
||||||
|
|
||||||
@admin.register(UE)
|
@admin.register(UV)
|
||||||
class UEAdmin(admin.ModelAdmin):
|
class UVAdmin(admin.ModelAdmin):
|
||||||
list_display = ("code", "title", "credit_type", "credits", "department")
|
list_display = ("code", "title", "credit_type", "credits", "department")
|
||||||
search_fields = ("code", "title", "department")
|
search_fields = ("code", "title", "department")
|
||||||
autocomplete_fields = ("author",)
|
autocomplete_fields = ("author",)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(UEComment)
|
@admin.register(UVComment)
|
||||||
class UECommentAdmin(admin.ModelAdmin):
|
class UVCommentAdmin(admin.ModelAdmin):
|
||||||
list_display = ("author", "ue", "grade_global", "publish_date")
|
list_display = ("author", "uv", "grade_global", "publish_date")
|
||||||
search_fields = (
|
search_fields = (
|
||||||
"author__username",
|
"author__username",
|
||||||
"author__first_name",
|
"author__first_name",
|
||||||
"author__last_name",
|
"author__last_name",
|
||||||
"ue__code",
|
"uv__code",
|
||||||
)
|
)
|
||||||
autocomplete_fields = ("author",)
|
autocomplete_fields = ("author",)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(UECommentReport)
|
@admin.register(UVCommentReport)
|
||||||
class UECommentReportAdmin(SearchModelAdmin):
|
class UVCommentReportAdmin(SearchModelAdmin):
|
||||||
list_display = ("reporter", "ue")
|
list_display = ("reporter", "uv")
|
||||||
search_fields = (
|
search_fields = (
|
||||||
"reporter__username",
|
"reporter__username",
|
||||||
"reporter__first_name",
|
"reporter__first_name",
|
||||||
"reporter__last_name",
|
"reporter__last_name",
|
||||||
"comment__ue__code",
|
"comment__uv__code",
|
||||||
)
|
)
|
||||||
autocomplete_fields = ("reporter",)
|
autocomplete_fields = ("reporter",)
|
||||||
|
|||||||
@@ -10,23 +10,23 @@ from ninja_extra.pagination import PageNumberPaginationExtra, PaginatedResponseS
|
|||||||
|
|
||||||
from api.auth import ApiKeyAuth
|
from api.auth import ApiKeyAuth
|
||||||
from api.permissions import HasPerm
|
from api.permissions import HasPerm
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
from pedagogy.schemas import SimpleUeSchema, UeFilterSchema, UeSchema
|
from pedagogy.schemas import SimpleUvSchema, UvFilterSchema, UvSchema
|
||||||
from pedagogy.utbm_api import UtbmApiClient
|
from pedagogy.utbm_api import UtbmApiClient
|
||||||
|
|
||||||
|
|
||||||
@api_controller("/ue")
|
@api_controller("/uv")
|
||||||
class UeController(ControllerBase):
|
class UvController(ControllerBase):
|
||||||
@route.get(
|
@route.get(
|
||||||
"/{code}",
|
"/{code}",
|
||||||
auth=[ApiKeyAuth(), SessionAuth()],
|
auth=[ApiKeyAuth(), SessionAuth()],
|
||||||
permissions=[
|
permissions=[
|
||||||
# this route will almost always be called in the context
|
# this route will almost always be called in the context
|
||||||
# of a UE creation/edition
|
# of a UV creation/edition
|
||||||
HasPerm(["pedagogy.add_ue", "pedagogy.change_ue"], op=operator.or_)
|
HasPerm(["pedagogy.add_uv", "pedagogy.change_uv"], op=operator.or_)
|
||||||
],
|
],
|
||||||
url_name="fetch_ue_from_utbm",
|
url_name="fetch_uv_from_utbm",
|
||||||
response=UeSchema,
|
response=UvSchema,
|
||||||
)
|
)
|
||||||
def fetch_from_utbm_api(
|
def fetch_from_utbm_api(
|
||||||
self,
|
self,
|
||||||
@@ -34,20 +34,20 @@ class UeController(ControllerBase):
|
|||||||
lang: Query[str] = "fr",
|
lang: Query[str] = "fr",
|
||||||
year: Query[Annotated[int, Ge(2010)] | None] = None,
|
year: Query[Annotated[int, Ge(2010)] | None] = None,
|
||||||
):
|
):
|
||||||
"""Fetch UE data from the UTBM API and returns it after some parsing."""
|
"""Fetch UV data from the UTBM API and returns it after some parsing."""
|
||||||
with UtbmApiClient() as client:
|
with UtbmApiClient() as client:
|
||||||
res = client.find_ue(lang, code, year)
|
res = client.find_uv(lang, code, year)
|
||||||
if res is None:
|
if res is None:
|
||||||
raise NotFound
|
raise NotFound
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@route.get(
|
@route.get(
|
||||||
"",
|
"",
|
||||||
response=PaginatedResponseSchema[SimpleUeSchema],
|
response=PaginatedResponseSchema[SimpleUvSchema],
|
||||||
url_name="fetch_ues",
|
url_name="fetch_uvs",
|
||||||
auth=[ApiKeyAuth(), SessionAuth()],
|
auth=[ApiKeyAuth(), SessionAuth()],
|
||||||
permissions=[HasPerm("pedagogy.view_ue")],
|
permissions=[HasPerm("pedagogy.view_uv")],
|
||||||
)
|
)
|
||||||
@paginate(PageNumberPaginationExtra, page_size=100)
|
@paginate(PageNumberPaginationExtra, page_size=100)
|
||||||
def fetch_ue_list(self, search: Query[UeFilterSchema]):
|
def fetch_uv_list(self, search: Query[UvFilterSchema]):
|
||||||
return search.filter(UE.objects.order_by("code").values())
|
return search.filter(UV.objects.order_by("code").values())
|
||||||
|
|||||||
@@ -26,14 +26,14 @@ from django.utils.translation import gettext_lazy as _
|
|||||||
|
|
||||||
from core.models import User
|
from core.models import User
|
||||||
from core.views.widgets.markdown import MarkdownInput
|
from core.views.widgets.markdown import MarkdownInput
|
||||||
from pedagogy.models import UE, UEComment, UECommentReport
|
from pedagogy.models import UV, UVComment, UVCommentReport
|
||||||
|
|
||||||
|
|
||||||
class UEForm(forms.ModelForm):
|
class UVForm(forms.ModelForm):
|
||||||
"""Form handeling creation and edit of an UE."""
|
"""Form handeling creation and edit of an UV."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UE
|
model = UV
|
||||||
fields = (
|
fields = (
|
||||||
"code",
|
"code",
|
||||||
"author",
|
"author",
|
||||||
@@ -82,14 +82,14 @@ class StarList(forms.NumberInput):
|
|||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
class UECommentForm(forms.ModelForm):
|
class UVCommentForm(forms.ModelForm):
|
||||||
"""Form handeling creation and edit of an UEComment."""
|
"""Form handeling creation and edit of an UVComment."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UEComment
|
model = UVComment
|
||||||
fields = (
|
fields = (
|
||||||
"author",
|
"author",
|
||||||
"ue",
|
"uv",
|
||||||
"grade_global",
|
"grade_global",
|
||||||
"grade_utility",
|
"grade_utility",
|
||||||
"grade_interest",
|
"grade_interest",
|
||||||
@@ -100,7 +100,7 @@ class UECommentForm(forms.ModelForm):
|
|||||||
widgets = {
|
widgets = {
|
||||||
"comment": MarkdownInput,
|
"comment": MarkdownInput,
|
||||||
"author": forms.HiddenInput,
|
"author": forms.HiddenInput,
|
||||||
"ue": forms.HiddenInput,
|
"uv": forms.HiddenInput,
|
||||||
"grade_global": StarList(5),
|
"grade_global": StarList(5),
|
||||||
"grade_utility": StarList(5),
|
"grade_utility": StarList(5),
|
||||||
"grade_interest": StarList(5),
|
"grade_interest": StarList(5),
|
||||||
@@ -108,35 +108,35 @@ class UECommentForm(forms.ModelForm):
|
|||||||
"grade_work_load": StarList(5),
|
"grade_work_load": StarList(5),
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, author_id, ue_id, is_creation, *args, **kwargs):
|
def __init__(self, author_id, uv_id, is_creation, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.fields["author"].queryset = User.objects.filter(id=author_id).all()
|
self.fields["author"].queryset = User.objects.filter(id=author_id).all()
|
||||||
self.fields["author"].initial = author_id
|
self.fields["author"].initial = author_id
|
||||||
self.fields["ue"].queryset = UE.objects.filter(id=ue_id).all()
|
self.fields["uv"].queryset = UV.objects.filter(id=uv_id).all()
|
||||||
self.fields["ue"].initial = ue_id
|
self.fields["uv"].initial = uv_id
|
||||||
self.is_creation = is_creation
|
self.is_creation = is_creation
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
self.cleaned_data = super().clean()
|
self.cleaned_data = super().clean()
|
||||||
ue = self.cleaned_data.get("ue")
|
uv = self.cleaned_data.get("uv")
|
||||||
author = self.cleaned_data.get("author")
|
author = self.cleaned_data.get("author")
|
||||||
|
|
||||||
if self.is_creation and ue and author and ue.has_user_already_commented(author):
|
if self.is_creation and uv and author and uv.has_user_already_commented(author):
|
||||||
self.add_error(
|
self.add_error(
|
||||||
None,
|
None,
|
||||||
forms.ValidationError(
|
forms.ValidationError(
|
||||||
_("This user has already commented on this UE"), code="invalid"
|
_("This user has already commented on this UV"), code="invalid"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
|
||||||
|
|
||||||
class UECommentReportForm(forms.ModelForm):
|
class UVCommentReportForm(forms.ModelForm):
|
||||||
"""Form handeling creation and edit of an UEReport."""
|
"""Form handeling creation and edit of an UVReport."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UECommentReport
|
model = UVCommentReport
|
||||||
fields = ("comment", "reporter", "reason")
|
fields = ("comment", "reporter", "reason")
|
||||||
widgets = {
|
widgets = {
|
||||||
"comment": forms.HiddenInput,
|
"comment": forms.HiddenInput,
|
||||||
@@ -148,22 +148,22 @@ class UECommentReportForm(forms.ModelForm):
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.fields["reporter"].queryset = User.objects.filter(id=reporter_id).all()
|
self.fields["reporter"].queryset = User.objects.filter(id=reporter_id).all()
|
||||||
self.fields["reporter"].initial = reporter_id
|
self.fields["reporter"].initial = reporter_id
|
||||||
self.fields["comment"].queryset = UEComment.objects.filter(id=comment_id).all()
|
self.fields["comment"].queryset = UVComment.objects.filter(id=comment_id).all()
|
||||||
self.fields["comment"].initial = comment_id
|
self.fields["comment"].initial = comment_id
|
||||||
|
|
||||||
|
|
||||||
class UECommentModerationForm(forms.Form):
|
class UVCommentModerationForm(forms.Form):
|
||||||
"""Form handeling bulk comment deletion."""
|
"""Form handeling bulk comment deletion."""
|
||||||
|
|
||||||
accepted_reports = forms.ModelMultipleChoiceField(
|
accepted_reports = forms.ModelMultipleChoiceField(
|
||||||
UECommentReport.objects.all(),
|
UVCommentReport.objects.all(),
|
||||||
label=_("Accepted reports"),
|
label=_("Accepted reports"),
|
||||||
widget=forms.CheckboxSelectMultiple,
|
widget=forms.CheckboxSelectMultiple,
|
||||||
required=False,
|
required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
denied_reports = forms.ModelMultipleChoiceField(
|
denied_reports = forms.ModelMultipleChoiceField(
|
||||||
UECommentReport.objects.all(),
|
UVCommentReport.objects.all(),
|
||||||
label=_("Denied reports"),
|
label=_("Denied reports"),
|
||||||
widget=forms.CheckboxSelectMultiple,
|
widget=forms.CheckboxSelectMultiple,
|
||||||
required=False,
|
required=False,
|
||||||
|
|||||||
@@ -2,36 +2,36 @@ from django.conf import settings
|
|||||||
from django.core.management import BaseCommand
|
from django.core.management import BaseCommand
|
||||||
|
|
||||||
from core.models import User
|
from core.models import User
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
from pedagogy.schemas import UeSchema
|
from pedagogy.schemas import UvSchema
|
||||||
from pedagogy.utbm_api import UtbmApiClient
|
from pedagogy.utbm_api import UtbmApiClient
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = "Update the UE guide"
|
help = "Update the UV guide"
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
seen_ues: set[int] = set()
|
seen_uvs: set[int] = set()
|
||||||
root_user = User.objects.get(pk=settings.SITH_ROOT_USER_ID)
|
root_user = User.objects.get(pk=settings.SITH_ROOT_USER_ID)
|
||||||
with UtbmApiClient() as client:
|
with UtbmApiClient() as client:
|
||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
"Fetching UEs from the UTBM API.\n"
|
"Fetching UVs from the UTBM API.\n"
|
||||||
"This may take a few minutes to complete."
|
"This may take a few minutes to complete."
|
||||||
)
|
)
|
||||||
for ue in client.fetch_ues():
|
for uv in client.fetch_uvs():
|
||||||
db_ue = UE.objects.filter(code=ue.code).first()
|
db_uv = UV.objects.filter(code=uv.code).first()
|
||||||
if db_ue is None:
|
if db_uv is None:
|
||||||
db_ue = UE(code=ue.code, author=root_user)
|
db_uv = UV(code=uv.code, author=root_user)
|
||||||
fields = list(UeSchema.model_fields.keys())
|
fields = list(UvSchema.model_fields.keys())
|
||||||
fields.remove("id")
|
fields.remove("id")
|
||||||
fields.remove("code")
|
fields.remove("code")
|
||||||
for field in fields:
|
for field in fields:
|
||||||
setattr(db_ue, field, getattr(ue, field))
|
setattr(db_uv, field, getattr(uv, field))
|
||||||
db_ue.save()
|
db_uv.save()
|
||||||
# if it's a creation, django will set the id when saving,
|
# if it's a creation, django will set the id when saving,
|
||||||
# so at this point, a db_ue will always have an id
|
# so at this point, a db_uv will always have an id
|
||||||
seen_ues.add(db_ue.id)
|
seen_uvs.add(db_uv.id)
|
||||||
# UEs that are in database but have not been returned by the API
|
# UVs that are in database but have not been returned by the API
|
||||||
# are considered as closed UEs
|
# are considered as closed UEs
|
||||||
UE.objects.exclude(id__in=seen_ues).update(semester="CLOSED")
|
UV.objects.exclude(id__in=seen_uvs).update(semester="CLOSED")
|
||||||
self.stdout.write(self.style.SUCCESS("UE guide updated successfully"))
|
self.stdout.write(self.style.SUCCESS("UV guide updated successfully"))
|
||||||
|
|||||||
@@ -1,140 +0,0 @@
|
|||||||
# Generated by Django 4.2.20 on 2025-04-08 10:12
|
|
||||||
|
|
||||||
import django.core.validators
|
|
||||||
import django.db.models.deletion
|
|
||||||
from django.conf import settings
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
dependencies = [
|
|
||||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
|
||||||
("pedagogy", "0003_alter_uv_language"),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RenameModel(old_name="UV", new_name="UE"),
|
|
||||||
migrations.RenameModel(old_name="UVComment", new_name="UEComment"),
|
|
||||||
migrations.RenameModel(old_name="UVCommentReport", new_name="UECommentReport"),
|
|
||||||
migrations.RenameModel(old_name="UVResult", new_name="UEResult"),
|
|
||||||
migrations.RenameField(model_name="ueresult", old_name="uv", new_name="ue"),
|
|
||||||
migrations.RenameField(model_name="uecomment", old_name="uv", new_name="ue"),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="credits",
|
|
||||||
field=models.PositiveIntegerField(verbose_name="credits"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="hours_CM",
|
|
||||||
field=models.PositiveIntegerField(default=0, verbose_name="hours CM"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="hours_TD",
|
|
||||||
field=models.PositiveIntegerField(default=0, verbose_name="hours TD"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="hours_TE",
|
|
||||||
field=models.PositiveIntegerField(default=0, verbose_name="hours TE"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="hours_THE",
|
|
||||||
field=models.PositiveIntegerField(default=0, verbose_name="hours THE"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="hours_TP",
|
|
||||||
field=models.PositiveIntegerField(default=0, verbose_name="hours TP"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="manager",
|
|
||||||
field=models.CharField(max_length=300, verbose_name="ue manager"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ueresult",
|
|
||||||
name="ue",
|
|
||||||
field=models.ForeignKey(
|
|
||||||
on_delete=django.db.models.deletion.CASCADE,
|
|
||||||
related_name="results",
|
|
||||||
to="pedagogy.ue",
|
|
||||||
verbose_name="ue",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="author",
|
|
||||||
field=models.ForeignKey(
|
|
||||||
on_delete=django.db.models.deletion.CASCADE,
|
|
||||||
related_name="ue_created",
|
|
||||||
to=settings.AUTH_USER_MODEL,
|
|
||||||
verbose_name="author",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ue",
|
|
||||||
name="code",
|
|
||||||
field=models.CharField(
|
|
||||||
max_length=10,
|
|
||||||
unique=True,
|
|
||||||
validators=[
|
|
||||||
django.core.validators.RegexValidator(
|
|
||||||
message=(
|
|
||||||
"The code of an UE must only contains "
|
|
||||||
"uppercase characters without accent and numbers"
|
|
||||||
),
|
|
||||||
regex="([A-Z0-9]+)",
|
|
||||||
)
|
|
||||||
],
|
|
||||||
verbose_name="code",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="uecomment",
|
|
||||||
name="author",
|
|
||||||
field=models.ForeignKey(
|
|
||||||
on_delete=django.db.models.deletion.CASCADE,
|
|
||||||
related_name="ue_comments",
|
|
||||||
to=settings.AUTH_USER_MODEL,
|
|
||||||
verbose_name="author",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="uecomment",
|
|
||||||
name="comment",
|
|
||||||
field=models.TextField(blank=True, default="", verbose_name="comment"),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="uecomment",
|
|
||||||
name="ue",
|
|
||||||
field=models.ForeignKey(
|
|
||||||
on_delete=django.db.models.deletion.CASCADE,
|
|
||||||
related_name="comments",
|
|
||||||
to="pedagogy.ue",
|
|
||||||
verbose_name="ue",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="uecommentreport",
|
|
||||||
name="reporter",
|
|
||||||
field=models.ForeignKey(
|
|
||||||
on_delete=django.db.models.deletion.CASCADE,
|
|
||||||
related_name="reported_ue_comment",
|
|
||||||
to=settings.AUTH_USER_MODEL,
|
|
||||||
verbose_name="reporter",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
migrations.AlterField(
|
|
||||||
model_name="ueresult",
|
|
||||||
name="user",
|
|
||||||
field=models.ForeignKey(
|
|
||||||
on_delete=django.db.models.deletion.CASCADE,
|
|
||||||
related_name="ue_results",
|
|
||||||
to=settings.AUTH_USER_MODEL,
|
|
||||||
verbose_name="user",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
@@ -36,8 +36,8 @@ from core.models import User
|
|||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
|
||||||
class UE(models.Model):
|
class UV(models.Model):
|
||||||
"""Contains infos about an UE (course)."""
|
"""Contains infos about an UV (course)."""
|
||||||
|
|
||||||
code = models.CharField(
|
code = models.CharField(
|
||||||
_("code"),
|
_("code"),
|
||||||
@@ -47,7 +47,7 @@ class UE(models.Model):
|
|||||||
validators.RegexValidator(
|
validators.RegexValidator(
|
||||||
regex="([A-Z0-9]+)",
|
regex="([A-Z0-9]+)",
|
||||||
message=_(
|
message=_(
|
||||||
"The code of an UE must only contains "
|
"The code of an UV must only contains "
|
||||||
"uppercase characters without accent and numbers"
|
"uppercase characters without accent and numbers"
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -55,7 +55,7 @@ class UE(models.Model):
|
|||||||
)
|
)
|
||||||
author = models.ForeignKey(
|
author = models.ForeignKey(
|
||||||
User,
|
User,
|
||||||
related_name="ue_created",
|
related_name="uv_created",
|
||||||
verbose_name=_("author"),
|
verbose_name=_("author"),
|
||||||
null=False,
|
null=False,
|
||||||
blank=False,
|
blank=False,
|
||||||
@@ -64,23 +64,29 @@ class UE(models.Model):
|
|||||||
credit_type = models.CharField(
|
credit_type = models.CharField(
|
||||||
_("credit type"),
|
_("credit type"),
|
||||||
max_length=10,
|
max_length=10,
|
||||||
choices=settings.SITH_PEDAGOGY_UE_TYPE,
|
choices=settings.SITH_PEDAGOGY_UV_TYPE,
|
||||||
default=settings.SITH_PEDAGOGY_UE_TYPE[0][0],
|
default=settings.SITH_PEDAGOGY_UV_TYPE[0][0],
|
||||||
)
|
)
|
||||||
manager = models.CharField(_("ue manager"), max_length=300)
|
manager = models.CharField(_("uv manager"), max_length=300)
|
||||||
semester = models.CharField(
|
semester = models.CharField(
|
||||||
_("semester"),
|
_("semester"),
|
||||||
max_length=20,
|
max_length=20,
|
||||||
choices=settings.SITH_PEDAGOGY_UE_SEMESTER,
|
choices=settings.SITH_PEDAGOGY_UV_SEMESTER,
|
||||||
default=settings.SITH_PEDAGOGY_UE_SEMESTER[0][0],
|
default=settings.SITH_PEDAGOGY_UV_SEMESTER[0][0],
|
||||||
)
|
)
|
||||||
language = models.CharField(
|
language = models.CharField(
|
||||||
_("language"),
|
_("language"),
|
||||||
max_length=10,
|
max_length=10,
|
||||||
choices=settings.SITH_PEDAGOGY_UE_LANGUAGE,
|
choices=settings.SITH_PEDAGOGY_UV_LANGUAGE,
|
||||||
default=settings.SITH_PEDAGOGY_UE_LANGUAGE[0][0],
|
default=settings.SITH_PEDAGOGY_UV_LANGUAGE[0][0],
|
||||||
)
|
)
|
||||||
credits = models.PositiveIntegerField(_("credits"))
|
credits = models.IntegerField(
|
||||||
|
_("credits"),
|
||||||
|
validators=[validators.MinValueValidator(0)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
|
)
|
||||||
|
# Double star type not implemented yet
|
||||||
|
|
||||||
department = models.CharField(
|
department = models.CharField(
|
||||||
_("departmenmt"),
|
_("departmenmt"),
|
||||||
@@ -89,9 +95,9 @@ class UE(models.Model):
|
|||||||
default=settings.SITH_PROFILE_DEPARTMENTS[-1][0],
|
default=settings.SITH_PROFILE_DEPARTMENTS[-1][0],
|
||||||
)
|
)
|
||||||
|
|
||||||
# All texts about the UE
|
# All texts about the UV
|
||||||
title = models.CharField(_("title"), max_length=300)
|
title = models.CharField(_("title"), max_length=300)
|
||||||
manager = models.CharField(_("ue manager"), max_length=300)
|
manager = models.CharField(_("uv manager"), max_length=300)
|
||||||
objectives = models.TextField(_("objectives"))
|
objectives = models.TextField(_("objectives"))
|
||||||
program = models.TextField(_("program"))
|
program = models.TextField(_("program"))
|
||||||
skills = models.TextField(_("skills"))
|
skills = models.TextField(_("skills"))
|
||||||
@@ -99,17 +105,47 @@ class UE(models.Model):
|
|||||||
|
|
||||||
# Hours types CM, TD, TP, THE and TE
|
# Hours types CM, TD, TP, THE and TE
|
||||||
# Kind of dirty but I have nothing else in mind for now
|
# Kind of dirty but I have nothing else in mind for now
|
||||||
hours_CM = models.PositiveIntegerField(_("hours CM"), default=0)
|
hours_CM = models.IntegerField(
|
||||||
hours_TD = models.PositiveIntegerField(_("hours TD"), default=0)
|
_("hours CM"),
|
||||||
hours_TP = models.PositiveIntegerField(_("hours TP"), default=0)
|
validators=[validators.MinValueValidator(0)],
|
||||||
hours_THE = models.PositiveIntegerField(_("hours THE"), default=0)
|
blank=False,
|
||||||
hours_TE = models.PositiveIntegerField(_("hours TE"), default=0)
|
null=False,
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
hours_TD = models.IntegerField(
|
||||||
|
_("hours TD"),
|
||||||
|
validators=[validators.MinValueValidator(0)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
hours_TP = models.IntegerField(
|
||||||
|
_("hours TP"),
|
||||||
|
validators=[validators.MinValueValidator(0)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
hours_THE = models.IntegerField(
|
||||||
|
_("hours THE"),
|
||||||
|
validators=[validators.MinValueValidator(0)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
hours_TE = models.IntegerField(
|
||||||
|
_("hours TE"),
|
||||||
|
validators=[validators.MinValueValidator(0)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
|
default=0,
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.code
|
return self.code
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse("pedagogy:ue_detail", kwargs={"ue_id": self.id})
|
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.id})
|
||||||
|
|
||||||
def __grade_average_generic(self, field):
|
def __grade_average_generic(self, field):
|
||||||
comments = self.comments.filter(**{field + "__gte": 0})
|
comments = self.comments.filter(**{field + "__gte": 0})
|
||||||
@@ -124,7 +160,7 @@ class UE(models.Model):
|
|||||||
This function checks that no other comment has been posted by a specified user.
|
This function checks that no other comment has been posted by a specified user.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
True if the user has already posted a comment on this UE, else False.
|
True if the user has already posted a comment on this UV, else False.
|
||||||
"""
|
"""
|
||||||
return self.comments.filter(author=user).exists()
|
return self.comments.filter(author=user).exists()
|
||||||
|
|
||||||
@@ -149,66 +185,78 @@ class UE(models.Model):
|
|||||||
return self.__grade_average_generic("grade_work_load")
|
return self.__grade_average_generic("grade_work_load")
|
||||||
|
|
||||||
|
|
||||||
class UECommentQuerySet(models.QuerySet):
|
class UVCommentQuerySet(models.QuerySet):
|
||||||
def viewable_by(self, user: User) -> Self:
|
def viewable_by(self, user: User) -> Self:
|
||||||
if user.has_perms(["pedagogy.view_uecomment", "pedagogy.view_uecommentreport"]):
|
if user.has_perms(["pedagogy.view_uvcomment", "pedagogy.view_uvcommentreport"]):
|
||||||
# the user can view ue comment reports,
|
# the user can view uv comment reports,
|
||||||
# so he can view non-moderated comments
|
# so he can view non-moderated comments
|
||||||
return self
|
return self
|
||||||
if user.has_perm("pedagogy.view_uecomment"):
|
if user.has_perm("pedagogy.view_uvcomment"):
|
||||||
return self.filter(reports=None)
|
return self.filter(reports=None)
|
||||||
return self.filter(author=user)
|
return self.filter(author=user)
|
||||||
|
|
||||||
def annotate_is_reported(self) -> Self:
|
def annotate_is_reported(self) -> Self:
|
||||||
return self.annotate(
|
return self.annotate(
|
||||||
is_reported=Exists(UECommentReport.objects.filter(comment=OuterRef("pk")))
|
is_reported=Exists(UVCommentReport.objects.filter(comment=OuterRef("pk")))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class UEComment(models.Model):
|
class UVComment(models.Model):
|
||||||
"""A comment about an UE."""
|
"""A comment about an UV."""
|
||||||
|
|
||||||
author = models.ForeignKey(
|
author = models.ForeignKey(
|
||||||
User,
|
User,
|
||||||
related_name="ue_comments",
|
related_name="uv_comments",
|
||||||
verbose_name=_("author"),
|
verbose_name=_("author"),
|
||||||
|
null=False,
|
||||||
|
blank=False,
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
)
|
)
|
||||||
ue = models.ForeignKey(
|
uv = models.ForeignKey(
|
||||||
UE, related_name="comments", verbose_name=_("ue"), on_delete=models.CASCADE
|
UV, related_name="comments", verbose_name=_("uv"), on_delete=models.CASCADE
|
||||||
)
|
)
|
||||||
comment = models.TextField(_("comment"), blank=True, default="")
|
comment = models.TextField(_("comment"), blank=True)
|
||||||
grade_global = models.IntegerField(
|
grade_global = models.IntegerField(
|
||||||
_("global grade"),
|
_("global grade"),
|
||||||
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
default=-1,
|
default=-1,
|
||||||
)
|
)
|
||||||
grade_utility = models.IntegerField(
|
grade_utility = models.IntegerField(
|
||||||
_("utility grade"),
|
_("utility grade"),
|
||||||
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
default=-1,
|
default=-1,
|
||||||
)
|
)
|
||||||
grade_interest = models.IntegerField(
|
grade_interest = models.IntegerField(
|
||||||
_("interest grade"),
|
_("interest grade"),
|
||||||
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
default=-1,
|
default=-1,
|
||||||
)
|
)
|
||||||
grade_teaching = models.IntegerField(
|
grade_teaching = models.IntegerField(
|
||||||
_("teaching grade"),
|
_("teaching grade"),
|
||||||
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
default=-1,
|
default=-1,
|
||||||
)
|
)
|
||||||
grade_work_load = models.IntegerField(
|
grade_work_load = models.IntegerField(
|
||||||
_("work load grade"),
|
_("work load grade"),
|
||||||
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
validators=[validators.MinValueValidator(-1), validators.MaxValueValidator(4)],
|
||||||
|
blank=False,
|
||||||
|
null=False,
|
||||||
default=-1,
|
default=-1,
|
||||||
)
|
)
|
||||||
publish_date = models.DateTimeField(_("publish date"), blank=True)
|
publish_date = models.DateTimeField(_("publish date"), blank=True)
|
||||||
|
|
||||||
objects = UECommentQuerySet.as_manager()
|
objects = UVCommentQuerySet.as_manager()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.ue} - {self.author}"
|
return f"{self.uv} - {self.author}"
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if self.publish_date is None:
|
if self.publish_date is None:
|
||||||
@@ -220,32 +268,30 @@ class UEComment(models.Model):
|
|||||||
# to use this model.
|
# to use this model.
|
||||||
# However, it seems that the implementation finally didn't happen.
|
# However, it seems that the implementation finally didn't happen.
|
||||||
# It should be discussed, when possible, of what to do with that :
|
# It should be discussed, when possible, of what to do with that :
|
||||||
# - go on and finally implement the UE results features ?
|
# - go on and finally implement the UV results features ?
|
||||||
# - or fuck go back and remove this model ?
|
# - or fuck go back and remove this model ?
|
||||||
class UEResult(models.Model):
|
class UVResult(models.Model):
|
||||||
"""Results got to an UE.
|
"""Results got to an UV.
|
||||||
|
|
||||||
Views will be implemented after the first release
|
Views will be implemented after the first release
|
||||||
Will list every UE done by an user
|
Will list every UV done by an user
|
||||||
Linked to user and ue
|
Linked to user
|
||||||
Contains a grade settings.SITH_PEDAGOGY_UE_RESULT_GRADE
|
uv
|
||||||
|
Contains a grade settings.SITH_PEDAGOGY_UV_RESULT_GRADE
|
||||||
a semester (P/A)20xx.
|
a semester (P/A)20xx.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
ue = models.ForeignKey(
|
uv = models.ForeignKey(
|
||||||
UE, related_name="results", verbose_name=_("ue"), on_delete=models.CASCADE
|
UV, related_name="results", verbose_name=_("uv"), on_delete=models.CASCADE
|
||||||
)
|
)
|
||||||
user = models.ForeignKey(
|
user = models.ForeignKey(
|
||||||
User,
|
User, related_name="uv_results", verbose_name=("user"), on_delete=models.CASCADE
|
||||||
related_name="ue_results",
|
|
||||||
verbose_name=_("user"),
|
|
||||||
on_delete=models.CASCADE,
|
|
||||||
)
|
)
|
||||||
grade = models.CharField(
|
grade = models.CharField(
|
||||||
_("grade"),
|
_("grade"),
|
||||||
max_length=10,
|
max_length=10,
|
||||||
choices=settings.SITH_PEDAGOGY_UE_RESULT_GRADE,
|
choices=settings.SITH_PEDAGOGY_UV_RESULT_GRADE,
|
||||||
default=settings.SITH_PEDAGOGY_UE_RESULT_GRADE[0][0],
|
default=settings.SITH_PEDAGOGY_UV_RESULT_GRADE[0][0],
|
||||||
)
|
)
|
||||||
semester = models.CharField(
|
semester = models.CharField(
|
||||||
_("semester"),
|
_("semester"),
|
||||||
@@ -254,21 +300,21 @@ class UEResult(models.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.user.username} ; {self.ue.code} ; {self.grade}"
|
return f"{self.user.username} ; {self.uv.code} ; {self.grade}"
|
||||||
|
|
||||||
|
|
||||||
class UECommentReport(models.Model):
|
class UVCommentReport(models.Model):
|
||||||
"""Report an inapropriate comment."""
|
"""Report an inapropriate comment."""
|
||||||
|
|
||||||
comment = models.ForeignKey(
|
comment = models.ForeignKey(
|
||||||
UEComment,
|
UVComment,
|
||||||
related_name="reports",
|
related_name="reports",
|
||||||
verbose_name=_("report"),
|
verbose_name=_("report"),
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
)
|
)
|
||||||
reporter = models.ForeignKey(
|
reporter = models.ForeignKey(
|
||||||
User,
|
User,
|
||||||
related_name="reported_ue_comment",
|
related_name="reported_uv_comment",
|
||||||
verbose_name=_("reporter"),
|
verbose_name=_("reporter"),
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
)
|
)
|
||||||
@@ -278,5 +324,5 @@ class UECommentReport(models.Model):
|
|||||||
return f"{self.reporter.username} : {self.reason}"
|
return f"{self.reporter.username} : {self.reason}"
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def ue(self):
|
def uv(self):
|
||||||
return self.comment.ue
|
return self.comment.uv
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ from ninja import FilterLookup, FilterSchema, ModelSchema, Schema
|
|||||||
from pydantic import AliasPath, ConfigDict, Field, TypeAdapter
|
from pydantic import AliasPath, ConfigDict, Field, TypeAdapter
|
||||||
from pydantic.alias_generators import to_camel
|
from pydantic.alias_generators import to_camel
|
||||||
|
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
|
|
||||||
|
|
||||||
class UtbmShortUeSchema(Schema):
|
class UtbmShortUvSchema(Schema):
|
||||||
"""Short representation of an UE in the UTBM API.
|
"""Short representation of an UV in the UTBM API.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
This schema holds only the fields we actually need.
|
This schema holds only the fields we actually need.
|
||||||
@@ -35,8 +35,8 @@ class WorkloadSchema(Schema):
|
|||||||
nbh: int
|
nbh: int
|
||||||
|
|
||||||
|
|
||||||
class SemesterUeState(Schema):
|
class SemesterUvState(Schema):
|
||||||
"""The state of the UE during either autumn or spring semester"""
|
"""The state of the UV during either autumn or spring semester"""
|
||||||
|
|
||||||
model_config = ConfigDict(alias_generator=to_camel)
|
model_config = ConfigDict(alias_generator=to_camel)
|
||||||
|
|
||||||
@@ -44,11 +44,11 @@ class SemesterUeState(Schema):
|
|||||||
ouvert: bool
|
ouvert: bool
|
||||||
|
|
||||||
|
|
||||||
ShortUeList = TypeAdapter(list[UtbmShortUeSchema])
|
ShortUvList = TypeAdapter(list[UtbmShortUvSchema])
|
||||||
|
|
||||||
|
|
||||||
class UtbmFullUeSchema(Schema):
|
class UtbmFullUvSchema(Schema):
|
||||||
"""Long representation of an UE in the UTBM API."""
|
"""Long representation of an UV in the UTBM API."""
|
||||||
|
|
||||||
model_config = ConfigDict(alias_generator=to_camel)
|
model_config = ConfigDict(alias_generator=to_camel)
|
||||||
|
|
||||||
@@ -71,11 +71,11 @@ class UtbmFullUeSchema(Schema):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SimpleUeSchema(ModelSchema):
|
class SimpleUvSchema(ModelSchema):
|
||||||
"""Our minimal representation of an UE."""
|
"""Our minimal representation of an UV."""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UE
|
model = UV
|
||||||
fields = [
|
fields = [
|
||||||
"id",
|
"id",
|
||||||
"title",
|
"title",
|
||||||
@@ -86,11 +86,11 @@ class SimpleUeSchema(ModelSchema):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class UeSchema(ModelSchema):
|
class UvSchema(ModelSchema):
|
||||||
"""Our complete representation of an UE"""
|
"""Our complete representation of an UV"""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = UE
|
model = UV
|
||||||
fields = [
|
fields = [
|
||||||
"id",
|
"id",
|
||||||
"title",
|
"title",
|
||||||
@@ -113,7 +113,7 @@ class UeSchema(ModelSchema):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class UeFilterSchema(FilterSchema):
|
class UvFilterSchema(FilterSchema):
|
||||||
search: Annotated[str | None, FilterLookup("code__icontains")] = None
|
search: Annotated[str | None, FilterLookup("code__icontains")] = None
|
||||||
semester: set[Literal["AUTUMN", "SPRING"]] | None = None
|
semester: set[Literal["AUTUMN", "SPRING"]] | None = None
|
||||||
credit_type: Annotated[
|
credit_type: Annotated[
|
||||||
@@ -132,12 +132,12 @@ class UeFilterSchema(FilterSchema):
|
|||||||
return Q()
|
return Q()
|
||||||
|
|
||||||
if len(value) < 3 or (len(value) < 5 and any(c.isdigit() for c in value)):
|
if len(value) < 3 or (len(value) < 5 and any(c.isdigit() for c in value)):
|
||||||
# Likely to be an UE code
|
# Likely to be an UV code
|
||||||
return Q(code__istartswith=value)
|
return Q(code__istartswith=value)
|
||||||
|
|
||||||
qs = list(
|
qs = list(
|
||||||
SearchQuerySet()
|
SearchQuerySet()
|
||||||
.models(UE)
|
.models(UV)
|
||||||
.autocomplete(auto=html.escape(value))
|
.autocomplete(auto=html.escape(value))
|
||||||
.values_list("pk", flat=True)
|
.values_list("pk", flat=True)
|
||||||
)
|
)
|
||||||
@@ -147,7 +147,7 @@ class UeFilterSchema(FilterSchema):
|
|||||||
def filter_semester(self, value: set[str] | None) -> Q:
|
def filter_semester(self, value: set[str] | None) -> Q:
|
||||||
"""Special filter for the semester.
|
"""Special filter for the semester.
|
||||||
|
|
||||||
If either "SPRING" or "AUTUMN" is given, UE that are available
|
If either "SPRING" or "AUTUMN" is given, UV that are available
|
||||||
during "AUTUMN_AND_SPRING" will be filtered.
|
during "AUTUMN_AND_SPRING" will be filtered.
|
||||||
"""
|
"""
|
||||||
if not value:
|
if not value:
|
||||||
|
|||||||
@@ -25,28 +25,28 @@ from django.db import models
|
|||||||
from haystack import indexes, signals
|
from haystack import indexes, signals
|
||||||
|
|
||||||
from core.search_indexes import BigCharFieldIndex
|
from core.search_indexes import BigCharFieldIndex
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
|
|
||||||
|
|
||||||
class IndexSignalProcessor(signals.BaseSignalProcessor):
|
class IndexSignalProcessor(signals.BaseSignalProcessor):
|
||||||
"""Auto update index on CRUD operations."""
|
"""Auto update index on CRUD operations."""
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
# Listen only to the ``UE`` model.
|
# Listen only to the ``UV`` model.
|
||||||
models.signals.post_save.connect(self.handle_save, sender=UE)
|
models.signals.post_save.connect(self.handle_save, sender=UV)
|
||||||
models.signals.post_delete.connect(self.handle_delete, sender=UE)
|
models.signals.post_delete.connect(self.handle_delete, sender=UV)
|
||||||
|
|
||||||
def teardown(self):
|
def teardown(self):
|
||||||
# Disconnect only to the ``UE`` model.
|
# Disconnect only to the ``UV`` model.
|
||||||
models.signals.post_save.disconnect(self.handle_save, sender=UE)
|
models.signals.post_save.disconnect(self.handle_save, sender=UV)
|
||||||
models.signals.post_delete.disconnect(self.handle_delete, sender=UE)
|
models.signals.post_delete.disconnect(self.handle_delete, sender=UV)
|
||||||
|
|
||||||
|
|
||||||
class UEIndex(indexes.SearchIndex, indexes.Indexable):
|
class UVIndex(indexes.SearchIndex, indexes.Indexable):
|
||||||
"""Indexer class for UEs."""
|
"""Indexer class for UVs."""
|
||||||
|
|
||||||
text = BigCharFieldIndex(document=True, use_template=True)
|
text = BigCharFieldIndex(document=True, use_template=True)
|
||||||
auto = indexes.EdgeNgramField(use_template=True)
|
auto = indexes.EdgeNgramField(use_template=True)
|
||||||
|
|
||||||
def get_model(self):
|
def get_model(self):
|
||||||
return UE
|
return UV
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { History, getCurrentUrlParams, updateQueryString } from "#core:utils/history";
|
import { History, getCurrentUrlParams, updateQueryString } from "#core:utils/history";
|
||||||
import { ueFetchUeList } from "#openapi";
|
import { uvFetchUvList } from "#openapi";
|
||||||
|
|
||||||
const pageDefault = 1;
|
const pageDefault = 1;
|
||||||
const pageSizeDefault = 100;
|
const pageSizeDefault = 100;
|
||||||
|
|
||||||
document.addEventListener("alpine:init", () => {
|
document.addEventListener("alpine:init", () => {
|
||||||
Alpine.data("ue_search", () => ({
|
Alpine.data("uv_search", () => ({
|
||||||
ues: {
|
uvs: {
|
||||||
count: 0,
|
count: 0,
|
||||||
next: null,
|
next: null,
|
||||||
previous: null,
|
previous: null,
|
||||||
@@ -103,12 +103,16 @@ document.addEventListener("alpine:init", () => {
|
|||||||
args[param] = value;
|
args[param] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.ues = (await ueFetchUeList({ query: args })).data;
|
this.uvs = (
|
||||||
|
await uvFetchUvList({
|
||||||
|
query: args,
|
||||||
|
})
|
||||||
|
).data;
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
maxPage() {
|
maxPage() {
|
||||||
return Math.ceil(this.ues.count / this.page_size);
|
return Math.ceil(this.uvs.count / this.page_size);
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ $large-devices: 992px;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ue-list {
|
#uv-list {
|
||||||
font-size: 1.1em;
|
font-size: 1.1em;
|
||||||
|
|
||||||
overflow-wrap: break-word;
|
overflow-wrap: break-word;
|
||||||
@@ -164,10 +164,10 @@ $large-devices: 992px;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ue_detail {
|
#uv_detail {
|
||||||
color: #062f38;
|
color: #062f38;
|
||||||
|
|
||||||
.ue-quick-info-container {
|
.uv-quick-info-container {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 20% 20% 20% 20% auto;
|
grid-template-columns: 20% 20% 20% 20% auto;
|
||||||
grid-template-rows: auto auto;
|
grid-template-rows: auto auto;
|
||||||
@@ -254,20 +254,20 @@ $large-devices: 992px;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.ue-details-container {
|
.uv-details-container {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 150px 100px auto;
|
grid-template-columns: 150px 100px auto;
|
||||||
grid-template-rows: 156px 1fr;
|
grid-template-rows: 156px 1fr;
|
||||||
grid-template-areas:
|
grid-template-areas:
|
||||||
"grade grade-stars ue-infos"
|
"grade grade-stars uv-infos"
|
||||||
". . ue-infos";
|
". . uv-infos";
|
||||||
|
|
||||||
@media screen and (max-width: $large-devices) {
|
@media screen and (max-width: $large-devices) {
|
||||||
grid-template-columns: 50% 50%;
|
grid-template-columns: 50% 50%;
|
||||||
grid-template-rows: auto auto;
|
grid-template-rows: auto auto;
|
||||||
grid-template-areas:
|
grid-template-areas:
|
||||||
"grade grade-stars"
|
"grade grade-stars"
|
||||||
"ue-infos ue-infos";
|
"uv-infos uv-infos";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,8 +290,8 @@ $large-devices: 992px;
|
|||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
.ue-infos {
|
.uv-infos {
|
||||||
grid-area: ue-infos;
|
grid-area: uv-infos;
|
||||||
padding-left: 10px;
|
padding-left: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,10 +23,10 @@
|
|||||||
{% endblock head %}
|
{% endblock head %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% if user.has_perm("pedagogy.add_ue") %}
|
{% if user.has_perm("pedagogy.add_uv") %}
|
||||||
<div class="action-bar">
|
<div class="action-bar">
|
||||||
<p>
|
<p>
|
||||||
<a href="{{ url('pedagogy:ue_create') }}">{% trans %}Create UE{% endtrans %}</a>
|
<a href="{{ url('pedagogy:uv_create') }}">{% trans %}Create UV{% endtrans %}</a>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<a href="{{ url('pedagogy:moderation') }}">{% trans %}Moderate comments{% endtrans %}</a>
|
<a href="{{ url('pedagogy:moderation') }}">{% trans %}Moderate comments{% endtrans %}</a>
|
||||||
@@ -34,7 +34,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<br/>
|
<br/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="pedagogy" x-data="ue_search" x-cloak>
|
<div class="pedagogy" x-data="uv_search" x-cloak>
|
||||||
<form id="search_form">
|
<form id="search_form">
|
||||||
<div class="search-form-container">
|
<div class="search-form-container">
|
||||||
<div class="search-bar">
|
<div class="search-bar">
|
||||||
@@ -89,43 +89,43 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<table id="ue-list">
|
<table id="uv-list">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<td>{% trans %}UE{% endtrans %}</td>
|
<td>{% trans %}UV{% endtrans %}</td>
|
||||||
<td>{% trans %}Title{% endtrans %}</td>
|
<td>{% trans %}Title{% endtrans %}</td>
|
||||||
<td>{% trans %}Department{% endtrans %}</td>
|
<td>{% trans %}Department{% endtrans %}</td>
|
||||||
<td>{% trans %}Credit type{% endtrans %}</td>
|
<td>{% trans %}Credit type{% endtrans %}</td>
|
||||||
<td><i class="fa fa-leaf"></i></td>
|
<td><i class="fa fa-leaf"></i></td>
|
||||||
<td><i class="fa-regular fa-sun"></i></td>
|
<td><i class="fa-regular fa-sun"></i></td>
|
||||||
{%- if user.has_perm("pedagogy.change_ue") -%}
|
{%- if user.has_perm("pedagogy.change_uv") -%}
|
||||||
<td>{% trans %}Edit{% endtrans %}</td>
|
<td>{% trans %}Edit{% endtrans %}</td>
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
{%- if user.has_perm("pedagogy.delete_ue") -%}
|
{%- if user.has_perm("pedagogy.delete_uv") -%}
|
||||||
<td>{% trans %}Delete{% endtrans %}</td>
|
<td>{% trans %}Delete{% endtrans %}</td>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody :aria-busy="loading">
|
<tbody :aria-busy="loading">
|
||||||
<template x-for="ue in ues.results" :key="ue.id">
|
<template x-for="uv in uvs.results" :key="uv.id">
|
||||||
<tr
|
<tr
|
||||||
@click="window.location.href = `/pedagogy/ue/${ue.id}`"
|
@click="window.location.href = `/pedagogy/uv/${uv.id}`"
|
||||||
class="clickable"
|
class="clickable"
|
||||||
:class="{closed: ue.semester === 'CLOSED'}"
|
:class="{closed: uv.semester === 'CLOSED'}"
|
||||||
>
|
>
|
||||||
<td><a :href="`/pedagogy/ue/${ue.id}`" x-text="ue.code"></a></td>
|
<td><a :href="`/pedagogy/uv/${uv.id}`" x-text="uv.code"></a></td>
|
||||||
<td class="title"
|
<td class="title"
|
||||||
x-text="ue.title + (ue.semester === 'CLOSED' ? ' ({% trans %}closed ue{% endtrans %})' : '')"
|
x-text="uv.title + (uv.semester === 'CLOSED' ? ' ({% trans %}closed uv{% endtrans %})' : '')"
|
||||||
></td>
|
></td>
|
||||||
<td x-text="ue.department"></td>
|
<td x-text="uv.department"></td>
|
||||||
<td x-text="ue.credit_type"></td>
|
<td x-text="uv.credit_type"></td>
|
||||||
<td><i :class="ue.semester.includes('AUTUMN') && 'fa fa-leaf'"></i></td>
|
<td><i :class="uv.semester.includes('AUTUMN') && 'fa fa-leaf'"></i></td>
|
||||||
<td><i :class="ue.semester.includes('SPRING') && 'fa-regular fa-sun'"></i></td>
|
<td><i :class="uv.semester.includes('SPRING') && 'fa-regular fa-sun'"></i></td>
|
||||||
{%- if user.has_perm("pedagogy.change_ue") -%}
|
{%- if user.has_perm("pedagogy.change_uv") -%}
|
||||||
<td><a :href="`/pedagogy/ue/${ue.id}/edit`">{% trans %}Edit{% endtrans %}</a></td>
|
<td><a :href="`/pedagogy/uv/${uv.id}/edit`">{% trans %}Edit{% endtrans %}</a></td>
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
{%- if user.has_perm("pedagogy.delete_ue") -%}
|
{%- if user.has_perm("pedagogy.delete_uv") -%}
|
||||||
<td><a :href="`/pedagogy/ue/${ue.id}/delete`">{% trans %}Delete{% endtrans %}</a></td>
|
<td><a :href="`/pedagogy/uv/${uv.id}/delete`">{% trans %}Delete{% endtrans %}</a></td>
|
||||||
{%- endif -%}
|
{%- endif -%}
|
||||||
</tr>
|
</tr>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{% extends "core/base.jinja" %}
|
{% extends "core/base.jinja" %}
|
||||||
|
|
||||||
{% block title %}
|
{% block title %}
|
||||||
{% trans %}UE comment moderation{% endtrans %}
|
{% trans %}UV comment moderation{% endtrans %}
|
||||||
{% endblock title %}
|
{% endblock title %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<td>{% trans %}UE{% endtrans %}</td>
|
<td>{% trans %}UV{% endtrans %}</td>
|
||||||
<td>{% trans %}Comment{% endtrans %}</td>
|
<td>{% trans %}Comment{% endtrans %}</td>
|
||||||
<td>{% trans %}Reason{% endtrans %}</td>
|
<td>{% trans %}Reason{% endtrans %}</td>
|
||||||
<td>{% trans %}Action{% endtrans %}</td>
|
<td>{% trans %}Action{% endtrans %}</td>
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
<form action="{{ url('pedagogy:moderation') }}" method="post" enctype="multipart/form-data">
|
<form action="{{ url('pedagogy:moderation') }}" method="post" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<tr>
|
<tr>
|
||||||
<td><a href="{{ url('pedagogy:ue_detail', ue_id=report.comment.ue_id) }}#{{ report.comment.ue_id }}">{{ report.comment.ue }}</a></td>
|
<td><a href="{{ url('pedagogy:uv_detail', uv_id=report.comment.uv.id) }}#{{ report.comment.uv.id }}">{{ report.comment.uv }}</a></td>
|
||||||
<td>{{ report.comment.comment|markdown }}</td>
|
<td>{{ report.comment.comment|markdown }}</td>
|
||||||
<td>{{ report.reason|markdown }}</td>
|
<td>{{ report.reason|markdown }}</td>
|
||||||
<td>
|
<td>
|
||||||
|
|||||||
@@ -7,12 +7,12 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block title %}
|
{% block title %}
|
||||||
{% trans %}UE Details{% endtrans %}
|
{% trans %}UV Details{% endtrans %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="pedagogy">
|
<div class="pedagogy">
|
||||||
<div id="ue_detail">
|
<div id="uv_detail">
|
||||||
<button onclick='(function(){
|
<button onclick='(function(){
|
||||||
// If comes from the guide page, go back with history
|
// If comes from the guide page, go back with history
|
||||||
if (document.referrer.replace(/\?(.+)/gm,"").endsWith(`{{ url("pedagogy:guide") }}`)){
|
if (document.referrer.replace(/\?(.+)/gm,"").endsWith(`{{ url("pedagogy:guide") }}`)){
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
|
|
||||||
<h1>{{ object.code }} - {{ object.title }}</h1>
|
<h1>{{ object.code }} - {{ object.title }}</h1>
|
||||||
<br>
|
<br>
|
||||||
<div class="ue-quick-info-container">
|
<div class="uv-quick-info-container">
|
||||||
<div class="hours-cm">
|
<div class="hours-cm">
|
||||||
<b>{% trans %}CM: {% endtrans %}</b>{{ object.hours_CM }}
|
<b>{% trans %}CM: {% endtrans %}</b>{{ object.hours_CM }}
|
||||||
</div>
|
</div>
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
<div class="ue-details-container">
|
<div class="uv-details-container">
|
||||||
<div class="grade">
|
<div class="grade">
|
||||||
<p>{% trans %}Global grade{% endtrans %}</p>
|
<p>{% trans %}Global grade{% endtrans %}</p>
|
||||||
<p>{% trans %}Utility{% endtrans %}</p>
|
<p>{% trans %}Utility{% endtrans %}</p>
|
||||||
@@ -70,7 +70,7 @@
|
|||||||
<p>{{ display_star(object.grade_teaching_average) }}</p>
|
<p>{{ display_star(object.grade_teaching_average) }}</p>
|
||||||
<p>{{ display_star(object.grade_work_load_average) }}</p>
|
<p>{{ display_star(object.grade_work_load_average) }}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="ue-infos">
|
<div class="uv-infos">
|
||||||
<p><b>{% trans %}Objectives{% endtrans %}</b></p>
|
<p><b>{% trans %}Objectives{% endtrans %}</b></p>
|
||||||
<p>{{ object.objectives|markdown }}</p>
|
<p>{{ object.objectives|markdown }}</p>
|
||||||
<p><b>{% trans %}Program{% endtrans %}</b></p>
|
<p><b>{% trans %}Program{% endtrans %}</b></p>
|
||||||
@@ -86,21 +86,21 @@
|
|||||||
<br>
|
<br>
|
||||||
{% if object.has_user_already_commented(user) %}
|
{% if object.has_user_already_commented(user) %}
|
||||||
<div id="leave_comment_not_allowed">
|
<div id="leave_comment_not_allowed">
|
||||||
<p>{% trans %}You already posted a comment on this UE. If you want to comment again, please modify or delete your previous comment.{% endtrans %}</p>
|
<p>{% trans %}You already posted a comment on this UV. If you want to comment again, please modify or delete your previous comment.{% endtrans %}</p>
|
||||||
</div>
|
</div>
|
||||||
{% elif user.has_perm("pedagogy.add_uecomment") %}
|
{% elif user.has_perm("pedagogy.add_uvcomment") %}
|
||||||
<details class="accordion" id="leave_comment">
|
<details class="accordion" id="leave_comment">
|
||||||
<summary>{% trans %}Leave comment{% endtrans %}</summary>
|
<summary>{% trans %}Leave comment{% endtrans %}</summary>
|
||||||
<div class="accordion-content">
|
<div class="accordion-content">
|
||||||
<form action="{{ url('pedagogy:ue_detail', ue_id=object.id) }}" method="post" enctype="multipart/form-data">
|
<form action="{{ url('pedagogy:uv_detail', uv_id=object.id) }}" method="post" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="leave-comment-grid-container">
|
<div class="leave-comment-grid-container">
|
||||||
<div class="form-stars">
|
<div class="form-stars">
|
||||||
{{ form.author.errors }}
|
{{ form.author.errors }}
|
||||||
{{ form.ue.errors }}
|
{{ form.uv.errors }}
|
||||||
|
|
||||||
{{ form.author }}
|
{{ form.author }}
|
||||||
{{ form.ue }}
|
{{ form.uv }}
|
||||||
|
|
||||||
<div class="input-stars">
|
<div class="input-stars">
|
||||||
<label for="{{ form.grade_global.id_for_label }}">{{ form.grade_global.label }} :</label>
|
<label for="{{ form.grade_global.id_for_label }}">{{ form.grade_global.label }} :</label>
|
||||||
@@ -170,7 +170,7 @@
|
|||||||
|
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<div class="anchor">
|
<div class="anchor">
|
||||||
<a href="{{ url('pedagogy:ue_detail', ue_id=ue.id) }}#{{ comment.id }}"><i class="fa fa-paragraph"></i></a>
|
<a href="{{ url('pedagogy:uv_detail', uv_id=uv.id) }}#{{ comment.id }}"><i class="fa fa-paragraph"></i></a>
|
||||||
</div>
|
</div>
|
||||||
{{ comment.comment|markdown }}
|
{{ comment.comment|markdown }}
|
||||||
</div>
|
</div>
|
||||||
@@ -9,19 +9,19 @@ from model_bakery.recipe import Recipe
|
|||||||
|
|
||||||
from core.baker_recipes import subscriber_user
|
from core.baker_recipes import subscriber_user
|
||||||
from core.models import Group, User
|
from core.models import Group, User
|
||||||
from pedagogy.models import UE
|
from pedagogy.models import UV
|
||||||
|
|
||||||
|
|
||||||
class TestUESearch(TestCase):
|
class TestUVSearch(TestCase):
|
||||||
"""Test UE guide rights for view and API."""
|
"""Test UV guide rights for view and API."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.root = User.objects.get(username="root")
|
cls.root = User.objects.get(username="root")
|
||||||
cls.url = reverse("api:fetch_ues")
|
cls.url = reverse("api:fetch_uvs")
|
||||||
ue_recipe = Recipe(UE, author=cls.root)
|
uv_recipe = Recipe(UV, author=cls.root)
|
||||||
ues = [
|
uvs = [
|
||||||
ue_recipe.prepare(
|
uv_recipe.prepare(
|
||||||
code="AP4A",
|
code="AP4A",
|
||||||
credit_type="CS",
|
credit_type="CS",
|
||||||
semester="AUTUMN",
|
semester="AUTUMN",
|
||||||
@@ -32,7 +32,7 @@ class TestUESearch(TestCase):
|
|||||||
"Concepts fondamentaux et mise en pratique avec le langage C++"
|
"Concepts fondamentaux et mise en pratique avec le langage C++"
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
ue_recipe.prepare(
|
uv_recipe.prepare(
|
||||||
code="MT01",
|
code="MT01",
|
||||||
credit_type="CS",
|
credit_type="CS",
|
||||||
semester="AUTUMN",
|
semester="AUTUMN",
|
||||||
@@ -40,10 +40,10 @@ class TestUESearch(TestCase):
|
|||||||
manager="ben",
|
manager="ben",
|
||||||
title="Intégration1. Algèbre linéaire - Fonctions de deux variables",
|
title="Intégration1. Algèbre linéaire - Fonctions de deux variables",
|
||||||
),
|
),
|
||||||
ue_recipe.prepare(
|
uv_recipe.prepare(
|
||||||
code="PHYS11", credit_type="CS", semester="AUTUMN", department="TC"
|
code="PHYS11", credit_type="CS", semester="AUTUMN", department="TC"
|
||||||
),
|
),
|
||||||
ue_recipe.prepare(
|
uv_recipe.prepare(
|
||||||
code="TNEV",
|
code="TNEV",
|
||||||
credit_type="TM",
|
credit_type="TM",
|
||||||
semester="SPRING",
|
semester="SPRING",
|
||||||
@@ -51,10 +51,10 @@ class TestUESearch(TestCase):
|
|||||||
manager="moss",
|
manager="moss",
|
||||||
title="tnetennba",
|
title="tnetennba",
|
||||||
),
|
),
|
||||||
ue_recipe.prepare(
|
uv_recipe.prepare(
|
||||||
code="MT10", credit_type="TM", semester="AUTUMN", department="IMSI"
|
code="MT10", credit_type="TM", semester="AUTUMN", department="IMSI"
|
||||||
),
|
),
|
||||||
ue_recipe.prepare(
|
uv_recipe.prepare(
|
||||||
code="DA50",
|
code="DA50",
|
||||||
credit_type="TM",
|
credit_type="TM",
|
||||||
semester="AUTUMN_AND_SPRING",
|
semester="AUTUMN_AND_SPRING",
|
||||||
@@ -62,7 +62,7 @@ class TestUESearch(TestCase):
|
|||||||
manager="francky",
|
manager="francky",
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
UE.objects.bulk_create(ues)
|
UV.objects.bulk_create(uvs)
|
||||||
call_command("update_index")
|
call_command("update_index")
|
||||||
|
|
||||||
def test_permissions(self):
|
def test_permissions(self):
|
||||||
@@ -93,7 +93,7 @@ class TestUESearch(TestCase):
|
|||||||
"""Test that the return data format is correct"""
|
"""Test that the return data format is correct"""
|
||||||
self.client.force_login(self.root)
|
self.client.force_login(self.root)
|
||||||
res = self.client.get(self.url + "?search=PA00")
|
res = self.client.get(self.url + "?search=PA00")
|
||||||
ue = UE.objects.get(code="PA00")
|
uv = UV.objects.get(code="PA00")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
assert json.loads(res.content) == {
|
assert json.loads(res.content) == {
|
||||||
"count": 1,
|
"count": 1,
|
||||||
@@ -101,12 +101,12 @@ class TestUESearch(TestCase):
|
|||||||
"previous": None,
|
"previous": None,
|
||||||
"results": [
|
"results": [
|
||||||
{
|
{
|
||||||
"id": ue.id,
|
"id": uv.id,
|
||||||
"title": ue.title,
|
"title": uv.title,
|
||||||
"code": ue.code,
|
"code": uv.code,
|
||||||
"credit_type": ue.credit_type,
|
"credit_type": uv.credit_type,
|
||||||
"semester": ue.semester,
|
"semester": uv.semester,
|
||||||
"department": ue.department,
|
"department": uv.department,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ class TestUESearch(TestCase):
|
|||||||
def test_search_by_text(self):
|
def test_search_by_text(self):
|
||||||
self.client.force_login(self.root)
|
self.client.force_login(self.root)
|
||||||
for query, expected in (
|
for query, expected in (
|
||||||
# UE code search case insensitive
|
# UV code search case insensitive
|
||||||
("m", {"MT01", "MT10"}),
|
("m", {"MT01", "MT10"}),
|
||||||
("M", {"MT01", "MT10"}),
|
("M", {"MT01", "MT10"}),
|
||||||
("mt", {"MT01", "MT10"}),
|
("mt", {"MT01", "MT10"}),
|
||||||
@@ -126,24 +126,24 @@ class TestUESearch(TestCase):
|
|||||||
):
|
):
|
||||||
res = self.client.get(self.url + f"?search={query}")
|
res = self.client.get(self.url + f"?search={query}")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
assert {ue["code"] for ue in json.loads(res.content)["results"]} == expected
|
assert {uv["code"] for uv in json.loads(res.content)["results"]} == expected
|
||||||
|
|
||||||
def test_search_by_credit_type(self):
|
def test_search_by_credit_type(self):
|
||||||
self.client.force_login(self.root)
|
self.client.force_login(self.root)
|
||||||
res = self.client.get(self.url + "?credit_type=CS")
|
res = self.client.get(self.url + "?credit_type=CS")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
codes = [ue["code"] for ue in json.loads(res.content)["results"]]
|
codes = [uv["code"] for uv in json.loads(res.content)["results"]]
|
||||||
assert codes == ["AP4A", "MT01", "PHYS11"]
|
assert codes == ["AP4A", "MT01", "PHYS11"]
|
||||||
res = self.client.get(self.url + "?credit_type=CS&credit_type=OM")
|
res = self.client.get(self.url + "?credit_type=CS&credit_type=OM")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
codes = {ue["code"] for ue in json.loads(res.content)["results"]}
|
codes = {uv["code"] for uv in json.loads(res.content)["results"]}
|
||||||
assert codes == {"AP4A", "MT01", "PHYS11", "PA00"}
|
assert codes == {"AP4A", "MT01", "PHYS11", "PA00"}
|
||||||
|
|
||||||
def test_search_by_semester(self):
|
def test_search_by_semester(self):
|
||||||
self.client.force_login(self.root)
|
self.client.force_login(self.root)
|
||||||
res = self.client.get(self.url + "?semester=SPRING")
|
res = self.client.get(self.url + "?semester=SPRING")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
codes = {ue["code"] for ue in json.loads(res.content)["results"]}
|
codes = {uv["code"] for uv in json.loads(res.content)["results"]}
|
||||||
assert codes == {"DA50", "TNEV", "PA00"}
|
assert codes == {"DA50", "TNEV", "PA00"}
|
||||||
|
|
||||||
def test_search_multiple_filters(self):
|
def test_search_multiple_filters(self):
|
||||||
@@ -152,7 +152,7 @@ class TestUESearch(TestCase):
|
|||||||
self.url + "?semester=AUTUMN&credit_type=CS&department=TC"
|
self.url + "?semester=AUTUMN&credit_type=CS&department=TC"
|
||||||
)
|
)
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
codes = {ue["code"] for ue in json.loads(res.content)["results"]}
|
codes = {uv["code"] for uv in json.loads(res.content)["results"]}
|
||||||
assert codes == {"MT01", "PHYS11"}
|
assert codes == {"MT01", "PHYS11"}
|
||||||
|
|
||||||
def test_search_fails(self):
|
def test_search_fails(self):
|
||||||
@@ -163,15 +163,15 @@ class TestUESearch(TestCase):
|
|||||||
|
|
||||||
def test_search_pa00_fail(self):
|
def test_search_pa00_fail(self):
|
||||||
self.client.force_login(self.root)
|
self.client.force_login(self.root)
|
||||||
# Search with UE code
|
# Search with UV code
|
||||||
response = self.client.get(reverse("pedagogy:guide"), {"search": "IFC"})
|
response = self.client.get(reverse("pedagogy:guide"), {"search": "IFC"})
|
||||||
self.assertNotContains(response, text="PA00")
|
self.assertNotContains(response, text="PA00")
|
||||||
|
|
||||||
# Search with first letter of UE code
|
# Search with first letter of UV code
|
||||||
response = self.client.get(reverse("pedagogy:guide"), {"search": "I"})
|
response = self.client.get(reverse("pedagogy:guide"), {"search": "I"})
|
||||||
self.assertNotContains(response, text="PA00")
|
self.assertNotContains(response, text="PA00")
|
||||||
|
|
||||||
# Search with UE manager
|
# Search with UV manager
|
||||||
response = self.client.get(reverse("pedagogy:guide"), {"search": "GILLES"})
|
response = self.client.get(reverse("pedagogy:guide"), {"search": "GILLES"})
|
||||||
self.assertNotContains(response, text="PA00")
|
self.assertNotContains(response, text="PA00")
|
||||||
|
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ from pytest_django.asserts import assertRedirects
|
|||||||
|
|
||||||
from core.baker_recipes import old_subscriber_user, subscriber_user
|
from core.baker_recipes import old_subscriber_user, subscriber_user
|
||||||
from core.models import Notification, User
|
from core.models import Notification, User
|
||||||
from pedagogy.models import UE, UEComment, UECommentReport
|
from pedagogy.models import UV, UVComment, UVCommentReport
|
||||||
|
|
||||||
|
|
||||||
def create_ue_template(user_id, code="IFC1", exclude_list=None):
|
def create_uv_template(user_id, code="IFC1", exclude_list=None):
|
||||||
"""Factory to help UE creation/update in post requests."""
|
"""Factory to help UV creation/update in post requests."""
|
||||||
if exclude_list is None:
|
if exclude_list is None:
|
||||||
exclude_list = []
|
exclude_list = []
|
||||||
ue = {
|
uv = {
|
||||||
"code": code,
|
"code": code,
|
||||||
"author": user_id,
|
"author": user_id,
|
||||||
"credit_type": "TM",
|
"credit_type": "TM",
|
||||||
@@ -74,15 +74,15 @@ def create_ue_template(user_id, code="IFC1", exclude_list=None):
|
|||||||
* Chaînes de caractères""",
|
* Chaînes de caractères""",
|
||||||
}
|
}
|
||||||
for excluded in exclude_list:
|
for excluded in exclude_list:
|
||||||
ue.pop(excluded)
|
uv.pop(excluded)
|
||||||
return ue
|
return uv
|
||||||
|
|
||||||
|
|
||||||
# UE class tests
|
# UV class tests
|
||||||
|
|
||||||
|
|
||||||
class TestUECreation(TestCase):
|
class TestUVCreation(TestCase):
|
||||||
"""Test ue creation."""
|
"""Test uv creation."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@@ -90,62 +90,62 @@ class TestUECreation(TestCase):
|
|||||||
cls.tutu = User.objects.get(username="tutu")
|
cls.tutu = User.objects.get(username="tutu")
|
||||||
cls.sli = User.objects.get(username="sli")
|
cls.sli = User.objects.get(username="sli")
|
||||||
cls.guy = User.objects.get(username="guy")
|
cls.guy = User.objects.get(username="guy")
|
||||||
cls.create_ue_url = reverse("pedagogy:ue_create")
|
cls.create_uv_url = reverse("pedagogy:uv_create")
|
||||||
|
|
||||||
def test_create_ue_admin_success(self):
|
def test_create_uv_admin_success(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.create_ue_url, create_ue_template(self.bibou.id)
|
self.create_uv_url, create_uv_template(self.bibou.id)
|
||||||
)
|
)
|
||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
assert UE.objects.filter(code="IFC1").exists()
|
assert UV.objects.filter(code="IFC1").exists()
|
||||||
|
|
||||||
def test_create_ue_pedagogy_admin_success(self):
|
def test_create_uv_pedagogy_admin_success(self):
|
||||||
self.client.force_login(self.tutu)
|
self.client.force_login(self.tutu)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.create_ue_url, create_ue_template(self.tutu.id)
|
self.create_uv_url, create_uv_template(self.tutu.id)
|
||||||
)
|
)
|
||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
assert UE.objects.filter(code="IFC1").exists()
|
assert UV.objects.filter(code="IFC1").exists()
|
||||||
|
|
||||||
def test_create_ue_unauthorized_fail(self):
|
def test_create_uv_unauthorized_fail(self):
|
||||||
# Test with anonymous user
|
# Test with anonymous user
|
||||||
response = self.client.post(self.create_ue_url, create_ue_template(0))
|
response = self.client.post(self.create_uv_url, create_uv_template(0))
|
||||||
assertRedirects(
|
assertRedirects(
|
||||||
response, reverse("core:login", query={"next": self.create_ue_url})
|
response, reverse("core:login", query={"next": self.create_uv_url})
|
||||||
)
|
)
|
||||||
|
|
||||||
# Test with subscribed user
|
# Test with subscribed user
|
||||||
self.client.force_login(self.sli)
|
self.client.force_login(self.sli)
|
||||||
response = self.client.post(self.create_ue_url, create_ue_template(self.sli.id))
|
response = self.client.post(self.create_uv_url, create_uv_template(self.sli.id))
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
# Test with non subscribed user
|
# Test with non subscribed user
|
||||||
self.client.force_login(self.guy)
|
self.client.force_login(self.guy)
|
||||||
response = self.client.post(self.create_ue_url, create_ue_template(self.guy.id))
|
response = self.client.post(self.create_uv_url, create_uv_template(self.guy.id))
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
# Check that the UE has never been created
|
# Check that the UV has never been created
|
||||||
assert not UE.objects.filter(code="IFC1").exists()
|
assert not UV.objects.filter(code="IFC1").exists()
|
||||||
|
|
||||||
def test_create_ue_bad_request_fail(self):
|
def test_create_uv_bad_request_fail(self):
|
||||||
self.client.force_login(self.tutu)
|
self.client.force_login(self.tutu)
|
||||||
|
|
||||||
# Test with wrong user id (if someone cheats on the hidden input)
|
# Test with wrong user id (if someone cheats on the hidden input)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.create_ue_url, create_ue_template(self.bibou.id)
|
self.create_uv_url, create_uv_template(self.bibou.id)
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
# Remove a required field
|
# Remove a required field
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.create_ue_url,
|
self.create_uv_url,
|
||||||
create_ue_template(self.tutu.id, exclude_list=["title"]),
|
create_uv_template(self.tutu.id, exclude_list=["title"]),
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
# Check that the UE has never been created
|
# Check that the UV hase never been created
|
||||||
assert not UE.objects.filter(code="IFC1").exists()
|
assert not UV.objects.filter(code="IFC1").exists()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@@ -171,8 +171,8 @@ def test_guide_anonymous_permission_denied(client: Client):
|
|||||||
assert res.status_code == 302
|
assert res.status_code == 302
|
||||||
|
|
||||||
|
|
||||||
class TestUEDelete(TestCase):
|
class TestUVDelete(TestCase):
|
||||||
"""Test UE deletion rights."""
|
"""Test UV deletion rights."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@@ -180,37 +180,37 @@ class TestUEDelete(TestCase):
|
|||||||
cls.tutu = User.objects.get(username="tutu")
|
cls.tutu = User.objects.get(username="tutu")
|
||||||
cls.sli = User.objects.get(username="sli")
|
cls.sli = User.objects.get(username="sli")
|
||||||
cls.guy = User.objects.get(username="guy")
|
cls.guy = User.objects.get(username="guy")
|
||||||
cls.ue = UE.objects.get(code="PA00")
|
cls.uv = UV.objects.get(code="PA00")
|
||||||
cls.delete_ue_url = reverse("pedagogy:ue_delete", kwargs={"ue_id": cls.ue.id})
|
cls.delete_uv_url = reverse("pedagogy:uv_delete", kwargs={"uv_id": cls.uv.id})
|
||||||
|
|
||||||
def test_ue_delete_root_success(self):
|
def test_uv_delete_root_success(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
self.client.post(self.delete_ue_url)
|
self.client.post(self.delete_uv_url)
|
||||||
assert not UE.objects.filter(pk=self.ue.pk).exists()
|
assert not UV.objects.filter(pk=self.uv.pk).exists()
|
||||||
|
|
||||||
def test_ue_delete_pedagogy_admin_success(self):
|
def test_uv_delete_pedagogy_admin_success(self):
|
||||||
self.client.force_login(self.tutu)
|
self.client.force_login(self.tutu)
|
||||||
self.client.post(self.delete_ue_url)
|
self.client.post(self.delete_uv_url)
|
||||||
assert not UE.objects.filter(pk=self.ue.pk).exists()
|
assert not UV.objects.filter(pk=self.uv.pk).exists()
|
||||||
|
|
||||||
def test_ue_delete_pedagogy_unauthorized_fail(self):
|
def test_uv_delete_pedagogy_unauthorized_fail(self):
|
||||||
# Anonymous user
|
# Anonymous user
|
||||||
response = self.client.post(self.delete_ue_url)
|
response = self.client.post(self.delete_uv_url)
|
||||||
assertRedirects(
|
assertRedirects(
|
||||||
response, reverse("core:login", query={"next": self.delete_ue_url})
|
response, reverse("core:login", query={"next": self.delete_uv_url})
|
||||||
)
|
)
|
||||||
assert UE.objects.filter(pk=self.ue.pk).exists()
|
assert UV.objects.filter(pk=self.uv.pk).exists()
|
||||||
|
|
||||||
for user in baker.make(User), subscriber_user.make():
|
for user in baker.make(User), subscriber_user.make():
|
||||||
with self.subTest():
|
with self.subTest():
|
||||||
self.client.force_login(user)
|
self.client.force_login(user)
|
||||||
response = self.client.post(self.delete_ue_url)
|
response = self.client.post(self.delete_uv_url)
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
assert UE.objects.filter(pk=self.ue.pk).exists()
|
assert UV.objects.filter(pk=self.uv.pk).exists()
|
||||||
|
|
||||||
|
|
||||||
class TestUEUpdate(TestCase):
|
class TestUVUpdate(TestCase):
|
||||||
"""Test UE update rights."""
|
"""Test UV update rights."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@@ -218,79 +218,79 @@ class TestUEUpdate(TestCase):
|
|||||||
cls.tutu = User.objects.get(username="tutu")
|
cls.tutu = User.objects.get(username="tutu")
|
||||||
cls.sli = User.objects.get(username="sli")
|
cls.sli = User.objects.get(username="sli")
|
||||||
cls.guy = User.objects.get(username="guy")
|
cls.guy = User.objects.get(username="guy")
|
||||||
cls.ue = UE.objects.get(code="PA00")
|
cls.uv = UV.objects.get(code="PA00")
|
||||||
cls.update_ue_url = reverse("pedagogy:ue_update", kwargs={"ue_id": cls.ue.id})
|
cls.update_uv_url = reverse("pedagogy:uv_update", kwargs={"uv_id": cls.uv.id})
|
||||||
|
|
||||||
def test_ue_update_root_success(self):
|
def test_uv_update_root_success(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
self.client.post(
|
self.client.post(
|
||||||
self.update_ue_url, create_ue_template(self.bibou.id, code="PA00")
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
||||||
)
|
)
|
||||||
self.ue.refresh_from_db()
|
self.uv.refresh_from_db()
|
||||||
assert self.ue.credit_type == "TM"
|
assert self.uv.credit_type == "TM"
|
||||||
|
|
||||||
def test_ue_update_pedagogy_admin_success(self):
|
def test_uv_update_pedagogy_admin_success(self):
|
||||||
self.client.force_login(self.tutu)
|
self.client.force_login(self.tutu)
|
||||||
self.client.post(
|
self.client.post(
|
||||||
self.update_ue_url, create_ue_template(self.bibou.id, code="PA00")
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
||||||
)
|
)
|
||||||
self.ue.refresh_from_db()
|
self.uv.refresh_from_db()
|
||||||
assert self.ue.credit_type == "TM"
|
assert self.uv.credit_type == "TM"
|
||||||
|
|
||||||
def test_ue_update_original_author_does_not_change(self):
|
def test_uv_update_original_author_does_not_change(self):
|
||||||
self.client.force_login(self.tutu)
|
self.client.force_login(self.tutu)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.update_ue_url,
|
self.update_uv_url,
|
||||||
create_ue_template(self.tutu.id, code="PA00"),
|
create_uv_template(self.tutu.id, code="PA00"),
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
self.ue.refresh_from_db()
|
self.uv.refresh_from_db()
|
||||||
assert self.ue.author == self.bibou
|
assert self.uv.author == self.bibou
|
||||||
|
|
||||||
def test_ue_update_pedagogy_unauthorized_fail(self):
|
def test_uv_update_pedagogy_unauthorized_fail(self):
|
||||||
# Anonymous user
|
# Anonymous user
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.update_ue_url, create_ue_template(self.bibou.id, code="PA00")
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
||||||
)
|
)
|
||||||
assertRedirects(
|
assertRedirects(
|
||||||
response, reverse("core:login", query={"next": self.update_ue_url})
|
response, reverse("core:login", query={"next": self.update_uv_url})
|
||||||
)
|
)
|
||||||
|
|
||||||
# Not subscribed user
|
# Not subscribed user
|
||||||
self.client.force_login(self.guy)
|
self.client.force_login(self.guy)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.update_ue_url, create_ue_template(self.bibou.id, code="PA00")
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
||||||
)
|
)
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
# Simply subscribed user
|
# Simply subscribed user
|
||||||
self.client.force_login(self.sli)
|
self.client.force_login(self.sli)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.update_ue_url, create_ue_template(self.bibou.id, code="PA00")
|
self.update_uv_url, create_uv_template(self.bibou.id, code="PA00")
|
||||||
)
|
)
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
# Check that the UE has not changed
|
# Check that the UV has not changed
|
||||||
self.ue.refresh_from_db()
|
self.uv.refresh_from_db()
|
||||||
assert self.ue.credit_type == "OM"
|
assert self.uv.credit_type == "OM"
|
||||||
|
|
||||||
|
|
||||||
# UEComment class tests
|
# UVComment class tests
|
||||||
|
|
||||||
|
|
||||||
def create_ue_comment_template(user_id, ue_code="PA00", exclude_list=None):
|
def create_uv_comment_template(user_id, uv_code="PA00", exclude_list=None):
|
||||||
"""Factory to help UEComment creation/update in post requests."""
|
"""Factory to help UVComment creation/update in post requests."""
|
||||||
if exclude_list is None:
|
if exclude_list is None:
|
||||||
exclude_list = []
|
exclude_list = []
|
||||||
comment = {
|
comment = {
|
||||||
"author": user_id,
|
"author": user_id,
|
||||||
"ue": UE.objects.get(code=ue_code).id,
|
"uv": UV.objects.get(code=uv_code).id,
|
||||||
"grade_global": 4,
|
"grade_global": 4,
|
||||||
"grade_utility": 4,
|
"grade_utility": 4,
|
||||||
"grade_interest": 4,
|
"grade_interest": 4,
|
||||||
"grade_teaching": -1,
|
"grade_teaching": -1,
|
||||||
"grade_work_load": 2,
|
"grade_work_load": 2,
|
||||||
"comment": "Superbe UE qui fait vivre la vie associative de l'école",
|
"comment": "Superbe UV qui fait vivre la vie associative de l'école",
|
||||||
}
|
}
|
||||||
for excluded in exclude_list:
|
for excluded in exclude_list:
|
||||||
comment.pop(excluded)
|
comment.pop(excluded)
|
||||||
@@ -298,7 +298,7 @@ def create_ue_comment_template(user_id, ue_code="PA00", exclude_list=None):
|
|||||||
|
|
||||||
|
|
||||||
class TestUVCommentCreationAndDisplay(TestCase):
|
class TestUVCommentCreationAndDisplay(TestCase):
|
||||||
"""Test UEComment creation and its display.
|
"""Test UVComment creation and its display.
|
||||||
|
|
||||||
Display and creation are the same view.
|
Display and creation are the same view.
|
||||||
"""
|
"""
|
||||||
@@ -309,124 +309,124 @@ class TestUVCommentCreationAndDisplay(TestCase):
|
|||||||
cls.tutu = User.objects.get(username="tutu")
|
cls.tutu = User.objects.get(username="tutu")
|
||||||
cls.sli = User.objects.get(username="sli")
|
cls.sli = User.objects.get(username="sli")
|
||||||
cls.guy = User.objects.get(username="guy")
|
cls.guy = User.objects.get(username="guy")
|
||||||
cls.ue = UE.objects.get(code="PA00")
|
cls.uv = UV.objects.get(code="PA00")
|
||||||
cls.ue_url = reverse("pedagogy:ue_detail", kwargs={"ue_id": cls.ue.id})
|
cls.uv_url = reverse("pedagogy:uv_detail", kwargs={"uv_id": cls.uv.id})
|
||||||
|
|
||||||
def test_create_ue_comment_admin_success(self):
|
def test_create_uv_comment_admin_success(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.ue_url, create_ue_comment_template(self.bibou.id)
|
self.uv_url, create_uv_comment_template(self.bibou.id)
|
||||||
)
|
)
|
||||||
assertRedirects(response, self.ue_url)
|
assertRedirects(response, self.uv_url)
|
||||||
response = self.client.get(self.ue_url)
|
response = self.client.get(self.uv_url)
|
||||||
self.assertContains(response, text="Superbe UE")
|
self.assertContains(response, text="Superbe UV")
|
||||||
|
|
||||||
def test_create_ue_comment_pedagogy_admin_success(self):
|
def test_create_uv_comment_pedagogy_admin_success(self):
|
||||||
self.client.force_login(self.tutu)
|
self.client.force_login(self.tutu)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.ue_url, create_ue_comment_template(self.tutu.id)
|
self.uv_url, create_uv_comment_template(self.tutu.id)
|
||||||
)
|
)
|
||||||
self.assertRedirects(response, self.ue_url)
|
self.assertRedirects(response, self.uv_url)
|
||||||
response = self.client.get(self.ue_url)
|
response = self.client.get(self.uv_url)
|
||||||
self.assertContains(response, text="Superbe UE")
|
self.assertContains(response, text="Superbe UV")
|
||||||
|
|
||||||
def test_create_ue_comment_subscriber_success(self):
|
def test_create_uv_comment_subscriber_success(self):
|
||||||
self.client.force_login(self.sli)
|
self.client.force_login(self.sli)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.ue_url, create_ue_comment_template(self.sli.id)
|
self.uv_url, create_uv_comment_template(self.sli.id)
|
||||||
)
|
)
|
||||||
self.assertRedirects(response, self.ue_url)
|
self.assertRedirects(response, self.uv_url)
|
||||||
response = self.client.get(self.ue_url)
|
response = self.client.get(self.uv_url)
|
||||||
self.assertContains(response, text="Superbe UE")
|
self.assertContains(response, text="Superbe UV")
|
||||||
|
|
||||||
def test_create_ue_comment_unauthorized_fail(self):
|
def test_create_uv_comment_unauthorized_fail(self):
|
||||||
nb_comments = self.ue.comments.count()
|
nb_comments = self.uv.comments.count()
|
||||||
# Test with anonymous user
|
# Test with anonymous user
|
||||||
response = self.client.post(self.ue_url, create_ue_comment_template(0))
|
response = self.client.post(self.uv_url, create_uv_comment_template(0))
|
||||||
assertRedirects(response, reverse("core:login", query={"next": self.ue_url}))
|
assertRedirects(response, reverse("core:login", query={"next": self.uv_url}))
|
||||||
|
|
||||||
# Test with non subscribed user
|
# Test with non subscribed user
|
||||||
self.client.force_login(self.guy)
|
self.client.force_login(self.guy)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.ue_url, create_ue_comment_template(self.guy.id)
|
self.uv_url, create_uv_comment_template(self.guy.id)
|
||||||
)
|
)
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
# Check that no comment has been created
|
# Check that no comment has been created
|
||||||
assert self.ue.comments.count() == nb_comments
|
assert self.uv.comments.count() == nb_comments
|
||||||
|
|
||||||
def test_create_ue_comment_bad_form_fail(self):
|
def test_create_uv_comment_bad_form_fail(self):
|
||||||
nb_comments = self.ue.comments.count()
|
nb_comments = self.uv.comments.count()
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.ue_url,
|
self.uv_url,
|
||||||
create_ue_comment_template(self.bibou.id, exclude_list=["grade_global"]),
|
create_uv_comment_template(self.bibou.id, exclude_list=["grade_global"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert self.ue.comments.count() == nb_comments
|
assert self.uv.comments.count() == nb_comments
|
||||||
|
|
||||||
def test_create_ue_comment_twice_fail(self):
|
def test_create_uv_comment_twice_fail(self):
|
||||||
# Checks that the has_user_already_commented method works proprely
|
# Checks that the has_user_already_commented method works proprely
|
||||||
assert not self.ue.has_user_already_commented(self.bibou)
|
assert not self.uv.has_user_already_commented(self.bibou)
|
||||||
|
|
||||||
# Create a first comment
|
# Create a first comment
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
self.client.post(self.ue_url, create_ue_comment_template(self.bibou.id))
|
self.client.post(self.uv_url, create_uv_comment_template(self.bibou.id))
|
||||||
|
|
||||||
# Checks that the has_user_already_commented method works proprely
|
# Checks that the has_user_already_commented method works proprely
|
||||||
assert self.ue.has_user_already_commented(self.bibou)
|
assert self.uv.has_user_already_commented(self.bibou)
|
||||||
|
|
||||||
# Create the second comment
|
# Create the second comment
|
||||||
comment = create_ue_comment_template(self.bibou.id)
|
comment = create_uv_comment_template(self.bibou.id)
|
||||||
comment["comment"] = "Twice"
|
comment["comment"] = "Twice"
|
||||||
response = self.client.post(self.ue_url, comment)
|
response = self.client.post(self.uv_url, comment)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert UEComment.objects.filter(comment__contains="Superbe UE").exists()
|
assert UVComment.objects.filter(comment__contains="Superbe UV").exists()
|
||||||
assert not UEComment.objects.filter(comment__contains="Twice").exists()
|
assert not UVComment.objects.filter(comment__contains="Twice").exists()
|
||||||
self.assertContains(
|
self.assertContains(
|
||||||
response,
|
response,
|
||||||
_(
|
_(
|
||||||
"You already posted a comment on this UE. "
|
"You already posted a comment on this UV. "
|
||||||
"If you want to comment again, "
|
"If you want to comment again, "
|
||||||
"please modify or delete your previous comment."
|
"please modify or delete your previous comment."
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ensure that there is no crash when no ue or no author is given
|
# Ensure that there is no crash when no uv or no author is given
|
||||||
self.client.post(
|
self.client.post(
|
||||||
self.ue_url, create_ue_comment_template(self.bibou.id, exclude_list=["ue"])
|
self.uv_url, create_uv_comment_template(self.bibou.id, exclude_list=["uv"])
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
self.client.post(
|
self.client.post(
|
||||||
self.ue_url,
|
self.uv_url,
|
||||||
create_ue_comment_template(self.bibou.id, exclude_list=["author"]),
|
create_uv_comment_template(self.bibou.id, exclude_list=["author"]),
|
||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
class TestUVCommentDelete(TestCase):
|
class TestUVCommentDelete(TestCase):
|
||||||
"""Test UEComment deletion rights."""
|
"""Test UVComment deletion rights."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
cls.comment = baker.make(UEComment)
|
cls.comment = baker.make(UVComment)
|
||||||
|
|
||||||
def test_ue_comment_delete_success(self):
|
def test_uv_comment_delete_success(self):
|
||||||
url = reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
url = reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
||||||
for user in (
|
for user in (
|
||||||
baker.make(User, is_superuser=True),
|
baker.make(User, is_superuser=True),
|
||||||
baker.make(
|
baker.make(
|
||||||
User, user_permissions=[Permission.objects.get(codename="view_ue")]
|
User, user_permissions=[Permission.objects.get(codename="view_uv")]
|
||||||
),
|
),
|
||||||
self.comment.author,
|
self.comment.author,
|
||||||
):
|
):
|
||||||
with self.subTest():
|
with self.subTest():
|
||||||
self.client.force_login(user)
|
self.client.force_login(user)
|
||||||
self.client.post(url)
|
self.client.post(url)
|
||||||
assert not UEComment.objects.filter(id=self.comment.id).exists()
|
assert not UVComment.objects.filter(id=self.comment.id).exists()
|
||||||
|
|
||||||
def test_ue_comment_delete_unauthorized_fail(self):
|
def test_uv_comment_delete_unauthorized_fail(self):
|
||||||
url = reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
url = reverse("pedagogy:comment_delete", kwargs={"comment_id": self.comment.id})
|
||||||
|
|
||||||
# Anonymous user
|
# Anonymous user
|
||||||
@@ -441,11 +441,11 @@ class TestUVCommentDelete(TestCase):
|
|||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
# Check that the comment still exists
|
# Check that the comment still exists
|
||||||
assert UEComment.objects.filter(id=self.comment.id).exists()
|
assert UVComment.objects.filter(id=self.comment.id).exists()
|
||||||
|
|
||||||
|
|
||||||
class TestUVCommentUpdate(TestCase):
|
class TestUVCommentUpdate(TestCase):
|
||||||
"""Test UEComment update rights."""
|
"""Test UVComment update rights."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
@@ -457,17 +457,17 @@ class TestUVCommentUpdate(TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Prepare a comment
|
# Prepare a comment
|
||||||
comment_kwargs = create_ue_comment_template(self.krophil.id)
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
||||||
comment_kwargs["author"] = self.krophil
|
comment_kwargs["author"] = self.krophil
|
||||||
comment_kwargs["ue"] = UE.objects.get(id=comment_kwargs["ue"])
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
||||||
self.comment = UEComment(**comment_kwargs)
|
self.comment = UVComment(**comment_kwargs)
|
||||||
self.comment.save()
|
self.comment.save()
|
||||||
|
|
||||||
# Prepare edit of this comment for post requests
|
# Prepare edit of this comment for post requests
|
||||||
self.comment_edit = create_ue_comment_template(self.krophil.id)
|
self.comment_edit = create_uv_comment_template(self.krophil.id)
|
||||||
self.comment_edit["comment"] = "Edited"
|
self.comment_edit["comment"] = "Edited"
|
||||||
|
|
||||||
def test_ue_comment_update_root_success(self):
|
def test_uv_comment_update_root_success(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
||||||
@@ -477,7 +477,7 @@ class TestUVCommentUpdate(TestCase):
|
|||||||
self.comment.refresh_from_db()
|
self.comment.refresh_from_db()
|
||||||
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
||||||
|
|
||||||
def test_ue_comment_update_author_success(self):
|
def test_uv_comment_update_author_success(self):
|
||||||
self.client.force_login(self.krophil)
|
self.client.force_login(self.krophil)
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id}),
|
||||||
@@ -487,7 +487,7 @@ class TestUVCommentUpdate(TestCase):
|
|||||||
self.comment.refresh_from_db()
|
self.comment.refresh_from_db()
|
||||||
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
self.assertEqual(self.comment.comment, self.comment_edit["comment"])
|
||||||
|
|
||||||
def test_ue_comment_update_unauthorized_fail(self):
|
def test_uv_comment_update_unauthorized_fail(self):
|
||||||
url = reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id})
|
url = reverse("pedagogy:comment_update", kwargs={"comment_id": self.comment.id})
|
||||||
# Anonymous user
|
# Anonymous user
|
||||||
response = self.client.post(url, self.comment_edit)
|
response = self.client.post(url, self.comment_edit)
|
||||||
@@ -506,7 +506,7 @@ class TestUVCommentUpdate(TestCase):
|
|||||||
self.comment.refresh_from_db()
|
self.comment.refresh_from_db()
|
||||||
self.assertNotEqual(self.comment.comment, self.comment_edit["comment"])
|
self.assertNotEqual(self.comment.comment, self.comment_edit["comment"])
|
||||||
|
|
||||||
def test_ue_comment_update_original_author_does_not_change(self):
|
def test_uv_comment_update_original_author_does_not_change(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
self.comment_edit["author"] = User.objects.get(username="root").id
|
self.comment_edit["author"] = User.objects.get(username="root").id
|
||||||
|
|
||||||
@@ -531,31 +531,31 @@ class TestUVModerationForm(TestCase):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# Prepare a comment
|
# Prepare a comment
|
||||||
comment_kwargs = create_ue_comment_template(self.krophil.id)
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
||||||
comment_kwargs["author"] = self.krophil
|
comment_kwargs["author"] = self.krophil
|
||||||
comment_kwargs["ue"] = UE.objects.get(id=comment_kwargs["ue"])
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
||||||
self.comment_1 = UEComment(**comment_kwargs)
|
self.comment_1 = UVComment(**comment_kwargs)
|
||||||
self.comment_1.save()
|
self.comment_1.save()
|
||||||
|
|
||||||
# Prepare another comment
|
# Prepare another comment
|
||||||
comment_kwargs = create_ue_comment_template(self.krophil.id)
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
||||||
comment_kwargs["author"] = self.krophil
|
comment_kwargs["author"] = self.krophil
|
||||||
comment_kwargs["ue"] = UE.objects.get(id=comment_kwargs["ue"])
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
||||||
self.comment_2 = UEComment(**comment_kwargs)
|
self.comment_2 = UVComment(**comment_kwargs)
|
||||||
self.comment_2.save()
|
self.comment_2.save()
|
||||||
|
|
||||||
# Prepare a comment report for comment 1
|
# Prepare a comment report for comment 1
|
||||||
self.report_1 = UECommentReport(
|
self.report_1 = UVCommentReport(
|
||||||
comment=self.comment_1, reporter=self.krophil, reason="C'est moche"
|
comment=self.comment_1, reporter=self.krophil, reason="C'est moche"
|
||||||
)
|
)
|
||||||
self.report_1.save()
|
self.report_1.save()
|
||||||
self.report_1_bis = UECommentReport(
|
self.report_1_bis = UVCommentReport(
|
||||||
comment=self.comment_1, reporter=self.krophil, reason="C'est moche 2"
|
comment=self.comment_1, reporter=self.krophil, reason="C'est moche 2"
|
||||||
)
|
)
|
||||||
self.report_1_bis.save()
|
self.report_1_bis.save()
|
||||||
|
|
||||||
# Prepare a comment report for comment 2
|
# Prepare a comment report for comment 2
|
||||||
self.report_2 = UECommentReport(
|
self.report_2 = UVCommentReport(
|
||||||
comment=self.comment_2, reporter=self.krophil, reason="C'est moche"
|
comment=self.comment_2, reporter=self.krophil, reason="C'est moche"
|
||||||
)
|
)
|
||||||
self.report_2.save()
|
self.report_2.save()
|
||||||
@@ -593,11 +593,11 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that nothing has changed
|
# Test that nothing has changed
|
||||||
assert UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
assert UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
assert UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
|
|
||||||
def test_delete_comment(self):
|
def test_delete_comment(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
@@ -607,14 +607,14 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that the comment and it's associated report has been deleted
|
# Test that the comment and it's associated report has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert not UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
# Test that the bis report has been deleted
|
# Test that the bis report has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
|
|
||||||
# Test that the other comment and report still exists
|
# Test that the other comment and report still exists
|
||||||
assert UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
|
|
||||||
def test_delete_comment_bulk(self):
|
def test_delete_comment_bulk(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
@@ -625,12 +625,12 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that comments and their associated reports has been deleted
|
# Test that comments and their associated reports has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert not UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
assert not UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
assert not UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert not UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
# Test that the bis report has been deleted
|
# Test that the bis report has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
|
|
||||||
def test_delete_comment_with_bis(self):
|
def test_delete_comment_with_bis(self):
|
||||||
# Test case if two reports targets the same comment and are both deleted
|
# Test case if two reports targets the same comment and are both deleted
|
||||||
@@ -642,10 +642,10 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that the comment and it's associated report has been deleted
|
# Test that the comment and it's associated report has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert not UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
# Test that the bis report has been deleted
|
# Test that the bis report has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
|
|
||||||
def test_delete_report(self):
|
def test_delete_report(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
@@ -655,14 +655,14 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that the report has been deleted and that the comment still exists
|
# Test that the report has been deleted and that the comment still exists
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
# Test that the bis report is still there
|
# Test that the bis report is still there
|
||||||
assert UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
|
|
||||||
# Test that the other comment and report still exists
|
# Test that the other comment and report still exists
|
||||||
assert UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
|
|
||||||
def test_delete_report_bulk(self):
|
def test_delete_report_bulk(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
@@ -679,12 +679,12 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that every reports has been deleted
|
# Test that every reports has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
assert not UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
# Test that comments still exists
|
# Test that comments still exists
|
||||||
assert UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
|
|
||||||
def test_delete_mixed(self):
|
def test_delete_mixed(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
@@ -698,15 +698,15 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that report 2 and his comment has been deleted
|
# Test that report 2 and his comment has been deleted
|
||||||
assert not UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
assert not UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert not UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
|
|
||||||
# Test that report 1 has been deleted and it's comment still exists
|
# Test that report 1 has been deleted and it's comment still exists
|
||||||
assert not UECommentReport.objects.filter(id=self.report_1.id).exists()
|
assert not UVCommentReport.objects.filter(id=self.report_1.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
|
|
||||||
# Test that report 1 bis is still there
|
# Test that report 1 bis is still there
|
||||||
assert UECommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_1_bis.id).exists()
|
||||||
|
|
||||||
def test_delete_mixed_with_bis(self):
|
def test_delete_mixed_with_bis(self):
|
||||||
self.client.force_login(self.bibou)
|
self.client.force_login(self.bibou)
|
||||||
@@ -720,16 +720,16 @@ class TestUVModerationForm(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
|
|
||||||
# Test that report 1 and 1 bis has been deleted
|
# Test that report 1 and 1 bis has been deleted
|
||||||
assert not UECommentReport.objects.filter(
|
assert not UVCommentReport.objects.filter(
|
||||||
id__in=[self.report_1.id, self.report_1_bis.id]
|
id__in=[self.report_1.id, self.report_1_bis.id]
|
||||||
).exists()
|
).exists()
|
||||||
|
|
||||||
# Test that comment 1 has been deleted
|
# Test that comment 1 has been deleted
|
||||||
assert not UEComment.objects.filter(id=self.comment_1.id).exists()
|
assert not UVComment.objects.filter(id=self.comment_1.id).exists()
|
||||||
|
|
||||||
# Test that report and comment 2 still exists
|
# Test that report and comment 2 still exists
|
||||||
assert UECommentReport.objects.filter(id=self.report_2.id).exists()
|
assert UVCommentReport.objects.filter(id=self.report_2.id).exists()
|
||||||
assert UEComment.objects.filter(id=self.comment_2.id).exists()
|
assert UVComment.objects.filter(id=self.comment_2.id).exists()
|
||||||
|
|
||||||
|
|
||||||
class TestUVCommentReportCreate(TestCase):
|
class TestUVCommentReportCreate(TestCase):
|
||||||
@@ -743,10 +743,10 @@ class TestUVCommentReportCreate(TestCase):
|
|||||||
self.tutu = User.objects.get(username="tutu")
|
self.tutu = User.objects.get(username="tutu")
|
||||||
|
|
||||||
# Prepare a comment
|
# Prepare a comment
|
||||||
comment_kwargs = create_ue_comment_template(self.krophil.id)
|
comment_kwargs = create_uv_comment_template(self.krophil.id)
|
||||||
comment_kwargs["author"] = self.krophil
|
comment_kwargs["author"] = self.krophil
|
||||||
comment_kwargs["ue"] = UE.objects.get(id=comment_kwargs["ue"])
|
comment_kwargs["uv"] = UV.objects.get(id=comment_kwargs["uv"])
|
||||||
self.comment = UEComment(**comment_kwargs)
|
self.comment = UVComment(**comment_kwargs)
|
||||||
self.comment.save()
|
self.comment.save()
|
||||||
|
|
||||||
def create_report_test(self, username: str, *, success: bool):
|
def create_report_test(self, username: str, *, success: bool):
|
||||||
@@ -763,7 +763,7 @@ class TestUVCommentReportCreate(TestCase):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
else:
|
else:
|
||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
self.assertEqual(UECommentReport.objects.all().exists(), success)
|
self.assertEqual(UVCommentReport.objects.all().exists(), success)
|
||||||
|
|
||||||
def test_create_report_root_success(self):
|
def test_create_report_root_success(self):
|
||||||
self.create_report_test("root", success=True)
|
self.create_report_test("root", success=True)
|
||||||
@@ -783,7 +783,7 @@ class TestUVCommentReportCreate(TestCase):
|
|||||||
url, {"comment": self.comment.id, "reporter": 0, "reason": "C'est moche"}
|
url, {"comment": self.comment.id, "reporter": 0, "reason": "C'est moche"}
|
||||||
)
|
)
|
||||||
assertRedirects(response, reverse("core:login", query={"next": url}))
|
assertRedirects(response, reverse("core:login", query={"next": url}))
|
||||||
assert not UECommentReport.objects.all().exists()
|
assert not UVCommentReport.objects.all().exists()
|
||||||
|
|
||||||
def test_notifications(self):
|
def test_notifications(self):
|
||||||
assert not self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").exists()
|
assert not self.tutu.notifications.filter(type="PEDAGOGY_MODERATION").exists()
|
||||||
|
|||||||
@@ -24,40 +24,40 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from pedagogy.views import (
|
from pedagogy.views import (
|
||||||
UECommentDeleteView,
|
UVCommentDeleteView,
|
||||||
UECommentReportCreateView,
|
UVCommentReportCreateView,
|
||||||
UECommentUpdateView,
|
UVCommentUpdateView,
|
||||||
UECreateView,
|
UVCreateView,
|
||||||
UEDeleteView,
|
UVDeleteView,
|
||||||
UEDetailFormView,
|
UVDetailFormView,
|
||||||
UEGuideView,
|
UVGuideView,
|
||||||
UEModerationFormView,
|
UVModerationFormView,
|
||||||
UEUpdateView,
|
UVUpdateView,
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# Urls displaying the actual application for visitors
|
# Urls displaying the actual application for visitors
|
||||||
path("", UEGuideView.as_view(), name="guide"),
|
path("", UVGuideView.as_view(), name="guide"),
|
||||||
path("ue/<int:ue_id>/", UEDetailFormView.as_view(), name="ue_detail"),
|
path("uv/<int:uv_id>/", UVDetailFormView.as_view(), name="uv_detail"),
|
||||||
path(
|
path(
|
||||||
"comment/<int:comment_id>/edit/",
|
"comment/<int:comment_id>/edit/",
|
||||||
UECommentUpdateView.as_view(),
|
UVCommentUpdateView.as_view(),
|
||||||
name="comment_update",
|
name="comment_update",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
"comment/<int:comment_id>/delete/",
|
"comment/<int:comment_id>/delete/",
|
||||||
UECommentDeleteView.as_view(),
|
UVCommentDeleteView.as_view(),
|
||||||
name="comment_delete",
|
name="comment_delete",
|
||||||
),
|
),
|
||||||
path(
|
path(
|
||||||
"comment/<int:comment_id>/report/",
|
"comment/<int:comment_id>/report/",
|
||||||
UECommentReportCreateView.as_view(),
|
UVCommentReportCreateView.as_view(),
|
||||||
name="comment_report",
|
name="comment_report",
|
||||||
),
|
),
|
||||||
# Moderation
|
# Moderation
|
||||||
path("moderation/", UEModerationFormView.as_view(), name="moderation"),
|
path("moderation/", UVModerationFormView.as_view(), name="moderation"),
|
||||||
# Administration : Create Update Delete Edit
|
# Administration : Create Update Delete Edit
|
||||||
path("ue/create/", UECreateView.as_view(), name="ue_create"),
|
path("uv/create/", UVCreateView.as_view(), name="uv_create"),
|
||||||
path("ue/<int:ue_id>/delete/", UEDeleteView.as_view(), name="ue_delete"),
|
path("uv/<int:uv_id>/delete/", UVDeleteView.as_view(), name="uv_delete"),
|
||||||
path("ue/<int:ue_id>/edit/", UEUpdateView.as_view(), name="ue_update"),
|
path("uv/<int:uv_id>/edit/", UVUpdateView.as_view(), name="uv_update"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Set of functions to interact with the UTBM UE api."""
|
"""Set of functions to interact with the UTBM UV api."""
|
||||||
|
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
@@ -6,14 +6,14 @@ import requests
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
|
|
||||||
from pedagogy.schemas import ShortUeList, UeSchema, UtbmFullUeSchema, UtbmShortUeSchema
|
from pedagogy.schemas import ShortUvList, UtbmFullUvSchema, UtbmShortUvSchema, UvSchema
|
||||||
|
|
||||||
|
|
||||||
class UtbmApiClient(requests.Session):
|
class UtbmApiClient(requests.Session):
|
||||||
"""A wrapper around `requests.Session` to perform requests to the UTBM UE API."""
|
"""A wrapper around `requests.Session` to perform requests to the UTBM UV API."""
|
||||||
|
|
||||||
BASE_URL = settings.SITH_PEDAGOGY_UTBM_API
|
BASE_URL = settings.SITH_PEDAGOGY_UTBM_API
|
||||||
_cache = {"short_ues": {}}
|
_cache = {"short_uvs": {}}
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def current_year(self) -> int:
|
def current_year(self) -> int:
|
||||||
@@ -22,83 +22,83 @@ class UtbmApiClient(requests.Session):
|
|||||||
response = self.get(url)
|
response = self.get(url)
|
||||||
return response.json()[-1]["annee"]
|
return response.json()[-1]["annee"]
|
||||||
|
|
||||||
def fetch_short_ues(
|
def fetch_short_uvs(
|
||||||
self, lang: str = "fr", year: int | None = None
|
self, lang: str = "fr", year: int | None = None
|
||||||
) -> list[UtbmShortUeSchema]:
|
) -> list[UtbmShortUvSchema]:
|
||||||
"""Get the list of UEs in their short format from the UTBM API"""
|
"""Get the list of UVs in their short format from the UTBM API"""
|
||||||
if year is None:
|
if year is None:
|
||||||
year = self.current_year
|
year = self.current_year
|
||||||
if lang not in self._cache["short_ues"]:
|
if lang not in self._cache["short_uvs"]:
|
||||||
self._cache["short_ues"][lang] = {}
|
self._cache["short_uvs"][lang] = {}
|
||||||
if year not in self._cache["short_ues"][lang]:
|
if year not in self._cache["short_uvs"][lang]:
|
||||||
url = f"{self.BASE_URL}/uvs/{lang}/{year}"
|
url = f"{self.BASE_URL}/uvs/{lang}/{year}"
|
||||||
response = self.get(url)
|
response = self.get(url)
|
||||||
ues = ShortUeList.validate_json(response.content)
|
uvs = ShortUvList.validate_json(response.content)
|
||||||
self._cache["short_ues"][lang][year] = ues
|
self._cache["short_uvs"][lang][year] = uvs
|
||||||
return self._cache["short_ues"][lang][year]
|
return self._cache["short_uvs"][lang][year]
|
||||||
|
|
||||||
def fetch_ues(
|
def fetch_uvs(
|
||||||
self, lang: str = "fr", year: int | None = None
|
self, lang: str = "fr", year: int | None = None
|
||||||
) -> Iterator[UeSchema]:
|
) -> Iterator[UvSchema]:
|
||||||
"""Fetch all UEs from the UTBM API, parsed in a format that we can use.
|
"""Fetch all UVs from the UTBM API, parsed in a format that we can use.
|
||||||
|
|
||||||
Warning:
|
Warning:
|
||||||
We need infos from the full ue schema, and the UTBM UE API
|
We need infos from the full uv schema, and the UTBM UV API
|
||||||
has no route to get all of them at once.
|
has no route to get all of them at once.
|
||||||
We must do one request per UE (for a total of around 730 UEs),
|
We must do one request per UV (for a total of around 730 UVs),
|
||||||
which takes a lot of time.
|
which takes a lot of time.
|
||||||
Hopefully, there seems to be no rate-limit, so an error
|
Hopefully, there seems to be no rate-limit, so an error
|
||||||
in the middle of the process isn't likely to occur.
|
in the middle of the process isn't likely to occur.
|
||||||
"""
|
"""
|
||||||
if year is None:
|
if year is None:
|
||||||
year = self.current_year
|
year = self.current_year
|
||||||
shorts_ues = self.fetch_short_ues(lang, year)
|
shorts_uvs = self.fetch_short_uvs(lang, year)
|
||||||
# When UEs are common to multiple branches (like most HUMA)
|
# When UVs are common to multiple branches (like most HUMA)
|
||||||
# the UTBM API duplicates them for every branch.
|
# the UTBM API duplicates them for every branch.
|
||||||
# We have no way in our db to link a UE to multiple formations,
|
# We have no way in our db to link a UV to multiple formations,
|
||||||
# so we just create a single UE, which formation is the one
|
# so we just create a single UV, which formation is the one
|
||||||
# of the first UE found in the list.
|
# of the first UV found in the list.
|
||||||
# For example, if we have CC01 (TC), CC01 (IMSI) and CC01 (EDIM),
|
# For example, if we have CC01 (TC), CC01 (IMSI) and CC01 (EDIM),
|
||||||
# we will only keep CC01 (TC).
|
# we will only keep CC01 (TC).
|
||||||
unique_short_ues = {}
|
unique_short_uvs = {}
|
||||||
for ue in shorts_ues:
|
for uv in shorts_uvs:
|
||||||
if ue.code not in unique_short_ues:
|
if uv.code not in unique_short_uvs:
|
||||||
unique_short_ues[ue.code] = ue
|
unique_short_uvs[uv.code] = uv
|
||||||
for ue in unique_short_ues.values():
|
for uv in unique_short_uvs.values():
|
||||||
ue_url = f"{self.BASE_URL}/uv/{lang}/{year}/{ue.code}/{ue.code_formation}"
|
uv_url = f"{self.BASE_URL}/uv/{lang}/{year}/{uv.code}/{uv.code_formation}"
|
||||||
response = requests.get(ue_url)
|
response = requests.get(uv_url)
|
||||||
full_ue = UtbmFullUeSchema.model_validate_json(response.content)
|
full_uv = UtbmFullUvSchema.model_validate_json(response.content)
|
||||||
yield make_clean_ue(ue, full_ue)
|
yield make_clean_uv(uv, full_uv)
|
||||||
|
|
||||||
def find_uu(self, lang: str, code: str, year: int | None = None) -> UeSchema | None:
|
def find_uv(self, lang: str, code: str, year: int | None = None) -> UvSchema | None:
|
||||||
"""Find an UE from the UTBM API."""
|
"""Find an UV from the UTBM API."""
|
||||||
# query the UE list
|
# query the UV list
|
||||||
if not year:
|
if not year:
|
||||||
year = self.current_year
|
year = self.current_year
|
||||||
# the UTBM API has no way to fetch a single short ue,
|
# the UTBM API has no way to fetch a single short uv,
|
||||||
# and short ues contain infos that we need and are not
|
# and short uvs contain infos that we need and are not
|
||||||
# in the full ue schema, so we must fetch everything.
|
# in the full uv schema, so we must fetch everything.
|
||||||
short_ues = self.fetch_short_ues(lang, year)
|
short_uvs = self.fetch_short_uvs(lang, year)
|
||||||
short_ue = next((ue for ue in short_ues if ue.code == code), None)
|
short_uv = next((uv for uv in short_uvs if uv.code == code), None)
|
||||||
if short_ue is None:
|
if short_uv is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# get detailed information about the UE
|
# get detailed information about the UV
|
||||||
ue_url = f"{self.BASE_URL}/uv/{lang}/{year}/{code}/{short_ue.code_formation}"
|
uv_url = f"{self.BASE_URL}/uv/{lang}/{year}/{code}/{short_uv.code_formation}"
|
||||||
response = requests.get(ue_url)
|
response = requests.get(uv_url)
|
||||||
full_ue = UtbmFullUeSchema.model_validate_json(response.content)
|
full_uv = UtbmFullUvSchema.model_validate_json(response.content)
|
||||||
return make_clean_ue(short_ue, full_ue)
|
return make_clean_uv(short_uv, full_uv)
|
||||||
|
|
||||||
|
|
||||||
def make_clean_ue(short_ue: UtbmShortUeSchema, full_ue: UtbmFullUeSchema) -> UeSchema:
|
def make_clean_uv(short_uv: UtbmShortUvSchema, full_uv: UtbmFullUvSchema) -> UvSchema:
|
||||||
"""Cleans the data up so that it corresponds to our data representation.
|
"""Cleans the data up so that it corresponds to our data representation.
|
||||||
|
|
||||||
Some of the needed information are in the short ue schema, some
|
Some of the needed information are in the short uv schema, some
|
||||||
other in the full ue schema.
|
other in the full uv schema.
|
||||||
Thus we combine those information to obtain a data schema suitable
|
Thus we combine those information to obtain a data schema suitable
|
||||||
for our needs.
|
for our needs.
|
||||||
"""
|
"""
|
||||||
if full_ue.departement == "Pôle Humanités":
|
if full_uv.departement == "Pôle Humanités":
|
||||||
department = "HUMA"
|
department = "HUMA"
|
||||||
else:
|
else:
|
||||||
department = {
|
department = {
|
||||||
@@ -112,9 +112,9 @@ def make_clean_ue(short_ue: UtbmShortUeSchema, full_ue: UtbmFullUeSchema) -> UeS
|
|||||||
"ED": "EDIM",
|
"ED": "EDIM",
|
||||||
"AI": "GI",
|
"AI": "GI",
|
||||||
"AM": "MC",
|
"AM": "MC",
|
||||||
}.get(short_ue.code_formation, "NA")
|
}.get(short_uv.code_formation, "NA")
|
||||||
|
|
||||||
match short_ue.ouvert_printemps, short_ue.ouvert_automne:
|
match short_uv.ouvert_printemps, short_uv.ouvert_automne:
|
||||||
case True, True:
|
case True, True:
|
||||||
semester = "AUTUMN_AND_SPRING"
|
semester = "AUTUMN_AND_SPRING"
|
||||||
case True, False:
|
case True, False:
|
||||||
@@ -124,22 +124,22 @@ def make_clean_ue(short_ue: UtbmShortUeSchema, full_ue: UtbmFullUeSchema) -> UeS
|
|||||||
case _:
|
case _:
|
||||||
semester = "CLOSED"
|
semester = "CLOSED"
|
||||||
|
|
||||||
return UeSchema(
|
return UvSchema(
|
||||||
title=full_ue.libelle or "",
|
title=full_uv.libelle or "",
|
||||||
code=full_ue.code,
|
code=full_uv.code,
|
||||||
credit_type=short_ue.code_categorie or "FREE",
|
credit_type=short_uv.code_categorie or "FREE",
|
||||||
semester=semester,
|
semester=semester,
|
||||||
language=short_ue.code_langue.upper(),
|
language=short_uv.code_langue.upper(),
|
||||||
credits=full_ue.credits_ects,
|
credits=full_uv.credits_ects,
|
||||||
department=department,
|
department=department,
|
||||||
hours_THE=next((i.nbh for i in full_ue.activites if i.code == "THE"), 0) // 60,
|
hours_THE=next((i.nbh for i in full_uv.activites if i.code == "THE"), 0) // 60,
|
||||||
hours_TD=next((i.nbh for i in full_ue.activites if i.code == "TD"), 0) // 60,
|
hours_TD=next((i.nbh for i in full_uv.activites if i.code == "TD"), 0) // 60,
|
||||||
hours_TP=next((i.nbh for i in full_ue.activites if i.code == "TP"), 0) // 60,
|
hours_TP=next((i.nbh for i in full_uv.activites if i.code == "TP"), 0) // 60,
|
||||||
hours_TE=next((i.nbh for i in full_ue.activites if i.code == "TE"), 0) // 60,
|
hours_TE=next((i.nbh for i in full_uv.activites if i.code == "TE"), 0) // 60,
|
||||||
hours_CM=next((i.nbh for i in full_ue.activites if i.code == "CM"), 0) // 60,
|
hours_CM=next((i.nbh for i in full_uv.activites if i.code == "CM"), 0) // 60,
|
||||||
manager=full_ue.respo_automne or full_ue.respo_printemps or "",
|
manager=full_uv.respo_automne or full_uv.respo_printemps or "",
|
||||||
objectives=full_ue.objectifs or "",
|
objectives=full_uv.objectifs or "",
|
||||||
program=full_ue.programme or "",
|
program=full_uv.programme or "",
|
||||||
skills=full_ue.acquisition_competences or "",
|
skills=full_uv.acquisition_competences or "",
|
||||||
key_concepts=full_ue.acquisition_notions or "",
|
key_concepts=full_uv.acquisition_notions or "",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -38,39 +38,39 @@ from core.auth.mixins import PermissionOrAuthorRequiredMixin
|
|||||||
from core.models import Notification, User
|
from core.models import Notification, User
|
||||||
from core.views import DetailFormView
|
from core.views import DetailFormView
|
||||||
from pedagogy.forms import (
|
from pedagogy.forms import (
|
||||||
UECommentForm,
|
UVCommentForm,
|
||||||
UECommentModerationForm,
|
UVCommentModerationForm,
|
||||||
UECommentReportForm,
|
UVCommentReportForm,
|
||||||
UEForm,
|
UVForm,
|
||||||
)
|
)
|
||||||
from pedagogy.models import UE, UEComment, UECommentReport
|
from pedagogy.models import UV, UVComment, UVCommentReport
|
||||||
|
|
||||||
|
|
||||||
class UEDetailFormView(PermissionRequiredMixin, DetailFormView):
|
class UVDetailFormView(PermissionRequiredMixin, DetailFormView):
|
||||||
"""Display every comment of an UE and detailed infos about it.
|
"""Display every comment of an UV and detailed infos about it.
|
||||||
|
|
||||||
Allow to comment the UE.
|
Allow to comment the UV.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model = UE
|
model = UV
|
||||||
pk_url_kwarg = "ue_id"
|
pk_url_kwarg = "uv_id"
|
||||||
template_name = "pedagogy/ue_detail.jinja"
|
template_name = "pedagogy/uv_detail.jinja"
|
||||||
form_class = UECommentForm
|
form_class = UVCommentForm
|
||||||
permission_required = "pedagogy.view_ue"
|
permission_required = "pedagogy.view_uv"
|
||||||
|
|
||||||
def has_permission(self):
|
def has_permission(self):
|
||||||
if self.request.method == "POST" and not self.request.user.has_perm(
|
if self.request.method == "POST" and not self.request.user.has_perm(
|
||||||
"pedagogy.add_uecomment"
|
"pedagogy.add_uvcomment"
|
||||||
):
|
):
|
||||||
# if it's a POST request, the user is trying to add a new UEComment
|
# if it's a POST request, the user is trying to add a new UVComment
|
||||||
# thus he also needs the "add_uecomment" permission
|
# thus he also needs the "add_uvcomment" permission
|
||||||
return False
|
return False
|
||||||
return super().has_permission()
|
return super().has_permission()
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
kwargs["author_id"] = self.request.user.id
|
kwargs["author_id"] = self.request.user.id
|
||||||
kwargs["ue_id"] = self.object.id
|
kwargs["uv_id"] = self.object.id
|
||||||
kwargs["is_creation"] = True
|
kwargs["is_creation"] = True
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
@@ -89,68 +89,68 @@ class UEDetailFormView(PermissionRequiredMixin, DetailFormView):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
# once the new ue comment has been saved
|
# once the new uv comment has been saved
|
||||||
# redirect to the same page we are currently
|
# redirect to the same page we are currently
|
||||||
return self.request.path
|
return self.request.path
|
||||||
|
|
||||||
|
|
||||||
class UECommentUpdateView(PermissionOrAuthorRequiredMixin, UpdateView):
|
class UVCommentUpdateView(PermissionOrAuthorRequiredMixin, UpdateView):
|
||||||
"""Allow edit of a given comment."""
|
"""Allow edit of a given comment."""
|
||||||
|
|
||||||
model = UEComment
|
model = UVComment
|
||||||
form_class = UECommentForm
|
form_class = UVCommentForm
|
||||||
pk_url_kwarg = "comment_id"
|
pk_url_kwarg = "comment_id"
|
||||||
template_name = "core/edit.jinja"
|
template_name = "core/edit.jinja"
|
||||||
permission_required = "pedagogy.change_uecomment"
|
permission_required = "pedagogy.change_uvcomment"
|
||||||
author_field = "author"
|
author_field = "author"
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
kwargs["author_id"] = self.object.author_id
|
kwargs["author_id"] = self.object.author_id
|
||||||
kwargs["ue_id"] = self.object.ue_id
|
kwargs["uv_id"] = self.object.uv_id
|
||||||
kwargs["is_creation"] = False
|
kwargs["is_creation"] = False
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse("pedagogy:ue_detail", kwargs={"ue_id": self.object.ue_id})
|
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.object.uv_id})
|
||||||
|
|
||||||
|
|
||||||
class UECommentDeleteView(PermissionOrAuthorRequiredMixin, DeleteView):
|
class UVCommentDeleteView(PermissionOrAuthorRequiredMixin, DeleteView):
|
||||||
"""Allow to delete a given comment."""
|
"""Allow delete of a given comment."""
|
||||||
|
|
||||||
model = UEComment
|
model = UVComment
|
||||||
pk_url_kwarg = "comment_id"
|
pk_url_kwarg = "comment_id"
|
||||||
template_name = "core/delete_confirm.jinja"
|
template_name = "core/delete_confirm.jinja"
|
||||||
permission_required = "pedagogy.delete_uecomment"
|
permission_required = "pedagogy.delete_uvcomment"
|
||||||
author_field = "author"
|
author_field = "author"
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse("pedagogy:ue_detail", kwargs={"ue_id": self.object.ue_id})
|
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.object.uv_id})
|
||||||
|
|
||||||
|
|
||||||
class UEGuideView(PermissionRequiredMixin, TemplateView):
|
class UVGuideView(PermissionRequiredMixin, TemplateView):
|
||||||
"""UE guide main page."""
|
"""UV guide main page."""
|
||||||
|
|
||||||
template_name = "pedagogy/guide.jinja"
|
template_name = "pedagogy/guide.jinja"
|
||||||
permission_required = "pedagogy.view_ue"
|
permission_required = "pedagogy.view_uv"
|
||||||
|
|
||||||
|
|
||||||
class UECommentReportCreateView(PermissionRequiredMixin, CreateView):
|
class UVCommentReportCreateView(PermissionRequiredMixin, CreateView):
|
||||||
"""Create a new report for an inappropriate comment."""
|
"""Create a new report for an inapropriate comment."""
|
||||||
|
|
||||||
model = UECommentReport
|
model = UVCommentReport
|
||||||
form_class = UECommentReportForm
|
form_class = UVCommentReportForm
|
||||||
template_name = "core/edit.jinja"
|
template_name = "core/edit.jinja"
|
||||||
permission_required = "pedagogy.add_uecommentreport"
|
permission_required = "pedagogy.add_uvcommentreport"
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
self.ue_comment = get_object_or_404(UEComment, pk=kwargs["comment_id"])
|
self.uv_comment = get_object_or_404(UVComment, pk=kwargs["comment_id"])
|
||||||
return super().dispatch(request, *args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
kwargs["reporter_id"] = self.request.user.id
|
kwargs["reporter_id"] = self.request.user.id
|
||||||
kwargs["comment_id"] = self.ue_comment.id
|
kwargs["comment_id"] = self.uv_comment.id
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
@@ -172,35 +172,35 @@ class UECommentReportCreateView(PermissionRequiredMixin, CreateView):
|
|||||||
return resp
|
return resp
|
||||||
|
|
||||||
def get_success_url(self):
|
def get_success_url(self):
|
||||||
return reverse("pedagogy:ue_detail", kwargs={"ue_id": self.ue_comment.ue_id})
|
return reverse("pedagogy:uv_detail", kwargs={"uv_id": self.uv_comment.uv_id})
|
||||||
|
|
||||||
|
|
||||||
class UEModerationFormView(PermissionRequiredMixin, FormView):
|
class UVModerationFormView(PermissionRequiredMixin, FormView):
|
||||||
"""Moderation interface (Privileged)."""
|
"""Moderation interface (Privileged)."""
|
||||||
|
|
||||||
form_class = UECommentModerationForm
|
form_class = UVCommentModerationForm
|
||||||
template_name = "pedagogy/moderation.jinja"
|
template_name = "pedagogy/moderation.jinja"
|
||||||
permission_required = "pedagogy.delete_uecomment"
|
permission_required = "pedagogy.delete_uvcomment"
|
||||||
success_url = reverse_lazy("pedagogy:moderation")
|
success_url = reverse_lazy("pedagogy:moderation")
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
form_clean = form.clean()
|
form_clean = form.clean()
|
||||||
accepted = form_clean.get("accepted_reports", [])
|
accepted = form_clean.get("accepted_reports", [])
|
||||||
if len(accepted) > 0: # delete the reported comments
|
if len(accepted) > 0: # delete the reported comments
|
||||||
UEComment.objects.filter(reports__in=accepted).delete()
|
UVComment.objects.filter(reports__in=accepted).delete()
|
||||||
denied = form_clean.get("denied_reports", [])
|
denied = form_clean.get("denied_reports", [])
|
||||||
if len(denied) > 0: # delete the comments themselves
|
if len(denied) > 0: # delete the comments themselves
|
||||||
UECommentReport.objects.filter(id__in={d.id for d in denied}).delete()
|
UVCommentReport.objects.filter(id__in={d.id for d in denied}).delete()
|
||||||
return super().form_valid(form)
|
return super().form_valid(form)
|
||||||
|
|
||||||
|
|
||||||
class UECreateView(PermissionRequiredMixin, CreateView):
|
class UVCreateView(PermissionRequiredMixin, CreateView):
|
||||||
"""Add a new UE (Privileged)."""
|
"""Add a new UV (Privileged)."""
|
||||||
|
|
||||||
model = UE
|
model = UV
|
||||||
form_class = UEForm
|
form_class = UVForm
|
||||||
template_name = "pedagogy/ue_edit.jinja"
|
template_name = "pedagogy/uv_edit.jinja"
|
||||||
permission_required = "pedagogy.add_ue"
|
permission_required = "pedagogy.add_uv"
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
@@ -208,24 +208,24 @@ class UECreateView(PermissionRequiredMixin, CreateView):
|
|||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
|
||||||
class UEDeleteView(PermissionRequiredMixin, DeleteView):
|
class UVDeleteView(PermissionRequiredMixin, DeleteView):
|
||||||
"""Allow to delete an UE (Privileged)."""
|
"""Allow to delete an UV (Privileged)."""
|
||||||
|
|
||||||
model = UE
|
model = UV
|
||||||
pk_url_kwarg = "ue_id"
|
pk_url_kwarg = "uv_id"
|
||||||
template_name = "core/delete_confirm.jinja"
|
template_name = "core/delete_confirm.jinja"
|
||||||
permission_required = "pedagogy.delete_ue"
|
permission_required = "pedagogy.delete_uv"
|
||||||
success_url = reverse_lazy("pedagogy:guide")
|
success_url = reverse_lazy("pedagogy:guide")
|
||||||
|
|
||||||
|
|
||||||
class UEUpdateView(PermissionRequiredMixin, UpdateView):
|
class UVUpdateView(PermissionRequiredMixin, UpdateView):
|
||||||
"""Allow to edit an UE (Privilegied)."""
|
"""Allow to edit an UV (Privilegied)."""
|
||||||
|
|
||||||
model = UE
|
model = UV
|
||||||
form_class = UEForm
|
form_class = UVForm
|
||||||
pk_url_kwarg = "ue_id"
|
pk_url_kwarg = "uv_id"
|
||||||
template_name = "pedagogy/ue_edit.jinja"
|
template_name = "pedagogy/uv_edit.jinja"
|
||||||
permission_required = "pedagogy.change_ue"
|
permission_required = "pedagogy.change_uv"
|
||||||
|
|
||||||
def get_form_kwargs(self):
|
def get_form_kwargs(self):
|
||||||
kwargs = super().get_form_kwargs()
|
kwargs = super().get_form_kwargs()
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ from sas.models import Album, PeoplePictureRelation, Picture, PictureModerationR
|
|||||||
|
|
||||||
@admin.register(Picture)
|
@admin.register(Picture)
|
||||||
class PictureAdmin(admin.ModelAdmin):
|
class PictureAdmin(admin.ModelAdmin):
|
||||||
list_display = ("name", "parent", "date", "size", "is_moderated")
|
list_display = ("name", "parent", "is_moderated")
|
||||||
search_fields = ("name",)
|
search_fields = ("name",)
|
||||||
autocomplete_fields = ("owner", "parent", "edit_groups", "view_groups", "moderator")
|
autocomplete_fields = ("owner", "parent", "moderator")
|
||||||
|
|
||||||
|
|
||||||
@admin.register(PeoplePictureRelation)
|
@admin.register(PeoplePictureRelation)
|
||||||
@@ -33,9 +33,9 @@ class PeoplePictureRelationAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
@admin.register(Album)
|
@admin.register(Album)
|
||||||
class AlbumAdmin(admin.ModelAdmin):
|
class AlbumAdmin(admin.ModelAdmin):
|
||||||
list_display = ("name", "parent", "date", "owner", "is_moderated")
|
list_display = ("name", "parent")
|
||||||
search_fields = ("name",)
|
search_fields = ("name",)
|
||||||
autocomplete_fields = ("owner", "parent", "edit_groups", "view_groups")
|
autocomplete_fields = ("parent", "edit_groups", "view_groups")
|
||||||
|
|
||||||
|
|
||||||
@admin.register(PictureModerationRequest)
|
@admin.register(PictureModerationRequest)
|
||||||
|
|||||||
64
sas/api.py
64
sas/api.py
@@ -2,8 +2,10 @@ from typing import Any, Literal
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.shortcuts import get_list_or_404
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from ninja import Body, File, Query
|
from ninja import Body, Query, UploadedFile
|
||||||
|
from ninja.errors import HttpError
|
||||||
from ninja.security import SessionAuth
|
from ninja.security import SessionAuth
|
||||||
from ninja_extra import ControllerBase, api_controller, paginate, route
|
from ninja_extra import ControllerBase, api_controller, paginate, route
|
||||||
from ninja_extra.exceptions import NotFound, PermissionDenied
|
from ninja_extra.exceptions import NotFound, PermissionDenied
|
||||||
@@ -16,11 +18,12 @@ from api.permissions import (
|
|||||||
CanAccessLookup,
|
CanAccessLookup,
|
||||||
CanEdit,
|
CanEdit,
|
||||||
CanView,
|
CanView,
|
||||||
|
HasPerm,
|
||||||
IsInGroup,
|
IsInGroup,
|
||||||
IsRoot,
|
IsRoot,
|
||||||
)
|
)
|
||||||
from core.models import Notification, User
|
from core.models import Notification, User
|
||||||
from core.schemas import UploadedImage
|
from core.utils import get_list_exact_or_404
|
||||||
from sas.models import Album, PeoplePictureRelation, Picture
|
from sas.models import Album, PeoplePictureRelation, Picture
|
||||||
from sas.schemas import (
|
from sas.schemas import (
|
||||||
AlbumAutocompleteSchema,
|
AlbumAutocompleteSchema,
|
||||||
@@ -28,6 +31,7 @@ from sas.schemas import (
|
|||||||
AlbumSchema,
|
AlbumSchema,
|
||||||
IdentifiedUserSchema,
|
IdentifiedUserSchema,
|
||||||
ModerationRequestSchema,
|
ModerationRequestSchema,
|
||||||
|
MoveAlbumSchema,
|
||||||
PictureFilterSchema,
|
PictureFilterSchema,
|
||||||
PictureSchema,
|
PictureSchema,
|
||||||
)
|
)
|
||||||
@@ -69,6 +73,48 @@ class AlbumController(ControllerBase):
|
|||||||
Album.objects.viewable_by(self.context.request.user).order_by("-date")
|
Album.objects.viewable_by(self.context.request.user).order_by("-date")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@route.patch("/parent", permissions=[IsAuthenticated])
|
||||||
|
def change_album_parent(self, payload: list[MoveAlbumSchema]):
|
||||||
|
"""Change parents of albums
|
||||||
|
|
||||||
|
Note:
|
||||||
|
For this operation to work, the user must be authorized
|
||||||
|
to edit both the moved albums and their new parent.
|
||||||
|
"""
|
||||||
|
user: User = self.context.request.user
|
||||||
|
albums: list[Album] = get_list_exact_or_404(
|
||||||
|
Album, pk__in={a.id for a in payload}
|
||||||
|
)
|
||||||
|
if not user.has_perm("sas.change_album"):
|
||||||
|
unauthorized = [a.id for a in albums if not user.can_edit(a)]
|
||||||
|
raise PermissionDenied(
|
||||||
|
f"You can't move the following albums : {unauthorized}"
|
||||||
|
)
|
||||||
|
parents: list[Album] = get_list_exact_or_404(
|
||||||
|
Album, pk__in={a.new_parent_id for a in payload}
|
||||||
|
)
|
||||||
|
if not user.has_perm("sas.change_album"):
|
||||||
|
unauthorized = [a.id for a in parents if not user.can_edit(a)]
|
||||||
|
raise PermissionDenied(
|
||||||
|
f"You can't move to the following albums : {unauthorized}"
|
||||||
|
)
|
||||||
|
id_to_new_parent = {i.id: i.new_parent_id for i in payload}
|
||||||
|
for album in albums:
|
||||||
|
album.parent_id = id_to_new_parent[album.id]
|
||||||
|
# known caveat : moving an album won't move it's thumbnail.
|
||||||
|
# E.g. if the album foo/bar is moved to foo/baz,
|
||||||
|
# the thumbnail will still be foo/bar/thumb.webp
|
||||||
|
# This has no impact for the end user
|
||||||
|
# and doing otherwise would be hard for us to implement,
|
||||||
|
# because we would then have to manage rollbacks on fail.
|
||||||
|
Album.objects.bulk_update(albums, fields=["parent_id"])
|
||||||
|
|
||||||
|
@route.delete("", permissions=[HasPerm("sas.delete_album")])
|
||||||
|
def delete_album(self, album_ids: list[int]):
|
||||||
|
# known caveat : deleting an album doesn't delete the pictures on the disk.
|
||||||
|
# It's a db only operation.
|
||||||
|
albums: list[Album] = get_list_or_404(Album, pk__in=album_ids)
|
||||||
|
|
||||||
|
|
||||||
@api_controller("/sas/picture")
|
@api_controller("/sas/picture")
|
||||||
class PicturesController(ControllerBase):
|
class PicturesController(ControllerBase):
|
||||||
@@ -96,7 +142,7 @@ class PicturesController(ControllerBase):
|
|||||||
return (
|
return (
|
||||||
filters.filter(Picture.objects.viewable_by(user))
|
filters.filter(Picture.objects.viewable_by(user))
|
||||||
.distinct()
|
.distinct()
|
||||||
.order_by("-parent__date", "date")
|
.order_by("-parent__event_date", "created_at")
|
||||||
.select_related("owner", "parent")
|
.select_related("owner", "parent")
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -110,27 +156,25 @@ class PicturesController(ControllerBase):
|
|||||||
},
|
},
|
||||||
url_name="upload_picture",
|
url_name="upload_picture",
|
||||||
)
|
)
|
||||||
def upload_picture(self, album_id: Body[int], picture: File[UploadedImage]):
|
def upload_picture(self, album_id: Body[int], picture: UploadedFile):
|
||||||
album = self.get_object_or_exception(Album, pk=album_id)
|
album = self.get_object_or_exception(Album, pk=album_id)
|
||||||
user = self.context.request.user
|
user = self.context.request.user
|
||||||
self_moderate = user.has_perm("sas.moderate_sasfile")
|
self_moderate = user.has_perm("sas.moderate_sasfile")
|
||||||
new = Picture(
|
new = Picture(
|
||||||
parent=album,
|
parent=album,
|
||||||
name=picture.name,
|
name=picture.name,
|
||||||
file=picture,
|
original=picture,
|
||||||
owner=user,
|
owner=user,
|
||||||
is_moderated=self_moderate,
|
is_moderated=self_moderate,
|
||||||
is_folder=False,
|
|
||||||
mime_type=picture.content_type,
|
|
||||||
)
|
)
|
||||||
if self_moderate:
|
if self_moderate:
|
||||||
new.moderator = user
|
new.moderator = user
|
||||||
|
new.generate_thumbnails()
|
||||||
try:
|
try:
|
||||||
new.generate_thumbnails()
|
|
||||||
new.full_clean()
|
new.full_clean()
|
||||||
new.save()
|
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
return self.create_response({"detail": dict(e)}, status_code=409)
|
raise HttpError(status_code=409, message=str(e)) from e
|
||||||
|
new.save()
|
||||||
|
|
||||||
@route.get(
|
@route.get(
|
||||||
"/{picture_id}/identified",
|
"/{picture_id}/identified",
|
||||||
|
|||||||
@@ -1,18 +1,35 @@
|
|||||||
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from model_bakery import seq
|
from model_bakery import seq
|
||||||
from model_bakery.recipe import Recipe
|
from model_bakery.recipe import Recipe
|
||||||
|
|
||||||
from sas.models import Picture
|
from core.utils import RED_PIXEL_PNG
|
||||||
|
from sas.models import Album, Picture
|
||||||
|
|
||||||
|
album_recipe = Recipe(
|
||||||
|
Album,
|
||||||
|
name=seq("Album "),
|
||||||
|
thumbnail=SimpleUploadedFile(
|
||||||
|
name="thumb.webp", content=b"", content_type="image/webp"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
picture_recipe = Recipe(
|
picture_recipe = Recipe(
|
||||||
Picture,
|
Picture,
|
||||||
is_in_sas=True,
|
|
||||||
is_folder=False,
|
|
||||||
is_moderated=True,
|
is_moderated=True,
|
||||||
name=seq("Picture "),
|
name=seq("Picture "),
|
||||||
|
original=SimpleUploadedFile(
|
||||||
|
# compressed and thumbnail are generated on save (except if bulk creating).
|
||||||
|
# For this step no to fail, original must be a valid image.
|
||||||
|
name="img.png",
|
||||||
|
content=RED_PIXEL_PNG,
|
||||||
|
content_type="image/png",
|
||||||
|
),
|
||||||
|
compressed=SimpleUploadedFile(
|
||||||
|
name="img.webp", content=b"", content_type="image/webp"
|
||||||
|
),
|
||||||
|
thumbnail=SimpleUploadedFile(
|
||||||
|
name="img.webp", content=b"", content_type="image/webp"
|
||||||
|
),
|
||||||
)
|
)
|
||||||
"""A SAS Picture fixture.
|
"""A SAS Picture fixture."""
|
||||||
|
|
||||||
Warnings:
|
|
||||||
If you don't `bulk_create` this, you need
|
|
||||||
to explicitly set the parent album, or it won't work
|
|
||||||
"""
|
|
||||||
|
|||||||
@@ -48,13 +48,12 @@ class PictureEditForm(forms.ModelForm):
|
|||||||
class AlbumEditForm(forms.ModelForm):
|
class AlbumEditForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Album
|
model = Album
|
||||||
fields = ["name", "date", "file", "parent", "edit_groups"]
|
fields = ["name", "date", "thumbnail", "parent", "edit_groups"]
|
||||||
widgets = {
|
widgets = {
|
||||||
"parent": AutoCompleteSelectAlbum,
|
"parent": AutoCompleteSelectAlbum,
|
||||||
"edit_groups": AutoCompleteSelectMultipleGroup,
|
"edit_groups": AutoCompleteSelectMultipleGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
name = forms.CharField(max_length=Album.NAME_MAX_LENGTH, label=_("file name"))
|
|
||||||
date = forms.DateField(label=_("Date"), widget=SelectDate, required=True)
|
date = forms.DateField(label=_("Date"), widget=SelectDate, required=True)
|
||||||
recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False)
|
recursive = forms.BooleanField(label=_("Apply rights recursively"), required=False)
|
||||||
|
|
||||||
|
|||||||
357
sas/migrations/0006_move_the_whole_sas.py
Normal file
357
sas/migrations/0006_move_the_whole_sas.py
Normal file
@@ -0,0 +1,357 @@
|
|||||||
|
# Generated by Django 4.2.17 on 2025-01-22 21:53
|
||||||
|
import collections
|
||||||
|
import itertools
|
||||||
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
from django.db.migrations.state import StateApps
|
||||||
|
|
||||||
|
import sas.models
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import core.models
|
||||||
|
|
||||||
|
# NB : tous les commentaires sont écrits en français,
|
||||||
|
# parce qu'on est sur des opérations qui sont complexes,
|
||||||
|
# et qui sont surtout DANGEREUSES.
|
||||||
|
# Ici, la clarté des explications prime sur toute autre considération.
|
||||||
|
|
||||||
|
|
||||||
|
def copy_albums_and_pictures(apps: StateApps, schema_editor):
|
||||||
|
SithFile: type[core.models.SithFile] = apps.get_model("core", "SithFile")
|
||||||
|
Album: type[sas.models.Album] = apps.get_model("sas", "Album")
|
||||||
|
Picture: type[sas.models.Picture] = apps.get_model("sas", "Picture")
|
||||||
|
logger = logging.getLogger("django")
|
||||||
|
|
||||||
|
# Il y a environ 1800 albums, 257k photos et 488k identifications
|
||||||
|
# d'utilisateurs dans la db de prod.
|
||||||
|
# En supposant qu'une insertion prenne 10ms (ce qui est très optimiste),
|
||||||
|
# migrer tous les enregistrements de la db prendrait plus de 2h.
|
||||||
|
# C'est trop long.
|
||||||
|
# Mais d'un autre côté, j'ai pas assez confiance dans les capacités de nos
|
||||||
|
# machines pour charger presque un million d'objets en mémoire.
|
||||||
|
# Pour faire un compromis, les albums sont migrés individuellement un à un,
|
||||||
|
# mais tous les objets liés à ces albums
|
||||||
|
# (photos, groupes de vue, groupe d'édition, identification d'utilisateurs)
|
||||||
|
# sont migrés en tas.
|
||||||
|
#
|
||||||
|
# Ordre des opérations :
|
||||||
|
# 1. On migre les albums 1 à 1 (il y en a 1800, donc c'est relativement court)
|
||||||
|
# 2. On migre les photos par paquet de 2500 (soit ~une centaine d'opérations)
|
||||||
|
# 3. On migre tous les groupes de vue et tous les groupes d'édition des albums
|
||||||
|
#
|
||||||
|
# Au total, la migration devrait demander aux alentours de 2000 insertions,
|
||||||
|
# ce qui est un compromis acceptable entre une migration
|
||||||
|
# pas trop longue et une RAM pas trop surchargée.
|
||||||
|
#
|
||||||
|
# Pour ce qui est de la répartition des tables, quatre nouvelles tables
|
||||||
|
# sont créées : sas_album, sas_picture,
|
||||||
|
# sas_pictureviewgroups et sas_picture_editgroups.
|
||||||
|
# Tous les albums et toutes les photos qui sont dans core_sithfile
|
||||||
|
# vont être copiés dans ces tables.
|
||||||
|
# Comme les albums sont migrés un à un, ils recevront une nouvelle
|
||||||
|
# clef primaire.
|
||||||
|
# Pour les photos, en revanche, c'est beaucoup plus sûr de leur donner
|
||||||
|
# le même id que celui qu'il y avait dans core_sithfile.
|
||||||
|
#
|
||||||
|
# Les identifications des photos ne sont pas migrées pour l'instant.
|
||||||
|
# Ce qu'on va faire, c'est qu'on va changer la contrainte de clef étrangère
|
||||||
|
# sur la colonne des photos pour pointer vers sas_picture
|
||||||
|
# au lieu de core_sithfile.
|
||||||
|
# Cependant, pour que ça marche,
|
||||||
|
# il faut qu'au moment où ce changement est effectué,
|
||||||
|
# toutes les clefs primaires référencées existent à la fois dans
|
||||||
|
# les deux tables, sinon les contraintes d'intégrité ne sont pas respectées.
|
||||||
|
# La migration de ce fichier va donc s'occuper de créer les nouvelles tables
|
||||||
|
# et d'y copier les données nécessaires.
|
||||||
|
# Puis une deuxième migration s'occupera de changer les contraintes.
|
||||||
|
# Et enfin une troisième migration supprimera les anciennes données.
|
||||||
|
#
|
||||||
|
# Pavé César
|
||||||
|
|
||||||
|
albums = SithFile.objects.filter(is_in_sas=True, is_folder=True).prefetch_related(
|
||||||
|
"view_groups", "edit_groups"
|
||||||
|
)
|
||||||
|
old_albums = collections.deque(
|
||||||
|
albums.filter(parent_id=settings.SITH_SAS_ROOT_DIR_ID)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Changement de représentation en DB.
|
||||||
|
# Dans l'ancien système, un fichier était dans le SAS si
|
||||||
|
# un fichier spécial (le SAS_ROOT) était parmi ses ancêtres.
|
||||||
|
# Comme maintenant les fichiers du SAS sont dans des tables à part,
|
||||||
|
# il ne peut plus y avoir de confusion.
|
||||||
|
# Les photos ont donc obligatoirement un parent (qui est un album)
|
||||||
|
# et les albums peuvent avoir un parent null.
|
||||||
|
# Un album sans parent est considéré comme se trouvant à la racine
|
||||||
|
# de l'arborescence.
|
||||||
|
# En quelque sorte, None est le nouveau SITH_SAS_ROOT_DIR_ID
|
||||||
|
album_id_old_to_new = {settings.SITH_SAS_ROOT_DIR_ID: None}
|
||||||
|
|
||||||
|
logger.info(f"migrating {albums.count()} albums")
|
||||||
|
while len(old_albums) > 0:
|
||||||
|
# Comme les albums référencent leur parent, les albums doivent être migrés
|
||||||
|
# par ordre croissant de profondeur dans l'arborescence.
|
||||||
|
# Chaque album est donc pris par la gauche de la file
|
||||||
|
# et ses enfants ajoutés sur la droite.
|
||||||
|
old_album = old_albums.popleft()
|
||||||
|
old_albums.extend(list(albums.filter(parent=old_album)))
|
||||||
|
new_album = Album.objects.create(
|
||||||
|
parent_id=album_id_old_to_new[old_album.parent_id],
|
||||||
|
event_date=old_album.date.date(),
|
||||||
|
name=old_album.name,
|
||||||
|
thumbnail=(old_album.file or None),
|
||||||
|
is_moderated=old_album.is_moderated,
|
||||||
|
)
|
||||||
|
# on garde un dictionnaire qui associe les id des albums dans l'ancienne table
|
||||||
|
# à leur id dans la nouvelle table, pour pouvoir recréer
|
||||||
|
# les liens de parenté entre albums
|
||||||
|
album_id_old_to_new[old_album.id] = new_album.id
|
||||||
|
|
||||||
|
pictures = SithFile.objects.filter(is_in_sas=True, is_folder=False)
|
||||||
|
nb_pictures = pictures.count()
|
||||||
|
logger.info(f"migrating {nb_pictures} pictures")
|
||||||
|
for i, pictures_batch in enumerate(itertools.batched(pictures, 2500), start=1):
|
||||||
|
Picture.objects.bulk_create(
|
||||||
|
[
|
||||||
|
Picture(
|
||||||
|
id=p.id,
|
||||||
|
name=p.name,
|
||||||
|
parent_id=album_id_old_to_new[p.parent_id],
|
||||||
|
thumbnail=p.thumbnail,
|
||||||
|
compressed=p.compressed,
|
||||||
|
original=p.file,
|
||||||
|
owner_id=p.owner_id,
|
||||||
|
created_at=p.date,
|
||||||
|
is_moderated=p.is_moderated,
|
||||||
|
asked_for_removal=p.asked_for_removal,
|
||||||
|
moderator_id=p.moderator_id,
|
||||||
|
)
|
||||||
|
for p in pictures_batch
|
||||||
|
]
|
||||||
|
)
|
||||||
|
logger.info(f"Migrated {min(i * 2500, nb_pictures)} / {nb_pictures} pictures")
|
||||||
|
|
||||||
|
logger.info("Migrating album groups")
|
||||||
|
albums = SithFile.objects.filter(is_in_sas=True, is_folder=True).exclude(
|
||||||
|
id=settings.SITH_SAS_ROOT_DIR_ID
|
||||||
|
)
|
||||||
|
Album.edit_groups.through.objects.bulk_create(
|
||||||
|
[
|
||||||
|
Album.view_groups.through(
|
||||||
|
album=album_id_old_to_new[g.sithfile_id], group_id=g.group_id
|
||||||
|
)
|
||||||
|
for g in SithFile.view_groups.through.objects.filter(sithfile__in=albums)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
Album.edit_groups.through.objects.bulk_create(
|
||||||
|
[
|
||||||
|
Album.view_groups.through(
|
||||||
|
album=album_id_old_to_new[g.sithfile_id], group_id=g.group_id
|
||||||
|
)
|
||||||
|
for g in SithFile.view_groups.through.objects.filter(sithfile__in=albums)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
("core", "0044_alter_userban_options"),
|
||||||
|
("sas", "0005_alter_sasfile_options"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
# les relations et les demandes de modération étaient liées à SithFile,
|
||||||
|
# via le model proxy Picture.
|
||||||
|
# Pour que la migration marche malgré la disparition du modèle Proxy,
|
||||||
|
# on change la relation pour qu'elle pointe directement vers SithFile
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="peoplepicturerelation",
|
||||||
|
name="picture",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="people",
|
||||||
|
to="core.sithfile",
|
||||||
|
verbose_name="picture",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="picturemoderationrequest",
|
||||||
|
name="picture",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="moderation_requests",
|
||||||
|
to="core.sithfile",
|
||||||
|
verbose_name="Picture",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.DeleteModel(name="Album"),
|
||||||
|
migrations.DeleteModel(name="Picture"),
|
||||||
|
migrations.DeleteModel(name="SasFile"),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="Album",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"thumbnail",
|
||||||
|
models.FileField(
|
||||||
|
max_length=256,
|
||||||
|
upload_to=sas.models.get_thumbnail_directory,
|
||||||
|
verbose_name="thumbnail",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("name", models.CharField(max_length=100, verbose_name="name")),
|
||||||
|
(
|
||||||
|
"event_date",
|
||||||
|
models.DateField(
|
||||||
|
default=django.utils.timezone.localdate,
|
||||||
|
help_text="The date on which the photos in this album were taken",
|
||||||
|
verbose_name="event date",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"is_moderated",
|
||||||
|
models.BooleanField(default=False, verbose_name="is moderated"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"edit_groups",
|
||||||
|
models.ManyToManyField(
|
||||||
|
related_name="editable_albums",
|
||||||
|
to="core.group",
|
||||||
|
verbose_name="edit groups",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"parent",
|
||||||
|
models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="children",
|
||||||
|
to="sas.album",
|
||||||
|
verbose_name="parent",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"view_groups",
|
||||||
|
models.ManyToManyField(
|
||||||
|
related_name="viewable_albums",
|
||||||
|
to="core.group",
|
||||||
|
verbose_name="view groups",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={"verbose_name": "album"},
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="Picture",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"thumbnail",
|
||||||
|
models.FileField(
|
||||||
|
unique=True,
|
||||||
|
upload_to=sas.models.get_thumbnail_directory,
|
||||||
|
verbose_name="thumbnail",
|
||||||
|
max_length=256,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("name", models.CharField(max_length=256, verbose_name="file name")),
|
||||||
|
(
|
||||||
|
"original",
|
||||||
|
models.FileField(
|
||||||
|
unique=True,
|
||||||
|
upload_to=sas.models.get_directory,
|
||||||
|
verbose_name="original image",
|
||||||
|
max_length=256,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"compressed",
|
||||||
|
models.FileField(
|
||||||
|
unique=True,
|
||||||
|
upload_to=sas.models.get_compressed_directory,
|
||||||
|
verbose_name="compressed image",
|
||||||
|
max_length=256,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("created_at", models.DateTimeField(default=django.utils.timezone.now)),
|
||||||
|
(
|
||||||
|
"is_moderated",
|
||||||
|
models.BooleanField(default=False, verbose_name="is moderated"),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"asked_for_removal",
|
||||||
|
models.BooleanField(
|
||||||
|
default=False, verbose_name="asked for removal"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"moderator",
|
||||||
|
models.ForeignKey(
|
||||||
|
blank=True,
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.SET_NULL,
|
||||||
|
related_name="moderated_pictures",
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"owner",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.PROTECT,
|
||||||
|
related_name="owned_pictures",
|
||||||
|
to=settings.AUTH_USER_MODEL,
|
||||||
|
verbose_name="owner",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"parent",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="pictures",
|
||||||
|
to="sas.album",
|
||||||
|
verbose_name="album",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
options={"abstract": False, "verbose_name": "picture"},
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name="picture",
|
||||||
|
constraint=models.UniqueConstraint(
|
||||||
|
fields=("name", "parent"), name="sas_picture_unique_per_album"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AddConstraint(
|
||||||
|
model_name="album",
|
||||||
|
constraint=models.UniqueConstraint(
|
||||||
|
fields=("name", "parent"), name="unique_album_name_if_same_parent"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.RunPython(
|
||||||
|
copy_albums_and_pictures,
|
||||||
|
reverse_code=migrations.RunPython.noop,
|
||||||
|
elidable=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# Generated by Django 4.2.17 on 2025-01-25 23:50
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
dependencies = [("sas", "0006_move_the_whole_sas")]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="peoplepicturerelation",
|
||||||
|
name="picture",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="people",
|
||||||
|
to="sas.picture",
|
||||||
|
verbose_name="picture",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="picturemoderationrequest",
|
||||||
|
name="picture",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE,
|
||||||
|
related_name="moderation_requests",
|
||||||
|
to="sas.picture",
|
||||||
|
verbose_name="Picture",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
||||||
401
sas/models.py
401
sas/models.py
@@ -18,29 +18,52 @@ from __future__ import annotations
|
|||||||
import contextlib
|
import contextlib
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import ClassVar, Self
|
from typing import TYPE_CHECKING, ClassVar, Self
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.core.files.base import ContentFile
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Exists, OuterRef, Q
|
from django.db.models import Exists, OuterRef, Q
|
||||||
|
from django.db.models.deletion import Collector
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.utils.functional import cached_property
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from core.models import Notification, SithFile, User
|
from core.models import Group, Notification, User
|
||||||
from core.utils import exif_auto_rotate, resize_image
|
from core.utils import exif_auto_rotate, resize_image
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from django.db.models.fields.files import FieldFile
|
||||||
|
|
||||||
class SasFile(SithFile):
|
|
||||||
"""Proxy model for any file in the SAS.
|
def get_directory(instance: SasFile, filename: str):
|
||||||
|
return f"./{instance.parent_path}/{filename}"
|
||||||
|
|
||||||
|
|
||||||
|
def get_compressed_directory(instance: SasFile, filename: str):
|
||||||
|
return f"./.compressed/{instance.parent_path}/{filename}"
|
||||||
|
|
||||||
|
|
||||||
|
def get_thumbnail_directory(instance: SasFile, filename: str):
|
||||||
|
if isinstance(instance, Album):
|
||||||
|
_, extension = filename.rsplit(".", 1)
|
||||||
|
filename = f"{instance.name}/thumb.{extension}"
|
||||||
|
return f"./.thumbnails/{instance.parent_path}/{filename}"
|
||||||
|
|
||||||
|
|
||||||
|
class SasFile(models.Model):
|
||||||
|
"""Abstract model for SAS files
|
||||||
|
|
||||||
May be used to have logic that should be shared by both
|
May be used to have logic that should be shared by both
|
||||||
[Picture][sas.models.Picture] and [Album][sas.models.Album].
|
[Picture][sas.models.Picture] and [Album][sas.models.Album].
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
proxy = True
|
abstract = True
|
||||||
permissions = [
|
permissions = [
|
||||||
("moderate_sasfile", "Can moderate SAS files"),
|
("moderate_sasfile", "Can moderate SAS files"),
|
||||||
("view_unmoderated_sasfile", "Can view not moderated SAS files"),
|
("view_unmoderated_sasfile", "Can view not moderated SAS files"),
|
||||||
@@ -65,6 +88,169 @@ class SasFile(SithFile):
|
|||||||
def can_be_edited_by(self, user):
|
def can_be_edited_by(self, user):
|
||||||
return user.has_perm("sas.change_sasfile")
|
return user.has_perm("sas.change_sasfile")
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def parent_path(self) -> str:
|
||||||
|
"""The parent location in the SAS album tree (e.g. `SAS/foo/bar`)."""
|
||||||
|
return "/".join(["SAS", *[p.name for p in self.parent_list]])
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def parent_list(self) -> list[Album]:
|
||||||
|
"""The ancestors of this SAS object.
|
||||||
|
|
||||||
|
The result is ordered from the direct parent to the farthest one.
|
||||||
|
"""
|
||||||
|
parents = []
|
||||||
|
current = self.parent
|
||||||
|
while current is not None:
|
||||||
|
parents.append(current)
|
||||||
|
current = current.parent
|
||||||
|
return parents
|
||||||
|
|
||||||
|
|
||||||
|
class AlbumQuerySet(models.QuerySet):
|
||||||
|
def viewable_by(self, user: User) -> Self:
|
||||||
|
"""Filter the albums that this user can view.
|
||||||
|
|
||||||
|
Warning:
|
||||||
|
Calling this queryset method may add several additional requests.
|
||||||
|
"""
|
||||||
|
if user.is_root or user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID):
|
||||||
|
return self.all()
|
||||||
|
if user.was_subscribed:
|
||||||
|
return self.filter(is_moderated=True)
|
||||||
|
# known bug : if all children of an album are also albums
|
||||||
|
# then this album is excluded, even if one of the sub-albums should be visible.
|
||||||
|
# The fs-like navigation is likely to be half-broken for non-subscribers,
|
||||||
|
# but that's ok, since non-subscribers are expected to see only the albums
|
||||||
|
# containing pictures on which they have been identified (hence, very few).
|
||||||
|
# Most, if not all, of their albums will be displayed on the
|
||||||
|
# `latest albums` section of the SAS.
|
||||||
|
# Moreover, they will still see all of their picture in their profile.
|
||||||
|
return self.filter(
|
||||||
|
Exists(Picture.objects.filter(parent_id=OuterRef("pk")).viewable_by(user))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Album(SasFile):
|
||||||
|
NAME_MAX_LENGTH: ClassVar[int] = 50
|
||||||
|
|
||||||
|
name = models.CharField(_("name"), max_length=100)
|
||||||
|
parent = models.ForeignKey(
|
||||||
|
"self",
|
||||||
|
related_name="children",
|
||||||
|
verbose_name=_("parent"),
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
)
|
||||||
|
thumbnail = models.FileField(
|
||||||
|
upload_to=get_thumbnail_directory,
|
||||||
|
verbose_name=_("thumbnail"),
|
||||||
|
max_length=256,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
view_groups = models.ManyToManyField(
|
||||||
|
Group, related_name="viewable_albums", verbose_name=_("view groups"), blank=True
|
||||||
|
)
|
||||||
|
edit_groups = models.ManyToManyField(
|
||||||
|
Group, related_name="editable_albums", verbose_name=_("edit groups"), blank=True
|
||||||
|
)
|
||||||
|
event_date = models.DateField(
|
||||||
|
_("event date"),
|
||||||
|
help_text=_("The date on which the photos in this album were taken"),
|
||||||
|
default=timezone.localdate,
|
||||||
|
blank=True,
|
||||||
|
)
|
||||||
|
is_moderated = models.BooleanField(_("is moderated"), default=False)
|
||||||
|
|
||||||
|
objects = AlbumQuerySet.as_manager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = _("album")
|
||||||
|
constraints = [
|
||||||
|
models.UniqueConstraint(
|
||||||
|
fields=["name", "parent"],
|
||||||
|
name="unique_album_name_if_same_parent",
|
||||||
|
# TODO : add `nulls_distinct=True` after upgrading to django>=5.0
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Album {self.name}"
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
for user in User.objects.filter(
|
||||||
|
groups__id__in=[settings.SITH_GROUP_SAS_ADMIN_ID]
|
||||||
|
):
|
||||||
|
Notification(
|
||||||
|
user=user,
|
||||||
|
url=reverse("sas:moderation"),
|
||||||
|
type="SAS_MODERATION",
|
||||||
|
param="1",
|
||||||
|
).save()
|
||||||
|
|
||||||
|
def get_absolute_url(self):
|
||||||
|
return reverse("sas:album", kwargs={"album_id": self.id})
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
super().clean()
|
||||||
|
if "/" in self.name:
|
||||||
|
raise ValidationError(_("Character '/' not authorized in name"))
|
||||||
|
if self.parent_id is not None and (
|
||||||
|
self.id == self.parent_id or self in self.parent_list
|
||||||
|
):
|
||||||
|
raise ValidationError(_("Loop in album tree"), code="loop")
|
||||||
|
if self.thumbnail:
|
||||||
|
try:
|
||||||
|
Image.open(BytesIO(self.thumbnail.read()))
|
||||||
|
except Image.UnidentifiedImageError as e:
|
||||||
|
raise ValidationError(_("This is not a valid album thumbnail")) from e
|
||||||
|
|
||||||
|
def delete(self, *args, **kwargs):
|
||||||
|
"""Delete the album, all of its children and all linked disk files"""
|
||||||
|
collector = Collector(using="default")
|
||||||
|
collector.collect([self])
|
||||||
|
albums: set[Album] = collector.data[Album]
|
||||||
|
pictures: set[Picture] = collector.data[Picture]
|
||||||
|
files: list[FieldFile] = [
|
||||||
|
*[a.thumbnail for a in albums],
|
||||||
|
*[p.thumbnail for p in pictures],
|
||||||
|
*[p.compressed for p in pictures],
|
||||||
|
*[p.original for p in pictures],
|
||||||
|
]
|
||||||
|
# `bool(f)` checks that the file actually exists on the disk
|
||||||
|
files = [f for f in files if bool(f)]
|
||||||
|
folders = {Path(f.path).parent for f in files}
|
||||||
|
res = super().delete(*args, **kwargs)
|
||||||
|
# once the model instances have been deleted,
|
||||||
|
# delete the actual files.
|
||||||
|
for file in files:
|
||||||
|
# save=False ensures that django doesn't recreate the db record,
|
||||||
|
# which would make the whole deletion pointless
|
||||||
|
# cf. https://docs.djangoproject.com/en/stable/ref/models/fields/#django.db.models.fields.files.FieldFile.delete
|
||||||
|
file.delete(save=False)
|
||||||
|
for folder in folders:
|
||||||
|
# now that the files are deleted, remove the empty folders
|
||||||
|
if folder.is_dir() and next(folder.iterdir(), None) is None:
|
||||||
|
folder.rmdir()
|
||||||
|
return res
|
||||||
|
|
||||||
|
def get_download_url(self):
|
||||||
|
return reverse("sas:album_preview", kwargs={"album_id": self.id})
|
||||||
|
|
||||||
|
def generate_thumbnail(self):
|
||||||
|
p = (
|
||||||
|
self.pictures.exclude(thumbnail="").order_by("?").first()
|
||||||
|
or self.children.exclude(thumbnail="").order_by("?").first()
|
||||||
|
)
|
||||||
|
if p:
|
||||||
|
# The file is loaded into memory to duplicate it.
|
||||||
|
# It may not be the most efficient way, but thumbnails are
|
||||||
|
# usually quite small, so it's still ok
|
||||||
|
self.thumbnail = ContentFile(p.thumbnail.read(), name="thumb.webp")
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
class PictureQuerySet(models.QuerySet):
|
class PictureQuerySet(models.QuerySet):
|
||||||
def viewable_by(self, user: User) -> Self:
|
def viewable_by(self, user: User) -> Self:
|
||||||
@@ -80,23 +266,65 @@ class PictureQuerySet(models.QuerySet):
|
|||||||
return self.filter(people__user_id=user.id, is_moderated=True)
|
return self.filter(people__user_id=user.id, is_moderated=True)
|
||||||
|
|
||||||
|
|
||||||
class SASPictureManager(models.Manager):
|
|
||||||
def get_queryset(self):
|
|
||||||
return super().get_queryset().filter(is_in_sas=True, is_folder=False)
|
|
||||||
|
|
||||||
|
|
||||||
class Picture(SasFile):
|
class Picture(SasFile):
|
||||||
|
name = models.CharField(_("file name"), max_length=256)
|
||||||
|
parent = models.ForeignKey(
|
||||||
|
Album,
|
||||||
|
related_name="pictures",
|
||||||
|
verbose_name=_("album"),
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
)
|
||||||
|
thumbnail = models.FileField(
|
||||||
|
upload_to=get_thumbnail_directory,
|
||||||
|
verbose_name=_("thumbnail"),
|
||||||
|
max_length=256,
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
original = models.FileField(
|
||||||
|
upload_to=get_directory,
|
||||||
|
verbose_name=_("original image"),
|
||||||
|
max_length=256,
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
compressed = models.FileField(
|
||||||
|
upload_to=get_compressed_directory,
|
||||||
|
verbose_name=_("compressed image"),
|
||||||
|
max_length=256,
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
created_at = models.DateTimeField(default=timezone.now)
|
||||||
|
owner = models.ForeignKey(
|
||||||
|
User,
|
||||||
|
related_name="owned_pictures",
|
||||||
|
verbose_name=_("owner"),
|
||||||
|
on_delete=models.PROTECT,
|
||||||
|
)
|
||||||
|
|
||||||
|
is_moderated = models.BooleanField(_("is moderated"), default=False)
|
||||||
|
asked_for_removal = models.BooleanField(_("asked for removal"), default=False)
|
||||||
|
moderator = models.ForeignKey(
|
||||||
|
User,
|
||||||
|
related_name="moderated_pictures",
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
on_delete=models.SET_NULL,
|
||||||
|
)
|
||||||
|
|
||||||
|
objects = PictureQuerySet.as_manager()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
proxy = True
|
verbose_name = _("picture")
|
||||||
|
constraints = [
|
||||||
|
models.UniqueConstraint(
|
||||||
|
fields=["name", "parent"], name="sas_picture_unique_per_album"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
objects = SASPictureManager.from_queryset(PictureQuerySet)()
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
@property
|
def get_absolute_url(self):
|
||||||
def is_vertical(self):
|
return reverse("sas:picture", kwargs={"picture_id": self.id})
|
||||||
with open(settings.MEDIA_ROOT / self.file.name, "rb") as f:
|
|
||||||
im = Image.open(BytesIO(f.read()))
|
|
||||||
(w, h) = im.size
|
|
||||||
return (w / h) < 1
|
|
||||||
|
|
||||||
def get_download_url(self):
|
def get_download_url(self):
|
||||||
return reverse("sas:download", kwargs={"picture_id": self.id})
|
return reverse("sas:download", kwargs={"picture_id": self.id})
|
||||||
@@ -107,41 +335,34 @@ class Picture(SasFile):
|
|||||||
def get_download_thumb_url(self):
|
def get_download_thumb_url(self):
|
||||||
return reverse("sas:download_thumb", kwargs={"picture_id": self.id})
|
return reverse("sas:download_thumb", kwargs={"picture_id": self.id})
|
||||||
|
|
||||||
def get_absolute_url(self):
|
@property
|
||||||
return reverse("sas:picture", kwargs={"picture_id": self.id})
|
def is_vertical(self):
|
||||||
|
# original, compressed and thumbnail image have all three the same ratio,
|
||||||
|
# so the smallest one is used to tell if the image is vertical
|
||||||
|
im = Image.open(BytesIO(self.thumbnail.read()))
|
||||||
|
(w, h) = im.size
|
||||||
|
return w < h
|
||||||
|
|
||||||
def generate_thumbnails(self, *, overwrite=False):
|
def generate_thumbnails(self):
|
||||||
im = Image.open(BytesIO(self.file.read()))
|
im = Image.open(self.original)
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
im = exif_auto_rotate(im)
|
im = exif_auto_rotate(im)
|
||||||
# convert the compressed image and the thumbnail into webp
|
# convert the compressed image and the thumbnail into webp
|
||||||
# The original image keeps its original type, because it's not
|
|
||||||
# meant to be shown on the website, but rather to keep the real image
|
|
||||||
# for less frequent cases (like downloading the pictures of an user)
|
|
||||||
extension = self.mime_type.split("/")[-1]
|
|
||||||
# the HD version of the image doesn't need to be optimized, because :
|
# the HD version of the image doesn't need to be optimized, because :
|
||||||
# - it isn't frequently queried
|
# - it isn't frequently queried
|
||||||
# - optimizing large images takes a lot time, which greatly hinders the UX
|
# - optimizing large images takes a lot of time, which greatly hinders the UX
|
||||||
# - photographers usually already optimize their images
|
# - photographers usually already optimize their images
|
||||||
file = resize_image(im, max(im.size), extension, optimize=False)
|
|
||||||
thumb = resize_image(im, 200, "webp")
|
thumb = resize_image(im, 200, "webp")
|
||||||
compressed = resize_image(im, 1200, "webp")
|
compressed = resize_image(im, 1200, "webp")
|
||||||
if overwrite:
|
new_extension_name = str(Path(self.original.name).with_suffix(".webp"))
|
||||||
self.file.delete()
|
|
||||||
self.thumbnail.delete()
|
|
||||||
self.compressed.delete()
|
|
||||||
new_extension_name = str(Path(self.name).with_suffix(".webp"))
|
|
||||||
self.file = file
|
|
||||||
self.file.name = self.name
|
|
||||||
self.thumbnail = thumb
|
self.thumbnail = thumb
|
||||||
self.thumbnail.name = new_extension_name
|
self.thumbnail.name = new_extension_name
|
||||||
self.compressed = compressed
|
self.compressed = compressed
|
||||||
self.compressed.name = new_extension_name
|
self.compressed.name = new_extension_name
|
||||||
|
|
||||||
def rotate(self, degree):
|
def rotate(self, degree):
|
||||||
for attr in ["file", "compressed", "thumbnail"]:
|
for field in self.original, self.compressed, self.thumbnail:
|
||||||
name = self.__getattribute__(attr).name
|
with open(field.file, "r+b") as file:
|
||||||
with open(settings.MEDIA_ROOT / name, "r+b") as file:
|
|
||||||
if file:
|
if file:
|
||||||
im = Image.open(BytesIO(file.read()))
|
im = Image.open(BytesIO(file.read()))
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
@@ -154,110 +375,6 @@ class Picture(SasFile):
|
|||||||
progressive=True,
|
progressive=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_next(self):
|
|
||||||
if self.is_moderated:
|
|
||||||
pictures_qs = self.parent.children.filter(
|
|
||||||
is_moderated=True,
|
|
||||||
asked_for_removal=False,
|
|
||||||
is_folder=False,
|
|
||||||
id__gt=self.id,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
pictures_qs = Picture.objects.filter(id__gt=self.id, is_moderated=False)
|
|
||||||
return pictures_qs.order_by("id").first()
|
|
||||||
|
|
||||||
def get_previous(self):
|
|
||||||
if self.is_moderated:
|
|
||||||
pictures_qs = self.parent.children.filter(
|
|
||||||
is_moderated=True,
|
|
||||||
asked_for_removal=False,
|
|
||||||
is_folder=False,
|
|
||||||
id__lt=self.id,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
pictures_qs = Picture.objects.filter(id__lt=self.id, is_moderated=False)
|
|
||||||
return pictures_qs.order_by("-id").first()
|
|
||||||
|
|
||||||
|
|
||||||
class AlbumQuerySet(models.QuerySet):
|
|
||||||
def viewable_by(self, user: User) -> Self:
|
|
||||||
"""Filter the albums that this user can view.
|
|
||||||
|
|
||||||
Warning:
|
|
||||||
Calling this queryset method may add several additional requests.
|
|
||||||
"""
|
|
||||||
if user.has_perm("sas.moderate_sasfile"):
|
|
||||||
return self.all()
|
|
||||||
if user.was_subscribed:
|
|
||||||
return self.filter(Q(is_moderated=True) | Q(owner=user))
|
|
||||||
# known bug : if all children of an album are also albums
|
|
||||||
# then this album is excluded, even if one of the sub-albums should be visible.
|
|
||||||
# The fs-like navigation is likely to be half-broken for non-subscribers,
|
|
||||||
# but that's ok, since non-subscribers are expected to see only the albums
|
|
||||||
# containing pictures on which they have been identified (hence, very few).
|
|
||||||
# Most, if not all, of their albums will be displayed on the
|
|
||||||
# `latest albums` section of the SAS.
|
|
||||||
# Moreover, they will still see all of their picture in their profile.
|
|
||||||
return self.filter(
|
|
||||||
Exists(Picture.objects.filter(parent_id=OuterRef("pk")).viewable_by(user))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class SASAlbumManager(models.Manager):
|
|
||||||
def get_queryset(self):
|
|
||||||
return super().get_queryset().filter(is_in_sas=True, is_folder=True)
|
|
||||||
|
|
||||||
|
|
||||||
class Album(SasFile):
|
|
||||||
NAME_MAX_LENGTH: ClassVar[int] = 50
|
|
||||||
"""Maximum length of an album's name.
|
|
||||||
|
|
||||||
[SithFile][core.models.SithFile] have a maximum length
|
|
||||||
of 256 characters.
|
|
||||||
However, this limit is too high for albums.
|
|
||||||
Names longer than 50 characters are harder to read
|
|
||||||
and harder to display on the SAS page.
|
|
||||||
|
|
||||||
It is to be noted, though, that this does not
|
|
||||||
add or modify any db behaviour.
|
|
||||||
It's just a constant to be used in views and forms.
|
|
||||||
"""
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
proxy = True
|
|
||||||
|
|
||||||
objects = SASAlbumManager.from_queryset(AlbumQuerySet)()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def children_pictures(self):
|
|
||||||
return Picture.objects.filter(parent=self)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def children_albums(self):
|
|
||||||
return Album.objects.filter(parent=self)
|
|
||||||
|
|
||||||
def get_absolute_url(self):
|
|
||||||
if self.id == settings.SITH_SAS_ROOT_DIR_ID:
|
|
||||||
return reverse("sas:main")
|
|
||||||
return reverse("sas:album", kwargs={"album_id": self.id})
|
|
||||||
|
|
||||||
def get_download_url(self):
|
|
||||||
return reverse("sas:album_preview", kwargs={"album_id": self.id})
|
|
||||||
|
|
||||||
def generate_thumbnail(self):
|
|
||||||
p = (
|
|
||||||
self.children_pictures.order_by("?").first()
|
|
||||||
or self.children_albums.exclude(file=None)
|
|
||||||
.exclude(file="")
|
|
||||||
.order_by("?")
|
|
||||||
.first()
|
|
||||||
)
|
|
||||||
if p and p.file:
|
|
||||||
image = resize_image(Image.open(BytesIO(p.file.read())), 200, "webp")
|
|
||||||
self.file = image
|
|
||||||
self.file.name = f"{self.name}/thumb.webp"
|
|
||||||
self.save()
|
|
||||||
|
|
||||||
|
|
||||||
def sas_notification_callback(notif: Notification):
|
def sas_notification_callback(notif: Notification):
|
||||||
count = Picture.objects.filter(is_moderated=False).count()
|
count = Picture.objects.filter(is_moderated=False).count()
|
||||||
|
|||||||
@@ -55,7 +55,12 @@ class AlbumAutocompleteSchema(ModelSchema):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_path(obj: Album) -> str:
|
def resolve_path(obj: Album) -> str:
|
||||||
return str(Path(obj.get_parent_path()) / obj.name)
|
return str(Path(obj.parent_path) / obj.name)
|
||||||
|
|
||||||
|
|
||||||
|
class MoveAlbumSchema(Schema):
|
||||||
|
id: int
|
||||||
|
new_parent_id: int
|
||||||
|
|
||||||
|
|
||||||
class PictureFilterSchema(FilterSchema):
|
class PictureFilterSchema(FilterSchema):
|
||||||
@@ -70,7 +75,7 @@ class PictureFilterSchema(FilterSchema):
|
|||||||
class PictureSchema(ModelSchema):
|
class PictureSchema(ModelSchema):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Picture
|
model = Picture
|
||||||
fields = ["id", "name", "date", "size", "is_moderated", "asked_for_removal"]
|
fields = ["id", "name", "created_at", "is_moderated", "asked_for_removal"]
|
||||||
|
|
||||||
owner: UserProfileSchema
|
owner: UserProfileSchema
|
||||||
sas_url: str
|
sas_url: str
|
||||||
|
|||||||
@@ -125,3 +125,108 @@ document.addEventListener("alpine:init", () => {
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Todo: migrate to alpine.js if we have some time
|
||||||
|
// $("form#upload_form").submit(function (event) {
|
||||||
|
// const formData = new FormData($(this)[0]);
|
||||||
|
//
|
||||||
|
// if (!formData.get("album_name") && !formData.get("images").name) return false;
|
||||||
|
//
|
||||||
|
// if (!formData.get("images").name) {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// event.preventDefault();
|
||||||
|
//
|
||||||
|
// let errorList = this.querySelector("#upload_form ul.errorlist.nonfield");
|
||||||
|
// if (errorList === null) {
|
||||||
|
// errorList = document.createElement("ul");
|
||||||
|
// errorList.classList.add("errorlist", "nonfield");
|
||||||
|
// this.insertBefore(errorList, this.firstElementChild);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// while (errorList.childElementCount > 0)
|
||||||
|
// errorList.removeChild(errorList.firstElementChild);
|
||||||
|
//
|
||||||
|
// let progress = this.querySelector("progress");
|
||||||
|
// if (progress === null) {
|
||||||
|
// progress = document.createElement("progress");
|
||||||
|
// progress.value = 0;
|
||||||
|
// const p = document.createElement("p");
|
||||||
|
// p.appendChild(progress);
|
||||||
|
// this.insertBefore(p, this.lastElementChild);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// let dataHolder;
|
||||||
|
//
|
||||||
|
// if (formData.get("album_name")) {
|
||||||
|
// dataHolder = new FormData();
|
||||||
|
// dataHolder.set("csrfmiddlewaretoken", "{{ csrf_token }}");
|
||||||
|
// dataHolder.set("album_name", formData.get("album_name"));
|
||||||
|
// $.ajax({
|
||||||
|
// method: "POST",
|
||||||
|
// url: "{{ url('sas:album_upload', album_id=object.id) }}",
|
||||||
|
// data: dataHolder,
|
||||||
|
// processData: false,
|
||||||
|
// contentType: false,
|
||||||
|
// success: onSuccess,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// const images = formData.getAll("images");
|
||||||
|
// const imagesCount = images.length;
|
||||||
|
// let completeCount = 0;
|
||||||
|
//
|
||||||
|
// const poolSize = 1;
|
||||||
|
// const imagePool = [];
|
||||||
|
//
|
||||||
|
// while (images.length > 0 && imagePool.length < poolSize) {
|
||||||
|
// const image = images.shift();
|
||||||
|
// imagePool.push(image);
|
||||||
|
// sendImage(image);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// function sendImage(image) {
|
||||||
|
// dataHolder = new FormData();
|
||||||
|
// dataHolder.set("csrfmiddlewaretoken", "{{ csrf_token }}");
|
||||||
|
// dataHolder.set("images", image);
|
||||||
|
//
|
||||||
|
// $.ajax({
|
||||||
|
// method: "POST",
|
||||||
|
// url: "{{ url('sas:album_upload', album_id=object.id) }}",
|
||||||
|
// data: dataHolder,
|
||||||
|
// processData: false,
|
||||||
|
// contentType: false,
|
||||||
|
// })
|
||||||
|
// .fail(onSuccess.bind(undefined, image))
|
||||||
|
// .done(onSuccess.bind(undefined, image))
|
||||||
|
// .always(next.bind(undefined, image));
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// function next(image, _, __) {
|
||||||
|
// const index = imagePool.indexOf(image);
|
||||||
|
// const nextImage = images.shift();
|
||||||
|
//
|
||||||
|
// if (index !== -1) {
|
||||||
|
// imagePool.splice(index, 1);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (nextImage) {
|
||||||
|
// imagePool.push(nextImage);
|
||||||
|
// sendImage(nextImage);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// function onSuccess(image, data, _, __) {
|
||||||
|
// let errors = [];
|
||||||
|
//
|
||||||
|
// if ($(data.responseText).find(".errorlist.nonfield")[0])
|
||||||
|
// errors = Array.from($(data.responseText).find(".errorlist.nonfield")[0].children);
|
||||||
|
//
|
||||||
|
// while (errors.length > 0) errorList.appendChild(errors.shift());
|
||||||
|
//
|
||||||
|
// progress.value = ++completeCount / imagesCount;
|
||||||
|
// if (progress.value === 1 && errorList.children.length === 0)
|
||||||
|
// document.location.reload();
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ document.addEventListener("alpine:init", () => {
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
this.downloadPictures.map(async (p: PictureSchema) => {
|
this.downloadPictures.map(async (p: PictureSchema) => {
|
||||||
const imgName = `${p.album}/IMG_${p.date.replace(/[:\-]/g, "_")}${p.name.slice(p.name.lastIndexOf("."))}`;
|
const imgName = `${p.album}/IMG_${p.created_at.replace(/[:\-]/g, "_")}${p.name.slice(p.name.lastIndexOf("."))}`;
|
||||||
return zipWriter.add(imgName, new HttpReader(p.full_size_url), {
|
return zipWriter.add(imgName, new HttpReader(p.full_size_url), {
|
||||||
level: 9,
|
level: 9,
|
||||||
lastModDate: new Date(p.date),
|
lastModDate: new Date(p.created_at),
|
||||||
onstart: incrementProgressBar,
|
onstart: incrementProgressBar,
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -142,7 +142,8 @@ exportToHtml("loadViewer", (config: ViewerConfig) => {
|
|||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
full_size_url: "",
|
full_size_url: "",
|
||||||
owner: "",
|
owner: "",
|
||||||
date: new Date(),
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
|
created_at: new Date(),
|
||||||
identifications: [] as IdentifiedUserSchema[],
|
identifications: [] as IdentifiedUserSchema[],
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<code>
|
<code>
|
||||||
<a href="{{ url('sas:main') }}">SAS</a> / {{ print_path(album.parent) }} {{ album.get_display_name() }}
|
<a href="{{ url('sas:main') }}">SAS</a> / {{ print_path(album.parent) }} {{ album.name }}
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
{% set is_sas_admin = user.can_edit(album) %}
|
{% set is_sas_admin = user.can_edit(album) %}
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
<form action="" method="post" enctype="multipart/form-data">
|
<form action="" method="post" enctype="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="album-navbar">
|
<div class="album-navbar">
|
||||||
<h3>{{ album.get_display_name() }}</h3>
|
<h3>{{ album.name }}</h3>
|
||||||
|
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<a href="{{ url('sas:album_edit', album_id=album.id) }}">{% trans %}Edit{% endtrans %}</a>
|
<a href="{{ url('sas:album_edit', album_id=album.id) }}">{% trans %}Edit{% endtrans %}</a>
|
||||||
@@ -40,17 +40,17 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if clipboard %}
|
{# {% if clipboard %}#}
|
||||||
<div class="clipboard">
|
{# <div class="clipboard">#}
|
||||||
{% trans %}Clipboard: {% endtrans %}
|
{# {% trans %}Clipboard: {% endtrans %}#}
|
||||||
<ul>
|
{# <ul>#}
|
||||||
{% for f in clipboard %}
|
{# {% for f in clipboard["albums"] %}#}
|
||||||
<li>{{ f.get_full_path() }}</li>
|
{# <li>{{ f.get_full_path() }}</li>#}
|
||||||
{% endfor %}
|
{# {% endfor %}#}
|
||||||
</ul>
|
{# </ul>#}
|
||||||
<input name="clear" type="submit" value="{% trans %}Clear clipboard{% endtrans %}">
|
{# <input name="clear" type="submit" value="{% trans %}Clear clipboard{% endtrans %}">#}
|
||||||
</div>
|
{# </div>#}
|
||||||
{% endif %}
|
{# {% endif %}#}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if show_albums %}
|
{% if show_albums %}
|
||||||
@@ -73,8 +73,8 @@
|
|||||||
<div class="text">{% trans %}To be moderated{% endtrans %}</div>
|
<div class="text">{% trans %}To be moderated{% endtrans %}</div>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
{% if is_sas_admin %}
|
{% if edit_mode %}
|
||||||
<input type="checkbox" name="file_list" :value="album.id">
|
<input type="checkbox" name="album_list" :value="album.id">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
</template>
|
</template>
|
||||||
@@ -100,7 +100,7 @@
|
|||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
{% if is_sas_admin %}
|
{% if is_sas_admin %}
|
||||||
<input type="checkbox" name="file_list" :value="picture.id">
|
<input type="checkbox" name="picture_list" :value="picture.id">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
</template>
|
</template>
|
||||||
@@ -120,9 +120,9 @@
|
|||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="inputs">
|
<div class="inputs">
|
||||||
<p>
|
<p>
|
||||||
<label for="{{ upload_form.images.id_for_label }}">{{ upload_form.images.label }} :</label>
|
<label for="{{ form.images.id_for_label }}">{{ form.images.label }} :</label>
|
||||||
{{ upload_form.images|add_attr("x-ref=pictures") }}
|
{{ form.images|add_attr("x-ref=pictures") }}
|
||||||
<span class="helptext">{{ upload_form.images.help_text }}</span>
|
<span class="helptext">{{ form.images.help_text }}</span>
|
||||||
</p>
|
</p>
|
||||||
<input type="submit" value="{% trans %}Upload{% endtrans %}" />
|
<input type="submit" value="{% trans %}Upload{% endtrans %}" />
|
||||||
<progress x-ref="progress" x-show="sending"></progress>
|
<progress x-ref="progress" x-show="sending"></progress>
|
||||||
|
|||||||
@@ -1,19 +1,13 @@
|
|||||||
{% macro display_album(a, edit_mode) %}
|
{% macro display_album(a, edit_mode) %}
|
||||||
<a href="{{ url('sas:album', album_id=a.id) }}">
|
<a href="{{ url('sas:album', album_id=a.id) }}">
|
||||||
{% if a.file %}
|
{% if a.thumbnail %}
|
||||||
{% set img = a.get_download_url() %}
|
{% set img = a.get_download_url() %}
|
||||||
{% set src = a.name %}
|
{% set src = a.name %}
|
||||||
{% elif a.children.filter(is_folder=False, is_moderated=True).exists() %}
|
|
||||||
{% set picture = a.children.filter(is_folder=False).first().as_picture %}
|
|
||||||
{% set img = picture.get_download_thumb_url() %}
|
|
||||||
{% set src = picture.name %}
|
|
||||||
{% else %}
|
{% else %}
|
||||||
{% set img = static('core/img/sas.jpg') %}
|
{% set img = static('core/img/sas.jpg') %}
|
||||||
{% set src = "sas.jpg" %}
|
{% set src = "sas.jpg" %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div
|
<div class="album{% if not a.is_moderated %} not_moderated{% endif %}">
|
||||||
class="album{% if not a.is_moderated %} not_moderated{% endif %}"
|
|
||||||
>
|
|
||||||
<img src="{{ img }}" alt="{{ src }}" loading="lazy" />
|
<img src="{{ img }}" alt="{{ src }}" loading="lazy" />
|
||||||
{% if not a.is_moderated %}
|
{% if not a.is_moderated %}
|
||||||
<div class="overlay"> </div>
|
<div class="overlay"> </div>
|
||||||
@@ -31,7 +25,7 @@
|
|||||||
{% macro print_path(file) %}
|
{% macro print_path(file) %}
|
||||||
{% if file and file.parent %}
|
{% if file and file.parent %}
|
||||||
{{ print_path(file.parent) }}
|
{{ print_path(file.parent) }}
|
||||||
<a href="{{ url('sas:album', album_id=file.id) }}">{{ file.get_display_name() }}</a> /
|
<a href="{{ url("sas:album", album_id=file.id) }}">{{ file.name }}</a> /
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
{% extends "core/base.jinja" %}
|
{% extends "core/base.jinja" %}
|
||||||
|
|
||||||
{%- block additional_css -%}
|
{%- block additional_css -%}
|
||||||
<link defer rel="stylesheet" href="{{ static('bundled/core/components/ajax-select-index.css') }}">
|
<link rel="stylesheet" href="{{ static('bundled/core/components/ajax-select-index.css') }}">
|
||||||
<link defer rel="stylesheet" href="{{ static('core/components/ajax-select.scss') }}">
|
<link rel="stylesheet" href="{{ static('core/components/ajax-select.scss') }}">
|
||||||
<link defer rel="stylesheet" href="{{ static('sas/css/picture.scss') }}">
|
<link rel="stylesheet" href="{{ static('sas/css/picture.scss') }}">
|
||||||
{%- endblock -%}
|
{%- endblock -%}
|
||||||
|
|
||||||
{%- block additional_js -%}
|
{%- block additional_js -%}
|
||||||
@@ -104,7 +104,7 @@
|
|||||||
<span
|
<span
|
||||||
x-text="Intl.DateTimeFormat(
|
x-text="Intl.DateTimeFormat(
|
||||||
'{{ LANGUAGE_CODE }}', {dateStyle: 'long'}
|
'{{ LANGUAGE_CODE }}', {dateStyle: 'long'}
|
||||||
).format(new Date(currentPicture.date))"
|
).format(new Date(currentPicture.created_at))"
|
||||||
>
|
>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ class TestSas(TestCase):
|
|||||||
cls.user_b, cls.user_c = subscriber_user.make(_quantity=2)
|
cls.user_b, cls.user_c = subscriber_user.make(_quantity=2)
|
||||||
|
|
||||||
picture = picture_recipe.extend(owner=owner)
|
picture = picture_recipe.extend(owner=owner)
|
||||||
cls.album_a = baker.make(Album, is_in_sas=True, parent=sas)
|
cls.album_a = baker.make(Album)
|
||||||
cls.album_b = baker.make(Album, is_in_sas=True, parent=sas)
|
cls.album_b = baker.make(Album)
|
||||||
relation_recipe = Recipe(PeoplePictureRelation)
|
relation_recipe = Recipe(PeoplePictureRelation)
|
||||||
relations = []
|
relations = []
|
||||||
for album in cls.album_a, cls.album_b:
|
for album in cls.album_a, cls.album_b:
|
||||||
@@ -61,7 +61,7 @@ class TestPictureSearch(TestSas):
|
|||||||
self.client.force_login(self.user_b)
|
self.client.force_login(self.user_b)
|
||||||
res = self.client.get(self.url + f"?album_id={self.album_a.id}")
|
res = self.client.get(self.url + f"?album_id={self.album_a.id}")
|
||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
expected = list(self.album_a.children_pictures.values_list("id", flat=True))
|
expected = list(self.album_a.pictures.values_list("id", flat=True))
|
||||||
assert [i["id"] for i in res.json()["results"]] == expected
|
assert [i["id"] for i in res.json()["results"]] == expected
|
||||||
|
|
||||||
def test_filter_by_user(self):
|
def test_filter_by_user(self):
|
||||||
@@ -70,7 +70,7 @@ class TestPictureSearch(TestSas):
|
|||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
expected = list(
|
expected = list(
|
||||||
self.user_a.pictures.order_by(
|
self.user_a.pictures.order_by(
|
||||||
"-picture__parent__date", "picture__date"
|
"-picture__parent__event_date", "picture__created_at"
|
||||||
).values_list("picture_id", flat=True)
|
).values_list("picture_id", flat=True)
|
||||||
)
|
)
|
||||||
assert [i["id"] for i in res.json()["results"]] == expected
|
assert [i["id"] for i in res.json()["results"]] == expected
|
||||||
@@ -84,7 +84,7 @@ class TestPictureSearch(TestSas):
|
|||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
expected = list(
|
expected = list(
|
||||||
self.user_a.pictures.union(self.user_b.pictures.all())
|
self.user_a.pictures.union(self.user_b.pictures.all())
|
||||||
.order_by("-picture__parent__date", "picture__date")
|
.order_by("-picture__parent__event_date", "picture__created_at")
|
||||||
.values_list("picture_id", flat=True)
|
.values_list("picture_id", flat=True)
|
||||||
)
|
)
|
||||||
assert [i["id"] for i in res.json()["results"]] == expected
|
assert [i["id"] for i in res.json()["results"]] == expected
|
||||||
@@ -97,7 +97,7 @@ class TestPictureSearch(TestSas):
|
|||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
expected = list(
|
expected = list(
|
||||||
self.user_a.pictures.order_by(
|
self.user_a.pictures.order_by(
|
||||||
"-picture__parent__date", "picture__date"
|
"-picture__parent__event_date", "picture__created_at"
|
||||||
).values_list("picture_id", flat=True)
|
).values_list("picture_id", flat=True)
|
||||||
)
|
)
|
||||||
assert [i["id"] for i in res.json()["results"]] == expected
|
assert [i["id"] for i in res.json()["results"]] == expected
|
||||||
@@ -123,7 +123,7 @@ class TestPictureSearch(TestSas):
|
|||||||
assert res.status_code == 200
|
assert res.status_code == 200
|
||||||
expected = list(
|
expected = list(
|
||||||
self.user_b.pictures.intersection(self.user_a.pictures.all())
|
self.user_b.pictures.intersection(self.user_a.pictures.all())
|
||||||
.order_by("-picture__parent__date", "picture__date")
|
.order_by("-picture__parent__event_date", "picture__created_at")
|
||||||
.values_list("picture_id", flat=True)
|
.values_list("picture_id", flat=True)
|
||||||
)
|
)
|
||||||
assert [i["id"] for i in res.json()["results"]] == expected
|
assert [i["id"] for i in res.json()["results"]] == expected
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ from model_bakery import baker
|
|||||||
|
|
||||||
from core.baker_recipes import old_subscriber_user, subscriber_user
|
from core.baker_recipes import old_subscriber_user, subscriber_user
|
||||||
from core.models import User
|
from core.models import User
|
||||||
from sas.baker_recipes import picture_recipe
|
from sas.baker_recipes import album_recipe, picture_recipe
|
||||||
from sas.models import PeoplePictureRelation, Picture
|
from sas.models import Album, PeoplePictureRelation, Picture
|
||||||
|
|
||||||
|
|
||||||
class TestPictureQuerySet(TestCase):
|
class TestPictureQuerySet(TestCase):
|
||||||
@@ -67,3 +67,22 @@ def test_identifications_viewable_by_user():
|
|||||||
assert list(picture.people.viewable_by(identifications[1].user)) == [
|
assert list(picture.people.viewable_by(identifications[1].user)) == [
|
||||||
identifications[1]
|
identifications[1]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeleteAlbum(TestCase):
|
||||||
|
def setUp(cls):
|
||||||
|
cls.album: Album = album_recipe.make()
|
||||||
|
cls.album_pictures = picture_recipe.make(parent=cls.album, _quantity=5)
|
||||||
|
cls.sub_album = album_recipe.make(parent=cls.album)
|
||||||
|
cls.sub_album_pictures = picture_recipe.make(parent=cls.sub_album, _quantity=5)
|
||||||
|
|
||||||
|
def test_delete(self):
|
||||||
|
album_ids = [self.album.id, self.sub_album.id]
|
||||||
|
picture_ids = [
|
||||||
|
*[p.id for p in self.album_pictures],
|
||||||
|
*[p.id for p in self.sub_album_pictures],
|
||||||
|
]
|
||||||
|
self.album.delete()
|
||||||
|
# assert not p.exists()
|
||||||
|
assert not Album.objects.filter(id__in=album_ids).exists()
|
||||||
|
assert not Picture.objects.filter(id__in=picture_ids).exists()
|
||||||
|
|||||||
@@ -136,9 +136,7 @@ class TestAlbumUpload:
|
|||||||
class TestSasModeration(TestCase):
|
class TestSasModeration(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
album = baker.make(
|
album = baker.make(Album)
|
||||||
Album, parent_id=settings.SITH_SAS_ROOT_DIR_ID, is_moderated=True
|
|
||||||
)
|
|
||||||
cls.pictures = picture_recipe.make(
|
cls.pictures = picture_recipe.make(
|
||||||
parent=album, _quantity=10, _bulk_create=True
|
parent=album, _quantity=10, _bulk_create=True
|
||||||
)
|
)
|
||||||
|
|||||||
87
sas/views.py
87
sas/views.py
@@ -12,6 +12,7 @@
|
|||||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@@ -22,12 +23,12 @@ from django.shortcuts import get_object_or_404
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.safestring import SafeString
|
from django.utils.safestring import SafeString
|
||||||
from django.views.generic import CreateView, DetailView, TemplateView
|
from django.views.generic import CreateView, DetailView, TemplateView
|
||||||
from django.views.generic.edit import FormView, UpdateView
|
from django.views.generic.edit import FormMixin, FormView, UpdateView
|
||||||
|
|
||||||
from core.auth.mixins import CanEditMixin, CanViewMixin
|
from core.auth.mixins import CanEditMixin, CanViewMixin
|
||||||
from core.models import SithFile, User
|
from core.models import SithFile, User
|
||||||
from core.views import UseFragmentsMixin
|
from core.views import FileView, UseFragmentsMixin
|
||||||
from core.views.files import FileView, send_file
|
from core.views.files import send_raw_file
|
||||||
from core.views.mixins import FragmentMixin, FragmentRenderer
|
from core.views.mixins import FragmentMixin, FragmentRenderer
|
||||||
from core.views.user import UserTabsMixin
|
from core.views.user import UserTabsMixin
|
||||||
from sas.forms import (
|
from sas.forms import (
|
||||||
@@ -63,6 +64,7 @@ class AlbumCreateFragment(FragmentMixin, CreateView):
|
|||||||
|
|
||||||
|
|
||||||
class SASMainView(UseFragmentsMixin, TemplateView):
|
class SASMainView(UseFragmentsMixin, TemplateView):
|
||||||
|
form_class = AlbumCreateForm
|
||||||
template_name = "sas/main.jinja"
|
template_name = "sas/main.jinja"
|
||||||
|
|
||||||
def get_fragments(self) -> dict[str, FragmentRenderer]:
|
def get_fragments(self) -> dict[str, FragmentRenderer]:
|
||||||
@@ -79,12 +81,26 @@ class SASMainView(UseFragmentsMixin, TemplateView):
|
|||||||
root_user = User.objects.get(pk=settings.SITH_ROOT_USER_ID)
|
root_user = User.objects.get(pk=settings.SITH_ROOT_USER_ID)
|
||||||
return {"album_create_fragment": {"owner": root_user}}
|
return {"album_create_fragment": {"owner": root_user}}
|
||||||
|
|
||||||
|
def dispatch(self, request, *args, **kwargs):
|
||||||
|
if request.method == "POST" and not self.request.user.has_perm("sas.add_album"):
|
||||||
|
raise PermissionDenied
|
||||||
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_form(self, form_class=None):
|
||||||
|
if not self.request.user.has_perm("sas.add_album"):
|
||||||
|
return None
|
||||||
|
return super().get_form(form_class)
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
return super().get_form_kwargs() | {
|
||||||
|
"owner": User.objects.get(pk=settings.SITH_ROOT_USER_ID),
|
||||||
|
"parent": None,
|
||||||
|
}
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super().get_context_data(**kwargs)
|
kwargs = super().get_context_data(**kwargs)
|
||||||
albums_qs = Album.objects.viewable_by(self.request.user)
|
albums_qs = Album.objects.viewable_by(self.request.user)
|
||||||
kwargs["categories"] = list(
|
kwargs["categories"] = list(albums_qs.filter(parent=None).order_by("id"))
|
||||||
albums_qs.filter(parent_id=settings.SITH_SAS_ROOT_DIR_ID).order_by("id")
|
|
||||||
)
|
|
||||||
kwargs["latest"] = list(albums_qs.order_by("-id")[:5])
|
kwargs["latest"] = list(albums_qs.order_by("-id")[:5])
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
@@ -94,6 +110,9 @@ class PictureView(CanViewMixin, DetailView):
|
|||||||
pk_url_kwarg = "picture_id"
|
pk_url_kwarg = "picture_id"
|
||||||
template_name = "sas/picture.jinja"
|
template_name = "sas/picture.jinja"
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
return super().get_queryset().select_related("parent")
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
self.object = self.get_object()
|
||||||
if "rotate_right" in request.GET:
|
if "rotate_right" in request.GET:
|
||||||
@@ -103,31 +122,42 @@ class PictureView(CanViewMixin, DetailView):
|
|||||||
return super().get(request, *args, **kwargs)
|
return super().get(request, *args, **kwargs)
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
return super().get_context_data(**kwargs) | {
|
return super().get_context_data(**kwargs) | {"album": self.object.parent}
|
||||||
"album": Album.objects.get(children=self.object)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def send_album(request, album_id):
|
def send_album(request, album_id):
|
||||||
return send_file(request, album_id, Album)
|
album = get_object_or_404(Album, id=album_id)
|
||||||
|
if not album.can_be_viewed_by(request.user):
|
||||||
|
raise PermissionDenied
|
||||||
|
return send_raw_file(Path(album.thumbnail.path))
|
||||||
|
|
||||||
|
|
||||||
def send_pict(request, picture_id):
|
def send_pict(request, picture_id):
|
||||||
return send_file(request, picture_id, Picture)
|
picture = get_object_or_404(Picture, id=picture_id)
|
||||||
|
if not picture.can_be_viewed_by(request.user):
|
||||||
|
raise PermissionDenied
|
||||||
|
return send_raw_file(Path(picture.original.path))
|
||||||
|
|
||||||
|
|
||||||
def send_compressed(request, picture_id):
|
def send_compressed(request, picture_id):
|
||||||
return send_file(request, picture_id, Picture, "compressed")
|
picture = get_object_or_404(Picture, id=picture_id)
|
||||||
|
if not picture.can_be_viewed_by(request.user):
|
||||||
|
raise PermissionDenied
|
||||||
|
return send_raw_file(Path(picture.compressed.path))
|
||||||
|
|
||||||
|
|
||||||
def send_thumb(request, picture_id):
|
def send_thumb(request, picture_id):
|
||||||
return send_file(request, picture_id, Picture, "thumbnail")
|
picture = get_object_or_404(Picture, id=picture_id)
|
||||||
|
if not picture.can_be_viewed_by(request.user):
|
||||||
|
raise PermissionDenied
|
||||||
|
return send_raw_file(Path(picture.thumbnail.path))
|
||||||
|
|
||||||
|
|
||||||
class AlbumView(CanViewMixin, UseFragmentsMixin, DetailView):
|
class AlbumView(CanViewMixin, UseFragmentsMixin, FormMixin, DetailView):
|
||||||
model = Album
|
model = Album
|
||||||
pk_url_kwarg = "album_id"
|
pk_url_kwarg = "album_id"
|
||||||
template_name = "sas/album.jinja"
|
template_name = "sas/album.jinja"
|
||||||
|
form_class = PictureUploadForm
|
||||||
|
|
||||||
def get_fragments(self) -> dict[str, FragmentRenderer]:
|
def get_fragments(self) -> dict[str, FragmentRenderer]:
|
||||||
return {
|
return {
|
||||||
@@ -142,27 +172,32 @@ class AlbumView(CanViewMixin, UseFragmentsMixin, DetailView):
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise Http404 from e
|
raise Http404 from e
|
||||||
if "clipboard" not in request.session:
|
if "clipboard" not in request.session:
|
||||||
request.session["clipboard"] = []
|
request.session["clipboard"] = {"albums": [], "pictures": []}
|
||||||
return super().dispatch(request, *args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_form(self, *args, **kwargs):
|
||||||
|
if not self.request.user.can_edit(self.object):
|
||||||
|
return None
|
||||||
|
return super().get_form(*args, **kwargs)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
self.object = self.get_object()
|
self.object = self.get_object()
|
||||||
if not self.object.file:
|
form = self.get_form()
|
||||||
self.object.generate_thumbnail()
|
if not form:
|
||||||
if request.user.can_edit(self.object): # Handle the copy-paste functions
|
# the form is reserved for users that can edit this album.
|
||||||
FileView.handle_clipboard(request, self.object)
|
# If there is no form, it means the user has no right to do a POST
|
||||||
return HttpResponseRedirect(self.request.path)
|
raise PermissionDenied
|
||||||
|
FileView.handle_clipboard(self.request, self.object)
|
||||||
|
if not form.is_valid():
|
||||||
|
return self.form_invalid(form)
|
||||||
|
return self.form_valid(form)
|
||||||
|
|
||||||
def get_fragment_data(self) -> dict[str, dict[str, Any]]:
|
def get_fragment_data(self) -> dict[str, dict[str, Any]]:
|
||||||
return {"album_create_fragment": {"owner": self.request.user}}
|
return {"album_create_fragment": {"owner": self.request.user}}
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super().get_context_data(**kwargs)
|
kwargs = super().get_context_data(**kwargs)
|
||||||
if ids := self.request.session.get("clipboard", None):
|
kwargs["clipboard"] = {}
|
||||||
kwargs["clipboard"] = SithFile.objects.filter(id__in=ids)
|
|
||||||
kwargs["upload_form"] = PictureUploadForm()
|
|
||||||
# if True, the albums will be fetched with a request to the API
|
|
||||||
# if False, the section won't be displayed at all
|
|
||||||
kwargs["show_albums"] = (
|
kwargs["show_albums"] = (
|
||||||
Album.objects.viewable_by(self.request.user)
|
Album.objects.viewable_by(self.request.user)
|
||||||
.filter(parent_id=self.object.id)
|
.filter(parent_id=self.object.id)
|
||||||
@@ -215,7 +250,7 @@ class ModerationView(TemplateView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
kwargs = super().get_context_data(**kwargs)
|
kwargs = super().get_context_data(**kwargs)
|
||||||
kwargs["albums_to_moderate"] = Album.objects.filter(
|
kwargs["albums_to_moderate"] = Album.objects.filter(
|
||||||
is_moderated=False, is_in_sas=True, is_folder=True
|
is_moderated=False
|
||||||
).order_by("id")
|
).order_by("id")
|
||||||
pictures = Picture.objects.filter(is_moderated=False).select_related("parent")
|
pictures = Picture.objects.filter(is_moderated=False).select_related("parent")
|
||||||
kwargs["pictures"] = pictures
|
kwargs["pictures"] = pictures
|
||||||
|
|||||||
@@ -439,7 +439,7 @@ SITH_SUBSCRIPTION_LOCATIONS = [
|
|||||||
|
|
||||||
SITH_COUNTER_BARS = [(1, "MDE"), (2, "Foyer"), (35, "La Gommette")]
|
SITH_COUNTER_BARS = [(1, "MDE"), (2, "Foyer"), (35, "La Gommette")]
|
||||||
|
|
||||||
SITH_PEDAGOGY_UE_TYPE = [
|
SITH_PEDAGOGY_UV_TYPE = [
|
||||||
("FREE", _("Free")),
|
("FREE", _("Free")),
|
||||||
("CS", _("CS")),
|
("CS", _("CS")),
|
||||||
("TM", _("TM")),
|
("TM", _("TM")),
|
||||||
@@ -451,21 +451,21 @@ SITH_PEDAGOGY_UE_TYPE = [
|
|||||||
("EXT", _("EXT")),
|
("EXT", _("EXT")),
|
||||||
]
|
]
|
||||||
|
|
||||||
SITH_PEDAGOGY_UE_SEMESTER = [
|
SITH_PEDAGOGY_UV_SEMESTER = [
|
||||||
("CLOSED", _("Closed")),
|
("CLOSED", _("Closed")),
|
||||||
("AUTUMN", _("Autumn")),
|
("AUTUMN", _("Autumn")),
|
||||||
("SPRING", _("Spring")),
|
("SPRING", _("Spring")),
|
||||||
("AUTUMN_AND_SPRING", _("Autumn and spring")),
|
("AUTUMN_AND_SPRING", _("Autumn and spring")),
|
||||||
]
|
]
|
||||||
|
|
||||||
SITH_PEDAGOGY_UE_LANGUAGE = [
|
SITH_PEDAGOGY_UV_LANGUAGE = [
|
||||||
("FR", _("French")),
|
("FR", _("French")),
|
||||||
("EN", _("English")),
|
("EN", _("English")),
|
||||||
("DE", _("German")),
|
("DE", _("German")),
|
||||||
("SP", _("Spanish")),
|
("SP", _("Spanish")),
|
||||||
]
|
]
|
||||||
|
|
||||||
SITH_PEDAGOGY_UE_RESULT_GRADE = [
|
SITH_PEDAGOGY_UV_RESULT_GRADE = [
|
||||||
("A", _("A")),
|
("A", _("A")),
|
||||||
("B", _("B")),
|
("B", _("B")),
|
||||||
("C", _("C")),
|
("C", _("C")),
|
||||||
|
|||||||
@@ -182,13 +182,12 @@ class OpenApi:
|
|||||||
path[action]["operationId"] = "_".join(
|
path[action]["operationId"] = "_".join(
|
||||||
desc["operationId"].split("_")[:-1]
|
desc["operationId"].split("_")[:-1]
|
||||||
)
|
)
|
||||||
|
|
||||||
schema = str(schema)
|
schema = str(schema)
|
||||||
|
|
||||||
if old_hash == sha1(schema.encode("utf-8")).hexdigest():
|
if old_hash == sha1(schema.encode("utf-8")).hexdigest():
|
||||||
logging.getLogger("django").info("✨ Api did not change, nothing to do ✨")
|
logging.getLogger("django").info("✨ Api did not change, nothing to do ✨")
|
||||||
return
|
return
|
||||||
|
|
||||||
with open(out, "w") as f:
|
out.write_text(schema)
|
||||||
_ = f.write(schema)
|
|
||||||
|
|
||||||
return subprocess.Popen(["npm", "run", "openapi"])
|
return subprocess.Popen(["npm", "run", "openapi"])
|
||||||
|
|||||||
Reference in New Issue
Block a user