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 |
|
-
|