mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 12:29:24 +00:00
WIP
This commit is contained in:
@ -691,9 +691,7 @@ class Command(BaseCommand):
|
||||
# SAS
|
||||
for f in self.SAS_FIXTURE_PATH.glob("*"):
|
||||
if f.is_dir():
|
||||
album = Album(name=f.name)
|
||||
album.clean()
|
||||
album.save()
|
||||
album = Album.objects.create(name=f.name, is_moderated=True)
|
||||
for p in f.iterdir():
|
||||
file = resize_image(Image.open(p), 1000, "WEBP")
|
||||
pict = Picture(
|
||||
@ -707,6 +705,7 @@ class Command(BaseCommand):
|
||||
pict.generate_thumbnails()
|
||||
pict.full_clean()
|
||||
pict.save()
|
||||
album.generate_thumbnail()
|
||||
|
||||
img_skia = Picture.objects.get(name="skia.jpg")
|
||||
img_sli = Picture.objects.get(name="sli.jpg")
|
||||
|
@ -896,8 +896,6 @@ class SithFile(models.Model):
|
||||
super().clean()
|
||||
if "/" in self.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 (
|
||||
self.parent is not None and self in self.get_parent_list()
|
||||
):
|
||||
|
@ -5,6 +5,7 @@ from typing import Callable
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
|
||||
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.models import Group, QuickUploadImage, SithFile, User
|
||||
from core.utils import RED_PIXEL_PNG
|
||||
from sas.baker_recipes import picture_recipe
|
||||
from sas.models import Picture
|
||||
from sith import settings
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@ -30,24 +31,19 @@ class TestImageAccess:
|
||||
lambda: baker.make(
|
||||
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]):
|
||||
"""Test that only authorized users can access the sas image."""
|
||||
user = user_factory()
|
||||
picture: SithFile = baker.make(
|
||||
Picture, parent=SithFile.objects.get(pk=settings.SITH_SAS_ROOT_DIR_ID)
|
||||
)
|
||||
assert picture.is_owned_by(user)
|
||||
picture = picture_recipe.make()
|
||||
assert user.can_edit(picture)
|
||||
|
||||
def test_sas_image_access_owner(self):
|
||||
"""Test that the owner of the image can access it."""
|
||||
user = baker.make(User)
|
||||
picture: Picture = baker.make(Picture, owner=user)
|
||||
assert picture.is_owned_by(user)
|
||||
picture = picture_recipe.make(owner=user)
|
||||
assert user.can_edit(picture)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"user_factory",
|
||||
@ -63,7 +59,7 @@ class TestImageAccess:
|
||||
user = user_factory()
|
||||
owner = baker.make(User)
|
||||
picture: Picture = baker.make(Picture, owner=owner)
|
||||
assert not picture.is_owned_by(user)
|
||||
assert not user.can_edit(picture)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
|
@ -12,20 +12,23 @@
|
||||
# OR WITHIN THE LOCAL FILE "LICENSE"
|
||||
#
|
||||
#
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, timedelta
|
||||
|
||||
# Image utils
|
||||
from io import BytesIO
|
||||
from typing import Final, Unpack
|
||||
from typing import Any, Final, Unpack
|
||||
|
||||
import PIL
|
||||
from django.conf import settings
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.files.uploadedfile import UploadedFile
|
||||
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 PIL import ExifTags
|
||||
from PIL.Image import Image, Resampling
|
||||
@ -44,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:
|
||||
"""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.
|
||||
|
Reference in New Issue
Block a user