Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrillkuettel committed Jul 10, 2024
1 parent a7fae57 commit 2ccb547
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/privatim/cli/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pyramid.paster import bootstrap
from sqlalchemy import select, func

Check warning on line 3 in src/privatim/cli/helpers.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/cli/helpers.py#L1-L3

Added lines #L1 - L3 were not covered by tests

from privatim.models import SearchableAssociatedFiles
from privatim.models.associated_file import SearchableAssociatedFiles
from privatim.models.file import SearchableFile
from privatim.orm import Base

Check warning on line 7 in src/privatim/cli/helpers.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/cli/helpers.py#L5-L7

Added lines #L5 - L7 were not covered by tests

Expand Down
126 changes: 3 additions & 123 deletions src/privatim/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import logging
from contextlib import contextmanager
from threading import Lock

from sqlalchemy import event

from sqlalchemy.orm import configure_mappers, Session, object_session

from privatim.models.associated_file import SearchableAssociatedFiles
from sqlalchemy.orm import configure_mappers

# XXX import or define all models here to ensure they are attached to the
# Base.metadata prior to any initialization routines
Expand Down Expand Up @@ -38,14 +31,13 @@
Statement
PasswordChangeToken
GeneralFile
SearchableFile
SearchableMixin


from typing import TYPE_CHECKING, Generator # noqa: E402
from typing import TYPE_CHECKING # noqa: E402
if TYPE_CHECKING:
from pyramid.config import Configurator
from sqlalchemy.orm import Mapper
from sqlalchemy.engine import Connection


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -79,116 +71,4 @@ def includeme(config: 'Configurator') -> None:
)


# def update_fulltext_search_text(
# mapper: 'Mapper[SearchableAssociatedFiles]',
# connection: 'Connection',
# target: SearchableAssociatedFiles,
# ) -> None:
# """
# Event listener for the 'files' relationship. Triggers a full reindex
# if any file changes.
#
# While potentially inefficient for large collections, it's typically
# fine as the number of files is expected to be small (1-5). Consider
# optimizing if performance issues arise.
# """
# for locale in locales:
# if hasattr(target, f'searchable_text_{locale}'):
# target.reindex_files()
#
#
# def register_search_listeners(
# model: 'type[SearchableAssociatedFiles]',
# ) -> None:
# event.listen(model, 'after_insert', update_fulltext_search_text)
# event.listen(
# model,
# 'after_update',
# update_fulltext_search_text,
# )
# event.listen(
# model,
# 'after_delete', # for edit form as well as delete
# update_fulltext_search_text,
# )


# def reindex_models_with_searchable_files() -> None:
#
# seen = set()
# for _ in Base.metadata.tables.values():
# for mapper in Base.registry.mappers:
# cls = mapper.class_
# if issubclass(cls, SearchableAssociatedFiles):
# if cls not in seen:
# register_search_listeners(cls)
# seen.add(cls)


# class ReindexTracker:
# def __init__(self) -> None:
# self.to_reindex: set[SearchableAssociatedFiles] = set()
# self._lock = Lock()
#
# @contextmanager
# def tracking(self) -> Generator[None, None, None]:
# with self._lock:
# try:
# yield
# finally:
# self.reindex_all()
#
# def add(self, instance: SearchableAssociatedFiles) -> None:
# with self._lock:
# self.to_reindex.add(instance)
#
# def clear(self) -> None:
# with self._lock:
# self.to_reindex.clear()
#
# def reindex_all(self) -> None:
# with self._lock:
# for instance in self.to_reindex:
# instance.reindex_files()
# self.clear()
#
#
# reindex_tracker = ReindexTracker()
#
#
# def searchable_file_change_listener(
# mapper: 'Mapper[SearchableFile]',
# connection: 'Connection',
# target: SearchableFile,
# ) -> None:
# session = object_session(target)
# if session is None:
# return # The instance is not associated with a session
#
# logger.debug(f"Change detected for SearchableFile: {target.filename}")
# try:
# for link in target.links:
# if isinstance(link, SearchableAssociatedFiles):
# logger.debug(f"Adding instance {link.id} to reindex tracker")
# reindex_tracker.add(link)
# except Exception as e:
# logger.exception(
# f"Error processing links for SearchableFile {target.id}: {str(e)}"
# )
#
#
# def before_commit_listener(session: Session) -> None:
# reindex_tracker.reindex_all()
#
#
# def setup_reindex_tracking() -> None:
# event.listen(
# SearchableFile, 'after_insert', searchable_file_change_listener
# )
# event.listen(Session, 'before_commit', before_commit_listener)
#
#
#
# setup_reindex_tracking()

configure_mappers()
24 changes: 15 additions & 9 deletions src/privatim/models/associated_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from io import BytesIO

from sqlalchemy import func, Index
from sqlalchemy.dialects.postgresql import TSVECTOR
Expand Down Expand Up @@ -32,7 +31,6 @@ class SearchableAssociatedFiles:
""" Same as AssociatedFiles but provides the toolkit to make a list of
files searchable, if they are pdfs. """


if TYPE_CHECKING:
id: Mapped[UUIDStrPK]
__tablename__: Any
Expand Down Expand Up @@ -76,10 +74,15 @@ def reindex_files(self) -> None:
text = ''
for file in files_by_locale[locale]:
try:
if file.file.get('saved', False):
_file_handle = file.file.file
inner = file.file
if inner.get('saved', False):
_file_handle = inner.file

Check warning on line 79 in src/privatim/models/associated_file.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/models/associated_file.py#L79

Added line #L79 was not covered by tests
else:
_file_handle = file.file.original_content
if inner.original_content is None:
_file_handle = (

Check warning on line 82 in src/privatim/models/associated_file.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/models/associated_file.py#L82

Added line #L82 was not covered by tests
inner.original_content) # type: ignore
else:
raise ValueError('No file content available')

pages, extract = extract_pdf_info(_file_handle)

Check warning on line 87 in src/privatim/models/associated_file.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/models/associated_file.py#L87

Added line #L87 was not covered by tests

Expand All @@ -89,10 +92,13 @@ def reindex_files(self) -> None:
text += '\n\n' + file.extract

Check warning on line 92 in src/privatim/models/associated_file.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/models/associated_file.py#L89-L92

Added lines #L89 - L92 were not covered by tests
except Exception as e:
if 'poppler error creating document' in str(e):
logger.error(f'Possible malformed pdf: '
f'{file.filename}')
logger.error(f"Error extracting text contents for file"
f" {file.id}: {str(e)}")
logger.error(

Check warning on line 95 in src/privatim/models/associated_file.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/models/associated_file.py#L95

Added line #L95 was not covered by tests
f'Possible malformed pdf: ' f'{file.filename}'
)
logger.error(
f'Error extracting text contents for file'
f' {file.id}: {str(e)}'
)

setattr(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/privatim/models/consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship
from pyramid.authorization import Allow
from pyramid.authorization import Authenticated
from sqlalchemy_utils import observes
from sqlalchemy_utils import observes # type: ignore[import-untyped]

from privatim.models.associated_file import SearchableAssociatedFiles
from privatim.models.commentable import Commentable
Expand Down
13 changes: 6 additions & 7 deletions src/privatim/orm/markup_text_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@

class MarkupText(_Base):
""" Text column that contains HTML/XML markup. """

impl = TEXT

cache_ok = True

def process_bind_param(
self,
value: str | None,
dialect: 'Dialect'
) -> Markup | None:

return None if value is None else escape(value)

Check warning on line 23 in src/privatim/orm/markup_text_type.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/orm/markup_text_type.py#L23

Added line #L23 was not covered by tests

def process_literal_param(
def process_literal_param( # type: ignore[override]
self,
value: str | None,
dialect: 'Dialect'
) -> Markup | None:

return None if value is None else escape(value)
) -> str | None:
if value is None:
return None
escaped = escape(value)
return str(escaped) # Convert Markup to str

Check warning on line 33 in src/privatim/orm/markup_text_type.py

View check run for this annotation

Codecov / codecov/patch

src/privatim/orm/markup_text_type.py#L30-L33

Added lines #L30 - L33 were not covered by tests

def process_result_value(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/privatim/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from privatim.forms.search_form import SearchForm
from privatim.layouts import Layout
from privatim.i18n import locales
from privatim.models.associated_file import SearchableAssociatedFiles

from privatim.models import SearchableAssociatedFiles
from privatim.models.file import SearchableFile
from privatim.models.searchable import searchable_models
from privatim.models.comment import Comment
Expand Down

0 comments on commit 2ccb547

Please sign in to comment.