def _compute_is_py3o_native_format(self):
     format = Formats()
     for rec in self:
         if not rec.report_type == "py3o":
             continue
         filetype = rec.py3o_filetype
         rec.is_py3o_native_format = format.get_format(filetype).native
Exemplo n.º 2
0
 def _get_py3o_filetypes(self):
     formats = Formats()
     names = formats.get_known_format_names()
     selections = []
     for name in names:
         description = name
         if formats.get_format(name).native:
             description = description + " " + _("(Native)")
         selections.append((name, description))
     return selections
Exemplo n.º 3
0
 def test_no_native_format_without_fusion_server(self):
     report = self.env.ref("report_py3o.res_users_report_py3o")
     formats = Formats()
     is_native = formats.get_format(report.py3o_filetype).native
     self.assertTrue(is_native)
     new_format = None
     for name in formats.get_known_format_names():
         format = formats.get_format(name)
         if not format.native:
             new_format = name
             break
     self.assertTrue(new_format)
     with self.assertRaises(ValidationError) as e:
         report.py3o_filetype = new_format
     self.assertEqual(
         e.exception.name, "Can not use not native format in local fusion. "
         "Please specify a Fusion Server")
 def _check_py3o_server_id(self):
     for report in self:
         if report.report_type != "py3o":
             continue
         is_native = Formats().get_format(report.py3o_filetype).native
         if ((not is_native or not report.py3o_is_local_fusion)
                 and not report.py3o_server_id):
             pass
Exemplo n.º 5
0
    def _create_single_report(self, model_instance, data, save_in_attachment):
        """ This function to generate our py3o report
        """
        self.ensure_one()
        report_xml = self.ir_actions_report_xml_id
        filetype = report_xml.py3o_filetype

        result_fd, result_path = tempfile.mkstemp(
            suffix='.' + filetype, prefix='p3o.report.tmp.')
        tmpl_data = self.get_template(model_instance)

        in_stream = StringIO(tmpl_data)
        with closing(os.fdopen(result_fd, 'w+')) as out_stream:
            template = Template(in_stream, out_stream, escape_false=True)
            localcontext = self._get_parser_context(model_instance, data)
            is_native = Formats().get_format(filetype).native
            if report_xml.py3o_is_local_fusion:
                template.render(localcontext)
                out_stream.seek(0)
                tmpl_data = out_stream.read()
                datadict = {}
            else:
                expressions = template.get_all_user_python_expression()
                py_expression = template.convert_py3o_to_python_ast(
                    expressions)
                convertor = Py3oConvertor()
                data_struct = convertor(py_expression)
                datadict = data_struct.render(localcontext)

        # if not is_native or not report_xml.py3o_is_local_fusion:
        #     # Call py3o.server to render the template in the desired format
        #     files = {
        #         'tmpl_file': tmpl_data,
        #     }
        #     fields = {
        #         "targetformat": filetype,
        #         "datadict": json.dumps(datadict),
        #         "image_mapping": "{}",
        #         "escape_false": "on",
        #     }
        #     if report_xml.py3o_is_local_fusion:
        #         fields['skipfusion'] = '1'
        #     r = requests.post(
        #         report_xml.py3o_server_id.url, data=fields, files=files)
        #     if r.status_code != 200:
        #         # server says we have an issue... let's tell that to enduser
        #         raise UserError(
        #             _('Fusion server error %s') % r.text,
        #         )

        #     chunk_size = 1024
        #     with open(result_path, 'w+') as fd:
        #         for chunk in r.iter_content(chunk_size):
        #             fd.write(chunk)
        if len(model_instance) == 1:
            self._postprocess_report(
                result_path, model_instance.id, save_in_attachment)
        return result_path
Exemplo n.º 6
0
 def _check_py3o_server_id(self):
     if self.report_type != "py3o":
         return
     is_native = Formats().get_format(self.py3o_filetype).native
     if ((not is_native or not self.py3o_is_local_fusion)
             and not self.py3o_server_id):
         raise ValidationError(
             _("Can not use not native format in local fusion. "
               "Please specify a Fusion Server"))
Exemplo n.º 7
0
 def _is_valid_template_filename(self, filename):
     """ Check if the filename can be used as py3o template
     """
     if filename and os.path.isfile(filename):
         fname, ext = os.path.splitext(filename)
         ext = ext.replace(".", "")
         try:
             fformat = Formats().get_format(ext)
             if fformat and fformat.native:
                 return True
         except UnkownFormatException:
             logger.warning("Invalid py3o template %s", filename, exc_info=1)
     logger.warning("%s is not a valid Py3o template filename", filename)
     return False
Exemplo n.º 8
0
class TestFormats(unittest.TestCase):

    def tearDown(self):
        pass

    def setUp(self):
        self.formats = Formats()

    def test_valid_format(self):
        """test that a valid format can be found"""
        res = self.formats.get_format(FORMAT_PDF)
        assert isinstance(res, Format)

        assert res.name == "pdf"
        assert res.odfname == "writer_pdf_Export"
        assert res.mimetype == "application/pdf"

    @raises(UnkownFormatException)
    def test_invalid(self):
        self.formats.get_format('invalidformat')

    def test_get_format_names(self):
        names = self.formats.get_known_format_names()
        assert FORMAT_PDF in names
 def _convert_single_report(self, result_path, model_instance, data):
     """Run a command to convert to our target format"""
     filetype = self.ir_actions_report_xml_id.py3o_filetype
     if not Formats().get_format(filetype).native:
         command = self._convert_single_report_cmd(
             result_path,
             model_instance,
             data,
         )
         logger.debug('Running command %s', command)
         output = subprocess.check_output(
             command,
             cwd=os.path.dirname(result_path),
         )
         logger.debug('Output was %s', output)
         self._cleanup_tempfiles([result_path])
         result_path, result_filename = os.path.split(result_path)
         result_path = os.path.join(
             result_path,
             '%s.%s' % (os.path.splitext(result_filename)[0], filetype))
     return result_path
Exemplo n.º 10
0
 def setUp(self):
     self.formats = Formats()