From 63d650fc87babd4a861536103c07ad750c408c59 Mon Sep 17 00:00:00 2001 From: Armin Ster Date: Mon, 20 Nov 2023 16:38:48 +0100 Subject: [PATCH] Add required attachment inline admin --- drf_attachments/admin.py | 13 ++++++++++--- tests/testapp/admin.py | 12 ++++++++++-- tests/testapp/migrations/0002_profile.py | 21 +++++++++++++++++++++ tests/testapp/models.py | 14 ++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 tests/testapp/migrations/0002_profile.py diff --git a/drf_attachments/admin.py b/drf_attachments/admin.py index 2ddb67a..3e74cff 100644 --- a/drf_attachments/admin.py +++ b/drf_attachments/admin.py @@ -5,7 +5,7 @@ from django.contrib.contenttypes.admin import GenericTabularInline from django.forms import ChoiceField, ModelForm from django.http import StreamingHttpResponse -from django.urls import NoReverseMatch, reverse, path +from django.urls import NoReverseMatch, path, reverse from django.utils.safestring import mark_safe from drf_attachments.config import config @@ -13,6 +13,7 @@ __all__ = [ "AttachmentInlineAdmin", + "RequiredAttachmentInlineAdmin", ] @@ -45,6 +46,7 @@ class AttachmentAdmin(admin.ModelAdmin, AttachmentAdminMixin): fields = ( "name", "context", + "meta", "content_type", "object_id", "content_object", @@ -81,7 +83,7 @@ def get_urls(self): path( "/download/", self.admin_site.admin_view(self.download_view), - name="drf_attachments_attachment_download" + name="drf_attachments_attachment_download", ), ] return custom_urls + urls @@ -93,7 +95,8 @@ def download_view(self, request, object_id): content_type=attachment.get_mime_type(), ) response["Content-Disposition"] = rfc5987_content_disposition( - (attachment.name if attachment.name else str(attachment.pk)) + attachment.get_extension() + (attachment.name if attachment.name else str(attachment.pk)) + + attachment.get_extension() ) return response @@ -132,3 +135,7 @@ def has_add_permission(self, request, obj=None): return False show_change_link = False + + +class RequiredAttachmentInlineAdmin(AttachmentInlineAdmin): + min_num = 1 diff --git a/tests/testapp/admin.py b/tests/testapp/admin.py index 47b6dab..cd91c8d 100644 --- a/tests/testapp/admin.py +++ b/tests/testapp/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin -from testapp.models import Diagram, File, PhotoAlbum, Thumbnail +from testapp.models import Diagram, File, PhotoAlbum, Profile, Thumbnail -from drf_attachments.admin import AttachmentInlineAdmin +from drf_attachments.admin import AttachmentInlineAdmin, RequiredAttachmentInlineAdmin @admin.register(PhotoAlbum) @@ -34,3 +34,11 @@ class FileAdmin(admin.ModelAdmin): inlines = [ AttachmentInlineAdmin, ] + + +@admin.register(Profile) +class ProfileAdmin(admin.ModelAdmin): + list_display = ("name",) + inlines = [ + RequiredAttachmentInlineAdmin, + ] diff --git a/tests/testapp/migrations/0002_profile.py b/tests/testapp/migrations/0002_profile.py new file mode 100644 index 0000000..5bea268 --- /dev/null +++ b/tests/testapp/migrations/0002_profile.py @@ -0,0 +1,21 @@ +# Generated by Django 3.2.23 on 2023-11-20 12:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("testapp", "0001_initial"), + ] + + operations = [ + migrations.CreateModel( + name="Profile", + fields=[ + ( + "name", + models.CharField(max_length=50, primary_key=True, serialize=False), + ), + ], + ), + ] diff --git a/tests/testapp/models.py b/tests/testapp/models.py index f00f8c4..0dc88c9 100644 --- a/tests/testapp/models.py +++ b/tests/testapp/models.py @@ -56,3 +56,17 @@ class AttachmentMeta: unique_upload = True min_size = 1_000 # Bytes max_size = 10_000 # Bytes + + +class Profile(models.Model): + """ + User profile with one avatar image as attachment. + """ + + name = models.CharField(max_length=50, primary_key=True) + attachments = AttachmentRelation() + + class AttachmentMeta: + valid_mime_types = ["image/jpeg"] + valid_extensions = [".jpg", ".jpeg"] + unique_upload = True