from __future__ import absolute_import, unicode_literals from celery import task, shared_task from celery.utils.log import get_task_logger import datetime import fileinput import os import re import shutil import smtplib import sys from pathlib import Path from os.path import basename, dirname, isfile from email.feedparser import FeedParser from email.message import EmailMessage from email.header import decode_header, make_header import django from django.conf import settings os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'procat2.settings') django.setup() from django.contrib.auth.models import User from .utils import clean_path, ensure_dir, set_file_perms, WORKDIR from .email import reply, reply_missing, reply_no_matches, send_error_email from .matching import find_marked_products from .spreadsheet import write_spreadsheet from procat2.settings import TREE_NAME logger = get_task_logger(__name__) def on_fail_handler(self, exc, task_id, args, kwargs, einfo): """Send an email if a task throws an exception.""" print(str(einfo)) send_error_email(f'ERROR: {TREE_NAME} celery task {task_id}', str(einfo)) # @shared_task(on_failure=on_fail_handler) # def test_fail(x, y): # test_fail_internal() # def test_fail_internal(): # raise KeyError() @shared_task(on_failure=on_fail_handler) def process_markup_pdf(pdf_path, username): if not Path(pdf_path).is_file(): print(f'No pdf - exiting ({pdf_path})') return user = User.objects.get(username=username) pdf_stem = Path(pdf_path).stem workdir = os.path.join(WORKDIR, clean_path(user.username), clean_path(pdf_stem)) ensure_dir(workdir) pdf_name = Path(pdf_path).name dest_path = os.path.join(workdir, pdf_name) print(f'copying pdf from {pdf_path}') print(f'copying pdf to {dest_path}') shutil.copy(pdf_path, dest_path) set_file_perms(dest_path) frm = str(make_header(decode_header(f'{user.get_full_name()} <{user.email}>'))) subject = str(make_header(decode_header(pdf_stem))) # find matches matches = find_marked_products(dest_path, workdir, debug=0) if not matches: print('no product matches') # reply_no_matches(frm, subject) return print(f'{len(matches)} product matches') # write spreadsheet xls_path = write_spreadsheet(matches, workdir, pdf_stem) if not xls_path: # TODO send error print(f'error creating spreadsheet') return webdav_dir = Path(pdf_path).parent xls_name = Path(xls_path).name xls_webdav_path = os.path.join(webdav_dir, xls_name) print(f'wrote spreadsheet: {xls_path}') print(f'copying xls to {xls_webdav_path}') shutil.copy(xls_path, xls_webdav_path) set_file_perms(xls_webdav_path) reply(frm, subject, xls_webdav_path, pdf_path)