Example #1
0
    def __call__(self, environ):
        from django.conf import settings

        # Set up middleware if needed. We couldn't do this earlier, because
        # settings weren't available.
        if self._request_middleware is None:
            self.load_middleware()

        request_started.disconnect(close_old_connections)
        request_started.send(sender=self.__class__)
        request_started.connect(close_old_connections)
        request = WSGIRequest(environ)
        # sneaky little hack so that we can easily get round
        # CsrfViewMiddleware.  This makes life easier, and is probably
        # required for backwards compatibility with external tests against
        # admin views.
        request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
        response = self.get_response(request)
        # We're emulating a WSGI server; we must call the close method
        # on completion.
        if response.streaming:
            response.streaming_content = closing_iterator_wrapper(response.streaming_content, response.close)
        else:
            request_finished.disconnect(close_old_connections)
            response.close()  # will fire request_finished
            request_finished.connect(close_old_connections)

        return response
Example #2
0
def closing_iterator_wrapper(iterable, close):
    try:
        yield from iterable
    finally:
        request_finished.disconnect(close_old_connections)
        close()                                 # will fire request_finished
        request_finished.connect(close_old_connections)
Example #3
0
    def _load_backend(self):
        """Load the caching backend.

        This will replace the current caching backend with a newly loaded
        one, based on the stored cache name.

        Only one thread at a time can load the cache backend. A counter
        is kept that keeps the load generation number. If several threads
        try to reload the backend at once, only one will succeed in doing
        so for that generation.
        """
        cur_load_gen = self._load_gen

        with self._load_lock:
            if self._load_gen == cur_load_gen:
                try:
                    # Django >= 1.7
                    from django.core.cache import caches

                    self._backend = caches[self._cache_name]
                except ImportError:
                    # Django < 1.7
                    from django.core.cache import get_cache

                    self._backend = get_cache(self._cache_name)

                # get_cache will attempt to connect to 'close', which we don't
                # want. Instead, go and disconnect this.
                request_finished.disconnect(self._backend.close)

                self._load_gen = cur_load_gen + 1
Example #4
0
def closing_iterator_wrapper(iterable, close):
    try:
        for item in iterable:
            yield item
    finally:
        request_finished.disconnect(close_connection)
        close()                                 # will fire request_finished
        request_finished.connect(close_connection)
Example #5
0
    def request_finished(self, **kwargs):
        from raven.contrib.django.models import client

        if getattr(self, '_txid', None):
            client.transaction.pop(self._txid)
            self._txid = None

        request_finished.disconnect(self.request_finished)
Example #6
0
 def setUp(self):
     request_finished.disconnect(es.hold.process,
                                 dispatch_uid='process_es_tasks_on_finish')
     self.gia_mock = mock.patch(
         'mkt.developers.tasks.generate_image_assets').__enter__()
     self.fi_mock = mock.patch(
         'mkt.developers.tasks.fetch_icon').__enter__()
     self.user = self.get_user()
     assert self.client.login(username=self.user.email, password='password')
Example #7
0
    def setUp(self):
        request_finished.disconnect(es.hold.process, dispatch_uid="process_es_tasks_on_finish")
        super(BasePackagedAppTest, self).setUp()
        self.app = Webapp.objects.get(pk=337141)
        self.app.update(is_packaged=True)
        self.version = self.app.current_version
        self.file = self.version.all_files[0]
        self.file.update(filename="mozball.zip")

        self.package = self.packaged_app_path("mozball.zip")
        self.upload = self.get_upload(abspath=self.package)
        self.upload.update(name="mozball.zip", is_webapp=True)
        self.url = reverse("submit.app")
        assert self.client.login(username="[email protected]", password="password")
Example #8
0
    def setUp(self):
        request_finished.disconnect(es.hold.process,
                                    dispatch_uid='process_es_tasks_on_finish')
        super(BasePackagedAppTest, self).setUp()
        self.app = Webapp.objects.get(pk=337141)
        self.app.update(is_packaged=True)
        self.version = self.app.current_version
        self.file = self.version.all_files[0]
        self.file.update(filename='mozball.zip')

        self.package = self.packaged_app_path('mozball.zip')
        self.upload = self.get_upload(abspath=self.package)
        self.upload.update(name='mozball.zip', is_webapp=True)
        self.url = reverse('submit.app.manifest')
        assert self.client.login(username='[email protected]',
                                 password='password')
        # Complete first step.
        self.client.post(reverse('submit.app.terms'),
                         {'read_dev_agreement': True})
Example #9
0
def remove_tempfiles_callback(sender, **kwargs): 
    '''
    remove older files from wms tempdir
    '''
    request_finished.disconnect(remove_tempfiles_callback)
    
    delay = time.time() - 60*60*24 # 1 day delay
    
    wms_temp_dir = os.path.join(settings.MEDIA_ROOT,'warp','wms')
    for tempfile in os.listdir(wms_temp_dir):
        filePath = os.path.join(wms_temp_dir,tempfile)
        if os.path.getmtime(filePath) < delay:
            os.remove(filePath)
            
    warp_dir = os.path.join(settings.MEDIA_ROOT,'warp')
    for tempfile in os.listdir(warp_dir):
        if tempfile.endswith(('.json', '.geojson', '.xml', '.log')):
            filePath = os.path.join(warp_dir,tempfile)
            if os.path.getmtime(filePath) < delay: 
                os.remove(filePath)
Example #10
0
    def __call__(self, environ):
        # Set up middleware if needed. We couldn't do this earlier, because
        # settings weren't available.
        if self._middleware_chain is None:
            self.load_middleware()

        request_started.disconnect(close_old_connections)
        request_started.send(sender=self.__class__, environ=environ)
        request_started.connect(close_old_connections)
        request = WSGIRequest(environ)
        # sneaky little hack so that we can easily get round
        # CsrfViewMiddleware.  This makes life easier, and is probably
        # required for backwards compatibility with external tests against
        # admin views.
        request._dont_enforce_csrf_checks = not self.enforce_csrf_checks

        # Request goes through middleware.
        response = self.get_response(request)

        # Simulate behaviors of most Web servers.
        conditional_content_removal(request, response)

        # Attach the originating request to the response so that it could be
        # later retrieved.
        response.wsgi_request = request

        # We're emulating a WSGI server; we must call the close method
        # on completion.
        if response.streaming:
            response.streaming_content = closing_iterator_wrapper(
                response.streaming_content, response.close)
        else:
            request_finished.disconnect(close_old_connections)
            response.close()                    # will fire request_finished
            request_finished.connect(close_old_connections)

        return response
Example #11
0
from __future__ import unicode_literals
Example #12
0
 def setUp(self):
     # Disable the request_finished signal during this test
     # to avoid interfering with the database connection.
     request_finished.disconnect(close_old_connections)
Example #13
0
 def tearDown(self):
     request_started.disconnect(self.register_started)
     request_finished.disconnect(self.register_finished)
Example #14
0
def trigger_restart(**kwargs):
    from cms.signals import urls_need_reloading

    request_finished.disconnect(trigger_restart, dispatch_uid=DISPATCH_UID)
    urls_need_reloading.send(sender=None)
Example #15
0
os.environ['DJANGO_SETTINGS_MODULE'] = 'stoutsd.settings'

# Google App Engine imports.
from google.appengine.ext.webapp import util

# Force Django to reload its settings.
from django.conf import settings
settings._target = None

import logging
import django.core.handlers.wsgi
import django.core.signals
import django.db
from django.core.signals import request_started, request_finished

def log_exception (*args, **kwargs):
    logging.exception('Exception in request:')

# Log errors.
request_started.connect(log_exception)

# Unregister the rollback event handler.
request_finished.disconnect(django.db._rollback_on_exception)

def main():
    util.run_wsgi_app(django.core.handlers.wsgi.WSGIHandler())

if __name__ == '__main__':
    main()