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 Oct 31, 2022
1 parent 5a1aa53 commit 4694d99
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 32 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ matrix:
include:
# Python 3.7
- python: 3.7
env: TOXENV=py37-django22,py37-django31,py37-django32,py37-django40
env: TOXENV=py37-django31,py37-django32,py37-django40

# Python 3.8
- python: 3.8
env: TOXENV=py38-django22,py38-django31,py38-django32,py38-django40
env: TOXENV=py38-django31,py38-django32,py38-django40

# Python 3.9
- python: 3.9
env: TOXENV=py39-django22,py39-django31,py39-django32,py39-django40
env: TOXENV=py39-django31,py39-django32,py39-django40

# Django Master
- python: 3.7
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ Django. Some awesome features are:

## Dependencies

- [django \>= 2.2](https://djangoproject.com/)
- [jsonfield](https://github.com/rpkilby/jsonfield)
- [django \>= 3.1](https://djangoproject.com/)
- [bleach](https://bleach.readthedocs.io/)

With this optional dependency, HTML emails are nicely rendered
Expand Down Expand Up @@ -550,7 +549,7 @@ POST_OFFICE = {
}
```

`CONTEXT_FIELD_CLASS` defaults to `jsonfield.JSONField`.
`CONTEXT_FIELD_CLASS` defaults to `django.db.models.JSONField`.

### Logging

Expand Down
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
5 changes: 2 additions & 3 deletions post_office/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import models, migrations
import jsonfield.fields
import post_office.fields
import post_office.validators
import post_office.models
Expand Down Expand Up @@ -38,8 +37,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', jsonfield.fields.JSONField(null=True, blank=True)),
('context', jsonfield.fields.JSONField(null=True, blank=True)),
('headers', models.TextField(null=True, blank=True)),
('context', models.TextField(null=True, blank=True)),
],
options={
},
Expand Down
5 changes: 2 additions & 3 deletions post_office/migrations/0004_auto_20160607_0901.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by Django 1.9.6 on 2016-06-07 07:01
from django.db import migrations, models
import django.db.models.deletion
import jsonfield.fields
import post_office.models


Expand Down Expand Up @@ -47,12 +46,12 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='email',
name='context',
field=jsonfield.fields.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=jsonfield.fields.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
5 changes: 2 additions & 3 deletions post_office/migrations/0008_attachment_headers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by Django 1.11.16 on 2018-11-30 08:54
from django.db import migrations
import jsonfield.fields
from django.db import models, migrations


class Migration(migrations.Migration):
Expand All @@ -13,6 +12,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='attachment',
name='headers',
field=jsonfield.fields.JSONField(blank=True, null=True, verbose_name='Headers'),
field=models.TextField(blank=True, null=True, verbose_name='Headers'),
),
]
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)
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def run_tests(self):
package_data={'': ['README.rst']},
install_requires=[
'bleach[css]',
'django>=2.2',
'jsonfield>=3.0',
'django>=3.1',
'pytz',
],
classifiers=[
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[tox]
envlist =
py{37,38,39}-django22
py{37,38,39}-django31
py{37,38,39}-django32
py{37,38,39}-django40
Expand All @@ -12,7 +11,6 @@ setenv =
DJANGO_SETTINGS_MODULE=post_office.test_settings

deps =
django22: Django<3
django31: Django<3.2
django32: Django<4.0
django40: Django<4.1
Expand Down

0 comments on commit 4694d99

Please sign in to comment.