def test_non_atomic_context_manager(self):
        from .test_connector import TestUser
        existing = TestUser.objects.create(username="******",
                                           field2="exists")

        with transaction.atomic():
            self.assertTrue(transaction.in_atomic_block())

            user = TestUser.objects.create(username="******", field2="bar")

            with transaction.non_atomic():
                # We're outside the transaction, so the user should not exist
                self.assertRaises(TestUser.DoesNotExist,
                                  TestUser.objects.get,
                                  pk=user.pk)
                self.assertFalse(transaction.in_atomic_block())

                with sleuth.watch(
                        "google.appengine.api.datastore.Get") as datastore_get:
                    TestUser.objects.get(
                        pk=existing.pk
                    )  #Should hit the cache, not the datastore

                self.assertFalse(datastore_get.called)

            with transaction.atomic(independent=True):
                user2 = TestUser.objects.create(username="******", field2="bar2")
                self.assertTrue(transaction.in_atomic_block())

                with transaction.non_atomic():
                    self.assertFalse(transaction.in_atomic_block())
                    self.assertRaises(TestUser.DoesNotExist,
                                      TestUser.objects.get,
                                      pk=user2.pk)

                    with transaction.non_atomic():
                        self.assertFalse(transaction.in_atomic_block())
                        self.assertRaises(TestUser.DoesNotExist,
                                          TestUser.objects.get,
                                          pk=user2.pk)

                        with sleuth.watch("google.appengine.api.datastore.Get"
                                          ) as datastore_get:
                            TestUser.objects.get(
                                pk=existing.pk
                            )  #Should hit the cache, not the datastore

                    self.assertFalse(transaction.in_atomic_block())
                    self.assertRaises(TestUser.DoesNotExist,
                                      TestUser.objects.get,
                                      pk=user2.pk)

                self.assertTrue(TestUser.objects.filter(pk=user2.pk).exists())
                self.assertTrue(transaction.in_atomic_block())
    def test_non_atomic_context_manager(self):
        from .test_connector import TestUser
        existing = TestUser.objects.create(username="******", field2="exists")

        with transaction.atomic():
            self.assertTrue(transaction.in_atomic_block())

            user = TestUser.objects.create(username="******", field2="bar")

            with transaction.non_atomic():
                # We're outside the transaction, so the user should not exist
                self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user.pk)
                self.assertFalse(transaction.in_atomic_block())

                with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get:
                    TestUser.objects.get(pk=existing.pk)  # Should hit the cache, not the datastore

                self.assertFalse(datastore_get.called)

            with transaction.atomic(independent=True):
                user2 = TestUser.objects.create(username="******", field2="bar2")
                self.assertTrue(transaction.in_atomic_block())

                with transaction.non_atomic():
                    self.assertFalse(transaction.in_atomic_block())
                    self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk)

                    with transaction.non_atomic():
                        self.assertFalse(transaction.in_atomic_block())
                        self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk)

                        with sleuth.watch("google.appengine.api.datastore.Get") as datastore_get:
                            # Should hit the cache, not the Datastore
                            TestUser.objects.get(pk=existing.pk)

                    self.assertFalse(transaction.in_atomic_block())
                    self.assertRaises(TestUser.DoesNotExist, TestUser.objects.get, pk=user2.pk)

                self.assertTrue(TestUser.objects.filter(pk=user2.pk).exists())
                self.assertTrue(transaction.in_atomic_block())
Beispiel #3
0
 def url(self, name):
     try:
         # Return a protocol-less URL, because django can't/won't pass
         # down an argument saying whether it should be secure or not
         with transaction.non_atomic():
             # This causes a Datastore lookup which we don't want to interfere with transactions
             return get_serving_url(self._get_blobinfo(name), secure_url=True)
     except (NotImageError, BlobKeyRequiredError, TransformationError, LargeImageError):
         # Django doesn't expect us to return None from this function, and in fact
         # relies on the "truthiness" of the return value when accessing .url on an
         # unsaved fieldfile. We just return the name which is effectively what Django
         # does in its default storage.
         return name
Beispiel #4
0
    def test_non_atomic_only(self):
        from .test_connector import TestFruit

        apple = TestFruit.objects.create(name="Apple", color="Red")
        apple.save()

        apple2 = TestFruit.objects.get(pk=apple.pk)

        with transaction.non_atomic():
            apple.delete()

        # Apple should no longer be in the cache!
        self.assertRaises(TestFruit.DoesNotExist, apple2.refresh_from_db)
Beispiel #5
0
    def build_base(self, instance):
        """Called by the model's post_save signal receiver when indexing an
        instance of that model.

        Args:
            instance: A Django model instance
        """
        self.pk = str(instance.pk)
        self.program = str(getattr(instance, "program_id", None))

        with transaction.non_atomic():
            self.build(instance)
            self.corpus = self.build_corpus(instance)
Beispiel #6
0
    def build_base(self, instance):
        """Called by the model's post_save signal receiver when indexing an
        instance of that model.

        Args:
            instance: A Django model instance
        """
        self.pk = str(instance.pk)
        self.program = str(getattr(instance, "program_id", None))

        with transaction.non_atomic():
            self.build(instance)
            self.corpus = self.build_corpus(instance)
    def test_non_atomic_only(self):
        from .test_connector import TestFruit

        apple = TestFruit.objects.create(name="Apple", color="Red")
        apple.save()

        apple2 = TestFruit.objects.get(pk=apple.pk)

        with transaction.non_atomic():
            apple.delete()

        # Apple should no longer be in the cache!
        self.assertRaises(TestFruit.DoesNotExist, apple2.refresh_from_db)
Beispiel #8
0
 def url(self, filename):
     try:
         # Return a protocol-less URL, because django can't/won't pass
         # down an argument saying whether it should be secure or not
         with transaction.non_atomic():
             # This causes a Datastore lookup which we don't want to interfere with transactions
             url = get_serving_url(self._get_blobkey(filename))
         return re.sub("http://", "//", url)
     except (TransformationError):
         # Sometimes TransformationError will be thrown if you call get_serving_url on video files
         # this is probably a bug in App Engine
         # Probably related to this: https://code.google.com/p/googleappengine/issues/detail?id=8601
         quoted_filename = urllib.quote(self._add_bucket(filename))
         return '{0}{1}'.format(self.api_url, quoted_filename)
Beispiel #9
0
 def url(self, name):
     try:
         # Return a protocol-less URL, because django can't/won't pass
         # down an argument saying whether it should be secure or not
         with transaction.non_atomic():
             # This causes a Datastore lookup which we don't want to interfere with transactions
             url = get_serving_url(self._get_blobinfo(name))
         return re.sub("http://", "//", url)
     except (NotImageError, BlobKeyRequiredError, TransformationError, LargeImageError):
         # Django doesn't expect us to return None from this function, and in fact
         # relies on the "truthiness" of the return value when accessing .url on an
         # unsaved fieldfile. We just return the name which is effectively what Django
         # does in its default storage.
         return name
Beispiel #10
0
    def url(self, filename):
        try:
            # Return a protocol-less URL, because django can't/won't pass
            # down an argument saying whether it should be secure or not
            with transaction.non_atomic():
                # This causes a Datastore lookup which we don't want to interfere with transactions
                return get_serving_url(self._get_blobkey(filename), secure_url=True)
        except (TransformationError, cloudstorage.NotFoundError):
            # Sometimes TransformationError will be thrown if you call get_serving_url on video files
            # this is probably a bug in App Engine
            # Probably related to this: https://code.google.com/p/googleappengine/issues/detail?id=8601

            # Also, Django requires that url() return something 'truthy' even if the file field hasn't been
            # saved yet so we do the same thing if the file is not found (just add the bucket to the filename)
            quoted_filename = urllib.quote(self._add_bucket(filename))
            return '{0}{1}'.format(self.api_url, quoted_filename)
Beispiel #11
0
    def url(self, filename):
        try:
            # Return a protocol-less URL, because django can't/won't pass
            # down an argument saying whether it should be secure or not
            with transaction.non_atomic():
                # This causes a Datastore lookup which we don't want to interfere with transactions
                return get_serving_url(self._get_blobkey(filename),
                                       secure_url=True)
        except (TransformationError, cloudstorage.NotFoundError):
            # Sometimes TransformationError will be thrown if you call get_serving_url on video files
            # this is probably a bug in App Engine
            # Probably related to this: https://code.google.com/p/googleappengine/issues/detail?id=8601

            # Also, Django requires that url() return something 'truthy' even if the file field hasn't been
            # saved yet so we do the same thing if the file is not found (just add the bucket to the filename)
            quoted_filename = urllib.quote(self._add_bucket(filename))
            return '{0}{1}'.format(self.api_url, quoted_filename)
Beispiel #12
0
 def _create_upload_url(self):
     # Creating the upload URL can't be atomic, otherwise the session
     # key will not be consistent when uploading the file
     with transaction.non_atomic():
         return create_upload_url(
             reverse('djangae_internal_upload_handler'))
Beispiel #13
0
 def _create_upload_url(self):
     # Creating the upload URL can't be atomic, otherwise the session
     # key will not be consistent when uploading the file
     with transaction.non_atomic():
         return create_upload_url(reverse('djangae_internal_upload_handler'))