# Part of Open eObs. See LICENSE file for full copyright and licensing details.
import base64
import logging
from openerp import api
from openerp.addons.nh_odoo_fixes import validate
from openerp.exceptions import AccessError
from openerp.osv import osv, fields
_logger = logging.getLogger(__name__)
[docs]class print_observation_report_wizard(osv.TransientModel):
"""
The model is used to capture options selected by the user for a report and
then to delegate to 'nh.clinical.observation_report' to actually generate
it.
The user can select a datetime range
that they are interested in using the 'Start Time' and 'End Time' fields,
only patient data created within that time frame will be shown on the
generated report.
"""
_name = 'nh.clinical.observation_report_wizard'
_columns = {
'start_time': fields.datetime('Start Time'),
'end_time': fields.datetime('End Time'),
}
@api.constrains('start_time', 'end_time')
def _not_in_the_future(self):
"""
It doesn't make sense for the start or end time of a report to be in
the future, a report can only show existing data.
:return: No return, just side-effects.
"""
validate.not_in_the_future_multiple_args(self.start_time,
self.end_time)
@api.constrains('start_time', 'end_time')
def _start_time_not_after_end_time(self):
if self.start_time and self.end_time:
validate.start_datetime_not_after_end_datetime(
self.start_time, self.end_time
)
[docs] def get_filename(self, patient_identifier):
"""
Get the date the report was printed and combine that with the supplied
patient identifier to create the filename for the report
:param patient_identifier: string representing the Patient's identifier
- Most likely to be the NHS Number
:return: filename for the report
:rtype: str
"""
datetime_utils_pool = self.env['datetime_utils']
return '{patient_identifier}_{date}_observation_report.pdf'.format(
patient_identifier=patient_identifier,
date=datetime_utils_pool.get_localised_time(
return_string=True,
return_string_format='%Y%m%d'
)
)
[docs] @api.multi
def print_report(self):
data = self
data.spell_id = self.env.context['active_id']
spell_model = self.env['nh.clinical.spell']
# get PDF version of the report
report_model = self.env['report']
report_pdf = report_model.get_pdf(
self, 'nh.clinical.observation_report', data=data)
attachment_id = None
# save it as an attachment in the Database
# Use the spell ID to find the patient's NHS number
spell = spell_model.browse(data.spell_id)
patient = spell.patient_id
patient_nhs = patient.patient_identifier
attachment = {
'name': 'nh.clinical.observation_report',
'datas': base64.encodestring(report_pdf),
'datas_fname': self.get_filename(patient_nhs),
'res_model': 'nh.clinical.observation_report_wizard',
'res_id': self.id,
}
try:
attachment_id = self.env['ir.attachment'].create(attachment).id
except AccessError:
_logger.warning(
'Cannot save PDF report %r as attachment', attachment['name'])
else:
_logger.info(
'The PDF document %s is now saved in the database',
attachment['name'])
return {
'type': 'ir_actions_action_nh_clinical_download_report',
'id': attachment_id,
}