Skip to content

Commit

Permalink
optimization of AttachmentInline
Browse files Browse the repository at this point in the history
preventing iteration over all attachments in database
  • Loading branch information
petr.prikryl committed Feb 20, 2023
1 parent 52b2900 commit 1a17814
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
12 changes: 4 additions & 8 deletions post_office/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions post_office/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
},
Expand Down
4 changes: 2 additions & 2 deletions post_office/migrations/0004_auto_20160607_0901.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion post_office/migrations/0008_attachment_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
]
27 changes: 27 additions & 0 deletions post_office/migrations/0012_jsonfield.py
Original file line number Diff line number Diff line change
@@ -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"),
),
]

0 comments on commit 1a17814

Please sign in to comment.