From 58f86fd3d7815bfe9785b9feec696aa33bacc788 Mon Sep 17 00:00:00 2001 From: Griatch Date: Wed, 3 Nov 2021 23:18:50 +0100 Subject: [PATCH] Make common typeclass.delete return True/False to match Object.delete api for consistency. Resolve #2398 --- CHANGELOG.md | 2 ++ evennia/accounts/accounts.py | 10 ++++++++++ evennia/comms/comms.py | 8 ++++++++ evennia/scripts/scripts.py | 23 ++++++++++++++++++----- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13909c5a12..1ba095fe36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,6 +114,8 @@ Up requirements to Django 3.2+, Twisted 21+ `"Evennia webclient (websocket:firefox)"` or `"evennia webclient (ajax:chrome)"`. - `TagHandler.add/has(tag=...)` kwarg changed to `add/has(key=...)` for consistency with other handlers. +- Make `DefaultScript.delete`, `DefaultChannel.delete` and `DefaultAccount.delete` return + bool True/False if deletion was successful (like `DefaultObject.delete` before them) ### Evennia 0.9.5 (2019-2020) diff --git a/evennia/accounts/accounts.py b/evennia/accounts/accounts.py index 4874c8fd29..ca2b492c72 100644 --- a/evennia/accounts/accounts.py +++ b/evennia/accounts/accounts.py @@ -853,6 +853,12 @@ class DefaultAccount(AccountDB, metaclass=TypeclassBase): `*args` and `**kwargs` are passed on to the base delete mechanism (these are usually not used). + Return: + bool: If deletion was successful. Only time it fails would be + if the Account was already deleted. Note that even on a failure, + connected resources (nicks/aliases etc) will still have been + deleted. + """ for session in self.sessions.all(): # unpuppeting all objects and disconnecting the user, if any @@ -868,7 +874,11 @@ class DefaultAccount(AccountDB, metaclass=TypeclassBase): self.attributes.clear() self.nicks.clear() self.aliases.clear() + if not self.pk: + return False super().delete(*args, **kwargs) + return True + # methods inherited from database model diff --git a/evennia/comms/comms.py b/evennia/comms/comms.py index c75458fdb1..68d61864b7 100644 --- a/evennia/comms/comms.py +++ b/evennia/comms/comms.py @@ -399,12 +399,20 @@ class DefaultChannel(ChannelDB, metaclass=TypeclassBase): """ Deletes channel. + Returns: + bool: If deletion was successful. Only time it can fail would be + if channel was already deleted. Even if it were to fail, all subscribers + will be disconnected. + """ self.attributes.clear() self.aliases.clear() for subscriber in self.subscriptions.all(): self.disconnect(subscriber) + if not self.pk: + return False super().delete() + return True def channel_prefix(self): """ diff --git a/evennia/scripts/scripts.py b/evennia/scripts/scripts.py index f2fcc12c95..28689a3cfa 100644 --- a/evennia/scripts/scripts.py +++ b/evennia/scripts/scripts.py @@ -447,11 +447,18 @@ class ScriptBase(ScriptDB, metaclass=TypeclassBase): is if wanting to delete the script from the at_stop method - setting this will then avoid an infinite recursion. + Returns: + bool: If deletion was successful or not. Only time this can fail would be if + the script was already previously deleted, or `at_script_delete` returns + False. + """ + if not self.pk or not self.at_script_delete(): + return False if stop_task: self._stop_task() - self.at_script_delete() super().delete() + return True def at_script_creation(self): """ @@ -462,10 +469,13 @@ class ScriptBase(ScriptDB, metaclass=TypeclassBase): def at_script_delete(self): """ - Called when script is deleted, after at_stop. + Called when script is deleted, before the script timer stops. + + Returns: + bool: If False, deletion is aborted. """ - pass + return True def is_valid(self): """ @@ -725,10 +735,13 @@ class DefaultScript(ScriptBase): def at_script_delete(self): """ - Called when the Script is deleted, after at_stop(). + Called when the Script is deleted, before stopping the timer. + + Returns: + bool: If False, the deletion is aborted. """ - pass + return True def at_server_reload(self): """