Ejemplo n.º 1
0
 def reassign_page_nums_after_delete(self, deleted_pages: list,
                                     page_count: int):
     """
     :page_count: page count BEFORE delete operation
     """
     pairs = get_assigns_after_delete(total_pages=page_count,
                                      deleted_pages=deleted_pages)
     for new_version_page_num, old_version_page_num in pairs:
         page = self.pages.get(number=old_version_page_num)
         page.number = new_version_page_num
         page.save()
Ejemplo n.º 2
0
    def delete_pages(self, doc_path, page_numbers, skip_migration=False):
        """
        Delets pages in the document pointed by doc_path.
        doc_path is an instance of mglib.path.DocumentPath

        In case of success returns document's new version.
        """

        if not isinstance(page_numbers, list):
            logger.error("Expecting list argument")
            return False

        src_doc_path = doc_path
        dst_doc_path = DocumentPath.copy_from(src_doc_path,
                                              version=doc_path.version + 1)
        self.make_sure_path_exists(self.abspath(dst_doc_path))
        stapler.delete_pages(self.abspath(src_doc_path),
                             self.abspath(dst_doc_path), page_numbers)

        if skip_migration:
            return doc_path.version + 1

        page_count = self.get_pagecount(doc_path)

        if len(page_numbers) > page_count:
            logger.error(
                f"deleted_pages({page_numbers}) > page_count({page_count})")
            return

        assigns = get_assigns_after_delete(total_pages=page_count,
                                           deleted_pages=page_numbers)
        for a in assigns:
            for step in Steps():
                src_page_path = PagePath(document_path=src_doc_path,
                                         page_num=a[1],
                                         step=step,
                                         page_count=page_count)
                dst_page_path = PagePath(document_path=dst_doc_path,
                                         page_num=a[0],
                                         step=step,
                                         page_count=page_count -
                                         len(page_numbers))
                self.copy_page(src_page_path=src_page_path,
                               dst_page_path=dst_page_path)

        return doc_path.version + 1