Projet

Général

Profil

0001-formdefs-add-more-attributes-to-the-XML-export-and-t.patch

Frédéric Péters, 25 avril 2014 13:01

Télécharger (10,3 ko)

Voir les différences:

Subject: [PATCH] formdefs: add more attributes to the XML export, and tests

 tests/test_formdef_import.py |  96 ++++++++++++++++++++++++++++++++++++++++
 wcs/formdef.py               | 101 ++++++++++++++++++++++++++++++++-----------
 2 files changed, 172 insertions(+), 25 deletions(-)
 create mode 100644 tests/test_formdef_import.py
tests/test_formdef_import.py
1
import sys
2
import shutil
3
import StringIO
4
import tempfile
5

  
6
from quixote import cleanup
7
from wcs import publisher
8

  
9
from wcs.formdef import FormDef, fields
10
from wcs.admin.forms import ET, indent
11

  
12

  
13
def setup_module(module):
14
    cleanup()
15

  
16
    global pub, req
17

  
18
    publisher.WcsPublisher.APP_DIR = tempfile.mkdtemp()
19
    pub = publisher.WcsPublisher.create_publisher()
20

  
21

  
22
def teardown_module(module):
23
    shutil.rmtree(pub.APP_DIR)
24

  
25
def assert_import_export_works(formdef, include_id=False):
26
    formdef2 = FormDef.import_from_xml_tree(
27
            formdef.export_to_xml(include_id=include_id), include_id=include_id)
28
    assert ET.tostring(indent(formdef.export_to_xml(include_id=include_id))
29
            ) == ET.tostring(indent(formdef2.export_to_xml(include_id=include_id)))
30
    return formdef2
31

  
32
def test_empty():
33
    formdef = FormDef()
34
    formdef.name = 'empty'
35
    assert_import_export_works(formdef)
36

  
37
def test_text_attributes():
38
    formdef = FormDef()
39
    formdef.name = 'Foo'
40
    formdef.url_name = 'foo'
41
    f2 = assert_import_export_works(formdef)
42
    assert f2.url_name == formdef.url_name
43

  
44
def test_boolean_attributes():
45
    formdef = FormDef()
46
    formdef.name = 'Foo'
47
    formdef.url_name = 'foo'
48
    formdef.confirmation = True
49
    formdef.allow_drafts = True
50
    f2 = assert_import_export_works(formdef)
51
    assert f2.allow_drafts == formdef.allow_drafts
52
    assert f2.confirmation == formdef.confirmation
53

  
54
def test_a_field():
55
    formdef = FormDef()
56
    formdef.name = 'Foo'
57
    formdef.fields = [
58
            fields.StringField(type='string', id=1, label='Bar', size=40)
59
            ]
60
    f2 = assert_import_export_works(formdef)
61
    assert len(f2.fields) == len(formdef.fields)
62

  
63
def test_more_fields():
64
    formdef = FormDef()
65
    formdef.name = 'Blah'
66
    formdef.fields = [
67
            fields.TextField(type='text', label='Bar', pre=True),
68
            fields.EmailField(type='email', label='Bar'),
69
            fields.BoolField(type='bool', label='Bar'),
70
            fields.DateField(type='date', label='Bar', minimum_date='2014-01-01'),
71
            fields.ItemField(type='item', label='Bar', items=['foo', 'bar', 'baz']),
72
            ]
73
    f2 = assert_import_export_works(formdef)
74
    assert len(f2.fields) == len(formdef.fields)
75
    assert f2.fields[2].type == formdef.fields[2].type
76
    assert f2.fields[3].minimum_date == formdef.fields[3].minimum_date
77
    assert f2.fields[4].items == formdef.fields[4].items
78

  
79
def test_include_id():
80
    formdef = FormDef()
81
    formdef.name = 'Blah'
82
    formdef.fields = [
83
            fields.TextField(type='text', label='Bar', pre=True),
84
            fields.EmailField(type='email',  label='Bar'),
85
            fields.BoolField(type='bool', label='Bar'),
86
            fields.DateField(type='date', label='Bar', minimum_date='2014-01-01'),
87
            fields.ItemField(type='item', label='Bar', items=['foo', 'bar', 'baz']),
88
            ]
89
    for field in formdef.fields:
90
        field.id = formdef.get_new_field_id()
91
    formdef.fields[4].id = '10'
92
    f2 = assert_import_export_works(formdef, include_id=True)
93
    assert len(f2.fields) == len(formdef.fields)
94
    assert f2.fields[0].id == formdef.fields[0].id
95
    assert f2.fields[4].id == formdef.fields[4].id
96

  
wcs/formdef.py
93 93

  
94 94
    max_field_id = None
95 95

  
96

  
97
    # declarations for serialization
98
    TEXT_ATTRIBUTES = ('name', 'url_name', 'signing',
99
            'publication_date', 'expiration_date')
100
    BOOLEAN_ATTRIBUTES = ('discussion', 'detailed_emails', 'disabled',
101
            'only_allow_one', 'allow_drafts', 'disabled_redirection',
102
            'always_advertise', 'private_status_and_history')
103

  
96 104
    def migrate(self):
97 105
        changed = False
98 106

  
......
398 406
        root['name'] = unicode(self.name, charset)
399 407
        if self.category:
400 408
            root['category'] = unicode(self.category.name, charset)
401
        for boolean_attribute in ('only_allow_one', 'allow_drafts', 'discussion',
402
                'confirmation', 'signing'):
409
        for boolean_attribute in self.BOOLEAN_ATTRIBUTES:
403 410
            value = getattr(self, boolean_attribute)
404 411
            if value:
405 412
                value = 'true'
......
415 422
    def export_to_xml(self, include_id=False):
416 423
        charset = get_publisher().site_charset
417 424
        root = ET.Element('formdef')
418
        ET.SubElement(root, 'name').text = unicode(self.name, charset)
419
        if self.url_name:
420
            ET.SubElement(root, 'url_name').text = unicode(self.url_name, charset)
421
        if self.category:
422
            ET.SubElement(root, 'category').text = unicode(self.category.name, charset)
423
        for boolean_attribute in ('only_allow_one', 'allow_drafts', 'discussion',
424
                'confirmation', 'signing'):
425
        for text_attribute in self.TEXT_ATTRIBUTES:
426
            if not hasattr(self, text_attribute) or not getattr(self, text_attribute):
427
                continue
428
            ET.SubElement(root, text_attribute).text = unicode(
429
                    getattr(self, text_attribute), charset)
430
        for boolean_attribute in self.BOOLEAN_ATTRIBUTES:
431
            if not hasattr(self, boolean_attribute):
432
                continue
425 433
            value = getattr(self, boolean_attribute)
426 434
            if value:
427 435
                value = 'true'
428 436
            else:
429 437
                value = 'false'
430 438
            ET.SubElement(root, boolean_attribute).text = value
439

  
440
        if self.category:
441
            elem = ET.SubElement(root, 'category')
442
            elem.text = unicode(self.category.name, charset)
443
            if include_id:
444
                elem.attrib['category_id'] = str(self.category.id)
445

  
446
        if self.workflow:
447
            elem = ET.SubElement(root, 'workflow')
448
            elem.text = unicode(self.workflow.name, charset)
449
            if include_id:
450
                elem.attrib['workflow_id'] = str(self.workflow.id)
451

  
431 452
        if self.max_field_id:
432 453
            ET.SubElement(root, 'max_field_id').text = str(self.max_field_id)
433
        only_allow_one = False
434
        allow_drafts = False
454

  
455
        if self.last_modification_time:
456
            elem = ET.SubElement(root, 'last_modification')
457
            elem.text = time.strftime('%Y-%m-%d %H:%M:%S', self.last_modification_time)
458
            if include_id:
459
                elem.attrib['user_id'] = str(self.last_modification_user_id)
460

  
435 461
        fields = ET.SubElement(root, 'fields')
436
        for field in self.fields:
462
        for field in self.fields or []:
437 463
            fields.append(field.export_to_xml(charset=charset, include_id=include_id))
438 464

  
439 465
        return root
......
453 479
        if tree.find('name') is None or not tree.find('name').text:
454 480
            raise ValueError()
455 481

  
456
        formdef.name = tree.find('name').text.encode(charset)
482
        for text_attribute in cls.TEXT_ATTRIBUTES:
483
            value = tree.find(text_attribute)
484
            if value is None:
485
                continue
486
            setattr(formdef, text_attribute, value.text.encode(charset))
487

  
488
        for boolean_attribute in cls.BOOLEAN_ATTRIBUTES:
489
            value = tree.find(boolean_attribute)
490
            if value is None:
491
                continue
492
            setattr(formdef, boolean_attribute, value.text == 'true')
493

  
457 494
        formdef.fields = []
458 495
        for i, field in enumerate(tree.find('fields')):
459 496
            try:
......
473 510
            else:
474 511
                formdef.max_field_id = max([lax_int(x.id) for x in formdef.fields])+1
475 512

  
513
        if tree.find('last_modification') is not None:
514
            node = tree.find('last_modification')
515
            self.last_modification_time = time.strptime(node.text, '%Y-%m-%d %H:%M:%S')
516
            if include_id and elem.attrib.get('user_id'):
517
                self.last_modification_user_id = elem.attrib.get('user_id')
518

  
476 519
        if tree.find('category') is not None:
477
            category = tree.find('category').text.encode(charset)
478
            cats = Category.select()
479
            for c in cats:
480
                if c.name == category:
481
                    formdef.category_id = c.id
482
                    break
520
            category_node = tree.find('category')
521
            if include_id and category_node.attrib.get('category_id'):
522
                formdef.category_id = int(category_node.attrib.get('category_id'))
523
            else:
524
                category = category_node.text.encode(charset)
525
                cats = Category.select()
526
                for c in Category.select():
527
                    if c.name == category:
528
                        formdef.category_id = c.id
529
                        break
483 530

  
484
        for boolean_attribute in ('only_allow_one', 'allow_drafts', 'discussion',
485
                'confirmation', 'signing'):
486
            value = tree.find(boolean_attribute)
487
            if value is None:
488
                continue
489
            setattr(formdef, boolean_attribute, value.text == 'true')
531
        if tree.find('workflow') is not None:
532
            workflow_node = tree.find('workflow')
533
            if include_id and workflow_node.attrib.get('workflow_id'):
534
                formdef.workflow_id = workflow_node.attrib.get('workflow_id')
535
            else:
536
                workflow = workflow_node.text.encode(charset)
537
                for w in Workflow.select():
538
                    if c.name == workflow:
539
                        formdef.workflow_id = c.id
540
                        break
490 541

  
491 542
        return formdef
492 543
    import_from_xml_tree = classmethod(import_from_xml_tree)
493
-