Exemple #1
0
 def testRecoverDeleted(self):
     # Delete the test model.
     with create_revision():
         self.test11.delete()
     self.assertEqual(TestFollowModel.objects.count(), 0)
     self.assertEqual(ReversionTestModel1.objects.count(), 1)
     # Recover the test model.
     with create_revision():
         get_deleted(ReversionTestModel1)[0].revision.revert()
     # Make sure it was recovered.
     self.assertEqual(TestFollowModel.objects.count(), 1)
     self.assertEqual(ReversionTestModel1.objects.count(), 2)
 def testRecoverDeleted(self):
     # Delete the test model.
     with create_revision():
         self.test11.delete()
     self.assertEqual(TestFollowModel.objects.count(), 0)
     self.assertEqual(ReversionTestModel1.objects.count(), 1)
     # Recover the test model.
     with create_revision():
         get_deleted(ReversionTestModel1)[0].revision.revert()
     # Make sure it was recovered.
     self.assertEqual(TestFollowModel.objects.count(), 1)
     self.assertEqual(ReversionTestModel1.objects.count(), 2)
Exemple #3
0
 def testCanGetDeleted(self):
     with create_revision():
         self.test11.delete()
         self.test21.delete()
     # Test a model with an int pk.
     versions = get_deleted(ReversionTestModel1)
     self.assertEqual(len(versions), 1)
     self.assertEqual(versions[0].field_dict["name"], "model1 instance1 version2")
     # Test a model with a str pk.
     versions = get_deleted(ReversionTestModel2)
     self.assertEqual(len(versions), 1)
     self.assertEqual(versions[0].field_dict["name"], "model2 instance1 version2")
 def testCanGetDeleted(self):
     with create_revision():
         self.test11.delete()
         self.test21.delete()
     # Test a model with an int pk.
     versions = get_deleted(ReversionTestModel1)
     self.assertEqual(len(versions), 1)
     self.assertEqual(versions[0].field_dict["name"],
                      "model1 instance1 version2")
     # Test a model with a str pk.
     versions = get_deleted(ReversionTestModel2)
     self.assertEqual(len(versions), 1)
     self.assertEqual(versions[0].field_dict["name"],
                      "model2 instance1 version2")
Exemple #5
0
def log_object_removal(sender, instance, **kwargs):
    """Custom post-delete hook.

    We want to know who was responsible for an object deletion.
    """
    from reversion.models import Version

    if not reversion.is_registered(sender):
        return
    del_list = reversion.get_deleted(sender)
    try:
        version = del_list.get(object_id=instance.id)
    except Version.DoesNotExist:
        return
    logger = logging.getLogger("modoboa.admin")
    msg = _("%(object)s '%(name)s' %(action)s by ") % {
        "object": smart_text(version.content_type).capitalize(),
        "name": version.object_repr,
        "action": _("deleted")
    }
    request = get_request()
    if request:
        msg += _("user {}").format(request.user.username)
    else:
        msg += _("management command")
    logger.critical(msg)
Exemple #6
0
 def get_users(pk_list):
     users = []
     for pk in pk_list:
         try:
             u = HAWCUser.objects.get(pk=pk)
         except:
             deleted_users = reversion.get_deleted(HAWCUser)
             u = deleted_users.get(pk=pk)
         users.append(u.get_full_name())
     return '<br>'.join(sorted(users))
Exemple #7
0
 def test_that_can_safely_restore_deleted_object(self):
     model = None
     amount = Money(100, 'GHS')
     with reversion.create_revision():
         model = RevisionedModel.objects.create(amount=amount)
         model.save()
     model.delete()
     version = reversion.get_deleted(RevisionedModel)[0]
     version.revision.revert()
     model = RevisionedModel.objects.get(pk=1)
     self.assertEquals(model.amount, amount)
    def get_many_to_something(self, ids, related_model, is_reverse=False):

        # get instance of reversion.models.Revision():
        # A group of related object versions.
        old_revision = self.version.revision

        # Get a queryset with all related objects.
        versions = old_revision.version_set.filter(
            content_type=ContentType.objects.get_for_model(related_model),
            object_id__in=ids
        )

        if self.has_int_pk:
            # The primary_keys would be stored in a text field -> convert it to integers
            # This is interesting in different places!
            for version in versions:
                version.object_id = int(version.object_id)

        missing_objects = []
        missing_ids = []

        if self.field_name not in self.adapter.follow:
            # This models was not registered with follow relations
            # Try to fill missing related objects
            target_ids = set(ids)
            actual_ids = set([version.object_id for version in versions])
            missing_ids1 = target_ids.difference(actual_ids)

            # logger.debug(self.field_name, "target: %s - actual: %s - missing: %s" % (target_ids, actual_ids, missing_ids1))
            if missing_ids1:
                missing_objects = related_model.objects.all().filter(pk__in=missing_ids1)
                missing_ids = list(target_ids.difference(set(missing_objects.values_list('pk', flat=True))))

        deleted = []
        if is_reverse:
            true_missing_objects = []
            for o in missing_objects:
                for ver in reversion.get_for_object(o):
                    # An object can only be missing if it actually existed prior to this version
                    # Otherwise its a new item
                    if ver.revision.date_created < old_revision.date_created:
                        true_missing_objects.append(o)
            missing_objects = true_missing_objects
            deleted = [d for d in reversion.get_deleted(related_model) if d.revision == old_revision]
        return versions, missing_objects, missing_ids, deleted
    def test_revert_deleted_object(self):
        """
        After deleting an object with revision, I should be able to revert to the last non-deleted version.

        Unfortunately, the django-reversion code is a bit dirty at this point so the reversion.get_deleted(...)
        method call does not work out of the box.

        We would have to implement it manually when/if needed.
        """
        obj = SimpleObj.objects.skip_cdms().create(
            name='test', cdms_pk='cdms-pk'
        )
        obj_id = obj.pk
        self.reset_revisions()

        self.assertEqual(Version.objects.count(), 0)
        self.assertEqual(Revision.objects.count(), 0)

        obj.save()
        obj.delete()

        # check cdms calls
        self.assertEqual(self.mocked_cdms_api.get.call_count, 1)
        self.assertEqual(self.mocked_cdms_api.update.call_count, 1)
        self.assertEqual(self.mocked_cdms_api.delete.call_count, 1)
        self.assertAPINotCalled(['list', 'create'])

        self.mocked_cdms_api.reset_mock()

        # check versions
        self.assertEqual(Version.objects.count(), 1)
        self.assertEqual(Revision.objects.count(), 1)

        deleted_list = reversion.get_deleted(SimpleObj)
        delete_version = deleted_list.get(id=obj_id)

        # revert deleted version
        delete_version.revert()

        obj = SimpleObj.objects.skip_cdms().get(pk=obj_id)
        self.assertEqual(obj.name, 'test')
Exemple #10
0
def log_object_removal(sender, instance, **kwargs):
    """Custom post-delete hook.

    We want to know who was responsible for an object deletion.
    """
    from reversion.models import Version

    if not reversion.is_registered(sender):
        return
    del_list = reversion.get_deleted(sender)
    try:
        version = del_list.get(object_id=instance.id)
    except Version.DoesNotExist:
        return
    logger = logging.getLogger("modoboa.admin")
    msg = _("%(object)s '%(name)s' %(action)s by user %(user)s") % {
        "object": smart_text(version.content_type).capitalize(),
        "name": version.object_repr, "action": _("deleted"),
        "user": get_request().user.username
    }
    logger.critical(msg)
Exemple #11
0
def log_object_removal(sender, instance, **kwargs):
    """Custom post-delete hook.

    We want to know who was responsible for an object deletion.
    """
    from reversion.models import Version
    from modoboa.lib.signals import get_request

    if not reversion.is_registered(sender):
        return
    del_list = reversion.get_deleted(sender)
    try:
        version = del_list.get(object_id=instance.id)
    except Version.DoesNotExist:
        return
    logger = logging.getLogger("modoboa.admin")
    msg = _("%(object)s '%(name)s' %(action)s by user %(user)s") % {
        "object": unicode(version.content_type).capitalize(),
        "name": version.object_repr, "action": _("deleted"),
        "user": get_request().user.username
    }
    logger.critical(msg)