Commit 0113b6cd authored by Enrico Zini's avatar Enrico Zini
Browse files

Added an option to download all signed statements in a mbox

parent d0706227
......@@ -117,6 +117,7 @@
{% else %}
(no mail archive yet)
{% endif %}
<a href="{% url 'process_download_statements' pk=process.pk %}">(signed statements mbox)</a>
</td>
</tr>
......
......@@ -25,4 +25,5 @@ urlpatterns = [
url(r'^(?P<pk>\d+)/mailbox/download$', views.MailArchive.as_view(), name="process_mailbox_download"), # TODO: test
url(r'^(?P<pk>\d+)/mailbox$', views.DisplayMailArchive.as_view(), name="process_mailbox_show"), # TODO: test
url(r'^(?P<pk>\d+)/update_keycheck$', views.UpdateKeycheck.as_view(), name="process_update_keycheck"), # TODO: test
url(r'^(?P<pk>\d+)/download_statements$', views.DownloadStatements.as_view(), name="process_download_statements"), # TODO: test
]
......@@ -420,3 +420,32 @@ class UpdateKeycheck(RequirementMixin, View):
key.update_key()
key.update_check_sigs()
return redirect(self.requirement.get_absolute_url())
class DownloadStatements(VisitProcessMixin, View):
def get(self, request, *args, **kw):
import mailbox
import email.utils
import tempfile
import time
with tempfile.NamedTemporaryFile(mode="wb+") as outfile:
mbox = mailbox.mbox(path=outfile.name, create=True)
for req in self.process.requirements.all():
for stm in req.statements.all():
msg = mailbox.Message()
msg["From"] = email.utils.formataddr((stm.uploaded_by.fullname, stm.uploaded_by.email))
msg["Subject"] = "Signed statement for " + req.get_type_display()
msg["Date"] = email.utils.formatdate(time.mktime(stm.uploaded_time.timetuple()))
msg.set_payload(stm.statement, "utf-8")
mbox.add(msg)
mbox.close()
outfile.seek(0)
data = outfile.read()
res = http.HttpResponse(data, content_type="text/plain")
res["Content-Disposition"] = "attachment; filename={}.mbox".format(self.person.lookup_key)
return res
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment