-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(forum_files): setup app, model and tests
- Loading branch information
1 parent
e32996e
commit 22cc18b
Showing
8 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 4.2.3 on 2023-09-04 10:45 | ||
|
||
import django.db.models.deletion | ||
import storages.backends.s3boto3 | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="PublicFile", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("created", models.DateTimeField(auto_now_add=True, verbose_name="Creation date")), | ||
("updated", models.DateTimeField(auto_now=True, verbose_name="Update date")), | ||
( | ||
"file", | ||
models.ImageField( | ||
storage=storages.backends.s3boto3.S3Boto3Storage(bucket_name="set-bucket-name-public"), | ||
upload_to="", | ||
), | ||
), | ||
("keywords", models.CharField(max_length=255)), | ||
("user", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
"verbose_name": "Fichier public", | ||
"verbose_name_plural": "Fichiers publics", | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import ValidationError | ||
from django.db import models | ||
from machina.models.abstract_models import DatedModel | ||
from storages.backends.s3boto3 import S3Boto3Storage | ||
|
||
from lacommunaute.users.models import User | ||
|
||
|
||
public_bucket = settings.AWS_STORAGE_BUCKET_NAME_PUBLIC | ||
|
||
|
||
def validate_image_size(value): | ||
max_size = 1024 * 1024 * 8 | ||
|
||
if value.size > max_size: | ||
raise ValidationError("L'image ne doit pas dépasser 1 Mo") | ||
|
||
|
||
class PublicFile(DatedModel): | ||
# vincentporte : assumed this feature for superuser purpose will be break in dev environment | ||
# if Storages is not a S3 bucket | ||
file = models.ImageField( | ||
storage=S3Boto3Storage(bucket_name=public_bucket, file_overwrite=False, default_acl="public-read"), | ||
validators=[validate_image_size], | ||
) | ||
user = models.ForeignKey(User, on_delete=models.PROTECT) | ||
keywords = models.CharField(max_length=255) | ||
|
||
class Meta: | ||
verbose_name = "fichier public" | ||
verbose_name_plural = "fichiers publics" | ||
|
||
def get_file_url(self): | ||
return self.file.url.split("?")[0] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import faker | ||
import moto | ||
import pytest | ||
from django.conf import settings | ||
from django.db.models.fields.files import ImageField | ||
|
||
from lacommunaute.forum_file.models import PublicFile | ||
from lacommunaute.users.factories import UserFactory | ||
|
||
|
||
fake = faker.Faker() | ||
|
||
|
||
@pytest.fixture(name="s3_bucket") | ||
def s3_bucket_fixture(): | ||
with moto.mock_s3(): | ||
yield settings.AWS_STORAGE_BUCKET_NAME_PUBLIC | ||
|
||
|
||
@pytest.fixture(name="public_file") | ||
def public_file_fixture(s3_bucket): | ||
public_file = PublicFile.objects.create( | ||
file="test.jpg", | ||
user=UserFactory(), | ||
keywords=fake.words(3), | ||
) | ||
return public_file | ||
|
||
|
||
def test_get_file_url(db, public_file): | ||
expected_file_url = f"{settings.MEDIA_URL}{settings.AWS_STORAGE_BUCKET_NAME_PUBLIC}/{public_file.file.name}" | ||
assert public_file.get_file_url() == expected_file_url | ||
|
||
|
||
def test_size_validator(db, public_file): | ||
with pytest.raises(Exception): | ||
public_file.file.size = 1024 * 1024 * 8 + 1 | ||
public_file.save() | ||
|
||
|
||
def test_file_field_is_imagefield(db): | ||
assert isinstance(PublicFile._meta.get_field("file"), ImageField) |