Beispiel #1
0
 def to_objectchange(self, action):
     # Remove MPTT-internal fields
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         object_data=serialize_object(self, exclude=["level", "lft", "rght", "tree_id"]),
     )
 def to_objectchange(self, action):
     """Remove actual and intended configuration from changelog."""
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         object_data=serialize_object(self, exclude=["actual", "intended"]),
     )
Beispiel #3
0
 def to_objectchange(self, action):
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         # related_object=self.virtual_machine,
         object_data=serialize_object(self),
     )
Beispiel #4
0
 def to_objectchange(self, action):
     # Annotate the assigned object, if any
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         related_object=self.assigned_object,
         object_data=serialize_object(self),
     )
Beispiel #5
0
 def to_objectchange(self, action):
     # Annotate the parent VirtualMachine
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         related_object=self.virtual_machine,
         object_data=serialize_object(self),
     )
Beispiel #6
0
 def to_objectchange(self, action):
     """
     Return a new ObjectChange representing a change made to this object. This will typically be called automatically
     by ChangeLoggingMiddleware.
     """
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         object_data=serialize_object(self),
     )
 def to_objectchange(self, action):
     # Annotate the parent DeviceType
     try:
         device_type = self.device_type
     except ObjectDoesNotExist:
         # The parent DeviceType has already been deleted
         device_type = None
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         related_object=device_type,
         object_data=serialize_object(self),
     )
Beispiel #8
0
 def to_objectchange(self, action):
     # Annotate the parent field
     try:
         field = self.field
     except ObjectDoesNotExist:
         # The parent field has already been deleted
         field = None
     return ObjectChange(
         changed_object=self,
         object_repr=str(self),
         action=action,
         related_object=field,
         object_data=serialize_object(self),
     )
Beispiel #9
0
    def to_objectchange(self, action, related_object=None, object_data_extra=None, object_data_exclude=None):
        """
        Return a new ObjectChange representing a change made to this object. This will typically be called automatically
        by ChangeLoggingMiddleware.
        """

        return ObjectChange(
            changed_object=self,
            object_repr=str(self),
            action=action,
            object_data=serialize_object(self, extra=object_data_extra, exclude=object_data_exclude),
            object_data_v2=serialize_object_v2(self),
            related_object=related_object,
        )
Beispiel #10
0
    def to_objectchange(self, action):
        # Annotate the parent Circuit
        try:
            related_object = self.circuit
        except Circuit.DoesNotExist:
            # Parent circuit has been deleted
            related_object = None

        return ObjectChange(
            changed_object=self,
            object_repr=str(self),
            action=action,
            related_object=related_object,
            object_data=serialize_object(self),
        )
Beispiel #11
0
def migrate_history(apps, schema_editor):
    """
    Migrate Django LogEntry objects to Nautobot ObjectChange objects.
    """
    ContentType = apps.get_model("contenttypes", "ContentType")
    CustomField = apps.get_model("extras", "CustomField")
    LogEntry = apps.get_model("admin", "LogEntry")
    ObjectChange = apps.get_model("extras", "ObjectChange")
    custom_field_type = ContentType.objects.get_for_model(CustomField)
    for custom_field in CustomField.objects.all():
        for log_entry in LogEntry.objects.filter(
                content_type=custom_field_type,
                object_id=custom_field.id).order_by("action_time"):
            if log_entry.action_flag == ADDITION:
                action = ObjectChangeActionChoices.ACTION_CREATE
            if log_entry.action_flag == CHANGE:
                action = ObjectChangeActionChoices.ACTION_UPDATE
            if log_entry.action_flag == DELETION:
                action = ObjectChangeActionChoices.ACTION_DELETE
            # Unfortunately, we can't get the serialized_object before the change so the calculated Difference
            # will be empty.  So we capture the Django change message to include in object_data to provide some
            # representation of the change
            object_data = serialize_object(custom_field)
            # object_data needs ascii, remove unicode characters from change message
            object_data["changelog_message"] = get_change_message(
                log_entry).encode("ascii", "ignore").decode()
            object_change = ObjectChange(
                user=log_entry.user,
                action=action,
                changed_object_type=custom_field_type,
                changed_object_id=custom_field.id,
                # Django LogEntry messages don't contain a request id so we make one up
                request_id=uuid.uuid4(),
                object_data=object_data,
            )
            object_change.save()
            # The time is forced to now on creation of an ObjectChange object, so update it to the action time
            object_change.time = log_entry.action_time
            object_change.save()