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."
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)
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)
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)
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
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)
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)
def process_exception(self, request, exception): """Closes the revision.""" revision.invalidate()
def process_exception(self, request, exception): """Closes the revision.""" revision.invalidate()