Projet

Général

Profil

0001-workflows-encode-model-file-in-base64-in-import-expo.patch

Frédéric Péters, 16 décembre 2015 10:24

Télécharger (3,66 ko)

Voir les différences:

Subject: [PATCH] workflows: encode model file in base64 in import/export
 (#9350)

 tests/test_workflow_import.py | 20 +++++++++++++++++---
 wcs/wf/export_to_model.py     |  9 +++++++--
 2 files changed, 24 insertions(+), 5 deletions(-)
tests/test_workflow_import.py
214 214

  
215 215
    export_to = ExportToModel()
216 216
    export_to.label = 'test'
217
    upload = Upload('/foo/bar', content_type='text/rtf')
217
    upload = Upload('/foo/bar', content_type='application/vnd.oasis.opendocument.text')
218
    file_content = '''PK\x03\x04\x14\x00\x00\x08\x00\x00\'l\x8eG^\xc62\x0c\'\x00'''
218 219
    upload.fp = StringIO.StringIO()
219
    upload.fp.write('HELLO WORLD')
220
    upload.fp.write(file_content)
220 221
    upload.fp.seek(0)
221 222
    export_to.model_file = UploadedFile(publisher.WcsPublisher.APP_DIR, None, upload)
222 223
    st1.items.append(export_to)
223 224
    export_to.parent = st1
224 225

  
225 226
    assert wf.possible_status[0].items[0].model_file.base_filename == 'bar'
226
    assert wf.possible_status[0].items[0].model_file.base_filename == 'bar'
227 227
    wf2 = assert_import_export_works(wf)
228 228
    assert wf2.possible_status[0].items[0].model_file.base_filename == 'bar'
229
    assert wf2.possible_status[0].items[0].model_file.get_file().read() == file_content
229 230

  
231
    # and test with an empty file
232
    export_to = ExportToModel()
233
    export_to.label = 'test'
234
    upload = Upload('/foo/bar', content_type='text/rtf')
235
    file_content = ''
236
    upload.fp = StringIO.StringIO()
237
    upload.fp.write(file_content)
238
    upload.fp.seek(0)
239
    export_to.model_file = UploadedFile(publisher.WcsPublisher.APP_DIR, None, upload)
240
    st1.items = [export_to]
241
    export_to.parent = st1
242
    wf2 = assert_import_export_works(wf)
243
    assert wf2.possible_status[0].items[0].model_file.get_file().read() == file_content
230 244

  
231 245
def test_export_roles():
232 246
    wf = Workflow(name='roles')
wcs/wf/export_to_model.py
14 14
# You should have received a copy of the GNU General Public License
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import base64
17 18
from StringIO import StringIO
18 19
from xml.etree import ElementTree as ET
19 20
import zipfile
......
331 332
        el = ET.SubElement(xml_item, 'model_file')
332 333
        ET.SubElement(el, 'base_filename').text = self.model_file.base_filename
333 334
        ET.SubElement(el, 'content_type').text = self.model_file.content_type
334
        ET.SubElement(el, 'content').text = self.model_file.get_file().read()
335
        ET.SubElement(el, 'b64_content').text = base64.encodestring(
336
                self.model_file.get_file().read())
335 337

  
336 338
    def model_file_init_with_xml(self, elem, charset, include_id=False):
337 339
        if elem is None:
338 340
            return
339 341
        base_filename = elem.find('base_filename').text
340 342
        content_type = elem.find('content_type').text
341
        content = elem.find('content').text
343
        if elem.find('b64_content') is not None:
344
            content = base64.decodestring(elem.find('b64_content').text)
345
        if elem.find('content') is not None:
346
            content = elem.find('content').text
342 347

  
343 348
        ids = (self.parent.parent.id, self.parent.id, self.id)
344 349
        filename = 'export_to_model-%s-%s-%s.upload' % ids
345
-