Add abstractmethod to Groupable methods that write; dunder methods.

The read-only methods like get_* and has_* don't need to be overridden,
but for anything that writes I don't want to forget to override them
with feature decorators etc.
This commit is contained in:
voussoir 2020-09-20 12:04:49 -07:00
parent 2a89cf33d3
commit a8f5f858b7

View file

@ -2,6 +2,7 @@
This file provides the data objects that should not be instantiated directly, This file provides the data objects that should not be instantiated directly,
but are returned by the PDB accesses. but are returned by the PDB accesses.
''' '''
import abc
import os import os
import PIL.Image import PIL.Image
import send2trash import send2trash
@ -80,11 +81,11 @@ class ObjectBase:
return None return None
return self.photodb.get_user(id=self.author_id) return self.photodb.get_user(id=self.author_id)
class GroupableMixin: class GroupableMixin(metaclass=abc.ABCMeta):
group_getter_many = None group_getter_many = None
group_table = None group_table = None
def _lift_children(self): def __lift_children(self):
''' '''
If this object has parents, the parents adopt all of its children. If this object has parents, the parents adopt all of its children.
Otherwise, this object is at the root level, so the parental Otherwise, this object is at the root level, so the parental
@ -100,7 +101,7 @@ class GroupableMixin:
for parent in parents: for parent in parents:
parent.add_children(children) parent.add_children(children)
def _add_child(self, member): def __add_child(self, member):
self.assert_same_type(member) self.assert_same_type(member)
if member == self: if member == self:
@ -121,13 +122,15 @@ class GroupableMixin:
} }
self.photodb.sql_insert(table=self.group_table, data=data) self.photodb.sql_insert(table=self.group_table, data=data)
@abc.abstractmethod
def add_child(self, member): def add_child(self, member):
return self._add_child(member) return self.__add_child(member)
@abc.abstractmethod
def add_children(self, members): def add_children(self, members):
bail = True bail = True
for member in members: for member in members:
bail = bail and (self._add_child(member) is BAIL) bail = bail and (self.__add_child(member) is BAIL)
if bail: if bail:
return BAIL return BAIL
@ -137,6 +140,7 @@ class GroupableMixin:
if self.photodb != other.photodb: if self.photodb != other.photodb:
raise TypeError(f'Objects must belong to the same PhotoDB.') raise TypeError(f'Objects must belong to the same PhotoDB.')
@abc.abstractmethod
def delete(self, *, delete_children=False): def delete(self, *, delete_children=False):
''' '''
Delete this object's relationships to other groupables. Delete this object's relationships to other groupables.
@ -154,7 +158,7 @@ class GroupableMixin:
for child in self.get_children(): for child in self.get_children():
child.delete(delete_children=True) child.delete(delete_children=True)
else: else:
self._lift_children() self.__lift_children()
# Note that this part comes after the deletion of children to prevent # Note that this part comes after the deletion of children to prevent
# issues of recursion. # issues of recursion.
@ -199,6 +203,7 @@ class GroupableMixin:
row = self.photodb.sql_select_one(query, [self.id, member.id]) row = self.photodb.sql_select_one(query, [self.id, member.id])
return row is not None return row is not None
@abc.abstractmethod
def remove_child(self, member): def remove_child(self, member):
if not self.has_child(member): if not self.has_child(member):
return BAIL return BAIL