From 1a1781480a8dfd6fc2884a39a9b080ef1cfa1650 Mon Sep 17 00:00:00 2001 From: "petr.prikryl" Date: Mon, 20 Feb 2023 22:54:08 +0100 Subject: [PATCH] optimization of AttachmentInline preventing iteration over all attachments in database --- post_office/admin.py | 12 +++------ post_office/migrations/0001_initial.py | 4 +-- .../migrations/0004_auto_20160607_0901.py | 4 +-- .../migrations/0008_attachment_headers.py | 2 +- post_office/migrations/0012_jsonfield.py | 27 +++++++++++++++++++ 5 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 post_office/migrations/0012_jsonfield.py diff --git a/post_office/admin.py b/post_office/admin.py index 4e1bb172..361438e8 100644 --- a/post_office/admin.py +++ b/post_office/admin.py @@ -17,7 +17,6 @@ from django.utils.translation import gettext_lazy as _ from .fields import CommaSeparatedEmailField -from .mail import send from .models import STATUS, Attachment, Email, EmailTemplate, Log from .sanitizer import clean_html @@ -34,6 +33,7 @@ class AttachmentInline(admin.StackedInline): model = Attachment.emails.through extra = 0 autocomplete_fields = ["attachment"] + parent_obj = None def get_formset(self, request, obj=None, **kwargs): self.parent_obj = obj @@ -48,13 +48,9 @@ def get_queryset(self, request): if self.parent_obj: queryset = queryset.filter(email=self.parent_obj) - inlined_attachments = [ - a.id - for a in queryset - if isinstance(a.attachment.headers, dict) - and a.attachment.headers.get("Content-Disposition", "").startswith("inline") - ] - return queryset.exclude(id__in=inlined_attachments) + return queryset.exclude( + **{"attachment__headers__Content-Disposition__startswith": "inline"} + ) class LogInline(admin.TabularInline): diff --git a/post_office/migrations/0001_initial.py b/post_office/migrations/0001_initial.py index 0f80ca5e..72d5b7af 100644 --- a/post_office/migrations/0001_initial.py +++ b/post_office/migrations/0001_initial.py @@ -38,8 +38,8 @@ class Migration(migrations.Migration): ('created', models.DateTimeField(auto_now_add=True, db_index=True)), ('last_updated', models.DateTimeField(auto_now=True, db_index=True)), ('scheduled_time', models.DateTimeField(db_index=True, null=True, blank=True)), - ('headers', models.JSONField(null=True, blank=True)), - ('context', models.JSONField(null=True, blank=True)), + ('headers', models.TextField(null=True, blank=True)), + ('context', models.TextField(null=True, blank=True)), ], options={ }, diff --git a/post_office/migrations/0004_auto_20160607_0901.py b/post_office/migrations/0004_auto_20160607_0901.py index 30b21590..1ce9b2f6 100644 --- a/post_office/migrations/0004_auto_20160607_0901.py +++ b/post_office/migrations/0004_auto_20160607_0901.py @@ -47,12 +47,12 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='email', name='context', - field=models.JSONField(blank=True, null=True, verbose_name='Context'), + field=models.TextField(blank=True, null=True, verbose_name='Context'), ), migrations.AlterField( model_name='email', name='headers', - field=models.JSONField(blank=True, null=True, verbose_name='Headers'), + field=models.TextField(blank=True, null=True, verbose_name='Headers'), ), migrations.AlterField( model_name='email', diff --git a/post_office/migrations/0008_attachment_headers.py b/post_office/migrations/0008_attachment_headers.py index 1b014123..30c4df48 100644 --- a/post_office/migrations/0008_attachment_headers.py +++ b/post_office/migrations/0008_attachment_headers.py @@ -12,6 +12,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='attachment', name='headers', - field=models.JSONField(blank=True, null=True, verbose_name='Headers'), + field=models.TextField(blank=True, null=True, verbose_name='Headers'), ), ] diff --git a/post_office/migrations/0012_jsonfield.py b/post_office/migrations/0012_jsonfield.py new file mode 100644 index 00000000..cc6cdff7 --- /dev/null +++ b/post_office/migrations/0012_jsonfield.py @@ -0,0 +1,27 @@ +# Generated by Django 3.2.15 on 2022-09-14 15:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("post_office", "0011_models_help_text"), + ] + + operations = [ + migrations.AlterField( + model_name="attachment", + name="headers", + field=models.JSONField(blank=True, null=True, verbose_name="Headers"), + ), + migrations.AlterField( + model_name="email", + name="context", + field=models.JSONField(blank=True, null=True, verbose_name="Context"), + ), + migrations.AlterField( + model_name="email", + name="headers", + field=models.JSONField(blank=True, null=True, verbose_name="Headers"), + ), + ]