lims_email: log each mailing

This commit is contained in:
Adrián Bernardi 2020-11-10 11:44:06 -03:00
parent d71ed21627
commit c97323cca5
7 changed files with 111 additions and 10 deletions

View file

@ -13,6 +13,7 @@ def register():
configuration.Cron,
results_report.ResultsReportVersionDetail,
results_report.ResultsReport,
results_report.ResultsReportMailing,
results_report.SendResultsReportStart,
results_report.SendResultsReportSucceed,
results_report.SendResultsReportFailed,

View file

@ -14,6 +14,10 @@ msgctxt "field:lims.configuration,mail_ack_report_subject:"
msgid "Email subject of Acknowledgment of results report"
msgstr "Asunto del correo de Acuse de informe de resultados"
msgctxt "field:lims.results_report,mailings:"
msgid "Mailings"
msgstr "Envíos"
msgctxt "field:lims.results_report,sent:"
msgid "Sent"
msgstr "Enviado"
@ -22,6 +26,18 @@ msgctxt "field:lims.results_report,sent_date:"
msgid "Sent date"
msgstr "Fecha de enviado"
msgctxt "field:lims.results_report.mailing,addresses:"
msgid "Addresses"
msgstr "Direcciones"
msgctxt "field:lims.results_report.mailing,date:"
msgid "Date"
msgstr "Fecha"
msgctxt "field:lims.results_report.mailing,results_report:"
msgid "Results Report"
msgstr "Informe de resultados"
msgctxt "field:lims_email.send_results_report.failed,reports_not_ready:"
msgid "Reports not ready"
msgstr "Informes no listos"
@ -54,6 +70,10 @@ msgctxt "model:ir.ui.menu,name:menu_send_results_report"
msgid "Send Results Reports"
msgstr "Enviar Informes de resultados"
msgctxt "model:lims.results_report.mailing,name:"
msgid "Results Report Mailing"
msgstr "Envío de Informe de resultados"
msgctxt "model:lims_email.send_results_report.failed,name:"
msgid "Send Results Report"
msgstr "Enviar Informe de resultados"
@ -94,6 +114,10 @@ msgctxt "view:lims.configuration:"
msgid "Mail Acknowledgment Report"
msgstr "Email Acuse de Informe"
msgctxt "view:lims.results_report.mailing:"
msgid "Time"
msgstr "Hora"
msgctxt "view:lims.results_report:"
msgid "Time"
msgstr "Hora"

View file

@ -9,7 +9,7 @@ from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from PyPDF2 import PdfFileMerger
from trytond.model import ModelView, fields
from trytond.model import ModelSQL, ModelView, fields
from trytond.wizard import Wizard, StateView, StateTransition, Button
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
@ -51,6 +51,8 @@ class ResultsReport(metaclass=PoolMeta):
sent = fields.Boolean('Sent', readonly=True)
sent_date = fields.DateTime('Sent date', readonly=True)
mailings = fields.One2Many('lims.results_report.mailing',
'results_report', 'Mailings', readonly=True)
@classmethod
def _get_modified_fields(cls):
@ -200,6 +202,39 @@ class ResultsReport(metaclass=PoolMeta):
return filename
class ResultsReportMailing(ModelSQL, ModelView):
'Results Report Mailing'
__name__ = 'lims.results_report.mailing'
results_report = fields.Many2One('lims.results_report', 'Results Report',
required=True, ondelete='CASCADE', select=True)
date = fields.Function(fields.DateTime('Date'),
'get_date', searcher='search_date')
addresses = fields.Char('Addresses', readonly=True)
@classmethod
def __setup__(cls):
super().__setup__()
cls._order.insert(0, ('date', 'DESC'))
def get_date(self, name):
return self.create_date.replace(microsecond=0)
@classmethod
def search_date(cls, name, clause):
cursor = Transaction().connection.cursor()
operator_ = clause[1:2][0]
cursor.execute('SELECT id '
'FROM "' + cls._table + '" '
'WHERE create_date' + operator_ + ' %s',
clause[2:3])
return [('id', 'in', [x[0] for x in cursor.fetchall()])]
@classmethod
def order_date(cls, tables):
return cls.create_date.convert_order('create_date', tables, cls)
class ResultsReportAnnulation(metaclass=PoolMeta):
__name__ = 'lims.results_report_annulation'
@ -357,7 +392,7 @@ class SendResultsReport(Wizard):
reports_not_sent = []
for group in self.get_grouped_reports(active_ids).values():
group['reports_ready'] = []
group['to_addrs'] = []
group['to_addrs'] = {}
for report in group['reports']:
logger.info('Send Results Report: %s', report.number)
@ -406,7 +441,7 @@ class SendResultsReport(Wizard):
report.number)
if group['cie_fraction_type']:
group['to_addrs'].append(config.email_qa)
group['to_addrs'][config.email_qa] = 'QA'
else:
samples = ResultsSample.search([
('version_detail.report_version.results_report',
@ -419,15 +454,16 @@ class SendResultsReport(Wizard):
getattr(entry.invoice_party,
'block_reports_automatic_sending')):
continue
group['to_addrs'].extend([c.contact.email
for c in entry.report_contacts
if c.contact.report_contact])
for c in entry.report_contacts:
if c.contact.report_contact:
group['to_addrs'][c.contact.email] = (
c.contact.party_full_name)
if not group['reports_ready']:
continue
# Email sending
to_addrs = list(set(group['to_addrs']))
to_addrs = list(group['to_addrs'].keys())
if not to_addrs:
reports_not_sent.extend(
[r[0] for r in group['reports_ready']])
@ -458,9 +494,12 @@ class SendResultsReport(Wizard):
continue
logger.info('Send Results Report: Sent')
ResultsReport.write(
[r[0] for r in group['reports_ready']],
{'sent': True, 'sent_date': datetime.now()})
addresses = ', '.join(['"%s" <%s>' % (v, k)
for k, v in group['to_addrs'].items()])
ResultsReport.write([r[0] for r in group['reports_ready']], {
'sent': True, 'sent_date': datetime.now(),
'mailings': [('create', [{'addresses': addresses}])],
})
if reports_not_ready or reports_not_sent:
logger.warning('Send Results Report: FAILED')

View file

@ -15,6 +15,24 @@
<field name="inherit" ref="lims.lims_results_report_view_list"/>
<field name="name">results_report_list</field>
</record>
<record model="ir.ui.view" id="lims_results_report_view_form">
<field name="model">lims.results_report</field>
<field name="inherit" ref="lims.lims_results_report_view_form"/>
<field name="name">results_report_form</field>
</record>
<!-- Results Report Mailing -->
<record model="ir.ui.view" id="lims_results_report_mailing_view_list">
<field name="model">lims.results_report.mailing</field>
<field name="type">tree</field>
<field name="name">results_report_mailing_list</field>
</record>
<record model="ir.ui.view" id="lims_results_report_mailing_view_form">
<field name="model">lims.results_report.mailing</field>
<field name="type">form</field>
<field name="name">results_report_mailing_form</field>
</record>
<!-- Wizard Send Results Report -->

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<data>
<xpath expr="/form/field[@name='versions']" position="after">
<field name="mailings" colspan="4"/>
</xpath>
</data>

View file

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<form>
<label name="date"/>
<field name="date"/>
<label name="addresses"/>
<field name="addresses"/>
</form>

View file

@ -0,0 +1,6 @@
<?xml version="1.0"?>
<tree>
<field name="date" widget="date"/>
<field name="date" widget="time" string="Time"/>
<field name="addresses" expand="1"/>
</tree>