|
20 | 20 | from euphorie.content.utils import IToolTypesInfo |
21 | 21 | from io import BytesIO |
22 | 22 | from markdownify import markdownify |
| 23 | +from plone import api |
23 | 24 | from plone.autoform.form import AutoExtensibleForm |
24 | 25 | from plone.base.utils import safe_bytes |
25 | 26 | from plone.dexterity.utils import createContentInContainer |
@@ -215,6 +216,47 @@ class SurveyImporter: |
215 | 216 |
|
216 | 217 | def __init__(self, context): |
217 | 218 | self.context = context |
| 219 | + self.options = {} |
| 220 | + self.conditions = [] |
| 221 | + |
| 222 | + def ImportOption(self, node, choice): |
| 223 | + """ |
| 224 | + Create a new :obj:`euphorie.content.option` object for a |
| 225 | + :obj:`euphorie.content.module` given the details for a Option as an XML |
| 226 | + node. |
| 227 | +
|
| 228 | + :returns: :obj:`euphorie.content.option`. |
| 229 | + """ |
| 230 | + option = createContentInContainer( |
| 231 | + choice, "euphorie.option", title=str(node.title) |
| 232 | + ) |
| 233 | + option.external_id = attr_unicode(node, "external-id") |
| 234 | + option.description = el_unicode( |
| 235 | + node, "description", is_etranslate_compatible=self.is_etranslate_compatible |
| 236 | + ) |
| 237 | + self.options[el_unicode(node, "condition-id")] = option |
| 238 | + |
| 239 | + def ImportChoice(self, node, module): |
| 240 | + """ |
| 241 | + Create a new :obj:`euphorie.content.choice` object for a |
| 242 | + :obj:`euphorie.content.module` given the details for a Choice as an XML |
| 243 | + node. |
| 244 | +
|
| 245 | + :returns: :obj:`euphorie.content.choice`. |
| 246 | + """ |
| 247 | + choice = createContentInContainer( |
| 248 | + module, "euphorie.choice", title=str(node.title) |
| 249 | + ) |
| 250 | + choice.external_id = attr_unicode(node, "external-id") |
| 251 | + choice.description = el_unicode( |
| 252 | + node, "description", is_etranslate_compatible=self.is_etranslate_compatible |
| 253 | + ) |
| 254 | + choice.allow_multiple_options = el_bool(node, "allow-multiple-options") |
| 255 | + condition = el_unicode(node, "condition") |
| 256 | + if condition: |
| 257 | + self.conditions.append((choice, condition)) |
| 258 | + for child in node.iterchildren(tag=XMLNS + "option"): |
| 259 | + self.ImportOption(child, choice) |
218 | 260 |
|
219 | 261 | def ImportImage(self, node): |
220 | 262 | """Import a base64 encoded image from an XML node. |
@@ -350,6 +392,9 @@ def ImportModule(self, node, survey): |
350 | 392 | for child in node.iterchildren(tag=XMLNS + "risk"): |
351 | 393 | self.ImportRisk(child, module) |
352 | 394 |
|
| 395 | + for child in node.iterchildren(tag=XMLNS + "choice"): |
| 396 | + self.ImportChoice(child, module) |
| 397 | + |
353 | 398 | for child in node.iterchildren(tag=XMLNS + "module"): |
354 | 399 | self.ImportModule(child, module) |
355 | 400 |
|
@@ -396,6 +441,9 @@ def ImportProfileQuestion(self, node, survey): |
396 | 441 | for child in node.iterchildren(tag=XMLNS + "risk"): |
397 | 442 | self.ImportRisk(child, profile) |
398 | 443 |
|
| 444 | + for child in node.iterchildren(tag=XMLNS + "choice"): |
| 445 | + self.ImportChoice(child, profile) |
| 446 | + |
399 | 447 | for child in node.iterchildren(tag=XMLNS + "module"): |
400 | 448 | self.ImportModule(child, profile) |
401 | 449 | return profile |
@@ -468,13 +516,26 @@ def ImportSurvey(self, node, group, version_title): |
468 | 516 | x.replace(COMMA_REPLACEMENT, ",").strip() |
469 | 517 | for x in el_unicode(node, "tool-category", "").split(",") |
470 | 518 | ] |
| 519 | + |
| 520 | + self.options = {} |
| 521 | + self.conditions = [] |
| 522 | + |
471 | 523 | for child in node.iterchildren(): |
472 | 524 | if child.tag == XMLNS + "profile-question": |
473 | 525 | self.ImportProfileQuestion(child, survey) |
474 | 526 | elif child.tag == XMLNS + "module": |
475 | 527 | self.ImportModule(child, survey) |
476 | 528 | elif child.tag == XMLNS + "training_question": |
477 | 529 | self.ImportTrainingQuestion(child, survey) |
| 530 | + |
| 531 | + for choice, condition in self.conditions: |
| 532 | + for option_id in condition.split("|"): |
| 533 | + if option_id in self.options: |
| 534 | + api.relation.create( |
| 535 | + source=choice, |
| 536 | + target=self.options[option_id], |
| 537 | + relationship="condition", |
| 538 | + ) |
478 | 539 | return survey |
479 | 540 |
|
480 | 541 | def __call__( |
|
0 commit comments