mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-20 15:06:19 -05:00
Adding FileGroup for files that have been tagged and exist, but do not yet have a FileGroup when importing tag config.
This commit is contained in:
parent
32346dedc8
commit
ef35885797
3 changed files with 29 additions and 7 deletions
|
|
@ -47,7 +47,7 @@ logger = logger.getChild(__name__)
|
|||
|
||||
__all__ = ['list_directories_contents', 'delete', 'split_path_stem_and_suffix', 'refresh_files', 'search_files',
|
||||
'get_mimetype', 'split_file_name_words', 'get_primary_file', 'get_file_statistics',
|
||||
'search_file_suggestion_count',
|
||||
'search_file_suggestion_count', 'glob_shared_stem',
|
||||
'move', 'rename', 'delete_directory', 'handle_file_group_search_results']
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -284,6 +284,7 @@ def import_tags_config(session: Session = None):
|
|||
"""Reads the Tags and TagFiles from the config file, upserts them in the DB."""
|
||||
from modules.zim import lib as zim_lib
|
||||
from modules.zim.models import Zim, TagZimEntry
|
||||
from wrolpi.files.lib import glob_shared_stem
|
||||
|
||||
if PYTEST and not TEST_TAGS_CONFIG:
|
||||
logger.warning('Refusing to import tags without test tags config. '
|
||||
|
|
@ -340,6 +341,13 @@ def import_tags_config(session: Session = None):
|
|||
# Paths are absolute in the DB, relative in config.
|
||||
absolute_path = media_directory / primary_path
|
||||
file_group: FileGroup = file_groups_by_primary_path.get(absolute_path)
|
||||
if not file_group and absolute_path.is_file():
|
||||
# File exists, but is not yet in DB.
|
||||
files = glob_shared_stem(absolute_path)
|
||||
file_group = FileGroup.from_paths(session, *files)
|
||||
session.add(file_group)
|
||||
session.flush([file_group, ])
|
||||
|
||||
if tag and file_group:
|
||||
tag_file: TagFile = tag_files.get((tag.id, file_group.id))
|
||||
if not tag_file:
|
||||
|
|
|
|||
|
|
@ -219,13 +219,27 @@ async def test_import_tags_config(test_session, test_directory, test_tags_config
|
|||
file_group: FileGroup = test_session.query(FileGroup).one()
|
||||
assert file_group.tag_files, 'File was not tagged during import.'
|
||||
|
||||
# Importing again does not change count.
|
||||
await files_lib.refresh_files()
|
||||
|
||||
@pytest.mark.asyncio()
|
||||
async def test_import_tags_config_missing_file(test_session, test_directory, test_tags_config, example_singlefile):
|
||||
"""A FileGroup should be created (and tagged) when a file exists, but does not yet have a FileGroup."""
|
||||
with test_tags_config.open('wt') as fh:
|
||||
data = dict(
|
||||
# Tag names can contain Unicode characters.
|
||||
tags={'🦄': {'color': '#654321'}},
|
||||
tag_files=[
|
||||
('🦄', str(example_singlefile.relative_to(test_directory)), '2000-01-01 01:01:01'),
|
||||
],
|
||||
)
|
||||
yaml.dump(data, fh)
|
||||
|
||||
# Re-initialize config with the data we just saved.
|
||||
tags.get_tags_config().initialize()
|
||||
|
||||
tags.import_tags_config(test_session)
|
||||
assert test_session.query(tags.Tag).count() == 1
|
||||
assert test_session.query(tags.TagFile).count() == 1
|
||||
# Example file, and the config file.
|
||||
assert test_session.query(FileGroup).count() == 2
|
||||
|
||||
file_group: FileGroup = test_session.query(FileGroup).one()
|
||||
assert file_group.tag_files, 'File was not tagged during import.'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue