def test_has_object_been_changed_should_be_true(self): self.profile.description = 'Old description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() moderated_object.approve(by=self.user) user_profile = self.profile.__class__.objects.get(id=self.profile.id) self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() value = moderated_object.has_object_been_changed(user_profile) self.assertEqual(value, True)
def test_has_object_been_changed_should_be_true(self): self.profile.description = 'Old description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() moderated_object.approve(moderated_by=self.user) user_profile = UserProfile.objects.get(user__username='******') self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() value = moderated_object.has_object_been_changed(user_profile) self.assertEqual(value, True)
def test_post_save_handler_for_existing_object(self): 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.objects.get(user__username='******') moderated_object = ModeratedObject(content_object=profile) moderated_object.save() moderated_object.approve(by=self.user) profile.description = 'New description of user profile' profile.save() moderated_object = ModeratedObject.objects.get_for_instance(profile) original_object = moderated_object.changed_object self.assertEqual(original_object.description, 'New description of user profile') self.assertEqual(UserProfile.objects.get(pk=profile.pk).description, 'Old description') signals.pre_save.disconnect(self.moderation.pre_save_handler, UserProfile) signals.post_save.disconnect(self.moderation.post_save_handler, UserProfile)
def test_has_object_been_changed_should_be_false(self): moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() value = moderated_object.has_object_been_changed(self.profile) self.assertEqual(value, False)
def setUp(self): rf = RequestFactory() rf.login(username='******', password='******') self.request = rf.get('/admin/moderation/') self.request.user = User.objects.get(username='******') self.admin = ModeratedObjectAdmin(ModeratedObject, site) for user in User.objects.all(): ModeratedObject(content_object=user).save()
def test_filter_moderated_objects_returns_object(self): """Test if filter_moderated_objects return object when object doesn't have moderated object or deserialized object is <> object""" moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() self.profile.description = "New" self.profile.save() self.assertEqual( unicode(UserProfile.objects.all()), u'[<UserProfile: moderator - http://www.google.com>]')
def create_moderated_objects(queryset): contetn_type = ContentType.objects.get_for_model(queryset[0]) for imported_object in queryset: moderated_obj = ModeratedObject.objects.filter( object_pk=imported_object.id, content_type=contetn_type) if not moderated_obj: moderated_obj = ModeratedObject( object_pk=imported_object.id, content_type=contetn_type) moderated_obj.instance = imported_object moderated_obj.save() moderated_obj.automoderate(user=admin)
def setUp(self): self.moderation = setup_moderation([UserProfile]) rf = RequestFactory() rf.login(username='******', password='******') self.request = rf.get('/admin/moderation/') self.request.user = User.objects.get(username='******') self.request._messages = mock.Mock() self.admin = ModerationAdmin(UserProfile, site) self.profile = UserProfile.objects.get(user__username='******') self.moderated_obj = ModeratedObject(content_object=self.profile) self.moderated_obj.save()
def test_approve_moderated_object(self): """test if after object approval new data is saved.""" self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() moderated_object.approve(moderated_by=self.user) user_profile = UserProfile.objects.get(user__username='******') self.assertEqual(user_profile.description, 'New description')
def test_filter_moderated_objects_returns_empty_queryset(self): """Test filter_moderated_objects returns empty queryset for object that has moderated object""" ManagerClass = ModerationObjectsManager()(Manager) manager = ManagerClass() manager.model = UserProfile query_set = UserProfile._default_manager.all() moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() self.assertEqual(unicode(manager.filter_moderated_objects(query_set)), u"[]")
def test_change_of_deserialzed_object(self): self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() pk = moderated_object.pk self.profile.description = 'New changed description' moderated_object.changed_object = self.profile.description moderated_object.save() moderated_object = ModeratedObject.objects.get(pk=pk) self.assertEqual(moderated_object.changed_object.description, 'New changed description')
def post_save_handler(self, sender, instance, **kwargs): """ Creates new moderation object if instance is created, If instance exists and is only updated then save instance as content_object of moderated_object """ # check if object was loaded from fixture, bypass moderation if so if kwargs['raw']: return pk = instance.pk moderator = self.get_moderator(sender) if kwargs['created']: old_object = sender._default_manager.get(pk=pk) moderated_obj = ModeratedObject(content_object=old_object) if not moderator.visible_until_rejected: # Hide it by placing in draft state moderated_obj.moderation_state = MODERATION_DRAFT_STATE moderated_obj.save() moderator.inform_moderator(instance) return moderated_obj = ModeratedObject.objects.get_for_instance(instance) if (moderated_obj.moderation_status == MODERATION_STATUS_APPROVED and moderator.bypass_moderation_after_approval): # save new data in moderated object moderated_obj.changed_object = instance moderated_obj.save() return if moderated_obj.has_object_been_changed(instance): copied_instance = self._copy_model_instance(instance) if not moderator.visible_until_rejected: # Save instance with old data from changed_object, undoing # the changes that save() just saved to the database. moderated_obj.changed_object.save_base(raw=True) # Save the new data in moderated_object, so it will be applied # to the real record when the moderator approves the change. moderated_obj.changed_object = copied_instance moderated_obj.moderation_status = MODERATION_STATUS_PENDING moderated_obj.save() moderator.inform_moderator(instance) instance._moderated_object = moderated_obj
def test_get_of_existing_object_should_return_old_version_of_object(self): """Tests if after register of model class with moderation, when existing object is saved, when getting of object returns old version of object""" profile = UserProfile.objects.get(user__username='******') moderated_object = ModeratedObject(content_object=profile) moderated_object.save() moderated_object.approve(by=self.user) profile.description = "New description" profile.save() old_profile = UserProfile.objects.get(pk=profile.pk) self.assertEqual(old_profile.description, 'Old description')
def test_excluded_fields_should_be_excluded_from_changes(self): self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() self.profile = UserProfile.objects.get(user__username='******') changes = get_changes_between_models(moderated_object.changed_object, self.profile, excludes=['description']) self.assertEqual(unicode(changes), u"{u'userprofile__url': Change object: "\ u"http://www.google.com - http://www.google.com, "\ u"u'userprofile__user': Change object: 1 - 1}")
def test_get_changes_between_models(self): self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() self.profile = UserProfile.objects.get(user__username='******') changes = get_changes_between_models(moderated_object.changed_object, self.profile) self.assertEqual( unicode(changes), u"{u'userprofile__url': Change object: http://www.google.com"\ u" - http://www.google.com, u'userprofile__description': "\ u"Change object: New description - Old description, "\ u"u'userprofile__user': Change object: 1 - 1}")
def test_get_changes_between_models(self): self.profile.description = 'New description' moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() self.profile = UserProfile.objects.get(user__username='******') changes = get_changes_between_models(moderated_object.changed_object, self.profile) self.assertIn( "'userprofile__description': Change object: New description - " "Old description", str(changes)) self.assertIn("'userprofile__user': Change object: 1 - 1", str(changes)) self.assertIn( "'userprofile__url': Change object: http://www.google.com - " "http://www.google.com", str(changes))
def test_foreign_key_changes(self): self.profile.user = User.objects.get(username='******') moderated_object = ModeratedObject(content_object=self.profile) moderated_object.save() self.profile = UserProfile.objects.get(user__username='******') changes = get_changes_between_models(moderated_object.changed_object, self.profile) self.assertIn("'userprofile__user': Change object: 4 - 1", str(changes)) self.assertIn( "'userprofile__description': Change object: Old description - Old " "description", str(changes)) self.assertIn( "'userprofile__url': Change object: http://www" ".google.com - http://www.google.com", str(changes))
def test_get_or_create_moderated_object_exist(self): self.moderation.register(UserProfile) profile = UserProfile.objects.get(user__username='******') moderator = self.moderation.get_moderator(UserProfile) ModeratedObject(content_object=profile).save() profile.description = "New description" unchanged_obj = self.moderation._get_unchanged_object(profile) object = self.moderation._get_or_create_moderated_object( profile, unchanged_obj, moderator) self.assertNotEqual(object.pk, None) self.assertEqual(object.changed_object.description, 'Old description') self.moderation.unregister(UserProfile)
def post_save_handler(self, sender, instance, **kwargs): """ Creates new moderation object if instance is created, If instance exists and is only updated then save instance as content_object of moderated_object """ # check if object was loaded from fixture, bypass moderation if so if kwargs['raw']: return pk = instance.pk moderator = self.get_moderator(sender) if kwargs['created']: old_object = sender._default_manager.get(pk=pk) moderated_obj = ModeratedObject(content_object=old_object) moderated_obj.save() moderator.inform_moderator(instance) else: moderated_obj = ModeratedObject.objects.\ get_for_instance(instance) if moderated_obj.moderation_status == \ MODERATION_STATUS_APPROVED and \ moderator.bypass_moderation_after_approval: return if moderated_obj.has_object_been_changed(instance): copied_instance = self._copy_model_instance(instance) if not moderator.visible_until_rejected: # save instance with data from changed_object moderated_obj.changed_object.save_base(raw=True) # save new data in moderated object moderated_obj.changed_object = copied_instance moderated_obj.moderation_status = MODERATION_STATUS_PENDING moderated_obj.save() moderator.inform_moderator(instance) instance._moderated_object = moderated_obj
def _get_or_create_moderated_object(self, instance, unchanged_obj, moderator): """ Get or create ModeratedObject instance. If moderated object is not equal instance then serialize unchanged in moderated object in order to use it later in post_save_handler """ try: moderated_object = ModeratedObject.objects.\ get_for_instance(instance) except ObjectDoesNotExist: moderated_object = ModeratedObject(content_object=unchanged_obj) moderated_object.changed_object = unchanged_obj else: if moderated_object.has_object_been_changed(instance): if moderator.visible_until_rejected: moderated_object.changed_object = instance else: moderated_object.changed_object = unchanged_obj return moderated_object
def setUp(self): self.user = User.objects.get(username='******') obj = ModeratedObject(content_object=self.user) obj.save() self.user.moderated_object = obj self.moderator = GenericModerator(UserProfile)
def test_content_object_returns_deserialized_object(self): user = User.objects.get(username='******') moderated_object = ModeratedObject(content_object=user) moderated_object.save() content_object = self.admin.content_object(moderated_object) self.assertEqual(content_object, "admin")
def get_new_instance(unchanged_obj): moderated_object = ModeratedObject(content_object=unchanged_obj) moderated_object.changed_object = unchanged_obj return moderated_object