celery integration & basic markup task

This commit is contained in:
2019-10-13 21:57:12 -07:00
parent 46f4080b93
commit 7803ae2fb1
6 changed files with 166 additions and 6 deletions

View File

@ -1,24 +1,42 @@
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.urls import reverse
#from django.utils.translation import gettext as _
from django.core.files.uploadhandler import TemporaryFileUploadHandler
#from lazysignup.decorators import allow_lazy_user
#from account.decorators import login_required
from .tasks import process_message
#from procat2.models import Catalog
log = logging.getLogger(__name__)
@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)
#data = json.loads(body.decode('utf-8'))
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)
process_message.delay(tmpfile)
return JsonResponse({'success': True}, safe=False)