Project

General

Profile

0003-workflows-add-support-for-ODT-files-in-ExportToModel.patch

Benjamin Dauvergne, 12 February 2014 11:36 AM

Download (3.4 KB)

View differences:

Subject: [PATCH 3/4] workflows: add support for ODT files in ExportToModel

 - authorize .odt file for the ExportToModel status item model_file
 - reject odt template if OOoPy package is not found
 - add specific treatment of odt templates using OOoPy
 wcs/workflows.py | 39 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)
wcs/workflows.py
36 36
from quixote.html import htmltext
37 37
import qommon.errors
38 38

  
39
try:
40
    from ooopy.OOoPy import OOoPy
41
    from ooopy.Transformer import Transformer
42
    import ooopy.Transforms as Transforms
43
    ODT_SUPPORT = True
44
except ImportError:
45
    ODT_SUPPORT = False
46

  
39 47
from wcs.roles import Role, logged_users_role, get_user_roles
40 48
from wcs.formdata import Evolution
41 49
from wcs.fields import SubtitleField, TitleField, CommentField, PageField
......
1441 1449
        if fp.read(10).startswith('{\\rtf'):
1442 1450
            fp.seek(0)
1443 1451
            return 'rtf'
1452
        # ODT
1453
        if upload.content_type and upload.content_type == 'application/vnd.oasis.opendocument.text':
1454
            return 'odt'
1455
        if (upload.content_type and upload.content_type == 'application/octet-stream') or \
1456
                upload.content_type is None:
1457
            if upload.base_filename and upload.base_filename.endswith('.odt'):
1458
                return 'odt'
1444 1459

  
1445 1460
    def model_file_validation(self, upload):
1446
        if self.model_file_kind(upload) in ('rtf',):
1461
        kind = self.model_file_kind(upload)
1462
        if kind == 'odt' and not ODT_SUPPORT:
1463
            return False, _('ODT is not supported, install OOoPy')
1464
        if kind in ('rtf', 'odt'):
1447 1465
            return True, ''
1448
        return False, _('Only RTF files can be used')
1466
        return False, _('Only ODT and RTF files can be used')
1449 1467

  
1450 1468
    def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
1451 1469
        if 'label' in parameters:
......
1495 1513
            get_logger().error('error in template for export to model [%s], model could not be generated' % url)
1496 1514
            raise TemplatingError(_('Unknown error in the template: %s') % str(e))
1497 1515

  
1516
    def apply_template_to_formdata_odt(self, formdata):
1517
        '''Set value of set-variable, get-variable and input-variable node in
1518
           the ODT file.
1519
        '''
1520
        outfile = StringIO()
1521
        o = OOoPy(infile=self.model_file.get_file(), outfile=outfile)
1522
        context = context_from_formdata(formdata)
1523
        t = Transformer(o.mimetype,
1524
                Transforms.Editinfo(),
1525
                Transforms.Field_Replace(replace=context),
1526
                Transforms.Fix_OOo_Tag())
1527
        t.transform(o)
1528
        o.close()
1529
        return outfile.getvalue()
1530

  
1498 1531
    def apply_template_to_formdata(self, formdata):
1499 1532
        kind = self.model_file_kind(self.model_file)
1500 1533
        if kind == 'rtf':
1501 1534
            return self.apply_template_to_formdata_rtf(formdata)
1535
        elif kind == 'odt':
1536
            return self.apply_template_to_formdata_odt(formdata)
1502 1537
        else:
1503 1538
            raise NotImplementedError
1504 1539

  
1505
-