Skip to content

Commit 17672bb

Browse files
committed
Export/import of choices and options
1 parent be7add9 commit 17672bb

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

src/euphorie/content/browser/export.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from euphorie.content.browser.upload import COMMA_REPLACEMENT
1717
from euphorie.content.browser.upload import NSMAP
1818
from euphorie.content.browser.upload import ProfileQuestionLocationFields
19+
from euphorie.content.choice import IChoice
1920
from euphorie.content.module import IModule
21+
from euphorie.content.option import IOption
2022
from euphorie.content.profilequestion import IProfileQuestion
2123
from euphorie.content.risk import IKinneyEvaluation
2224
from euphorie.content.risk import IRisk
@@ -303,9 +305,9 @@ def exportSurvey(self, parent, survey):
303305
if self.include_intro_text and StripMarkup(survey.introduction):
304306
node = self._add_string_or_html(node, survey.introduction, "introduction")
305307
if survey.classification_code:
306-
etree.SubElement(node, "classification-code").text = (
307-
survey.classification_code
308-
)
308+
etree.SubElement(
309+
node, "classification-code"
310+
).text = survey.classification_code
309311
etree.SubElement(node, "language").text = survey.language
310312
enable_web_training = getattr(survey, "enable_web_training", False)
311313
if enable_web_training:
@@ -410,6 +412,8 @@ def exportProfileQuestion(self, parent, profile):
410412
self.exportModule(node, child)
411413
elif IRisk.providedBy(child):
412414
self.exportRisk(node, child)
415+
elif IChoice.providedBy(child):
416+
self.exportChoice(node, child)
413417
return node
414418

415419
def exportModule(self, parent, module):
@@ -442,6 +446,8 @@ def exportModule(self, parent, module):
442446
self.exportModule(node, child)
443447
elif IRisk.providedBy(child):
444448
self.exportRisk(node, child)
449+
elif IChoice.providedBy(child):
450+
self.exportChoice(node, child)
445451
return node
446452

447453
def exportRisk(self, parent, risk):
@@ -552,3 +558,33 @@ def exportTrainingQuestion(self, parent, training_question):
552558
training_question.wrong_answer_2
553559
)
554560
return node
561+
562+
def exportChoice(self, parent, choice):
563+
""":returns: An XML node with the details of an
564+
:obj:`euphorie.content.choice`."""
565+
node = etree.SubElement(parent, "choice")
566+
if getattr(choice, "external_id", None):
567+
node.attrib["external-id"] = choice.external_id
568+
etree.SubElement(node, "title").text = choice.title
569+
node = self._add_string_or_html(node, choice.description, "description")
570+
etree.SubElement(
571+
node,
572+
"allow-multiple-options",
573+
attrib={"value": "true" if choice.allow_multiple_options else "false"},
574+
)
575+
etree.SubElement(node, "condition").text = choice.get_client_condition()
576+
for child in choice.values():
577+
if IOption.providedBy(child):
578+
self.exportOption(node, child)
579+
580+
def exportOption(self, parent, option):
581+
""":returns: An XML node with the details of an
582+
:obj:`euphorie.content.option`."""
583+
node = etree.SubElement(parent, "option")
584+
if getattr(option, "external_id", None):
585+
node.attrib["external-id"] = option.external_id
586+
etree.SubElement(node, "title").text = option.title
587+
node = self._add_string_or_html(node, option.description, "description")
588+
etree.SubElement(node, "condition-id").text = "/".join(
589+
option.getPhysicalPath()[-3:]
590+
)

src/euphorie/content/browser/upload.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from euphorie.content.utils import IToolTypesInfo
2121
from io import BytesIO
2222
from markdownify import markdownify
23+
from plone import api
2324
from plone.autoform.form import AutoExtensibleForm
2425
from plone.base.utils import safe_bytes
2526
from plone.dexterity.utils import createContentInContainer
@@ -215,6 +216,47 @@ class SurveyImporter:
215216

216217
def __init__(self, context):
217218
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)
218260

219261
def ImportImage(self, node):
220262
"""Import a base64 encoded image from an XML node.
@@ -350,6 +392,9 @@ def ImportModule(self, node, survey):
350392
for child in node.iterchildren(tag=XMLNS + "risk"):
351393
self.ImportRisk(child, module)
352394

395+
for child in node.iterchildren(tag=XMLNS + "choice"):
396+
self.ImportChoice(child, module)
397+
353398
for child in node.iterchildren(tag=XMLNS + "module"):
354399
self.ImportModule(child, module)
355400

@@ -396,6 +441,9 @@ def ImportProfileQuestion(self, node, survey):
396441
for child in node.iterchildren(tag=XMLNS + "risk"):
397442
self.ImportRisk(child, profile)
398443

444+
for child in node.iterchildren(tag=XMLNS + "choice"):
445+
self.ImportChoice(child, profile)
446+
399447
for child in node.iterchildren(tag=XMLNS + "module"):
400448
self.ImportModule(child, profile)
401449
return profile
@@ -468,13 +516,26 @@ def ImportSurvey(self, node, group, version_title):
468516
x.replace(COMMA_REPLACEMENT, ",").strip()
469517
for x in el_unicode(node, "tool-category", "").split(",")
470518
]
519+
520+
self.options = {}
521+
self.conditions = []
522+
471523
for child in node.iterchildren():
472524
if child.tag == XMLNS + "profile-question":
473525
self.ImportProfileQuestion(child, survey)
474526
elif child.tag == XMLNS + "module":
475527
self.ImportModule(child, survey)
476528
elif child.tag == XMLNS + "training_question":
477529
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+
)
478539
return survey
479540

480541
def __call__(

0 commit comments

Comments
 (0)