소스 검색

review fixes

Arthur 13 시간 전
부모
커밋
78f40e084e
2개의 변경된 파일과 16개의 추가작업 그리고 2개의 파일을 삭제
  1. 2 1
      netbox/netbox/models/features.py
  2. 14 1
      netbox/netbox/tests/test_model_features.py

+ 2 - 1
netbox/netbox/models/features.py

@@ -702,7 +702,8 @@ class SyncedDataMixin(models.Model):
 
         :param save: If true, save() will be called after data has been synchronized
         """
-        if save:
+        # ManagedFile is not change-logged and so has no snapshot()
+        if save and hasattr(self, 'snapshot'):
             self.snapshot()
         self.sync_data()
         self.data_synced = timezone.now()

+ 14 - 1
netbox/netbox/tests/test_model_features.py

@@ -1,6 +1,7 @@
 import hashlib
 import uuid
 from unittest import skipIf
+from unittest.mock import patch
 
 from django.apps import apps
 from django.conf import settings
@@ -10,7 +11,7 @@ from django.test import RequestFactory, TestCase
 from django.utils.timezone import now
 from taggit.models import Tag
 
-from core.models import AutoSyncRecord, DataFile, DataSource, ObjectChange
+from core.models import AutoSyncRecord, DataFile, DataSource, ManagedFile, ObjectChange
 from dcim.models import Site
 from extras.models import (
     ConfigContext,
@@ -173,6 +174,18 @@ class ModelFeaturesTestCase(TestCase):
         self.assertEqual(change.prechange_data['template_code'], 'original\n')
         self.assertEqual(change.postchange_data['template_code'], 'updated\n')
 
+    @patch.object(ManagedFile, 'sync_data')
+    def test_synceddatamixin_sync_without_change_logging(self, mock_sync_data):
+        """Saving a sync succeeds for models which are not change-logged. Ref: #23617"""
+        datafile = self._create_data_file('script.py', b'# script\n')
+        obj = ManagedFile.objects.create(file_root='scripts', file_path='script.py', data_file=datafile)
+
+        obj.sync(save=True)
+
+        mock_sync_data.assert_called_once()
+        obj.refresh_from_db()
+        self.assertIsNotNone(obj.data_synced)
+
     def test_synceddatamixin_accepts_plain_text_template(self):
         """Text which is not a YAML document is still valid template content."""
         datafile = self._create_data_file('comment-only.j2', b'{# nothing here #}\n')