Skip to content

Commit

Permalink
support for conditional apply operation
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 26, 2014
1 parent e0c148d commit 0e27ea8
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/quorum/model.py
Expand Up @@ -234,14 +234,17 @@ 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.
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).
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0e27ea8

Please sign in to comment.