from datetime import datetime from marshmallow import pre_dump from marshmallow.exceptions import MarshmallowError class MissingContextError(MarshmallowError): pass class IsEntryBlueDotMixin: """Update is_entry field by a Blue Dot logic For usage put "utc_now": datetime.utcnow().date() to schema context! """ @pre_dump def blue_dot(self, data, *args, **kwargs): result = False last_added_date_time = data.get("last_added_date_time") is_entry = data["is_entry"] if last_added_date_time: if not self.context.get("utc_now"): raise MissingContextError("Missing utc_now in schema context") result = is_entry and datetime.fromisoformat(last_added_date_time[:19]).date() == self.context["utc_now"] data["is_entry"] = result return data