(survey)
We can assume quite some things in this function, because recognize
    properly handles it. ie.

     * Every image will be tagged correctly (as long as the data is known)
     * in duplex mode both the front/back image will be tagged, so we don't
       need to care about that here!
We can assume quite some things in this function, because recognize
    properly handles it. ie.
We can assume quite some things in this function because recognize properly handles it . ie .
def reorder(survey):
    """We can assume quite some things in this function, because recognize
    properly handles it. ie.

     * Every image will be tagged correctly (as long as the data is known)
     * in duplex mode both the front/back image will be tagged, so we don't
       need to care about that here!
    """

    image_count = survey.questionnaire.page_count
    # We have two images per page in simplex mode!
    if not survey.defs.duplex:
        image_count = image_count * 2

    # First, go over all sheets and figure out which ones need reordering.
    # For every sheet that isn't quite right, we extract the images, and delete
    # the sheet.
    # The images are put into a dictionnary using the questionnaire ID.
    # Each entry in the dictionary is a list.
    images = defaultdict(lambda : [])

    # Load all sheets into memory
    sheets = []
    survey.iterate(lambda: sheets.append(survey.get_sheet()))

    for sheet in sheets: # Use a flat copy to iterate over
        broken = False
        pages = set()
        for image in sheet.images:
            if sheet.questionnaire_id != image.questionnaire_id:
                broken = True
            if sheet.global_id != image.global_id:
                broken = True

            # Check that no page exists twice
            if image.page_number is not None and image.page_number in pages:
                broken = True
            pages.add(image.page_number)

        # Also consider incomplete sets broken, so that hopefully they will
        # be filled up with the correct page.
        if len(sheet.images) != image_count:
            broken = True

        if broken:
            # Drop from the list of sheets
            survey.delete_sheet(sheet)

            for image in sheet.images:
                images[(image.questionnaire_id, image.global_id)].append(image)

    # We have dictionnary of lists of images that needs to be put into sheets
    # again.
    # This could probably be more robust. We don't care about the questionnaire
    # ID itself here, just put each list into sheets, splitting it into many
    # if there are too many images.
    for img_list in images.values():

        while len(img_list) > 0:
            sheet = model.sheet.Sheet()
            survey.add_sheet(sheet)

            while len(img_list) > 0 and len(sheet.images) < image_count:
                sheet.add_image(img_list.pop(0))


    survey.save()
def reorder ( survey ) : image_count = survey . questionnaire . page_count # We have two images per page in simplex mode! if not survey . defs . duplex : image_count = image_count * 2 # First, go over all sheets and figure out which ones need reordering. # For every sheet that isn't quite right, we extract the images, and delete # the sheet. # The images are put into a dictionnary using the questionnaire ID. # Each entry in the dictionary is a list. images = defaultdict ( lambda : [ ] ) # Load all sheets into memory sheets = [ ] survey . iterate ( lambda : sheets . append ( survey . get_sheet ( ) ) ) for sheet in sheets : # Use a flat copy to iterate over broken = False pages = set ( ) for image in sheet . images : if sheet . questionnaire_id != image . questionnaire_id : broken = True if sheet . global_id != image . global_id : broken = True # Check that no page exists twice if image . page_number is not None and image . page_number in pages : broken = True pages . add ( image . page_number ) # Also consider incomplete sets broken, so that hopefully they will # be filled up with the correct page. if len ( sheet . images ) != image_count : broken = True if broken : # Drop from the list of sheets survey . delete_sheet ( sheet ) for image in sheet . images : images [ ( image . questionnaire_id , image . global_id ) ] . append ( image ) # We have dictionnary of lists of images that needs to be put into sheets # again. # This could probably be more robust. We don't care about the questionnaire # ID itself here, just put each list into sheets, splitting it into many # if there are too many images. for img_list in images . values ( ) : while len ( img_list ) > 0 : sheet = model . sheet . Sheet ( ) survey . add_sheet ( sheet ) while len ( img_list ) > 0 and len ( sheet . images ) < image_count : sheet . add_image ( img_list . pop ( 0 ) ) survey . save ( )