Project

General

Profile

0001-workflows-refactoring-in-ExportToModel.patch

Benjamin Dauvergne, 12 February 2014 11:36 AM

Download (5.57 KB)

View differences:

Subject: [PATCH 1/4] workflows: refactoring in ExportToModel

 - factorize building of the variable dictionnary for templates into the
	 context_from_formdata() method
 - factorize extraction of the model file content type
 - factorize templating on RTF files in a specific method
 wcs/workflows.py | 80 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 50 insertions(+), 30 deletions(-)
wcs/workflows.py
1141 1141
                    fire_and_forget=True)
1142 1142
register_item_class(SendmailWorkflowStatusItem)
1143 1143

  
1144

  
1145
def template_on_formdata(formdata, template, process=None):
1146
    dict = {}
1147
    dict.update(get_publisher().substitutions.get_context_variables())
1148
    dict['url'] = formdata.get_url()
1149
    dict['url_status'] = '%sstatus' % formdata.get_url()
1150
    dict['details'] = formdata.formdef.get_detailed_email_form(formdata, dict['url'])
1151
    dict['name'] = formdata.formdef.name
1152
    dict['number'] = formdata.id
1144
def context_from_formdata(formdata, process=None):
1145
    context = {}
1146
    context.update(get_publisher().substitutions.get_context_variables())
1147
    context['url'] = formdata.get_url()
1148
    context['url_status'] = '%sstatus' % formdata.get_url()
1149
    context['details'] = formdata.formdef.get_detailed_email_form(formdata, context['url'])
1150
    context['name'] = formdata.formdef.name
1151
    context['number'] = formdata.id
1153 1152
    if formdata.evolution and formdata.evolution[-1].comment:
1154
        dict['comment'] = formdata.evolution[-1].comment
1153
        context['comment'] = formdata.evolution[-1].comment
1155 1154
    else:
1156
        dict['comment'] = ''
1157
    dict.update(formdata.get_as_dict())
1155
        context['comment'] = ''
1156
    context.update(formdata.get_as_dict())
1158 1157

  
1159 1158
    # compatibility vars
1160
    dict['before'] = dict.get('form_previous_status')
1161
    dict['after'] = dict.get('form_status')
1162
    dict['evolution'] = dict.get('form_evolution')
1159
    context['before'] = context.get('form_previous_status')
1160
    context['after'] = context.get('form_status')
1161
    context['evolution'] = context.get('form_evolution')
1163 1162

  
1164 1163
    if process:
1165
        for key in dict:
1166
            dict[key] = process(dict[key])
1164
        for key in context:
1165
            context[key] = process(context[key])
1167 1166

  
1168 1167
    charset = get_publisher().site_charset
1169
    for k, v in dict.items():
1168
    for k, v in context.items():
1170 1169
        if isinstance(v, unicode):
1171
            dict[k] = v.encode(charset, 'ignore')
1170
            context[k] = v.encode(charset, 'ignore')
1171

  
1172
    return context
1173

  
1174
def template_on_formdata(formdata, template, process=None):
1175
    context = context_from_formdata(formdata, process=process)
1172 1176

  
1173 1177
    processor = ezt.Template(compress_whitespace=False)
1174 1178
    processor.parse(template or '')
1175 1179

  
1176 1180
    fd = StringIO()
1177
    processor.generate(fd, dict)
1181
    processor.generate(fd, context)
1178 1182

  
1179 1183
    return fd.getvalue()
1180 1184

  
......
1410 1414
                evo.comment = _('Form exported in a model')
1411 1415
            return formdata.get_url() + self.get_directory_name()
1412 1416

  
1413
    def model_file_validation(self, upload):
1417
    def model_file_kind(self, upload):
1418
        if hasattr(upload, 'fp'):
1419
            fp = upload.fp
1420
        elif hasattr(upload, 'get_file'):
1421
            fp = upload.get_file()
1422
        else:
1423
            raise NotImplementedError
1424
        # RTF
1414 1425
        if upload.content_type and upload.content_type == 'application/rtf':
1415
            return True, ''
1426
            return 'rtf'
1416 1427
        if (upload.content_type and upload.content_type == 'application/octet-stream') or \
1417 1428
                upload.content_type is None:
1418 1429
            if upload.base_filename and upload.base_filename.endswith('.rtf'):
1419
                return True, ''
1420
        upload.fp.seek(0)
1421
        if upload.read(10).startswith('{\\rtf'):
1422
            upload.fp.seek(0)
1430
                return 'rtf'
1431
        fp.seek(0)
1432
        if fp.read(10).startswith('{\\rtf'):
1433
            fp.seek(0)
1434
            return 'rtf'
1435

  
1436
    def model_file_validation(self, upload):
1437
        if self.model_file_kind(upload) in ('rtf',):
1423 1438
            return True, ''
1424 1439
        return False, _('Only RTF files can be used')
1425 1440

  
......
1457 1472
        return qommon.misc.simplify(self.label or 'export_to_model', space='_')
1458 1473
    directory_name = property(get_directory_name)
1459 1474

  
1460
    def apply_template_to_formdata(self, formdata):
1461
        process = None
1462
        if self.model_file.base_filename.endswith('.rtf'):
1463
            process = rtf_process
1475
    def apply_template_to_formdata_rtf(self, formdata):
1476
        process = rtf_process
1464 1477
        try:
1465 1478
            return template_on_formdata(formdata, self.model_file.get_file().read(),
1466 1479
                    process=process)
......
1473 1486
            get_logger().error('error in template for export to model [%s], model could not be generated' % url)
1474 1487
            raise TemplatingError(_('Unknown error in the template: %s') % str(e))
1475 1488

  
1489
    def apply_template_to_formdata(self, formdata):
1490
        kind = self.model_file_kind(self.model_file)
1491
        if kind == 'rtf':
1492
            return self.apply_template_to_formdata_rtf(formdata)
1493
        else:
1494
            raise NotImplementedError
1495

  
1476 1496
    def get_parameters(self):
1477 1497
        return ('label', 'model_file')
1478 1498

  
1479
-