Skip to content

Commit

Permalink
initial support for conditional form apply in operations
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 26, 2014
1 parent 0e27ea8 commit cf0f66a
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/quorum/model.py
Expand Up @@ -234,17 +234,18 @@ def __delattr__(self, name):
except AttributeError: pass

@classmethod
def new(cls, model = None, safe = True, apply = True, build = False, new = True):
def new(cls, model = None, form = True, safe = 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 form flag controls if the form context data should be
used in the case of an invalid/unset model value. This is considered
unsafe in many contexts.
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,13 +257,13 @@ def new(cls, model = None, safe = True, apply = True, build = False, new = True)
:type model: Dictionary
:param model: The map containing the model that is going to be applied
to the new instance to be created.
:type form: bool
:param form: If in case the provided model is not valid (unset) the model
should be retrieved from the current model in context, this is an unsafe
operation as it may create unwanted behavior (use it carefully).
: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 @@ -275,18 +276,20 @@ def new(cls, model = None, safe = True, apply = True, build = False, new = True)
model and after the proper validations are performed on it.
"""

if model == None: model = util.get_object() if form else dict()
model = cls.fill(model)
instance = cls()
apply and instance.apply(model, safe_a = safe)
instance.apply(model, form = form, 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, apply = True, build = False):
def old(cls, model = None, form = True, safe = True, build = False):
return cls.new(
model = model,
form = form,
safe = safe,
apply = apply,
build = build,
new = False
)
Expand All @@ -295,22 +298,20 @@ def old(cls, model = None, safe = True, apply = True, build = False):
def singleton(
cls,
model = None,
form = True,
safe = True,
apply = True,
build = False,
*args,
**kwargs
):
instance = cls.get(raise_e = False, *args, **kwargs)
if instance:
apply and instance.apply(model, safe_a = safe)
instance.apply(model, form = form, safe_a = safe)
else:
model = model or util.get_object()
model = cls.fill(model)
instance = cls.old(
model = model,
form = form,
safe = safe,
apply = apply,
build = build
)
return instance
Expand Down Expand Up @@ -1124,7 +1125,7 @@ def build_m(self, model = None, rules = True):
model = model or self.model
cls.build(model, rules = rules)

def apply(self, model = None, safe = None, safe_a = True):
def apply(self, model = None, form = True, safe = None, safe_a = True):
# calls the complete set of event handlers for the current
# apply operation, this should trigger changes in the model
self.pre_apply()
Expand All @@ -1147,7 +1148,7 @@ def apply(self, model = None, safe = None, safe_a = True):
# sources and then iterates over all the of the model
# values setting the values in the current intance's model
# then runs the type casting/conversion operation in it
model = model or util.get_object()
if model == None: model = util.get_object() if form else dict()
for name, value in legacy.eager(model.items()):
is_safe = safe.get(name, False)
if is_safe: continue
Expand Down

0 comments on commit cf0f66a

Please sign in to comment.