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 Sep 14, 2022
1 parent 59d320d commit d6efc9f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
13 changes: 5 additions & 8 deletions post_office/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,16 @@ def get_message_preview(instance):
class AttachmentInline(admin.StackedInline):
model = Attachment.emails.through
extra = 0
autocomplete_fields = ["attachment"]

def get_queryset(self, request):
"""
Exclude inlined attachments from queryset, because they usually have meaningless names and
are displayed anyway.
"""
queryset = super().get_queryset(request)
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 super().get_queryset(request).exclude(
**{"attachment__headers__Content-Disposition__startswith": "inline"}
)


class LogInline(admin.TabularInline):
Expand Down Expand Up @@ -314,6 +310,7 @@ def save_model(self, request, obj, form, change):
class AttachmentAdmin(admin.ModelAdmin):
list_display = ['name', 'file']
filter_horizontal = ['emails']
search_fields = ["name"]


admin.site.register(Email, EmailAdmin)
Expand Down
28 changes: 28 additions & 0 deletions post_office/migrations/0012_auto_20220914_1745.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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"),
),
]
5 changes: 2 additions & 3 deletions post_office/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from django.utils.encoding import smart_str
from django.utils.translation import pgettext_lazy, gettext_lazy as _
from django.utils import timezone
from jsonfield import JSONField

from post_office import cache
from post_office.fields import CommaSeparatedEmailField
Expand Down Expand Up @@ -68,7 +67,7 @@ class Email(models.Model):
help_text=_("Email won't be sent after this timestamp"))
message_id = models.CharField("Message-ID", null=True, max_length=255, editable=False)
number_of_retries = models.PositiveIntegerField(null=True, blank=True)
headers = JSONField(_('Headers'), blank=True, null=True)
headers = models.JSONField(_("Headers"), blank=True, null=True)
template = models.ForeignKey('post_office.EmailTemplate', blank=True,
null=True, verbose_name=_("Email template"),
on_delete=models.CASCADE)
Expand Down Expand Up @@ -316,7 +315,7 @@ class Attachment(models.Model):
emails = models.ManyToManyField(Email, related_name='attachments',
verbose_name=_('Emails'))
mimetype = models.CharField(max_length=255, default='', blank=True)
headers = JSONField(_('Headers'), blank=True, null=True)
headers = models.JSONField(_("Headers"), blank=True, null=True)

class Meta:
app_label = 'post_office'
Expand Down
5 changes: 3 additions & 2 deletions post_office/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def get_message_id_fqdn():
return get_config().get('MESSAGE_ID_FQDN', DNS_NAME)


CONTEXT_FIELD_CLASS = get_config().get('CONTEXT_FIELD_CLASS',
'jsonfield.JSONField')
CONTEXT_FIELD_CLASS = get_config().get(
"CONTEXT_FIELD_CLASS", "django.db.models.JSONField"
)
context_field_class = import_string(CONTEXT_FIELD_CLASS)

0 comments on commit d6efc9f

Please sign in to comment.