mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 21:59:58 -05:00
Always writing all tags when saving. Increasing tags file width to avoid breaking file names.
This commit is contained in:
parent
b85d868a1b
commit
7e23e2cfff
2 changed files with 19 additions and 25 deletions
|
|
@ -185,6 +185,7 @@ class ConfigFile:
|
||||||
"""
|
"""
|
||||||
file_name: str = None
|
file_name: str = None
|
||||||
default_config: dict = None
|
default_config: dict = None
|
||||||
|
width: int = None
|
||||||
|
|
||||||
def __init__(self, global_: bool = False):
|
def __init__(self, global_: bool = False):
|
||||||
self.file_lock = Lock()
|
self.file_lock = Lock()
|
||||||
|
|
@ -203,6 +204,8 @@ class ConfigFile:
|
||||||
with config_file.open('rt') as fh:
|
with config_file.open('rt') as fh:
|
||||||
self._config.update(yaml.load(fh, Loader=yaml.Loader))
|
self._config.update(yaml.load(fh, Loader=yaml.Loader))
|
||||||
|
|
||||||
|
self.width = self.width or 90
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<{self.__class__.__name__} file={self.get_file()}>'
|
return f'<{self.__class__.__name__} file={self.get_file()}>'
|
||||||
|
|
||||||
|
|
@ -236,7 +239,7 @@ class ConfigFile:
|
||||||
|
|
||||||
config.update({k: v for k, v in self._config.items() if v is not None})
|
config.update({k: v for k, v in self._config.items() if v is not None})
|
||||||
with config_file.open('wt') as fh:
|
with config_file.open('wt') as fh:
|
||||||
yaml.dump(config, fh)
|
yaml.dump(config, fh, width=self.width)
|
||||||
finally:
|
finally:
|
||||||
if acquired:
|
if acquired:
|
||||||
self.file_lock.release()
|
self.file_lock.release()
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,8 @@ class Tag(ModelHelper, Base):
|
||||||
logger.info(f'Tagging {file_group} with {self}')
|
logger.info(f'Tagging {file_group} with {self}')
|
||||||
session.add(tag_file)
|
session.add(tag_file)
|
||||||
session.flush([tag_file])
|
session.flush([tag_file])
|
||||||
get_tags_config().add_tag(tag_file)
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
get_tags_config().save_tags(session)
|
||||||
return tag_file
|
return tag_file
|
||||||
|
|
||||||
@optional_session
|
@optional_session
|
||||||
|
|
@ -72,8 +72,8 @@ class Tag(ModelHelper, Base):
|
||||||
.one_or_none()
|
.one_or_none()
|
||||||
if tag_file:
|
if tag_file:
|
||||||
session.delete(tag_file)
|
session.delete(tag_file)
|
||||||
get_tags_config().remove_tag(tag_file)
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
get_tags_config().save_tags(session)
|
||||||
else:
|
else:
|
||||||
logger.warning(f'Could not find tag_file for FileGroup.id={file_group.id}/Tag.id={self.id=}')
|
logger.warning(f'Could not find tag_file for FileGroup.id={file_group.id}/Tag.id={self.id=}')
|
||||||
|
|
||||||
|
|
@ -86,6 +86,7 @@ class Tag(ModelHelper, Base):
|
||||||
|
|
||||||
class TagsConfig(ConfigFile):
|
class TagsConfig(ConfigFile):
|
||||||
file_name = 'tags.yaml'
|
file_name = 'tags.yaml'
|
||||||
|
width = 500
|
||||||
|
|
||||||
default_config = dict(
|
default_config = dict(
|
||||||
tags=list(),
|
tags=list(),
|
||||||
|
|
@ -100,31 +101,21 @@ class TagsConfig(ConfigFile):
|
||||||
value = sorted(value, key=lambda i: (i[0].lower(), i[1]))
|
value = sorted(value, key=lambda i: (i[0].lower(), i[1]))
|
||||||
self.update({'tags': value})
|
self.update({'tags': value})
|
||||||
|
|
||||||
def add_tag(self, tag_file: TagFile):
|
def save_tags(self, session: Session):
|
||||||
from wrolpi.files.models import FileGroup
|
media_directory = get_media_directory()
|
||||||
file_group: FileGroup = tag_file.file_group
|
|
||||||
primary_path = str(file_group.primary_path.relative_to(get_media_directory()))
|
|
||||||
tag_name = tag_file.tag.name
|
|
||||||
tags = self.tags.copy()
|
|
||||||
|
|
||||||
value = [tag_name, primary_path]
|
from wrolpi.files.models import FileGroup
|
||||||
if value not in tags:
|
results = session.query(Tag, TagFile, FileGroup) \
|
||||||
|
.filter(TagFile.tag_id == Tag.id, TagFile.file_group_id == FileGroup.id) \
|
||||||
|
.order_by(FileGroup.primary_path)
|
||||||
|
|
||||||
|
tags = []
|
||||||
|
for tag, _, file_group in results:
|
||||||
|
value = [tag.name, str(file_group.primary_path.relative_to(media_directory))]
|
||||||
tags.append(value)
|
tags.append(value)
|
||||||
self.tags = tags
|
|
||||||
|
|
||||||
def remove_tag(self, tag_file: TagFile):
|
# Write tags to the config.
|
||||||
from wrolpi.files.models import FileGroup
|
self.tags = tags
|
||||||
file_group: FileGroup = tag_file.file_group
|
|
||||||
primary_path = str(file_group.primary_path.relative_to(get_media_directory()))
|
|
||||||
tag_name = tag_file.tag.name
|
|
||||||
tags = self.tags.copy()
|
|
||||||
|
|
||||||
for idx, value in enumerate(self.tags):
|
|
||||||
if value == [tag_name, primary_path]:
|
|
||||||
tags = tags.copy()
|
|
||||||
tags.pop(idx)
|
|
||||||
self.tags = tags
|
|
||||||
break
|
|
||||||
|
|
||||||
|
|
||||||
TAGS_CONFIG: TagsConfig = TagsConfig(global_=True)
|
TAGS_CONFIG: TagsConfig = TagsConfig(global_=True)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue