Skip to content

Commit

Permalink
Allow message download as .eml files in the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Mar 3, 2021
1 parent 1e00492 commit 8ab3f4e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
21 changes: 21 additions & 0 deletions froide/foirequest/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib import admin
from django.db import models
from django.shortcuts import redirect
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import PermissionDenied
from django.urls import reverse, reverse_lazy
Expand Down Expand Up @@ -346,6 +347,9 @@ def get_urls(self):
path('<int:pk>/resend-message/',
self.admin_site.admin_view(self.resend_message),
name='foirequest-foimessage-resend_message'),
path('<int:pk>/download-eml/',
self.admin_site.admin_view(self.download_eml),
name='foirequest-foimessage-download_eml'),
]
return my_urls + urls

Expand Down Expand Up @@ -406,6 +410,23 @@ def resend_message(self, request, pk):
self.message_user(request, _('Message was send again.'))
return redirect('admin:foirequest_foimessage_change', message.id)

def download_eml(self, request, pk):
if not self.has_change_permission(request):
raise PermissionDenied

message = FoiMessage.objects.get(pk=pk, kind='email')

response = HttpResponse(
message.as_mime_message().as_bytes(),
content_type='application/octet-stream'
)
response['Content-Disposition'] = (
'attachment; filename="message-{}.eml"'.format(
message.id
)
)
return response

def resend_messages(self, request, queryset):
if not request.method == 'POST':
raise PermissionDenied
Expand Down
45 changes: 43 additions & 2 deletions froide/foirequest/models/message.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import calendar
from email.utils import formatdate

from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.html import format_html
from django.utils.functional import cached_property
from django.core.mail import EmailMessage, EmailMultiAlternatives

from taggit.managers import TaggableManager
from taggit.models import TagBase, TaggedItemBase
Expand Down Expand Up @@ -380,8 +384,10 @@ def attachments(self):
self._attachments = list(self.foiattachment_set.all().order_by('id'))
return self._attachments

def get_mime_attachments(self):
return [(a.name, a.get_bytes(), a.filetype) for a in self.attachments]
def get_mime_attachments(self, attachments=None):
if attachments is None:
attachments = self.attachments
return [(a.name, a.get_bytes(), a.filetype) for a in attachments]

def get_original_attachments(self):
return [a for a in self.attachments if not a.is_redacted and not a.is_converted]
Expand Down Expand Up @@ -431,6 +437,41 @@ def get_public_body_recipient_form(self):
from ..forms import get_message_recipient_form
return get_message_recipient_form(foimessage=self)

def as_mime_message(self):
klass = EmailMessage
if self.html:
klass = EmailMultiAlternatives

headers = {
'Date': formatdate(
int(calendar.timegm(self.timestamp.timetuple()))
),
'Message-ID': self.email_message_id,
'X-Froide-Hint': 'replica',
'X-Froide-Message-Id': self.get_absolute_domain_short_url(),
}

if not self.is_response:
headers.update({
'Reply-To': self.sender_email
})

email = klass(
self.subject,
self.plaintext,
self.sender_email,
to=[self.recipient_email],
headers=headers,
)
if self.html:
email.attach_alternative(self.html, "text/html")

atts = self.get_original_attachments()
mime_atts = self.get_mime_attachments(attachments=atts)
for mime_data in mime_atts:
email.attach(*mime_data)
return email.message()

def has_delivery_status(self):
if not self.sent or self.is_response:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
<button type="submit" class="btn btn-secondary">{% blocktrans %}Resend message{% endblocktrans %}</button>
</form></li>
{% endif %}
{% if original and original.is_email %}
<li>
<a href="{% url 'admin:foirequest-foimessage-download_eml' pk=original.pk %}">
{% translate "Download as .eml" %}
</a>
</li>
{% endif %}
{{ block.super }}
{% endblock %}

0 comments on commit 8ab3f4e

Please sign in to comment.