context_managers.py 632 B

12345678910111213141516171819202122232425
  1. from contextlib import contextmanager
  2. from netbox.context import current_request, webhooks_queue
  3. from .webhooks import flush_webhooks
  4. @contextmanager
  5. def change_logging(request):
  6. """
  7. Enable change logging by connecting the appropriate signals to their receivers before code is run, and
  8. disconnecting them afterward.
  9. :param request: WSGIRequest object with a unique `id` set
  10. """
  11. current_request.set(request)
  12. webhooks_queue.set([])
  13. yield
  14. # Flush queued webhooks to RQ
  15. flush_webhooks(webhooks_queue.get())
  16. # Clear context vars
  17. current_request.set(None)
  18. webhooks_queue.set([])