celery integration & basic markup task
This commit is contained in:
95
markup/tasks.py
Normal file
95
markup/tasks.py
Normal file
@ -0,0 +1,95 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
from celery import task, shared_task
|
||||
import sys
|
||||
import datetime
|
||||
import fileinput
|
||||
import smtplib
|
||||
#from mailbox import Maildir
|
||||
from email.feedparser import FeedParser
|
||||
from email.message import EmailMessage
|
||||
|
||||
#from demoapp.models import Widget
|
||||
#from celery import Celery
|
||||
#app = Celery('procat2', broker='amqp://localhost')
|
||||
|
||||
#@app.task
|
||||
|
||||
@shared_task
|
||||
def add(x, y):
|
||||
return x + y
|
||||
|
||||
body_ok = """Hi,
|
||||
|
||||
Attached is your marked up catalog. In the near future you'll also
|
||||
get a spreadsheet and a new catalog with just the marked up items.
|
||||
|
||||
Enjoy,
|
||||
ProCatalog Markup Bot"""
|
||||
|
||||
body_missing = """Hi,
|
||||
|
||||
I couldn't find a pdf attached to your message. I can't do much
|
||||
without a marked up catalog pdf, so please include that when you try
|
||||
again.
|
||||
|
||||
Thanks,
|
||||
ProCatalog Markup Bot"""
|
||||
|
||||
|
||||
# def log_message(msg):
|
||||
# with open('/var/log/markup_submit.log', 'a') as f:
|
||||
# f.write("{} - {} / {}\n".format(datetime.datetime.now().isoformat(),
|
||||
# msg['From'], msg['Subject']))
|
||||
|
||||
def reply(frm, subj, attach):
|
||||
print('ok: {} - {}'.format(frm, subj))
|
||||
# msg = EmailMessage()
|
||||
# msg.set_content(body_ok)
|
||||
# msg.add_attachment(attach.get_payload(decode=True),
|
||||
# maintype=attach.get_content_maintype(),
|
||||
# subtype=attach.get_content_subtype(),
|
||||
# filename=attach.get_filename())
|
||||
# send(frm, subj, msg)
|
||||
|
||||
|
||||
def reply_missing(frm, subj):
|
||||
print('missing: {} - {}'.format(frm, subj))
|
||||
# msg = EmailMessage()
|
||||
# msg.set_content(body_missing)
|
||||
# send(frm, subj, msg)
|
||||
|
||||
|
||||
def send(frm, subj, msg):
|
||||
msg['From'] = 'Keen ProCatalog Markup Bot <support@procatalog.io>'
|
||||
msg['To'] = frm
|
||||
msg['Bcc'] = 'alx-markup@procatalog.io'
|
||||
msg['Subject'] = 'Re: {}'.format(subj)
|
||||
|
||||
maildir = Maildir('/tmp/markup_submit_mail')
|
||||
maildir.add(msg)
|
||||
|
||||
with smtplib.SMTP('localhost') as s:
|
||||
s.starttls()
|
||||
s.login('remotevm', '5gW311IOs')
|
||||
s.send_message(msg)
|
||||
|
||||
|
||||
@shared_task
|
||||
def process_message(path):
|
||||
parser = FeedParser()
|
||||
with open(path) as f:
|
||||
for line in f:
|
||||
parser.feed(line)
|
||||
msg = parser.close()
|
||||
|
||||
#print('read message: {}'.format(msg))
|
||||
#store_message(msg)
|
||||
#log_message(msg)
|
||||
|
||||
if msg.is_multipart():
|
||||
for attach in msg.walk():
|
||||
if attach.get_content_type() == 'application/pdf':
|
||||
reply(msg['From'], msg['Subject'], attach)
|
||||
break
|
||||
else:
|
||||
reply_missing(msg['From'], msg['Subject'])
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user