Ejemplo n.º 1
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."
Ejemplo n.º 2
0
def edit_object(object, field, format='rest'):
    revision.start()
    ext = {'rest':'rst','mdown':'md','txtile':'txt','html':'html','plain':'txt'}

    filename = tempfile.mktemp('.' + ext[format])
    original = getattr(object, field)
    open(filename,'w').write(original)
    mtime = os.path.getmtime(filename)

    p = subprocess.Popen((EDITOR, filename), stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
    while p.poll() is None:
        time.sleep(.2)
        ntime = os.path.getmtime(filename)
        if ntime > mtime:
            mtime = ntime
            text = open(filename).read()
            if text == original:
                continue
            setattr(object, field, text)
            object.save()

    text = open(filename).read()
    if text == original:
        return False
    setattr(object, field, text)
    object.save()
    revision.end()
Ejemplo n.º 3
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)
Ejemplo n.º 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) 
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
    def pre_save(self, model_instance, add):
        try:
            obj = model_instance._indirect_references.get(self, None)
        except AttributeError:
            obj = None

        if not obj: return None

        # Creates a version for current state
        revision.start()
        obj.save()
        revision.end()

        # Gets the latest version (the current one was created above)
        version = Version.objects.filter(
                content_type=self.indirect_content_type,
                object_id=obj.pk,
                ).latest('pk')

        # Returns version's id
        return version.pk
Ejemplo n.º 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
Ejemplo n.º 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) 
Ejemplo n.º 9
0
def edit_object(model, object):
    revision.start()

    filename = tempfile.mktemp('.rst')
    original = object_to_text(model, object)
    open(filename, 'w').write(original.encode('utf-8'))
    mtime = os.path.getmtime(filename)

    p = subprocess.Popen((EDITOR, filename), stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)

    while p.poll() is None:
        time.sleep(.2)
        ntime = os.path.getmtime(filename)
        if ntime > mtime:
            mtime = ntime
            text = open(filename).read().decode('utf-8')
            if text == original:
                continue
            original = text
            dct = object_from_text(model, text)
            if not dct:
                open('.blogit.bad', 'w').write(text.encode('utf-8'))
                print 'failed to parse'
            else:
                save_object(object, dct)
                newtext = object_to_text(model, object)
                open(filename, 'w').write(newtext.encode('utf-8'))

    text = open(filename).read()
    if text == original:
        return False
    dct = object_from_text(model, text)
    if not dct:
        open('.blogit.bad', 'w').write(text.encode('utf-8'))
        print 'failed to parse. saved text to ".blogit.bad"'
        return False
    else:
        save_object(object, dct)
    revision.end()
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
 def process_response(self, request, response):
     """Closes the revision."""
     while revision.is_active():
         revision.end()
     return response
Ejemplo n.º 12
0
 def tearDown(self):
     while revision.is_active():
         revision.end()
Ejemplo n.º 13
0
 def process_response(self, request, response):
     """Closes the revision."""
     if revision.is_active():
         revision.end()
     return response