Skip to content

Commit

Permalink
Merge pull request #10 from anexia/required-attachments
Browse files Browse the repository at this point in the history
Add required attachment inline admin
  • Loading branch information
anx-abruckner authored Nov 28, 2023
2 parents efecd91 + 63d650f commit 2bfcd16
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
13 changes: 10 additions & 3 deletions drf_attachments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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
from drf_attachments.models.models import Attachment

__all__ = [
"AttachmentInlineAdmin",
"RequiredAttachmentInlineAdmin",
]


Expand Down Expand Up @@ -45,6 +46,7 @@ class AttachmentAdmin(admin.ModelAdmin, AttachmentAdminMixin):
fields = (
"name",
"context",
"meta",
"content_type",
"object_id",
"content_object",
Expand Down Expand Up @@ -81,7 +83,7 @@ def get_urls(self):
path(
"<path:object_id>/download/",
self.admin_site.admin_view(self.download_view),
name="drf_attachments_attachment_download"
name="drf_attachments_attachment_download",
),
]
return custom_urls + urls
Expand All @@ -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
Expand Down Expand Up @@ -132,3 +135,7 @@ def has_add_permission(self, request, obj=None):
return False

show_change_link = False


class RequiredAttachmentInlineAdmin(AttachmentInlineAdmin):
min_num = 1
12 changes: 10 additions & 2 deletions tests/testapp/admin.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -34,3 +34,11 @@ class FileAdmin(admin.ModelAdmin):
inlines = [
AttachmentInlineAdmin,
]


@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
list_display = ("name",)
inlines = [
RequiredAttachmentInlineAdmin,
]
21 changes: 21 additions & 0 deletions tests/testapp/migrations/0002_profile.py
Original file line number Diff line number Diff line change
@@ -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),
),
],
),
]
14 changes: 14 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2bfcd16

Please sign in to comment.