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 15:51

Télécharger (10,5 ko)

Voir les différences:

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

 tests/test_formdef_import.py |  96 +++++++++++++++++++++++++++++++++++++++
 wcs/formdef.py               | 105 ++++++++++++++++++++++++++++++++-----------
 2 files changed, 176 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
        if include_id and self.id:
426
            root.attrib['id'] = self.id
427
        for text_attribute in list(self.TEXT_ATTRIBUTES):
428
            if not hasattr(self, text_attribute) or not getattr(self, text_attribute):
429
                continue
430
            ET.SubElement(root, text_attribute).text = unicode(
431
                    getattr(self, text_attribute), charset)
432
        for boolean_attribute in self.BOOLEAN_ATTRIBUTES:
433
            if not hasattr(self, boolean_attribute):
434
                continue
425 435
            value = getattr(self, boolean_attribute)
426 436
            if value:
427 437
                value = 'true'
428 438
            else:
429 439
                value = 'false'
430 440
            ET.SubElement(root, boolean_attribute).text = value
441

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

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

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

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

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

  
439 467
        return root
......
453 481
        if tree.find('name') is None or not tree.find('name').text:
454 482
            raise ValueError()
455 483

  
456
        formdef.name = tree.find('name').text.encode(charset)
484
        if include_id and tree.attrib.get('id'):
485
            formdef.id = int(tree.attrib.get('id'))
486
        for text_attribute in list(cls.TEXT_ATTRIBUTES):
487
            value = tree.find(text_attribute)
488
            if value is None:
489
                continue
490
            setattr(formdef, text_attribute, value.text.encode(charset))
491

  
492
        for boolean_attribute in cls.BOOLEAN_ATTRIBUTES:
493
            value = tree.find(boolean_attribute)
494
            if value is None:
495
                continue
496
            setattr(formdef, boolean_attribute, value.text == 'true')
497

  
457 498
        formdef.fields = []
458 499
        for i, field in enumerate(tree.find('fields')):
459 500
            try:
......
473 514
            else:
474 515
                formdef.max_field_id = max([lax_int(x.id) for x in formdef.fields])+1
475 516

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

  
476 523
        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
524
            category_node = tree.find('category')
525
            if include_id and category_node.attrib.get('category_id'):
526
                formdef.category_id = int(category_node.attrib.get('category_id'))
527
            else:
528
                category = category_node.text.encode(charset)
529
                cats = Category.select()
530
                for c in Category.select():
531
                    if c.name == category:
532
                        formdef.category_id = c.id
533
                        break
483 534

  
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')
535
        if tree.find('workflow') is not None:
536
            workflow_node = tree.find('workflow')
537
            if include_id and workflow_node.attrib.get('workflow_id'):
538
                formdef.workflow_id = workflow_node.attrib.get('workflow_id')
539
            else:
540
                workflow = workflow_node.text.encode(charset)
541
                for w in Workflow.select():
542
                    if c.name == workflow:
543
                        formdef.workflow_id = c.id
544
                        break
490 545

  
491 546
        return formdef
492 547
    import_from_xml_tree = classmethod(import_from_xml_tree)
493
-