예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
    def test_is_not_equal_instance_should_be_false(self):
        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        value = moderated_object._is_not_equal_instance(self.profile)

        self.assertEqual(value, False)
예제 #4
0
    def test_generate_diff(self):
        self.profile.description = 'New description'
        self.profile.url = 'http://new_url.com'

        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        self.profile = UserProfile.objects.get(user__username='******')
        self.profile.description = 'Old description'
        self.profile.url = 'http://old_url.com'
       
        fields_diff = generate_diff(self.profile,
                                    moderated_object.changed_object)

        description_diff = u'<del class="diff modified">Old </del>'\
                           u'<ins class="diff modified">New </ins>description'
        url_diff = u'http<del class="diff modified">old_url</del>'\
                   u'<ins class="diff modified">new_url</ins>.com'
 
        self.assertEqual(fields_diff, [
                                        {
                                         'verbose_name':'description',
                                         'diff': description_diff},
                                        {
                                         'verbose_name':'url',
                                         'diff': url_diff}])
예제 #5
0
 def test_approval_status_pending(self):
     """test if before object approval status is pending"""
     
     moderated_object = ModeratedObject(content_object=self.profile)
     moderated_object.save()
     
     self.assertEqual(moderated_object.moderation_status,
                      MODERATION_STATUS_PENDING)
예제 #6
0
    def test_filter_moderated_objects_returns_object(self):
        """Test if filter_moderated_objects return object when object 
        doesnt have moderated object or deserialised 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>]")
예제 #7
0
    def test_has_object_been_changed_should_be_true(self):
        self.profile.description = 'New description'

        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        user_profile = self.profile.__class__.objects.get(id=self.profile.id)

        value = moderated_object.has_object_been_changed(user_profile)

        self.assertEqual(value, True)
예제 #8
0
    def test_has_object_been_changed_should_be_true(self):
        self.profile.description = 'New description'

        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        user_profile = self.profile.__class__.objects.get(id=self.profile.id)

        value = moderated_object.has_object_been_changed(user_profile)

        self.assertEqual(value, True)
예제 #9
0
    def test_has_object_been_changed_should_be_true(self):
        self.profile.description = 'New description'

        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        user_profile = UserProfile.objects.get(user__username='******')

        value = moderated_object.has_object_been_changed(user_profile)

        self.assertEqual(value, True)
예제 #10
0
    def test_is_not_equal_instance_should_be_true(self):
        self.profile.description = 'New description'

        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        user_profile = UserProfile.objects.get(user__username='******')

        value = moderated_object._is_not_equal_instance(user_profile)

        self.assertEqual(value, True)
예제 #11
0
    def test_has_object_been_changed_should_be_true(self):
        self.profile.description = 'New description'

        moderated_object = ModeratedObject(content_object=self.profile)
        moderated_object.save()

        user_profile = UserProfile.objects.get(user__username='******')

        value = moderated_object.has_object_been_changed(user_profile)

        self.assertEqual(value, True)
예제 #12
0
    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>]')
예제 #13
0
    def test_deserialzed_object(self):
        moderated_object = ModeratedObject(content_object=self.profile)
        self.profile.description = "New description"
        moderated_object.changed_object = self.profile
        moderated_object.save()
        pk = moderated_object.pk

        moderated_object = ModeratedObject.objects.get(pk=pk)

        self.assertEqual(moderated_object.changed_object.description, "New description")

        self.assertEqual(moderated_object.content_object.description, u"Old description")
예제 #14
0
    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()

        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"[]")
예제 #15
0
    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='******')
        
        diff = get_changes_between_models(moderated_object.changed_object,
                                   self.profile)

        self.assertEqual(diff, {'description': ('New description',
                                                u'Profile description')})
예제 #16
0
    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}")
예제 #17
0
    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')
예제 #18
0
 def test_approve_moderated_object_new_model_instance(self):
     profile = UserProfile(description='Profile for new user',
                 url='http://www.test.com',
                 user=User.objects.get(username='******'))
     
     moderated_object = ModeratedObject(content_object=profile)
     moderated_object.save()
     
     moderated_object.approve(self.user)
     
     user_profile = UserProfile.objects.get(url='http://www.test.com')
     
     self.assertEqual(user_profile.description, 'Profile for new user')
예제 #19
0
    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()
예제 #20
0
    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}")
예제 #21
0
    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"[]")
예제 #22
0
class ModerationAdminSendMessageTestCase(SettingsTestCase):
    fixtures = ['test_users.json', 'test_moderation.json']
    urls = 'moderation.tests.urls.default'
    test_settings = 'moderation.tests.settings.generic'

    def setUp(self):
        rf = RequestFactory()
        rf.login(username='******', password='******')
        self.request = rf.get('/admin/moderation/')
        self.request.user = User.objects.get(username='******')
        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_send_message_when_object_has_no_moderated_object(self):
        profile = UserProfile(description='Profile for new user',
                    url='http://www.yahoo.com',
                    user=User.objects.get(username='******'))

        profile.save()

        self.admin.send_message(self.request, profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), u"This object is not registered "\
                                          u"with the moderation system.")

    def test_send_message_status_pending(self):
        self.moderated_obj.moderation_status = MODERATION_STATUS_PENDING
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), u"Object is not viewable on site,"\
                         u" it will be visible when moderator will accept it")

    def test_send_message_status_rejected(self):
        self.moderated_obj.moderation_status = MODERATION_STATUS_REJECTED
        self.moderated_obj.moderation_reason = u'Reason for rejection'
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), u"Object has been rejected by "\
                                 "moderator, reason: Reason for rejection")

    def test_send_message_status_approved(self):
        self.moderated_obj.moderation_status = MODERATION_STATUS_APPROVED
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), "Object has been approved by "\
                                           "moderator and is visible on site")
예제 #23
0
    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}")
예제 #24
0
    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='******')
        ModeratedObject(content_object=profile).save()

        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,
            u'Old description')

        signals.pre_save.disconnect(self.moderation.pre_save_handler,
                                    UserProfile)
        signals.post_save.disconnect(self.moderation.post_save_handler,
                                     UserProfile)
예제 #25
0
    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}")
예제 #26
0
 def test_moderate(self):
     self.profile.description = 'New description'
     self.profile.save()
     
     moderated_object = ModeratedObject(content_object=self.profile)
     moderated_object.save()
     
     self.profile.description = 'Old description'
     self.profile.save()
     
     moderated_object._moderate(MODERATION_STATUS_APPROVED,
                                self.user, "Reason")
     
     self.assertEqual(moderated_object.moderation_status,
                      MODERATION_STATUS_APPROVED)
     self.assertEqual(moderated_object.moderated_by, self.user)
     self.assertEqual(moderated_object.moderation_reason, "Reason")
예제 #27
0
 def test_reject_moderated_object(self):
     self.profile.description = 'New description'
     self.profile.save()
     
     moderated_object = ModeratedObject(content_object=self.profile)
     moderated_object.save()
     
     self.profile.description = "Old description"
     self.profile.save()
     
     moderated_object.reject(self.user)
     
     user_profile = UserProfile.objects.get(user__username='******')
     
     self.assertEqual(user_profile.description, "Old description")
     self.assertEqual(moderated_object.moderation_status,
                      MODERATION_STATUS_REJECTED)
예제 #28
0
    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))
예제 #29
0
    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()
예제 #30
0
    def _get_or_create_moderated_object(self, instance, unchanged_obj):
        """
        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)

            if moderated_object._is_not_equal_instance(instance):
                moderated_object.changed_object = unchanged_obj

        except ObjectDoesNotExist:
            moderated_object = ModeratedObject(content_object=unchanged_obj)
            moderated_object.changed_object = unchanged_obj

        return moderated_object
예제 #31
0
    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))
예제 #32
0
    def setUp(self):
        rf = RequestFactory()
        rf.login(username='******', password='******')
        self.request = rf.get('/admin/moderation/')
        self.request.user = User.objects.get(username='******')
        self.admin = ModerationAdmin(UserProfile, site)

        self.profile = UserProfile.objects.get(user__username='******')
        self.moderated_obj = ModeratedObject(content_object=self.profile)
        self.moderated_obj.save()
예제 #33
0
    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.admin = ModerationAdmin(UserProfile, site)

        self.profile = UserProfile.objects.get(user__username="******")
        self.moderated_obj = ModeratedObject(content_object=self.profile)
        self.moderated_obj.save()
예제 #34
0
    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()
            if instance.author and not moderator.is_auto_approve(instance, instance.author):
                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.moderation_reason = None
                moderated_obj.save()
                if moderated_obj.changed_by and not moderator.is_auto_approve(instance, moderated_obj.changed_by):
                    moderator.inform_moderator(instance)
                instance._moderated_object = moderated_obj
예제 #35
0
    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 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)
예제 #37
0
    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
예제 #38
0
    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)
예제 #39
0
    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')
예제 #40
0
    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')
예제 #41
0
    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')
예제 #42
0
    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
예제 #43
0
    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
예제 #44
0
 def get_new_instance(unchanged_obj):
     moderated_object = ModeratedObject(content_object=unchanged_obj)
     moderated_object.changed_object = unchanged_obj
     return moderated_object
예제 #45
0
 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")
예제 #46
0
class ModerationAdminSendMessageTestCase(TestCase):
    fixtures = ['test_users.json', 'test_moderation.json']

    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 tearDown(self):
        teardown_moderation()

    def test_send_message_when_object_has_no_moderated_object(self):
        profile = SuperUserProfile(description='Profile for new user',
                                   url='http://www.yahoo.com',
                                   user=User.objects.get(username='******'),
                                   super_power='text')

        profile.save()

        self.moderation.register(SuperUserProfile)

        self.admin.send_message(self.request, profile.pk)

        args, kwargs = self.request._messages.add.call_args
        level, message, tags = args
        self.assertEqual(str(message), "This object is not registered "
                                       "with the moderation system.")

    def test_send_message_status_pending(self):
        self.moderated_obj.status = MODERATION_STATUS_PENDING
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        args, kwargs = self.request._messages.add.call_args
        level, message, tags = args
        self.assertEqual(str(message),
                         "Object is not viewable on site, "
                         "it will be visible if moderator accepts it")

    def test_send_message_status_rejected(self):
        self.moderated_obj.status = MODERATION_STATUS_REJECTED
        self.moderated_obj.reason = 'Reason for rejection'
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        args, kwargs = self.request._messages.add.call_args
        level, message, tags = args
        self.assertEqual(str(message),
                         "Object has been rejected by "
                         "moderator, reason: Reason for rejection")

    def test_send_message_status_approved(self):
        self.moderated_obj.status = MODERATION_STATUS_APPROVED
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        args, kwargs = self.request._messages.add.call_args
        level, message, tags = args
        self.assertEqual(str(message), "Object has been approved by "
                                       "moderator and is visible on site")
예제 #47
0
class ModerationAdminSendMessageTestCase(SettingsTestCase):
    fixtures = ["test_users.json", "test_moderation.json"]
    urls = "moderation.tests.urls.default"
    test_settings = "moderation.tests.settings.generic"

    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.admin = ModerationAdmin(UserProfile, site)

        self.profile = UserProfile.objects.get(user__username="******")
        self.moderated_obj = ModeratedObject(content_object=self.profile)
        self.moderated_obj.save()

    def tearDown(self):
        teardown_moderation()

    def test_send_message_when_object_has_no_moderated_object(self):
        profile = SuperUserProfile(
            description="Profile for new user",
            url="http://www.yahoo.com",
            user=User.objects.get(username="******"),
            super_power="text",
        )

        profile.save()

        self.moderation.register(SuperUserProfile)

        self.admin.send_message(self.request, profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), u"This object is not registered " u"with the moderation system.")

    def test_send_message_status_pending(self):
        self.moderated_obj.moderation_status = MODERATION_STATUS_PENDING
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(
            unicode(message), u"Object is not viewable on site, " u"it will be visible if moderator accepts it"
        )

    def test_send_message_status_rejected(self):
        self.moderated_obj.moderation_status = MODERATION_STATUS_REJECTED
        self.moderated_obj.moderation_reason = u"Reason for rejection"
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), u"Object has been rejected by " u"moderator, reason: Reason for rejection")

    def test_send_message_status_approved(self):
        self.moderated_obj.moderation_status = MODERATION_STATUS_APPROVED
        self.moderated_obj.save()

        self.admin.send_message(self.request, self.profile.pk)

        message = self.request.user.message_set.get()
        self.assertEqual(unicode(message), "Object has been approved by " "moderator and is visible on site")
예제 #48
0
 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")
예제 #49
0
 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)