feat: upload file in contact form
This commit is contained in:
+44
-2
@@ -1,11 +1,13 @@
|
||||
from django.contrib import admin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
from django.http import FileResponse, Http404, HttpResponseRedirect
|
||||
from django.urls import path, reverse
|
||||
from django.utils.html import format_html
|
||||
|
||||
from .models import (
|
||||
AboutSection,
|
||||
AboutSectionItem,
|
||||
ContactSubmission,
|
||||
ContactSubmissionAttachment,
|
||||
CustomPage,
|
||||
CustomPageSection,
|
||||
CustomPageSectionItem,
|
||||
@@ -83,6 +85,21 @@ class AboutSectionAdmin(admin.ModelAdmin):
|
||||
)
|
||||
|
||||
|
||||
class ContactSubmissionAttachmentInline(admin.TabularInline):
|
||||
model = ContactSubmissionAttachment
|
||||
extra = 0
|
||||
can_delete = False
|
||||
fields = ("original_filename", "attachment_link", "uploaded_at")
|
||||
readonly_fields = ("original_filename", "attachment_link", "uploaded_at")
|
||||
|
||||
@admin.display(description="File")
|
||||
def attachment_link(self, obj):
|
||||
if not obj.file:
|
||||
return "—"
|
||||
url = reverse("admin:pages_contactattachment_download", args=[obj.pk])
|
||||
return format_html('<a href="{}">Download</a>', url)
|
||||
|
||||
|
||||
@admin.register(ContactSubmission)
|
||||
class ContactSubmissionAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "title", "email", "submitted_at", "is_read")
|
||||
@@ -90,11 +107,36 @@ class ContactSubmissionAdmin(admin.ModelAdmin):
|
||||
search_fields = ("name", "title", "description", "email")
|
||||
list_editable = ("is_read",)
|
||||
readonly_fields = ("name", "title", "description", "email", "submitted_at")
|
||||
inlines = [ContactSubmissionAttachmentInline]
|
||||
fieldsets = (
|
||||
(None, {"fields": ("name", "title", "email", "description")}),
|
||||
("Meta", {"fields": ("submitted_at", "is_read")}),
|
||||
)
|
||||
|
||||
def get_urls(self):
|
||||
urls = super().get_urls()
|
||||
custom_urls = [
|
||||
path(
|
||||
"attachment/<int:attachment_id>/download/",
|
||||
self.admin_site.admin_view(self.download_attachment),
|
||||
name="pages_contactattachment_download",
|
||||
),
|
||||
]
|
||||
return custom_urls + urls
|
||||
|
||||
def download_attachment(self, request, attachment_id):
|
||||
try:
|
||||
attachment = ContactSubmissionAttachment.objects.get(pk=attachment_id)
|
||||
except ContactSubmissionAttachment.DoesNotExist as exc:
|
||||
raise Http404 from exc
|
||||
if not attachment.file:
|
||||
raise Http404
|
||||
return FileResponse(
|
||||
attachment.file.open("rb"),
|
||||
as_attachment=True,
|
||||
filename=attachment.original_filename,
|
||||
)
|
||||
|
||||
|
||||
@admin.register(FAQEntry)
|
||||
class FAQEntryAdmin(admin.ModelAdmin):
|
||||
|
||||
Reference in New Issue
Block a user