From 97cc023866083a32ed29a11c80b4da7f39435c84 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 27 Sep 2020 13:01:19 -0700 Subject: [PATCH] Add Groupable methods has_ancestor, _descendant, _parent. --- etiquette/objects.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/etiquette/objects.py b/etiquette/objects.py index 4b32ed1..83ff07b 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -182,6 +182,9 @@ class GroupableMixin(metaclass=abc.ABCMeta): parents = set(self.group_getter_many(parent_ids)) return parents + def has_ancestor(self, ancestor): + return ancestor in self.walk_parents() + def has_any_child(self): query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? LIMIT 1' row = self.photodb.sql_select_one(query, [self.id]) @@ -198,6 +201,15 @@ class GroupableMixin(metaclass=abc.ABCMeta): row = self.photodb.sql_select_one(query, [self.id, member.id]) return row is not None + def has_descendant(self, descendant): + return self in descendant.walk_parents() + + def has_parent(self, parent): + self.assert_same_type(parent) + query = f'SELECT 1 FROM {self.group_table} WHERE parentid == ? AND memberid == ?' + row = self.photodb.sql_select_one(query, [parent.id, self.id]) + return row is not None + @abc.abstractmethod def remove_child(self, member): if not self.has_child(member):