From b9b5e2775812750d48cb05c4803d2da8ff9b3c43 Mon Sep 17 00:00:00 2001 From: Dominik George <dominik.george@teckids.org> Date: Thu, 7 Nov 2019 21:04:10 +0100 Subject: [PATCH] Add @Foo.method to add something as a method to a model. --- biscuit/core/mixins.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/biscuit/core/mixins.py b/biscuit/core/mixins.py index 8b31557cf..8b95bac4a 100644 --- a/biscuit/core/mixins.py +++ b/biscuit/core/mixins.py @@ -13,9 +13,7 @@ class ExtensibleModel(object): """ Mixin that adds class methods for glrofied monkey-patching. """ @classmethod - def property(cls, func: Callable[[], Any], name: Optional[str] = None) -> None: - """ Adds the passed callable as a property. """ - + def _safe_add(cls, obj: Any, name: Optional[str]) -> None: # Decide the name for the property if name is None: prop_name = func.__name__ @@ -30,7 +28,19 @@ class ExtensibleModel(object): raise ValueError('%s already used.' % prop_name) # Add function wrapped in property decorator if we got here - setattr(cls, prop_name, property(func)) + setattr(cls, prop_name, obj) + + @classmethod + def property(cls, func: Callable[[], Any], name: Optional[str] = None) -> None: + """ Adds the passed callable as a property. """ + + cls._safe_add(property(func), name) + + @classmethod + def method(cls, func: Callable[[], Any], name: Optional[str] = None) -> None: + """ Adds the passed callable as a property. """ + + cls._safe_add(func, name) class SchoolRelated(models.Model): -- GitLab