models: Extract common Uri methods to own class

master
Felix Stupp 2 years ago
parent f282ad7ff1
commit bdfcd43a5f
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -1,5 +1,6 @@
from __future__ import annotations
from abc import abstractmethod, abstractproperty
import base64
import dataclasses
from dataclasses import dataclass
@ -82,6 +83,72 @@ def thumbnail_sort_key(width: int, height: int) -> Tuple:
####
class UriHolder:
### abstracted
@abstractproperty
def _primary_uri(self) -> str:
"""Returns the primary uri of this object in a naive way."""
@abstractmethod
def _set_primary_uri(self, uri: str) -> None:
"""Sets the primary uri of this object in a naive way."""
@abstractproperty
def _get_uri_set(self) -> Set[str]:
"""Returns the uri set of this object in a naive way."""
@abstractmethod
def _set_uri_set(self, uri_set: Set[str]) -> None:
"""Sets the uri set of this object in a naive way."""
@abstractmethod
def _add_uri_to_set(self, uri: str) -> bool:
"""Adds a uri to the uri set of this object in a naive way.
Returns True if the uri was not in the uri set before.
"""
@abstractmethod
def _remove_uri_from_set(self, uri: str) -> bool:
"""Removes a uri to the uri set of this object in a naive way.
Returns True if the uri was in the uri set before.
"""
### implemented
@property
def primary_uri(self) -> str:
"""Returns the current primary uri of this object."""
return self._primary_uri
def is_primary_uri(self, compare_uri: str) -> bool:
"""Returns True if the given uri is equal to the current primary uri."""
return self.primary_uri == compare_uri
def set_primary_uri(self, uri: str) -> bool:
"""Sets the current primary of this object.
It will also add the uri to the uri set.
Returns True if the uri was not in the uri set before.
"""
ret = self._add_uri_to_set(uri) # might fail, so try first
self._set_primary_uri(uri)
return ret
def set_as_only_uri(self, uri: str) -> None:
self._set_uri_set({uri}) # might fail, so try first
self._set_primary_uri(uri)
def add_single_uri(self, uri: str) -> bool:
return self._add_uri_to_set(uri)
def add_uris(self, uri_list: Iterable[Optional[str]]) -> bool:
return any([self.add_single_uri(uri) for uri in set(uri_list) if uri])
@dataclass
class TagRootElement:
base: Tagable
@ -426,7 +493,7 @@ class MediaCollectionLink(db.Entity):
## Media Elements
class MediaElement(db.Entity, Tagable):
class MediaElement(db.Entity, UriHolder, Tagable):
id: int = orm.PrimaryKey(
int,
@ -583,6 +650,25 @@ class MediaElement(db.Entity, Tagable):
result |= link.collection.direct_tags
return result
### for UriHolder
@property
def _primary_uri(self) -> str:
return self.uri
def _set_primary_uri(self, uri: str) -> None:
self.uri = uri
@property
def _get_uri_set(self) -> Set[str]:
return {m.uri for m in self.uris}
def _set_uri_set(self, uri_set: Set[str]) -> None:
self.uris = set()
self.add_uris(uri_set)
### own methods
def merge_to(self, other: MediaElement):
if self.watched:
other.watched = True
@ -613,9 +699,6 @@ class MediaElement(db.Entity, Tagable):
) # TODO may replace with merge call
return False
def add_uris(self, uri_list: Iterable[str]) -> bool:
return all(self.add_single_uri(uri) for uri in set(uri_list))
def before_insert(self):
self.before_update()
@ -701,7 +784,7 @@ class MediaUriMapping(db.Entity):
## Media Collections
class MediaCollection(db.Entity, Tagable):
class MediaCollection(db.Entity, UriHolder, Tagable):
id: int = orm.PrimaryKey(
int,
@ -804,6 +887,25 @@ class MediaCollection(db.Entity, Tagable):
def assigned_tags(self) -> Set[Tag]:
return set(self.tag_list)
### for UriHolder
@property
def _primary_uri(self) -> str:
return self.uri
def _set_primary_uri(self, uri: str) -> None:
self.uri = uri
@property
def _get_uri_set(self) -> Set[str]:
return {m.uri for m in self.uris}
def _set_uri_set(self, uri_set: Set[str]) -> None:
self.uris = set()
self.add_uris(uri_set)
### others
@property
def stats(self) -> CollectionStats:
return CollectionStats.from_collection(self)
@ -847,9 +949,6 @@ class MediaCollection(db.Entity, Tagable):
) # TODO may replace with merge call
return False
def add_uris(self, uri_list: Iterable[str]) -> bool:
return all(self.add_single_uri(uri) for uri in set(uri_list))
def before_insert(self):
self.before_update()

Loading…
Cancel
Save