Skip to content

Commit

Permalink
feat(forum_files): setup app, model and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 5, 2023
1 parent e32996e commit 22cc18b
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"lacommunaute.event",
"lacommunaute.inclusion_connect",
"lacommunaute.pages",
"lacommunaute.forum_file",
]

INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + THIRD_PARTIES_APPS
Expand Down
2 changes: 1 addition & 1 deletion config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
EMAIL_USE_TLS = False

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = "/media/"
MEDIA_URL = f"{AWS_S3_ENDPOINT_URL}/" # noqa: F405

# STORAGE (django >= 4.2)
STORAGES = {
Expand Down
Empty file.
38 changes: 38 additions & 0 deletions lacommunaute/forum_file/migrations/0001_initial.py
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.
35 changes: 35 additions & 0 deletions lacommunaute/forum_file/models.py
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.
42 changes: 42 additions & 0 deletions lacommunaute/forum_file/tests/test_models.py
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)

0 comments on commit 22cc18b

Please sign in to comment.