parse and save email attachments
This commit is contained in:
parent
f9a95317a2
commit
734af10525
8 changed files with 357 additions and 76 deletions
59
core/mail/migrations/0003_emailattachment.py
Normal file
59
core/mail/migrations/0003_emailattachment.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
# Generated by Django 4.2.7 on 2024-01-09 20:56
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import files.models
|
||||
from mail.models import Email
|
||||
from mail.protocol import parse_email_body
|
||||
|
||||
|
||||
class NullLogger:
|
||||
def info(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def warning(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def debug(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('mail', '0002_printed_quotable'),
|
||||
]
|
||||
|
||||
def generate_email_attachments(apps, schema_editor):
|
||||
for email in Email.objects.all():
|
||||
raw = email.raw
|
||||
if raw is None:
|
||||
continue
|
||||
parsed, body, attachments = parse_email_body(raw.encode('utf-8'), NullLogger())
|
||||
email.attachments.clear()
|
||||
for attachment in attachments:
|
||||
email.attachments.add(attachment)
|
||||
email.body = body
|
||||
email.save()
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='EmailAttachment',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(blank=True, null=True)),
|
||||
('updated_at', models.DateTimeField(blank=True, null=True)),
|
||||
('deleted_at', models.DateTimeField(blank=True, null=True)),
|
||||
('file', models.ImageField(upload_to=files.models.hash_upload)),
|
||||
('mime_type', models.CharField(max_length=255)),
|
||||
('hash', models.CharField(max_length=64, unique=True)),
|
||||
('name', models.CharField(max_length=255)),
|
||||
('email',
|
||||
models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='attachments',
|
||||
to='mail.email')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.RunPython(generate_email_attachments),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue