Skip to content

Instantly share code, notes, and snippets.

@yzdann
Last active November 14, 2019 11:49
Show Gist options
  • Save yzdann/97cc35501a76663b7b5e783adaffbc2a to your computer and use it in GitHub Desktop.
Save yzdann/97cc35501a76663b7b5e783adaffbc2a to your computer and use it in GitHub Desktop.
@dataclass
class FieldUpdate
field_name: str
value: Any # ``Any`` and ``Union`` are taken from the ``typing`` module.
invalid_reason: Union[FieldErrors.Unknown, FieldErrors.Immutable, None]
class ModelUpdate:
def __init__(self, model_class, request_body):
updates = []
for field, value in request_body.items():
... # We'd check whether the field is known and mutable here.
updates.append(FieldUpdate(field, value, field_error))
self.updates = updates
def apply(self, model_obj):
if any(u.invalid_reason for u in self.updates):
raise InvalidUpdateError(...)
for update in self.updates:
setattr(model_obj, update.field, update.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment