2020-08-18 13:05:41 -04:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
2022-11-02 17:40:43 -04:00
|
|
|
from netbox.context import current_request, webhooks_queue
|
2021-05-28 16:07:27 -04:00
|
|
|
from .webhooks import flush_webhooks
|
2020-08-18 13:05:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def change_logging(request):
|
|
|
|
"""
|
|
|
|
Enable change logging by connecting the appropriate signals to their receivers before code is run, and
|
|
|
|
disconnecting them afterward.
|
|
|
|
|
|
|
|
:param request: WSGIRequest object with a unique `id` set
|
|
|
|
"""
|
2022-11-02 17:40:43 -04:00
|
|
|
current_request.set(request)
|
|
|
|
webhooks_queue.set([])
|
2020-08-18 13:05:41 -04:00
|
|
|
|
|
|
|
yield
|
|
|
|
|
2021-05-28 16:07:27 -04:00
|
|
|
# Flush queued webhooks to RQ
|
2022-11-02 17:40:43 -04:00
|
|
|
flush_webhooks(webhooks_queue.get())
|
2021-11-17 14:26:12 -05:00
|
|
|
|
2022-11-02 17:40:43 -04:00
|
|
|
# Clear context vars
|
|
|
|
current_request.set(None)
|
|
|
|
webhooks_queue.set([])
|