nautobot.apps.change_logging
¶
Classes and utilities related to Nautobot change logging.
nautobot.apps.change_logging.ChangeContext
¶
ChangeContext is used to describe a single transaction that may be related to one or more object changes. A unique id can be provided, otherwise one will be generated to relate any changes to this transaction. Convenience classes are provided for each context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
User object |
None
|
request
|
WSGIRequest
|
object to retrieve user from django rest framework after authentication is performed |
None
|
context
|
ObjectChangeEventContextChoices
|
Context of the transaction |
None
|
context_detail
|
Optional[str]
|
extra details about the transaction (ex: plugin name that initiated the change) |
''
|
change_id
|
Optional[UUID]
|
Object to uniquely identify the transaction. One will be generated if not supplied |
None
|
The next two parameters hold the state of updated objects as the database held it before the save, keyed
by primary key. get_snapshots() uses them as the prechange side of the diff. They are filled during
the save itself, and only for objects whose changes have a consumer, since nothing else reads them.
Only the v2 form is filled now. The v1 form stays for callers that supply their own data.
pre_object_data (dict): Optional dictionary of serialized object data to be used in the object snapshot
pre_object_data_v2 (dict): Optional dictionary of serialized object data to be used in the object snapshot
as_dict(instance=None)
¶
Return ChangeContext attributes in dictionary format
get_user(instance=None)
¶
Return self.user if set, otherwise return self.request.user
has_consumers(content_type, action)
¶
Whether anything would act on a change with this content type and action.
change_has_consumers() reads Redis, and outside a web request nothing memoizes that, so a job
saving thousands of objects would ask for the same answer thousands of times. The answer is
remembered here for the life of this context, which is one request or one job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
ContentType
|
Content type of the changed object. |
required |
action
|
str
|
One of the |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if at least one webhook, job hook, or event broker would be triggered. |
nautobot.apps.change_logging.JobChangeContext
¶
nautobot.apps.change_logging.JobHookChangeContext
¶
nautobot.apps.change_logging.ORMChangeContext
¶
nautobot.apps.change_logging.WebChangeContext
¶
nautobot.apps.change_logging.change_logging(change_context)
¶
Enable change logging by connecting the appropriate signals to their receivers before code is run, and disconnecting them afterward.
nautobot.apps.change_logging.web_request_context(user, context_detail='', change_id=None, context=ObjectChangeEventContextChoices.CONTEXT_ORM, request=None)
¶
Emulate the context of an HTTP request, which provides functions like change logging and webhook processing in response to data changes. This context manager is for use with low level utility tooling, such as the 'nautobot-server nbshell' management command.
By default, when working with the Django ORM, neither change logging nor webhook processing occur unless manually invoked and this context manager handles those functions. A valid User object must be provided.
Examples:
>>> from nautobot.extras.context_managers import web_request_context
>>> user = User.objects.get(username="admin")
>>> with web_request_context(user, context_detail="manual-fix"):
... lt = Location.objects.get(name="Root")
... lax = Location(name="LAX", location_type=lt)
... lax.validated_save()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
User
|
User object |
required |
context_detail
|
str
|
Optional extra details about the transaction (ex: plugin name that initiated the change) |
''
|
change_id
|
Optional[UUID]
|
Object to uniquely identify the transaction. One will be generated if not supplied |
None
|
context
|
str
|
Optional string value of the generated change log entries' "change_context" field.
Defaults to |
CONTEXT_ORM
|
request
|
Request
|
Optional web request instance, one will be generated if not supplied |
None
|