Skip to content

Commit

Permalink
bug fix + remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Nov 4, 2023
1 parent ec5507d commit 9c6d83f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 1,189 deletions.
4 changes: 0 additions & 4 deletions papermerge/core/models/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ def update_text_field(self, stream: io.StringIO):
Returns text read from IO stream
"""
logger.debug(
'update_text_field:'
f'len(page.stripped_text)=={len(self.stripped_text)}'
)
self.text = stream.read()
self.save()

Expand Down
31 changes: 16 additions & 15 deletions papermerge/core/page_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def copy_text_field(
# list of old_version page numbers
page_numbers=page_numbers
)

# updates page.text fields and document_version.text field
dst.update_text_field(streams)

Expand Down Expand Up @@ -251,7 +250,7 @@ def move_pages_mix(
src_old_version,
src_new_version,
moved_pages_count
] = copy_pages(source_page_ids)
] = copy_without_pages(source_page_ids)

moved_pages = Page.objects.filter(pk__in=source_page_ids)
moved_page_ids = [page.id for page in moved_pages]
Expand Down Expand Up @@ -325,7 +324,7 @@ def move_pages_replace(
src_old_version,
src_new_version,
moved_pages_count
] = copy_pages(source_page_ids)
] = copy_without_pages(source_page_ids)

moved_pages = Page.objects.filter(pk__in=source_page_ids)
moved_page_ids = [page.id for page in moved_pages]
Expand Down Expand Up @@ -384,7 +383,7 @@ def extract_pages(
old_doc_ver,
new_doc_ver,
moved_pages_count
] = copy_pages(source_page_ids)
] = copy_without_pages(source_page_ids)

if strategy == ExtractStrategy.ONE_PAGE_PER_DOC:
new_docs = extract_to_single_paged_docs(
Expand Down Expand Up @@ -496,20 +495,18 @@ def extract_to_multi_paged_doc(
return new_doc


def copy_pages(
def copy_without_pages(
page_ids: List[uuid.UUID]
) -> [PyDocVer, PyDocVer, int]:
"""Copy pages from src doc version to dst doc version
"""Copy all pages WHICH ARE NOT in `page_ids` list from src to dst
All pages are assumed to be from same source document version.
Source document version is the doc ver of the first page
(again, all pages are assumed to be part of same doc ver).
The destination doc ver is created. All pages referenced
by IDs are copied into newly created destination doc version.
The OCR data/page folder is copied along (reused).
Source is the document version of the first page.
Destination will be created as new document version.
Destination will have all source pages WHICH ARE NOT in the `page_ids` list.
Also sends INDEX UPDATE notification
The OCR data/page folder reused.
Also sends INDEX UPDATE notification.
"""
moved_pages = Page.objects.filter(pk__in=page_ids)
moved_page_ids = [page.id for page in moved_pages]
Expand All @@ -532,7 +529,7 @@ def copy_pages(
src_keys = [ # IDs of the pages which were not removed
page.id
for page in src_old_version.pages.order_by('number')
if not (page.id in moved_page_ids)
if not (page.id in moved_page_ids) # Notice the negation
]

dst_values = [
Expand All @@ -548,7 +545,11 @@ def copy_pages(
copy_text_field(
src=src_old_version,
dst=src_new_version,
page_numbers=[p.number for p in moved_pages]
page_numbers=[
p.number
for p in src_old_version.pages.all()
if not (p.id in moved_page_ids) # Notice the negation
]
)

notify_index_update(
Expand Down
26 changes: 14 additions & 12 deletions tests/core/test_page_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pytest

from papermerge.core.models import Document
from papermerge.core.page_ops import (apply_pages_op, copy_pages,
copy_text_field, extract_pages,
from papermerge.core.page_ops import (apply_pages_op, copy_text_field,
copy_without_pages, extract_pages,
move_pages)
from papermerge.core.pathlib import abs_page_path
from papermerge.core.schemas.pages import ExtractStrategy, MoveStrategy
Expand Down Expand Up @@ -636,12 +636,12 @@ def test_extract_two_pages_to_folder_each_page_in_separate_doc(_):


@patch('papermerge.core.signals.ocr_document_task')
def test_copy_pages(_):
def test_copy_without_pages(_):
"""Scenario
copy page 1
copy without page 1
ver X -> ver X + 1
S1 S1
S1 S2
S2
"""
user = user_recipe.make()
Expand All @@ -653,20 +653,22 @@ def test_copy_pages(_):
user=user,
include_ocr_data=True
)
doc_ver = src.versions.last()
orig_first_page = src.versions.last().pages.all()[0]
orig_second_page = src.versions.last().pages.all()[1]
orig_doc_ver = src.versions.last()
orig_first_page = orig_doc_ver.pages.all()[0]
orig_second_page = orig_doc_ver.pages.all()[1]
orig_first_page.text = "cat"
orig_second_page.text = "dog"
orig_first_page.save()
orig_second_page.save()
pages_to_copy = [doc_ver.pages.first().id]
# page containing "cat" / first page is left behind
pages_to_leave_behind = [orig_doc_ver.pages.first().id]

[old_ver, new_ver, page_count] = copy_pages(pages_to_copy)
[_, new_ver, _] = copy_without_pages(pages_to_leave_behind)

assert new_ver.pages.count() == 1
assert ['cat'] == [p.text for p in new_ver.pages.all()]
assert new_ver.text == 'cat'
# new version contains only 'dog'
assert ['dog'] == [p.text for p in new_ver.pages.all()]
assert new_ver.text == 'dog'


class PageDir:
Expand Down
27 changes: 0 additions & 27 deletions tests/core/views/test_groups.py

This file was deleted.

Loading

0 comments on commit 9c6d83f

Please sign in to comment.