Last active
November 14, 2019 11:49
-
-
Save yzdann/97cc35501a76663b7b5e783adaffbc2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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