markup: remove email submission endpoint
This commit is contained in:
@ -47,50 +47,6 @@ def on_fail_handler(self, exc, task_id, args, kwargs, einfo):
|
|||||||
# raise KeyError()
|
# raise KeyError()
|
||||||
|
|
||||||
|
|
||||||
@shared_task(on_failure=on_fail_handler)
|
|
||||||
def process_message(path):
|
|
||||||
parser = FeedParser()
|
|
||||||
with open(path) as f:
|
|
||||||
for line in f:
|
|
||||||
parser.feed(line)
|
|
||||||
msg = parser.close()
|
|
||||||
|
|
||||||
frm = str(make_header(decode_header(msg['From'])))
|
|
||||||
subject = str(make_header(decode_header(msg['Subject'])))
|
|
||||||
|
|
||||||
found_pdf = False
|
|
||||||
for attach in msg.walk():
|
|
||||||
if attach.get_content_type() == 'application/pdf':
|
|
||||||
process_attachment(frm, subject, attach)
|
|
||||||
found_pdf = True
|
|
||||||
|
|
||||||
if not found_pdf:
|
|
||||||
reply_missing(frm, subject)
|
|
||||||
|
|
||||||
|
|
||||||
def process_attachment(from_address, subject, attachment):
|
|
||||||
# write out pdf
|
|
||||||
pdf_name = attachment.get_filename()
|
|
||||||
pdf_name = str(make_header(decode_header(pdf_name)))
|
|
||||||
|
|
||||||
# if pdf name is in UUID format, use email subject
|
|
||||||
if re.match(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\.pdf', pdf_name):
|
|
||||||
pdf_name = f'{subject}.pdf'
|
|
||||||
|
|
||||||
print(f'Using pdf name: {pdf_name}')
|
|
||||||
|
|
||||||
pdf_base = Path(pdf_name).stem
|
|
||||||
workdir = os.path.join(WORKDIR, clean_path(from_address), pdf_base)
|
|
||||||
ensure_dir(workdir)
|
|
||||||
pdf_path = os.path.join(workdir, pdf_name)
|
|
||||||
print(f'saving pdf to {pdf_path}')
|
|
||||||
with open(pdf_path, 'wb') as att:
|
|
||||||
att.write(attachment.get_payload(decode=True))
|
|
||||||
set_file_perms(pdf_path)
|
|
||||||
|
|
||||||
process_pdf(pdf_path, from_address, subject, workdir)
|
|
||||||
|
|
||||||
|
|
||||||
@shared_task(on_failure=on_fail_handler)
|
@shared_task(on_failure=on_fail_handler)
|
||||||
def process_markup_pdf(pdf_path, username):
|
def process_markup_pdf(pdf_path, username):
|
||||||
if not Path(pdf_path).is_file():
|
if not Path(pdf_path).is_file():
|
||||||
|
|||||||
@ -1,10 +1,7 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from . import views
|
|
||||||
from .webdav import MarkupDavView
|
from .webdav import MarkupDavView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('submit', views.submit, name='markup_submit'),
|
|
||||||
#path('fail', views.fail, name='markup_fail'),
|
|
||||||
path('dav<path:path>', MarkupDavView.as_view()),
|
path('dav<path:path>', MarkupDavView.as_view()),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,51 +0,0 @@
|
|||||||
import os
|
|
||||||
import logging
|
|
||||||
import humanize
|
|
||||||
from tempfile import mkstemp
|
|
||||||
from shutil import copyfile
|
|
||||||
|
|
||||||
from django.core import serializers
|
|
||||||
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
|
|
||||||
from django.shortcuts import render, get_object_or_404
|
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
|
||||||
from django.views.decorators.http import require_http_methods
|
|
||||||
from django.core.files.uploadhandler import TemporaryFileUploadHandler
|
|
||||||
|
|
||||||
from .tasks import process_message
|
|
||||||
#from .tasks import test_fail
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @csrf_exempt
|
|
||||||
# def fail(request):
|
|
||||||
# test_fail.delay(1, 2)
|
|
||||||
# return JsonResponse({'success': True}, safe=False)
|
|
||||||
|
|
||||||
|
|
||||||
@csrf_exempt
|
|
||||||
@require_http_methods(["POST"])
|
|
||||||
def submit(request):
|
|
||||||
# always upload into a file
|
|
||||||
request.upload_handlers = [TemporaryFileUploadHandler(request)]
|
|
||||||
|
|
||||||
body = request.body
|
|
||||||
if not body or len(body) < 1:
|
|
||||||
return HttpResponse('Bad request: no data', status=400)
|
|
||||||
|
|
||||||
msg_file = request.FILES['file']
|
|
||||||
if not msg_file:
|
|
||||||
return HttpResponse('Bad request: no file', status=400)
|
|
||||||
|
|
||||||
msg_size = humanize.naturalsize(msg_file.size, gnu=True)
|
|
||||||
log.debug('message file size: {}'.format(msg_size))
|
|
||||||
|
|
||||||
_, tmpfile = mkstemp(suffix='.eml', prefix='markup_', dir=None, text=False)
|
|
||||||
log.debug('copy message file from {} to {}'.format(msg_file.temporary_file_path(), tmpfile))
|
|
||||||
copyfile(msg_file.temporary_file_path(), tmpfile)
|
|
||||||
os.chmod(tmpfile, 0o666)
|
|
||||||
|
|
||||||
process_message.delay(tmpfile)
|
|
||||||
|
|
||||||
return JsonResponse({'success': True}, safe=False)
|
|
||||||
Reference in New Issue
Block a user