-
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): manage object throught admin panels only
- Loading branch information
1 parent
44a13ca
commit 37a84d0
Showing
2 changed files
with
79 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.contrib import admin | ||
from django.utils.html import format_html | ||
|
||
from lacommunaute.forum_file.models import PublicFile | ||
|
||
|
||
class PublicFileAdmin(admin.ModelAdmin): | ||
list_display = ("user", "created", "file_url", "keywords") | ||
list_filter = ("created",) | ||
raw_id_fields = ("user",) | ||
search_fields = ("user__email", "keywords", "file__name") | ||
fields = ("file", "keywords") | ||
|
||
def save_model(self, request, obj, form, change): | ||
if not obj.user_id: | ||
obj.user = request.user | ||
obj.save() | ||
|
||
def file_url(self, obj): | ||
return format_html(f'<a href="{obj.get_file_url()}" target="_blank">{obj.get_file_url()}</a>') | ||
|
||
file_url.short_description = "URL du fichier" | ||
|
||
|
||
admin.site.register(PublicFile, PublicFileAdmin) |
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,54 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
import boto3 | ||
import faker | ||
import pytest | ||
from django.conf import settings | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from django.urls import reverse | ||
from moto import mock_s3 | ||
from PIL import Image | ||
|
||
from lacommunaute.forum_file.models import PublicFile | ||
from lacommunaute.users.factories import UserFactory | ||
|
||
|
||
fake = faker.Faker() | ||
|
||
# https://docs.getmoto.org/en/latest/docs/services/s3.html | ||
# Note that this only works if the environment variable is set before the mock is initialized. | ||
os.environ["MOTO_S3_CUSTOM_ENDPOINTS"] = settings.AWS_S3_ENDPOINT_URL | ||
|
||
|
||
@pytest.fixture(name="superuser") | ||
def superuser_fixture(): | ||
return UserFactory(is_superuser=True, is_staff=True) | ||
|
||
|
||
@pytest.fixture(name="image_content") | ||
def image_content_fixture(): | ||
img = Image.new("RGB", (1, 1), color="white") | ||
buffer = BytesIO() | ||
img.save(buffer, format="JPEG") | ||
return buffer.getvalue() | ||
|
||
|
||
@mock_s3 | ||
def test_file_is_saved_with_logged_user(client, db, superuser, image_content): | ||
conn = boto3.client("s3", endpoint_url=settings.AWS_S3_ENDPOINT_URL, region_name="us-east-1") | ||
conn.create_bucket(Bucket=settings.AWS_STORAGE_BUCKET_NAME_PUBLIC) | ||
client.force_login(superuser) | ||
not_the_logged_in_user = UserFactory() | ||
|
||
response = client.post( | ||
reverse("admin:forum_file_publicfile_add"), | ||
{ | ||
"file": SimpleUploadedFile("test.jpg", image_content, content_type="image/jpeg"), | ||
"user": not_the_logged_in_user.id, | ||
"keywords": fake.words(3), | ||
}, | ||
) | ||
assert response.status_code == 302 | ||
file = PublicFile.objects.get() | ||
assert file.user == superuser |