From 0e27ea8b78c19e30b2c9cee5a395baac7fef1aa7 Mon Sep 17 00:00:00 2001 From: joamag Date: Fri, 26 Dec 2014 14:01:08 +0000 Subject: [PATCH] support for conditional apply operation --- src/quorum/model.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/quorum/model.py b/src/quorum/model.py index ba46dcbf..92fa9caa 100644 --- a/src/quorum/model.py +++ b/src/quorum/model.py @@ -234,7 +234,7 @@ def __delattr__(self, name): except AttributeError: pass @classmethod - def new(cls, model = None, safe = True, build = False, new = True): + def new(cls, model = None, safe = True, apply = True, build = False, new = True): """ Creates a new instance of the model applying the provided model map to it after the instantiation of the class. @@ -242,6 +242,9 @@ def new(cls, model = None, safe = True, build = False, new = True): The optional safe flag makes sure that the model attributes marked as safe are going to be removed and are not valid for apply. + The optional apply flag controls if the provided model (or form) + should be applied to the new model's instance on creation. + The optional build flag may be used to define if the build operations that may be defined by the user on override should be called for this instance of entity (should be used only on retrieval). @@ -256,6 +259,10 @@ def new(cls, model = None, safe = True, build = False, new = True): :type safe: bool :param safe: If the attributes marked as safe in the model definition should be removed from the instance after the apply operation. + :type apply: bool + :param apply: If the apply operation should be performed, setting the + provided model (or the current form's model) in the created instance, + this may be used to avoid unwanted form application. :type build: bool :param build: If the "custom" build operation should be performed after the apply operation is performed so that new custom attributes may be @@ -269,26 +276,41 @@ def new(cls, model = None, safe = True, build = False, new = True): """ instance = cls() - instance.apply(model, safe_a = safe) + apply and instance.apply(model, safe_a = safe) build and cls.build(instance.model, map = False) new and instance.assert_is_new() return instance @classmethod - def old(cls, model = None, safe = True, build = False): - return cls.new(model = model, safe = safe, build = build, new = False) + def old(cls, model = None, safe = True, apply = True, build = False): + return cls.new( + model = model, + safe = safe, + apply = apply, + build = build, + new = False + ) @classmethod - def singleton(cls, model = None, safe = True, build = False, *args, **kwargs): + def singleton( + cls, + model = None, + safe = True, + apply = True, + build = False, + *args, + **kwargs + ): instance = cls.get(raise_e = False, *args, **kwargs) if instance: - instance.apply(model, safe_a = safe) + apply and instance.apply(model, safe_a = safe) else: model = model or util.get_object() model = cls.fill(model) instance = cls.old( model = model, safe = safe, + apply = apply, build = build ) return instance