Skip to content

Instantly share code, notes, and snippets.

@gigidn
Last active April 18, 2020 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigidn/0e48fb021a77587d614719f97306e2f4 to your computer and use it in GitHub Desktop.
Save gigidn/0e48fb021a77587d614719f97306e2f4 to your computer and use it in GitHub Desktop.
def notify(func):
""" Decorator that enables methods of a model to publish messages through NATS."""
def wrapper(*args):
db_name = args[0].env.cr.dbname
table_name = args[0]._table
# TODO: For the moment we are using a testing subject
subject = 'account.journal'
print("wrapper_notify")
if func.__name__ == 'unlink':
message = {"UUID": args[0].uuid, "DB_NAME": db_name, "TABLE_NAME": table_name}
EventModelMixin.nc.run_pub(subject, 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():
EventModelMixin.nc.run_pub(subject, message)
else:
pass
return func(args[0],args[1])
else:
message = {"UUID": args[0].uuid, "DB_NAME": db_name, "TABLE_NAME": table_name, "VALS": args[1]}
EventModelMixin.nc.run_pub(subject, 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'
@api.model
def _fill_uuid(self):
for record in self:
record.uuid = uuid.uuid4()
print("%d records updated with uuid.", len(self))
@notify
@api.multi
def unlink(self):
return super().unlink()
@notify
@api.multi
def write(self, vals):
record = super().write(vals)
return record
@notify
@api.model
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