Skip to content

Instantly share code, notes, and snippets.

Last active Apr 18, 2020
What would you like to do?
def notify(func):
""" Decorator that enables methods of a model to publish messages through NATS."""
def wrapper(*args):
db_name = args[0]
table_name = args[0]._table
# TODO: For the moment we are using a testing subject
subject = 'account.journal'
if func.__name__ == 'unlink':
message = {"UUID": args[0].uuid, "DB_NAME": db_name, "TABLE_NAME": table_name}, message)
return func(args[0])
elif func.__name__ == "write":
message = {"UUID": args[0].uuid, "DB_NAME": db_name, "TABLE_NAME": table_name, "VALS": args[1]}
if args[0].id and 'create' not in check_method_caller():, message)
return func(args[0],args[1])
message = {"UUID": args[0].uuid, "DB_NAME": db_name, "TABLE_NAME": table_name, "VALS": args[1]}, message)
return func(args[0], args[1])
return wrapper
class EventModelMixin(models.AbstractModel):
_name = 'event.model'
uuid = fields.Char('UUID', copy=False)
#qui usa quello che ti serve per fare la connessione all'esterno oppure mettilo nel wrapper a te la scelta
nc = NatsConnection()
_subject = 'module.topic'
def _fill_uuid(self):
for record in self:
record.uuid = uuid.uuid4()
print("%d records updated with uuid.", len(self))
def unlink(self):
return super().unlink()
def write(self, vals):
record = super().write(vals)
return record
def create(self, vals):
record = super().create(vals)
return record
#applicarla ad un modello - esempio
class EventExampleModel(models.Model):
_name = 'account.journal'
_inherit = ["account.journal", "event.model"]
_subject = 'account.journal'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment