def test_field_exclude(self): with revision: queen = Artist(name='Queen') queen.save() data = revision.data(queen) self.assertEqual(data['field'].keys(), ['_versions_status', 'name'])
def test_field_include(self): with revision: queen = Artist(name='Queen') queen.save() a_kind_of_magic = Album(artist=queen, title='A Kind of Magic') a_kind_of_magic.save() data = revision.data(a_kind_of_magic) self.assertEqual(data['field'].keys(), ['_versions_status', 'title'])
def commit(self): if self._versions_status == VERSIONS_STATUS_STAGED_DELETE: self._versions_status = VERSIONS_STATUS_DELETED elif self._versions_status == VERSIONS_STATUS_STAGED_EDITS: self._versions_status = VERSIONS_STATUS_PUBLISHED # We don't want to call our main save method, because we want to delay # staging the state of this model until we set the state of all unpublihsed manytomany edits. self._save_base() if self._versions_revision is None: data = revision.data(self) else: data = revision.version(self, rev=self._versions_revision) for name, ids in data['related'].items(): try: field = self._meta.get_field_by_name(name)[0] except: pass else: if isinstance(field, related.ManyToManyField): related_manager = getattr(self, name) if issubclass(related_manager.model, VersionsModel): existing_ids = set( list( related_manager.get_query_set( bypass=True, bypass_filter=True).values_list( 'pk', flat=True))) else: existing_ids = set( list(related_manager.values_list('pk', flat=True))) if self in revision._state.pending_related_updates and name in revision._state.pending_related_updates[ self]: updated_ids = revision._state.pending_related_updates[ self][name] else: updated_ids = ids if existing_ids.symmetric_difference(updated_ids): setattr(self, name, updated_ids) revision.stage(self)
def commit(self): if self._versions_status == VERSIONS_STATUS_STAGED_DELETE: self._versions_status = VERSIONS_STATUS_DELETED elif self._versions_status == VERSIONS_STATUS_STAGED_EDITS: self._versions_status = VERSIONS_STATUS_PUBLISHED # We don't want to call our main save method, because we want to delay # staging the state of this model until we set the state of all unpublihsed manytomany edits. self._save_base() if self._versions_revision is None: data = revision.data(self) else: data = revision.version(self, rev=self._versions_revision) for name, ids in data['related'].items(): try: field = self._meta.get_field_by_name(name)[0] except: pass else: if isinstance(field, related.ManyToManyField): related_manager = getattr(self, name) if issubclass(related_manager.model, VersionsModel): existing_ids = set(list(related_manager.get_query_set(bypass=True, bypass_filter=True).values_list('pk', flat=True))) else: existing_ids = set(list(related_manager.values_list('pk', flat=True))) if self in revision._state.pending_related_updates and name in revision._state.pending_related_updates[self]: updated_ids = revision._state.pending_related_updates[self][name] else: updated_ids = ids if existing_ids.symmetric_difference(updated_ids): setattr(self, name, updated_ids) revision.stage(self)
def test_related_model_edits(self): with revision: queen = Artist(name='Queen') queen.save() a_kind_of_magic = Album(artist=queen, title='A Kind of Magic') a_kind_of_magic.save() dont_lose_your_head = Song(album=a_kind_of_magic, title="Don't Lose Your Head") dont_lose_your_head.save() first_revision = revision.latest_transactions['default'] with revision: princes_of_the_universe = Song(album=a_kind_of_magic, title='Princes of the Universe') princes_of_the_universe.save() dont_lose_your_head.seconds = 278 dont_lose_your_head.save() second_revision = revision.latest_transactions['default'] with revision: princes_of_the_universe.seconds = 212 princes_of_the_universe.save() friends_will_be_friends = Song(album=a_kind_of_magic, title='Friends Will Be Friends') friends_will_be_friends.save() third_revision = revision.latest_transactions['default'] with revision: # the a_kind_of_magic album was not modified after the initial commit. Verify that we can retrieve the a_kind_of_magic model from the various versions first_a_kind_of_magic = Album.objects.version(first_revision).get(pk=a_kind_of_magic.pk) second_a_kind_of_magic = Album.objects.version(second_revision).get(pk=a_kind_of_magic.pk) third_a_kind_of_magic = Album.objects.version(third_revision).get(pk=a_kind_of_magic.pk) # Verify that the data is the same. self.assertEqual(revision.data(first_a_kind_of_magic)['field'], revision.data(second_a_kind_of_magic)['field']) self.assertEqual(revision.data(second_a_kind_of_magic)['field'], revision.data(third_a_kind_of_magic)['field']) # Verify that princes_of_the_universe does not exist at the first_revision (it was created on the second revision) self.assertRaises(ObjectDoesNotExist, first_a_kind_of_magic.songs.get, pk=princes_of_the_universe.pk) # Verify that retrieving the object from the reverse relationship and directly from the Song objects yield the same result. self.assertEqual(second_a_kind_of_magic.songs.get(pk=princes_of_the_universe.pk).__dict__, Song.objects.version(second_revision).get(pk=princes_of_the_universe.pk).__dict__) # Verify that retrieval of the object from the reverse relationship return the correct versions of the objects. second_princes_of_the_universe = second_a_kind_of_magic.songs.get(pk=princes_of_the_universe.pk) self.assertEqual(second_princes_of_the_universe.seconds, None) third_princes_of_the_universe = third_a_kind_of_magic.songs.get(pk=princes_of_the_universe.pk) self.assertEqual(third_princes_of_the_universe.seconds, 212) # Verify that the first revision of a_kind_of_magic has one song self.assertEquals(len(first_a_kind_of_magic.songs.all()), 1) # Verify that the second revision of a_kind_of_magic has two songs self.assertEquals(len(second_a_kind_of_magic.songs.all()), 2) # Verify that the third revision of a_kind_of_magic has three songs self.assertEquals(len(third_a_kind_of_magic.songs.all()), 3)