def test_excluded_field_should_always_be_saved(self): ''' Ensure that excluded fields are always saved to the object. ''' profile = UserProfile(description='Profile for new user', url='http://www.dominno.com', user=User.objects.get(username='******')) profile.save() profile.moderated_object.approve() profile = UserProfile.objects.get(id=profile.id) profile.description = 'New profile' profile.save() profile.url = 'http://dominno.pl' profile.save() profile.moderated_object.approve() profile = UserProfile.objects.get(id=profile.id) self.assertEqual(profile.description, 'New profile') self.assertEqual(profile.url, 'http://dominno.pl') profile.url = 'http://www.google.com' profile.save() profile.moderated_object.approve() profile = UserProfile.objects.get(id=profile.id) self.assertEqual(profile.url, 'http://www.google.com')
def test_instance_with_many_moderations(self): # Register a moderator that keeps the history # un-register UserProfile from moderation import moderation class KeepHistoryModerator(GenericModerator): keep_history = True moderation.unregister(UserProfile) moderation.register(UserProfile, KeepHistoryModerator) profile = UserProfile(description='Profile for new user', url='http://www.yahoo.com', user=self.user) profile.save() # There should only be one ModeratedObject self.assertEqual(1, ModeratedObject.objects.count()) profile.url = 'http://www.google.com' profile.save() # Should now be two self.assertEqual(2, ModeratedObject.objects.count()) # Getting for instance should return the one with the google url moderated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(profile.url, moderated_object.changed_object.url) # Get the first object, and does it have the yahoo address moderated_object_pk1 = ModeratedObject.objects.get(pk=1) self.assertEqual('http://www.yahoo.com', moderated_object_pk1.changed_object.url)
def test_save_handler_keep_history(self): # de-register current Moderator and replace it with one that # has keep_history set to True from moderation import moderation class KeepHistoryModerator(GenericModerator): keep_history = True notify_moderator = False moderation.unregister(UserProfile) moderation.register(UserProfile, KeepHistoryModerator) from django.db.models import signals signals.pre_save.connect(self.moderation.pre_save_handler, sender=UserProfile) signals.post_save.connect(self.moderation.post_save_handler, sender=UserProfile) profile = UserProfile(description='Profile for new user', url='http://www.yahoo.com', user=User.objects.get(username='******')) profile.save() moderated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(moderated_object.content_object, profile) # Now update it and make sure it gets the right history object... profile.url = 'http://www.google.com' profile.save() moderated_object = ModeratedObject.objects.get_for_instance(profile) self.assertEqual(moderated_object.content_object, profile) # There should only be two moderated objects self.assertEqual(2, ModeratedObject.objects.count()) # Approve the change moderated_object.approve(by=self.user, reason='Testing post save handlers') # There should *still* only be two moderated objects self.assertEqual(2, ModeratedObject.objects.count()) signals.pre_save.disconnect(self.moderation.pre_save_handler, UserProfile) signals.post_save.disconnect(self.moderation.post_save_handler, UserProfile) self.moderation = False