# HG changeset patch # User Paul Boddie # Date 1258156239 -3600 # Node ID 19ca9b0d9f4dc1daccc789c576cb3567d967308e # Parent abf35a8f5caa29cfb4e1a35a28efa87a0851e833 Fixed question moving. diff -r abf35a8f5caa -r 19ca9b0d9f4d examples/Common/Questionnaire/__init__.py --- a/examples/Common/Questionnaire/__init__.py Tue Jul 07 01:14:12 2009 +0200 +++ b/examples/Common/Questionnaire/__init__.py Sat Nov 14 00:50:39 2009 +0100 @@ -53,20 +53,33 @@ # NOTE: Potentially a DOM convenience method here. for question in selectors.get("move-question", []): + + # Only move a question to a different position. + + position = question.xpath("count(preceding-sibling::question) + 1") destination = int(question.getAttribute("destination")) - if destination > 1: - destination += 1 - root = questionnaire.documentElement - target = (root.xpath("question[position() = %d]" % destination) or [None])[0] - root.removeChild(question) - if target is not None: - try: - root.insertBefore(question, target) - except xml.dom.NotFoundErr: - target = (root.xpath("question[position() = %d]" % destination) or [None])[0] - root.insertBefore(question, target) - else: - root.appendChild(question) + + if destination != position: + + # Where the question is before the destination, find the + # question after the current one at that position, since the + # subsequent deletion of the question being moved will then + # reduce the destination by one. + + if destination > position: + destination += 1 + + root = questionnaire.documentElement + target = (root.xpath("question[position() = %d]" % destination) or [None])[0] + root.removeChild(question) + if target is not None: + try: + root.insertBefore(question, target) + except xml.dom.NotFoundErr: + target = (root.xpath("question[position() = %d]" % destination) or [None])[0] + root.insertBefore(question, target) + else: + root.appendChild(question) # Add questions using the normal request parameter.