Exemplo n.º 1
0
def mailagain(request):
    """
    Send an email again based on its history entry
    """
    history_entry = MailHistory.get(request.matchdict['id'])
    send_salary_sheet(
        request,
        history_entry.company,
        history_entry.filename,
        history_entry.filepath,
        True,
    )

    request.session.flash(u"Le mail a bien été envoyé")
    url = request.route_path('mailhistory')
    return HTTPFound(url)
Exemplo n.º 2
0
def mailagain(request):
    """
    Send an email again based on its history entry
    """
    history_entry = MailHistory.get(request.matchdict['id'])
    send_salary_sheet(
        request,
        history_entry.company,
        history_entry.filename,
        history_entry.filepath,
        True,
    )

    request.session.flash(u"Le mail a bien été envoyé")
    url = request.route_path('mailhistory')
    return HTTPFound(url)
Exemplo n.º 3
0
def async_mail_salarysheets(
    self, job_id, mails, force):
    """
    Asynchronously sent a bunch of emails with attached salarysheets

    :param int job_id: The id of the MailSendJob
    :param mails: a list of dict compound of
        {
            'id': company_id,
            'attachment': attachment filename,
            'attachment_path': attachment filepath,
            'message': The mail message,
            'subject': The mail subject,
            'company_id': The id of the company,
            'email': The email to send it to,
        }
    :param force: Should we force the mail sending
    """
    logger.info(u"We are launching an asynchronous mail sending operation")
    logger.info(u"  The job id : %s" % job_id)

    request = get_current_request()
    from autonomie.models.base import DBSESSION
    # Sleep a bit in case the db was slow
    time.sleep(10)

    # First testing if the job was created
    try:
        job = get_job(self.request, MailingJob, job_id)
    except NoResultFound:
        logger.exception(JOB_RETRIEVE_ERROR.format(job_id))
        return

    mail_count = 0
    error_count = 0
    error_messages = []
    for mail_datas in mails:
        # since we send a mail out of the transaction process, we need to commit
        # each mail_history instance to avoid sending and not storing the
        # history
        try:
            transaction.begin()
            company_id = mail_datas['company_id']
            email = mail_datas['email']

            if email is None:
                logger.error(u"no mail found for company {0}".format(
                    company_id)
                )
                continue
            else:
                message = mail_datas['message']
                subject = mail_datas['subject']
                logger.info(u"  The mail subject : %s" % subject)
                logger.info(u"  The mail message : %s" % message)

                mail_history = send_salary_sheet(
                    request,
                    email,
                    company_id,
                    mail_datas['attachment'],
                    mail_datas['attachment_path'],
                    force=force,
                    message=message,
                    subject=subject,
                )
                # Stores the history of this sent email
                DBSESSION().add(mail_history)

        except MailAlreadySent as e:
            error_count += 1
            msg = u"Ce fichier a déjà été envoyé {0}".format(
                mail_datas['attachment']
            )
            error_messages.append(msg)
            logger.exception(u"Mail already delivered")
            logger.error(u"* Part of the Task FAILED")
            continue

        except UndeliveredMail as e:
            error_count += 1
            msg = u"Impossible de délivrer de mail à l'entreprise {0} \
(mail : {1})".format(company_id, email)
            error_messages.append(msg)
            logger.exception(u"Unable to deliver an e-mail")
            logger.error(u"* Part of the Task FAILED")
            continue

        except Exception as e:
            error_count += 1
            transaction.abort()
            logger.exception(u"The transaction has been aborted")
            logger.error(u"* Part of the task FAILED !!!")
            error_messages.append(u"{0}".format(e))

        else:
            mail_count += 1
            transaction.commit()
            logger.info(u"The transaction has been commited")
            logger.info(u"* Part of the Task SUCCEEDED !!!")
            time.sleep(1)

    logger.info(u"-> Task finished")
    transaction.begin()
    job = get_job(self.request, MailingJob, job_id)
    logger.info(u"The job : %s" % job)
    job.jobid = self.request.id
    if error_count == 0:
        job.status = "completed"
    else:
        job.status = "failed"
    job.messages = [u"{0} mails ont été envoyés".format(mail_count)]
    job.messages.append(
        u"{0} mails n'ont pas pu être envoyés".format(error_count)
    )
    job.error_messages = error_messages
    DBSESSION().merge(job)
    logger.info(u"Committing the transaction")
    transaction.commit()
Exemplo n.º 4
0
def async_mail_salarysheets(
    self, job_id, mails, force):
    """
    Asynchronously sent a bunch of emails with attached salarysheets

    :param int job_id: The id of the MailSendJob
    :param mails: a list of dict compound of
        {
            'id': company_id,
            'attachment': attachment filename,
            'attachment_path': attachment filepath,
            'message': The mail message,
            'subject': The mail subject,
            'company_id': The id of the company,
            'email': The email to send it to,
        }
    :param force: Should we force the mail sending
    """
    logger.info(u"We are launching an asynchronous mail sending operation")
    logger.info(u"  The job id : %s" % job_id)

    request = get_current_request()
    from autonomie.models.base import DBSESSION
    # Sleep a bit in case the db was slow
    time.sleep(10)

    # First testing if the job was created
    try:
        job = get_job(self.request, MailingJob, job_id)
    except NoResultFound:
        logger.exception(JOB_RETRIEVE_ERROR.format(job_id))
        return

    mail_count = 0
    error_count = 0
    error_messages = []
    for mail_datas in mails:
        # since we send a mail out of the transaction process, we need to commit
        # each mail_history instance to avoid sending and not storing the
        # history
        try:
            transaction.begin()
            company_id = mail_datas['company_id']
            email = mail_datas['email']

            if email is None:
                logger.error(u"no mail found for company {0}".format(
                    company_id)
                )
                continue
            else:
                message = mail_datas['message']
                subject = mail_datas['subject']
                logger.info(u"  The mail subject : %s" % subject)
                logger.info(u"  The mail message : %s" % message)

                mail_history = send_salary_sheet(
                    request,
                    email,
                    company_id,
                    mail_datas['attachment'],
                    mail_datas['attachment_path'],
                    force=force,
                    message=message,
                    subject=subject,
                )
                # Stores the history of this sent email
                DBSESSION().add(mail_history)

        except MailAlreadySent as e:
            error_count += 1
            msg = u"Ce fichier a déjà été envoyé {0}".format(
                mail_datas['attachment']
            )
            error_messages.append(msg)
            logger.exception(u"Mail already delivered")
            logger.error(u"* Part of the Task FAILED")
            continue

        except UndeliveredMail as e:
            error_count += 1
            msg = u"Impossible de délivrer de mail à l'entreprise {0} \
(mail : {1})".format(company_id, email)
            error_messages.append(msg)
            logger.exception(u"Unable to deliver an e-mail")
            logger.error(u"* Part of the Task FAILED")
            continue

        except Exception as e:
            error_count += 1
            transaction.abort()
            logger.exception(u"The transaction has been aborted")
            logger.error(u"* Part of the task FAILED !!!")
            error_messages.append(u"{0}".format(e))

        else:
            mail_count += 1
            transaction.commit()
            logger.info(u"The transaction has been commited")
            logger.info(u"* Part of the Task SUCCEEDED !!!")
            time.sleep(1)

    logger.info(u"-> Task finished")
    transaction.begin()
    job = get_job(self.request, MailingJob, job_id)
    logger.info(u"The job : %s" % job)
    job.jobid = self.request.id
    if error_count == 0:
        job.status = "completed"
    else:
        job.status = "failed"
    job.messages = [u"{0} mails ont été envoyés".format(mail_count)]
    job.messages.append(
        u"{0} mails n'ont pas pu être envoyés".format(error_count)
    )
    job.error_messages = error_messages
    DBSESSION().merge(job)
    logger.info(u"Committing the transaction")
    transaction.commit()