Пример #1
0
    def test_get_unique_for_object_returns_appropriate_versions(self):
        user = User.objects.create(
            username='******' % random.randint(2, 100),
            email='*****@*****.**',
        )

        # make sure the random number is evenly divisible
        random_number_of_versions = abs(random.randint(2, 50) & (~0<<1)) 
        revision.register(User)
        user = User.objects.get(email='*****@*****.**')
        for i in range(random_number_of_versions/2):
            try:
                revision.start()
                user.username = '******' % i
                user.save()
            except:
                revision.invalidate()
            finally:
                revision.end()

        for i in range(random_number_of_versions/2, 0, -1):
            try:
                revision.start()
                user.username = '******' % i
                user.save()
            except:
                revision.invalidate()
            finally:
                revision.end()

        versions = Version.objects.get_unique_for_object(user)
        self.assertEqual(len(versions), random_number_of_versions)
Пример #2
0
 def test_get_deleted_object_raises_does_not_exist_on_none_available(self):
     user = User.objects.create(
         username='******' % random.randint(1, 100),
     )
     pk = user.pk
     user.delete()
     revision.register(User)
     self.assertRaises(Version.DoesNotExist, Version.objects.get_deleted_object, User, pk) 
Пример #3
0
 def _autoregister(self, model, follow=None):
     """Registers a model with reversion, if required."""
     if not revision.is_registered(model):
         follow = follow or []
         for parent_cls, field in model._meta.parents.items():
             if field:
                 # Proxy models do not have a parent field.
                 follow.append(field.name)
             self._autoregister(parent_cls)
         revision.register(model, follow=follow, format=self.reversion_format)
Пример #4
0
 def test_get_for_date_returns_version_if_one_is_available(self):
     revision.register(User)
     try:
         revision.start()
         user = User.objects.create(
             username='******' % random.randint(1, 100),
         )
     except:
         revision.invalidate()
     finally:
         revision.end()
     now = datetime.datetime.now()
     version = Version.objects.get_for_date(user, now) 
     self.assertTrue(version.revision.date_created <= now) 
Пример #5
0
    def handle(self, *apps, **options):
        get_module_and_target = lambda x : x.rsplit('.', 1)
        def load_module_and_target(mod, target):
            module = import_module(mod)
            target, pk = target.split(':')
            model = getattr(module, target)
            return model, pk 
        models = [load_module_and_target(module, target) for module, target in (get_module_and_target(app) for app in apps)]
        number = int(options['number'])
        comment = hashlib.md5(str(datetime.datetime.now())).hexdigest()
        print "About to create %d revisions of %d models." % (number, len(models))
        print "to remove these revisions, use Revision.objects.filter(comment='%s').delete()" % comment
        [revision.register(model) for model, creation in models]

        for i in range(number):
            for model, pk in models:
                revision.start()
                try:
                    revision.comment = comment
                    model = model.objects.get(pk=pk)
                    model.save()
                    sys.stdout.write('.')
                except:
                    revision.invalidate()
                    raise
                finally:
                    revision.end()
            sys.stdout.flush()
        print "finished."
Пример #6
0
 def test_get_for_object_returns_appropriate_versions(self):
     user = User.objects.create(
         username='******' % random.randint(1, 100),
         email='*****@*****.**',
     )
     random_number_of_versions = random.randint(1, 100)
     revision.register(User)
     for i in range(random_number_of_versions):
         try:
             revision.start()
             user.username = '******' % i
             user.save()
         except:
             revision.invalidate()
         finally:
             revision.end()
     versions = Version.objects.get_for_object(user)
     self.assertEqual(len(versions), random_number_of_versions)
Пример #7
0
        def setUp(self):
            """Sets up a versioned site model to test."""
            revision.register(Site)
            try:
                revision.start()
                site = Site.objects.create(name="site", domain="www.site-rev-1.com")
            except:
                revision.invalidate()
            finally:
                revision.end()

            try:
                revision.start()
                site.domain = "www.site-rev-2.com"
                site.save()
            except:
                revision.invalidate()
            finally:
                revision.end()

            self.site = site
Пример #8
0
 def test_get_deleted_object_returns_version_if_one_is_available(self):
     revision.register(User)
     pk = None
     try:
         revision.start()
         user = User.objects.create(
             username='******' % random.randint(1, 100),
         )
         pk = user.pk
     except:
         revision.invalidate()
     finally:
         revision.end()
     try:
         revision.start()
         user.delete()
     except:
         revision.invalidate()
     finally:
         revision.end()
     version = Version.objects.get_deleted_object(User, pk)
     self.assertEqual(version.object_version.object.pk, pk) 
Пример #9
0
    def test_get_deleted_returns_appropriate_list_of_deleted_objects_for_model(self):
        revision.register(User)
        random_number_of_users = random.randint(1, 100)
        for i in range(random_number_of_users):
            user = None
            try:
                revision.start()
                user = User.objects.create(
                    username='******' % i
                )
            except:
                revision.invalidate()
            finally:
                revision.end()
        try:
            revision.start()
            User.objects.all().delete()
        except:
            revision.invalidate()
        finally:
            revision.end()

        deleted = Version.objects.get_deleted(User)
        self.assertEqual(len(deleted), random_number_of_users)
Пример #10
0
import datetime

from django.db import models
from django.contrib.auth.models import User

from reversion_relations.fields import ReversionForeignKey
from reversion.revisions import revision

class Supplier(models.Model):
    name = models.CharField(max_length=100)
    location = models.TextField(blank=True)
    salary = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True)
    starred = models.BooleanField(default=False, blank=True)
    points = models.IntegerField(default=0, blank=True)
    date_foundation = models.DateField(blank=True, null=True)

    def __unicode__(self):
        return self.name

class Purchase(models.Model):
    date = models.DateTimeField(blank=True, default=datetime.datetime.now)
    supplier = ReversionForeignKey(Supplier, null=True, blank=True)
    user = models.ForeignKey(User, null=True, blank=True)

if not revision.is_registered(Supplier): revision.register(Supplier)
if not revision.is_registered(Purchase): revision.register(Purchase)

Пример #11
0
 def test_get_for_date_raises_does_not_exist_on_none_available(self):
     user = User.objects.create(
         username='******' % random.randint(1, 100),
     )
     revision.register(User)
     self.assertRaises(Version.DoesNotExist, Version.objects.get_for_date, user, datetime.datetime.now())