Пример #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."
Пример #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()
Пример #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)
Пример #4
0
    def test_process_exception_invalidates_revision(self):
        anything, anything_else = random.randint(1, 100), random.randint(1, 100)
        middleware = RevisionMiddleware()
        random_number_of_starts = random.randint(1, 100)
        for i in range(random_number_of_starts):
            revision.start()

        middleware.process_exception(anything, anything_else)
        self.assertTrue(revision._state.is_invalid)
Пример #5
0
    def test_process_response_leaves_revision_in_empty_state(self):
        anything, anything_else = random.randint(1, 100), random.randint(1, 100)
        middleware = RevisionMiddleware()
        random_number_of_starts = random.randint(1, 100)
        for i in range(random_number_of_starts):
            revision.start()

        middleware.process_response(anything, anything_else)
        self.assertEqual(revision._state.depth, 0)
Пример #6
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) 
Пример #7
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)
Пример #8
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
Пример #9
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
Пример #10
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) 
Пример #11
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()
Пример #12
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)
Пример #13
0
 def process_request(self, request):
     """Starts a new revision."""
     revision.start()
     if request.user.is_authenticated():
         revision.user = request.user
Пример #14
0
 def process_request(self, request):
     """Starts a new revision."""
     revision.start()
     if request.user.is_authenticated():
         revision.user = request.user