XSLTools

Changeset

691:19ca9b0d9f4d
2009-11-14 Paul Boddie raw files shortlog changelog graph Fixed question moving.
examples/Common/Questionnaire/__init__.py (file)
     1.1 --- a/examples/Common/Questionnaire/__init__.py	Tue Jul 07 01:14:12 2009 +0200
     1.2 +++ b/examples/Common/Questionnaire/__init__.py	Sat Nov 14 00:50:39 2009 +0100
     1.3 @@ -53,20 +53,33 @@
     1.4          # NOTE: Potentially a DOM convenience method here.
     1.5  
     1.6          for question in selectors.get("move-question", []):
     1.7 +
     1.8 +            # Only move a question to a different position.
     1.9 +
    1.10 +            position = question.xpath("count(preceding-sibling::question) + 1")
    1.11              destination = int(question.getAttribute("destination"))
    1.12 -            if destination > 1:
    1.13 -                destination += 1
    1.14 -            root = questionnaire.documentElement
    1.15 -            target = (root.xpath("question[position() = %d]" % destination) or [None])[0]
    1.16 -            root.removeChild(question)
    1.17 -            if target is not None:
    1.18 -                try:
    1.19 -                    root.insertBefore(question, target)
    1.20 -                except xml.dom.NotFoundErr:
    1.21 -                    target = (root.xpath("question[position() = %d]" % destination) or [None])[0]
    1.22 -                    root.insertBefore(question, target)
    1.23 -            else:
    1.24 -                root.appendChild(question)
    1.25 +
    1.26 +            if destination != position:
    1.27 +
    1.28 +                # Where the question is before the destination, find the
    1.29 +                # question after the current one at that position, since the
    1.30 +                # subsequent deletion of the question being moved will then
    1.31 +                # reduce the destination by one.
    1.32 +
    1.33 +                if destination > position:
    1.34 +                    destination += 1
    1.35 +
    1.36 +                root = questionnaire.documentElement
    1.37 +                target = (root.xpath("question[position() = %d]" % destination) or [None])[0]
    1.38 +                root.removeChild(question)
    1.39 +                if target is not None:
    1.40 +                    try:
    1.41 +                        root.insertBefore(question, target)
    1.42 +                    except xml.dom.NotFoundErr:
    1.43 +                        target = (root.xpath("question[position() = %d]" % destination) or [None])[0]
    1.44 +                        root.insertBefore(question, target)
    1.45 +                else:
    1.46 +                    root.appendChild(question)
    1.47  
    1.48          # Add questions using the normal request parameter.
    1.49