예제 #1
0
    def ready(self):
        from .patches import json
        json.patch()

        from djangae.db.backends.appengine.caching import reset_context
        from django.core.signals import request_finished, request_started

        request_finished.connect(reset_context, dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context, dispatch_uid="request_started_context_reset")

        from django.conf import settings
        contenttype_configuration_error = ImproperlyConfigured(
            "If you're using django.contrib.contenttypes, then you need "
            "to add djangae.contrib.contenttypes to INSTALLED_APPS after "
            "django.contrib.contenttypes."
        )
        if 'django.contrib.contenttypes' in settings.INSTALLED_APPS:
            from django.db import router, connections
            from django.contrib.contenttypes.models import ContentType
            conn = connections[router.db_for_read(ContentType)]

            if conn.settings_dict.get("ENGINE") != 'djangae.db.backends.appengine':
                # Don't enforce djangae.contrib.contenttypes if content types are being
                # saved to a different database backend
                return

            if not 'djangae.contrib.contenttypes' in settings.INSTALLED_APPS:
                # Raise error if User is using Django CT, but not Djangae
                raise contenttype_configuration_error
            else:
                if settings.INSTALLED_APPS.index('django.contrib.contenttypes') > \
                        settings.INSTALLED_APPS.index('djangae.contrib.contenttypes'):
                    # Raise error if User is using both Django and Djangae CT, but
                    # Django CT comes after Djangae CT
                    raise contenttype_configuration_error
예제 #2
0
def defer_celery_task(task, **task_kwargs):
    if django_settings.CELERY_ALWAYS_EAGER:
        task.apply_async(**task_kwargs)
    else:
        def schedule_task(sender, **kwargs):
            task.apply_async(**task_kwargs)
        request_finished.connect(schedule_task)
예제 #3
0
 def __init__(self):
     """Initializes the search context."""
     self._request_active = False
     self._stack = []
     # Connect to the signalling framework.
     request_started.connect(self._request_started_receiver)
     request_finished.connect(self._request_finished_receiver)
예제 #4
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
예제 #5
0
파일: client.py 프로젝트: CodeMonk/django
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)
예제 #6
0
파일: autoreload.py 프로젝트: Bouke/django
def inotify_code_changed():
    """
    Checks for changed code using inotify. After being called
    it blocks until a change event has been fired.
    """
    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm)

    def update_watch(sender=None, **kwargs):
        mask = (
            pyinotify.IN_MODIFY |
            pyinotify.IN_DELETE |
            pyinotify.IN_ATTRIB |
            pyinotify.IN_MOVED_FROM |
            pyinotify.IN_MOVED_TO |
            pyinotify.IN_CREATE
        )
        for path in gen_filenames():
            wm.add_watch(path, mask)

    # New modules may get imported when a request is processed.
    request_finished.connect(update_watch)

    # Block until an event happens.
    update_watch()
    notifier.check_events(timeout=None)
    notifier.stop()

    # If we are here the code must have changed.
    return True
예제 #7
0
def apphook_post_title_checker(instance, **kwargs):
    """
    Check if application_urls and path changed on the instance
    """
    if instance.publisher_is_draft:
        return
    old_title = getattr(instance, '_old_data', None)
    if not old_title:
        if instance.page.application_urls:
            request_finished.connect(
                trigger_restart,
                dispatch_uid=DISPATCH_UID
            )
    else:
        old_values = (
            old_title.published,
            old_title.page.application_urls,
            old_title.page.application_namespace,
            old_title.path,
            old_title.slug,
        )
        new_values = (
            instance.published,
            instance.page.application_urls,
            instance.page.application_namespace,
            instance.path,
            instance.slug,
        )
        if old_values != new_values and (old_values[2] or new_values[2]):
            request_finished.connect(trigger_restart, dispatch_uid=DISPATCH_UID)
예제 #8
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)
예제 #9
0
def apphook_post_delete_title_checker(instance, **kwargs):
    """
    Check if this was an apphook
    """
    from cms.cache import invalidate_cms_page_cache
    invalidate_cms_page_cache()
    if instance.page.application_urls:
        request_finished.connect(trigger_restart, dispatch_uid=DISPATCH_UID)
예제 #10
0
def closing_iterator_wrapper(iterable, close):
    try:
        for item in iterable:
            yield item
    finally:
        request_finished.disconnect(close_old_connections)
        close()  # will fire request_finished
        request_finished.connect(close_old_connections)
예제 #11
0
 def save(self, *args, **kwargs):
     super(Video, self).save(*args, **kwargs)
     if not hasattr(self, 'signal_uuid') and not self.has_initial_video:
         self.signal_uuid = uuid.uuid4()
         request_finished.connect(self.encode_video,
                                  weak=False,
                                  dispatch_uid='encode_video_%s' %
                                  self.signal_uuid)
예제 #12
0
def apphook_post_delete_title_checker(instance, **kwargs):
    """
    Check if this was an apphook
    """
    from cms.views import invalidate_cms_page_cache
    invalidate_cms_page_cache()
    if instance.page.application_urls:
        request_finished.connect(trigger_restart, dispatch_uid=DISPATCH_UID)
예제 #13
0
def defer_celery_task(task, **task_kwargs):
    if django_settings.CELERY_ALWAYS_EAGER:
        task.apply_async(**task_kwargs)
    else:

        def schedule_task(sender, **kwargs):
            task.apply_async(**task_kwargs)

        request_finished.connect(schedule_task)
예제 #14
0
파일: apps.py 프로젝트: MihailRussu/djangae
    def ready(self):
        from .patches.contenttypes import patch
        patch(sender=self)

        from djangae.db.backends.appengine.caching import reset_context
        from django.core.signals import request_finished, request_started

        request_finished.connect(reset_context, dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context, dispatch_uid="request_started_context_reset")
예제 #15
0
def apphook_post_page_checker(page):
    old_page = page.old_page
    if (old_page and (
                old_page.application_urls != page.application_urls or old_page.application_namespace != page.application_namespace)) or (
            not old_page and page.application_urls):

        from cms.cache import invalidate_cms_page_cache
        invalidate_cms_page_cache()
        request_finished.connect(trigger_restart, dispatch_uid=DISPATCH_UID)
예제 #16
0
    def __init__(self, keyspace, connection, *args, **kwargs):
        super(CachedDict, self).__init__(*args, **kwargs)

        self.keyspace = keyspace
        self.conn = connection

        self.remote_cache_key = 'RedisDict:%s' % (keyspace,)
        self.remote_cache_last_updated_key = 'RedisDict.last_updated:%s' % (keyspace,)

        request_finished.connect(self._cleanup)
예제 #17
0
def apphook_post_page_checker(page):
    old_page = page.old_page
    if (old_page and
        (old_page.application_urls != page.application_urls
         or old_page.application_namespace != page.application_namespace)) or (
             not old_page and page.application_urls):

        from cms.cache import invalidate_cms_page_cache
        invalidate_cms_page_cache()
        request_finished.connect(trigger_restart, dispatch_uid=DISPATCH_UID)
    def ready(self):
        from .patches.contenttypes import patch
        patch(sender=self)

        from djangae.db.backends.appengine.caching import reset_context
        from django.core.signals import request_finished, request_started

        request_finished.connect(reset_context,
                                 dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context,
                                dispatch_uid="request_started_context_reset")
예제 #19
0
    def process_request(self, request):
        self._txid = None

        SentryMiddleware.thread.request = request
        # we utilize request_finished as the exception gets reported
        # *after* process_response is executed, and thus clearing the
        # transaction there would leave it empty
        # XXX(dcramer): weakref's cause a threading issue in certain
        # versions of Django (e.g. 1.6). While they'd be ideal, we're under
        # the assumption that Django will always call our function except
        # in the situation of a process or thread dying.
        request_finished.connect(self.request_finished, weak=False)
예제 #20
0
 def ready(self):
     from django.contrib.contenttypes.management import create_contenttypes
     post_migrate.disconnect(create_contenttypes)
     post_migrate.disconnect(
         dispatch_uid='django.contrib.auth.management.create_permissions')
     post_migrate.connect(idseq_update_content_type_handler)
     post_migrate.connect(create_contenttypes)
     post_migrate.connect(idseq_update_others_handler)
     if IS_DJADMIN:
         logger.info(f'signals__request_finished_handler__djadmin_ignore')
         return
     request_finished.connect(request_finished_handler)
예제 #21
0
    def ready(self):
        from .signals import log_action, save_queue
        from .actions import create_action, save_queue as do_save_queue
        from .deletion import handle_deleted_items

        log_action.connect(create_action, dispatch_uid='actrack_action')
        deleting.connect(handle_deleted_items,
                         dispatch_uid='actrack_mkdeleted')

        save_queue.connect(do_save_queue, dispatch_uid='actrack_save')
        request_finished.connect(do_save_queue,
                                 dispatch_uid='actrack_save_on_exit')
예제 #22
0
    def process_request(self, request):
        self._txid = None

        SentryMiddleware.thread.request = request
        # we utilize request_finished as the exception gets reported
        # *after* process_response is executed, and thus clearing the
        # transaction there would leave it empty
        # XXX(dcramer): weakref's cause a threading issue in certain
        # versions of Django (e.g. 1.6). While they'd be ideal, we're under
        # the assumption that Django will always call our function except
        # in the situation of a process or thread dying.
        request_finished.connect(self.request_finished, weak=False)
예제 #23
0
def defer_celery_task(task, **task_kwargs):
    if django_settings.CELERY_ALWAYS_EAGER:
        return task.apply(**task_kwargs)
    else:
        from celery import current_task
        if current_task:
            #TODO: look into task chains in celery
            return task.apply(**task_kwargs)

        def schedule_task(sender, **task_kwargs):
            task.apply_async(**task_kwargs)

        request_finished.connect(schedule_task)
예제 #24
0
def defer_celery_task(task, **task_kwargs):
    if django_settings.CELERY_ALWAYS_EAGER:
        return task.apply(**task_kwargs)
    else:
        from celery import current_task
        if current_task:
            #TODO: look into task chains in celery
            return task.apply(**task_kwargs)

        def schedule_task(sender, **task_kwargs):
            task.apply_async(**task_kwargs)

        request_finished.connect(schedule_task)
예제 #25
0
def inotify_code_changed():
    """
<<<<<<< HEAD
    Checks for changed code using inotify. After being called
=======
    Check for changed code using inotify. After being called
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
    it blocks until a change event has been fired.
    """
    class EventHandler(pyinotify.ProcessEvent):
        modified_code = None

        def process_default(self, event):
            if event.path.endswith('.mo'):
                EventHandler.modified_code = I18N_MODIFIED
            else:
                EventHandler.modified_code = FILE_MODIFIED

    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm, EventHandler())

    def update_watch(sender=None, **kwargs):
        if sender and getattr(sender, 'handles_files', False):
            # No need to update watches when request serves files.
            # (sender is supposed to be a django.core.handlers.BaseHandler subclass)
            return
        mask = (
            pyinotify.IN_MODIFY |
            pyinotify.IN_DELETE |
            pyinotify.IN_ATTRIB |
            pyinotify.IN_MOVED_FROM |
            pyinotify.IN_MOVED_TO |
            pyinotify.IN_CREATE |
            pyinotify.IN_DELETE_SELF |
            pyinotify.IN_MOVE_SELF
        )
        for path in gen_filenames(only_new=True):
            wm.add_watch(path, mask)

    # New modules may get imported when a request is processed.
    request_finished.connect(update_watch)

    # Block until an event happens.
    update_watch()
    notifier.check_events(timeout=None)
    notifier.read_events()
    notifier.process_events()
    notifier.stop()

    # If we are here the code must have changed.
    return EventHandler.modified_code
예제 #26
0
파일: apps.py 프로젝트: andreipetre/djangae
    def ready(self):
        from .patches import json
        json.patch()

        from djangae.db.backends.appengine.caching import reset_context
        from djangae.db.migrations.signals import check_migrations
        from django.core.signals import request_finished, request_started
        from django.db.models.signals import pre_migrate

        request_finished.connect(reset_context, dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context, dispatch_uid="request_started_context_reset")
        pre_migrate.connect(check_migrations, dispatch_uid="pre_migrate_check_connections")

        self._check_content_types()
예제 #27
0
 def tick(self):
     request_finished.connect(self.request_processed)
     self.update_watches()
     while True:
         if self.processed_request.is_set():
             self.update_watches()
             self.processed_request.clear()
         try:
             self.client.receive()
         except pywatchman.WatchmanError as ex:
             self.check_server_status(ex)
         else:
             for sub in list(self.client.subs.keys()):
                 self._check_subscription(sub)
         yield
예제 #28
0
def register_signal_handlers():
    # we must pass the save signal from the regular model through to the proxy
    # model, because the wagtail listener is attached to the proxy model, not
    # the regular model.
    m2m_changed.connect(product_category_m2m_changed_signal_handler,
                        sender=Product.categories.through)
    post_save.connect(product_post_save_signal_handler, sender=Product)
    post_delete.connect(product_post_delete_signal_handler, sender=Product)
    post_save.connect(category_change_handler, sender=Category)
    post_delete.connect(category_change_handler, sender=Category)
    if settings.HANDLE_STOCKRECORD_CHANGES:
        post_save.connect(stockrecord_change_handler, sender=StockRecord)
        post_delete.connect(stockrecord_post_delete_handler,
                            sender=StockRecord)
    request_finished.connect(update_index.synchronize_searchindex)
예제 #29
0
 def tick(self):
     request_finished.connect(self.request_processed)
     self.update_watches()
     while True:
         if self.processed_request.is_set():
             self.update_watches()
             self.processed_request.clear()
         try:
             self.client.receive()
         except pywatchman.WatchmanError as ex:
             self.check_server_status(ex)
         else:
             for sub in list(self.client.subs.keys()):
                 self._check_subscription(sub)
         yield
예제 #30
0
    def ready(self):
        if not native_settings.TRANSIFEX_TOKEN:
            logger.warning(
                'Credentials for Transifex not found. Retrieving localized '
                'content will not be available; instead, source strings will'
                ' be displayed.')
            fetch_translations = False
        else:
            fetch_translations = any([
                # Start when forced
                (os.getenv('FORCE_TRANSLATIONS_SYNC', False) == 'true'),
                # Start for local development
                _segments_match(['manage.py', 'runserver'], sys.argv),
                # Start for gunicorn
                _segments_match(['gunicorn'], sys.argv),
            ])

        # Convert from [(<lang_code>, <name>), ...]
        # to [<locale>, ...]
        # e.g. from [('en-us', 'English (USA)'), ('fr-fr', 'French (France)')]
        # to ['en_US', 'fr_FR']
        languages = [to_locale(item[0]) for item in native_settings.LANGUAGES]

        # Create the missing policy lazily to avoid import issues
        # in Django settings files
        missing_policy = parse_rendering_policy(
            native_settings.TRANSIFEX_MISSING_POLICY)
        error_policy = parse_error_policy(
            native_settings.TRANSIFEX_ERROR_POLICY)
        init(native_settings.TRANSIFEX_TOKEN,
             languages,
             secret=native_settings.TRANSIFEX_SECRET,
             cds_host=native_settings.TRANSIFEX_CDS_HOST,
             missing_policy=missing_policy,
             error_policy=error_policy)

        if fetch_translations:
            logger.info('Fetching translations for languages: {}'.format(
                ', '.join(languages)))
            tx.fetch_translations()
            logger.info('Starting daemon for OTA translations update')

            sync_interval = native_settings.TRANSIFEX_SYNC_INTERVAL or 10 * 60
            daemon.start_daemon(interval=sync_interval)
            request_finished.connect(daemon.is_daemon_running)
        else:
            logger.info(
                'Starting up without fetching translations or OTA updates')
def inotify_code_changed():
    """
    Checks for changed code using inotify. After being called
    it blocks until a change event has been fired.
    """

    class EventHandler(pyinotify.ProcessEvent):
        modified_code = None

        def process_default(self, event):
            if event.path.endswith('.mo'):
                EventHandler.modified_code = I18N_MODIFIED
            else:
                EventHandler.modified_code = FILE_MODIFIED

    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm, EventHandler())

    def update_watch(sender=None, **kwargs):
        if sender and getattr(sender, 'handles_files', False):
            # No need to update watches when request serves files.
            # (sender is supposed to be a django.core.handlers.BaseHandler subclass)
            return
        mask = (
            pyinotify.IN_MODIFY |
            pyinotify.IN_DELETE |
            pyinotify.IN_ATTRIB |
            pyinotify.IN_MOVED_FROM |
            pyinotify.IN_MOVED_TO |
            pyinotify.IN_CREATE |
            pyinotify.IN_DELETE_SELF |
            pyinotify.IN_MOVE_SELF
        )
        for path in gen_filenames(only_new=True):
            wm.add_watch(path, mask)

    # New modules may get imported when a request is processed.
    request_finished.connect(update_watch)

    # Block until an event happens.
    update_watch()
    notifier.check_events(timeout=None)
    notifier.read_events()
    notifier.process_events()
    notifier.stop()

    # If we are here the code must have changed.
    return EventHandler.modified_code
예제 #32
0
    def __init__(self, model, key='pk', value=None, instances=False):
        assert value is not None

        self._cache = None
        self._last_updated = None

        self.model = model
        self.key = key
        self.value = value
        self.instances = instances

        self.cache_key = 'ModelDict:%s:%s' % (model.__name__, key)
        self.last_updated_cache_key = 'ModelDict.last_updated:%s:%s' % (model.__name__, key)
        request_finished.connect(self._cleanup)
        post_save.connect(self._post_save, sender=model)
        post_delete.connect(self._post_delete, sender=model)
예제 #33
0
    def do_benchmark():
        start_times = []
        times = []

        def handle_started(*args, **kwargs):
            start_times.append(time())

        def handle_finished(*args, **kwargs):
            times.append(time() - start_times[-1])

        request_started.connect(handle_started)
        request_finished.connect(handle_finished)
        try:
            yield times
        finally:
            request_started.disconnect(handle_started)
            request_finished.disconnect(handle_finished)
예제 #34
0
    def ready(self):
        from .patches import json
        json.patch()

        from djangae.db.backends.appengine.caching import reset_context
        from djangae.db.migrations.signals import check_migrations
        from django.core.signals import request_finished, request_started
        from django.db.models.signals import pre_migrate

        request_finished.connect(reset_context,
                                 dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context,
                                dispatch_uid="request_started_context_reset")
        pre_migrate.connect(check_migrations,
                            dispatch_uid="pre_migrate_check_connections")

        self._check_content_types()
예제 #35
0
 def tick(self):
     request_finished.connect(self.request_processed)
     self.update_watches()
     while True:
         if self.processed_request.is_set():
             self.update_watches()
             self.processed_request.clear()
         try:
             self.client.receive()
         except pywatchman.SocketTimeout:
             pass
         except pywatchman.WatchmanError as ex:
             logger.debug('Watchman error: %s, checking server status.', ex)
             self.check_server_status(ex)
         else:
             for sub in list(self.client.subs.keys()):
                 self._check_subscription(sub)
         yield
예제 #36
0
    def __init__(self, model, key="pk", value=None, instances=False, auto_create=False, *args, **kwargs):
        assert value is not None

        super(ModelDict, self).__init__(*args, **kwargs)

        self.key = key
        self.value = value

        self.model = model
        self.instances = instances
        self.auto_create = auto_create

        self.cache_key = "ModelDict:%s:%s" % (model.__name__, self.key)
        self.last_updated_cache_key = "ModelDict.last_updated:%s:%s" % (model.__name__, self.key)

        request_finished.connect(self._cleanup)
        post_save.connect(self._post_save, sender=model)
        post_delete.connect(self._post_delete, sender=model)
예제 #37
0
 def tick(self):
     request_finished.connect(self.request_processed)
     self.update_watches()
     while True:
         if self.processed_request.is_set():
             self.update_watches()
             self.processed_request.clear()
         try:
             self.client.receive()
         except pywatchman.SocketTimeout:
             pass
         except pywatchman.WatchmanError as ex:
             logger.debug('Watchman error: %s, checking server status.', ex)
             self.check_server_status(ex)
         else:
             for sub in list(self.client.subs.keys()):
                 self._check_subscription(sub)
         yield
예제 #38
0
def download_file(request, file_id):
    file_to_dl = [f for f in request.session['file_list']\
    if f.file_id == int(file_id)][0]

    f = NamedTemporaryFile(delete=False)

    def test_signal(sender, **kwargs):
        os.remove(f.name)

    request_finished.connect(test_signal, weak=False)
    request_finished.disconnect(test_signal)

    if file_to_dl.metadata_labels:
        f.write('<' + ' '.join(file_to_dl.metadata_labels) + '>\n')

    for text in file_to_dl.texts:
        if text.metadata:
            f.write('<' + ' '.join(text.metadata) + '>')
            f.write('\n')
        for sentence in text.sentences:
            for token in sentence.tokens:
                f.write(token.text_id + '\t' + token.token_id + '\t' +
                        token.form + '\t' + token.norm + '\t' + token.lemma +
                        '\t' + token.upos + '\t' + token.xpos + '\t' +
                        token.feats + '\t' + token.ufeats + '\t' + token.head +
                        '\t' + token.deprel + '\t' + token.deps + '\t' +
                        token.misc.strip() + '\n')
            f.write('\n')

    f.close()

    try:
        UploadedFile.objects.create(md5_checksum=get_md5(file_to_dl),
                                    normalized=file_to_dl.normalized)
    except:
        pass

    response = HttpResponse(FileWrapper(open(f.name)),
                            content_type='application/force-download')

    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(
        file_to_dl.filename)

    return response
예제 #39
0
    def process_view(self, request, func, args, kwargs):
        from raven.contrib.django.models import client

        try:
            self._txid = client.transaction.push(
                client.get_transaction_from_request(request))
        except Exception as exc:
            client.error_logger.exception(repr(exc))
        else:
            # we utilize request_finished as the exception gets reported
            # *after* process_response is executed, and thus clearing the
            # transaction there would leave it empty
            # XXX(dcramer): weakref's cause a threading issue in certain
            # versions of Django (e.g. 1.6). While they'd be ideal, we're under
            # the assumption that Django will always call our function except
            # in the situation of a process or thread dying.
            request_finished.connect(self.request_finished, weak=False)

        return None
예제 #40
0
def apphook_post_title_checker(instance, **kwargs):
    """
    Check if applciation_urls and path changed on the instance
    """
    if instance.publisher_is_draft:
        return
    old_title = getattr(instance, '_old_data', None)
    if not old_title:
        if instance.page.application_urls:
            request_finished.connect(trigger_restart,
                                     dispatch_uid=DISPATCH_UID)
    else:
        old_values = (old_title.published, old_title.page.application_urls,
                      old_title.page.application_namespace, old_title.path)
        new_values = (instance.published, instance.page.application_urls,
                      instance.page.application_namespace, instance.path)
        if old_values != new_values and (old_values[2] or new_values[2]):
            request_finished.connect(trigger_restart,
                                     dispatch_uid=DISPATCH_UID)
예제 #41
0
    def process_view(self, request, func, args, kwargs):
        from raven.contrib.django.models import client

        try:
            self._txid = client.transaction.push(
                self._get_transaction_from_request(request)
            )
        except Exception as exc:
            client.error_logger.exception(repr(exc))
        else:
            # we utilize request_finished as the exception gets reported
            # *after* process_response is executed, and thus clearing the
            # transaction there would leave it empty
            # XXX(dcramer): weakref's cause a threading issue in certain
            # versions of Django (e.g. 1.6). While they'd be ideal, we're under
            # the assumption that Django will always call our function except
            # in the situation of a process or thread dying.
            request_finished.connect(self.request_finished, weak=False)

        return None
예제 #42
0
    async def __call__(self, scope):
        # 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(is_async=True)
        # Extract body file from the scope, if provided.
        if "_body_file" in scope:
            body_file = scope.pop("_body_file")
        else:
            body_file = FakePayload("")

        request_started.disconnect(close_old_connections)
        await sync_to_async(request_started.send,
                            thread_sensitive=False)(sender=self.__class__,
                                                    scope=scope)
        request_started.connect(close_old_connections)
        request = ASGIRequest(scope, body_file)
        # 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 = await self.get_response_async(request)
        # Simulate behaviors of most Web servers.
        conditional_content_removal(request, response)
        # Attach the originating ASGI request to the response so that it could
        # be later retrieved.
        response.asgi_request = request
        # Emulate a server by calling the close method on completion.
        if response.streaming:
            response.streaming_content = await sync_to_async(
                closing_iterator_wrapper, thread_sensitive=False)(
                    response.streaming_content,
                    response.close,
                )
        else:
            request_finished.disconnect(close_old_connections)
            # Will fire request_finished.
            await sync_to_async(response.close, thread_sensitive=False)()
            request_finished.connect(close_old_connections)
        return response
예제 #43
0
파일: apps.py 프로젝트: palladius/djangae
    def ready(self):
        from .patches import json
        json.patch()

        from djangae.db.backends.appengine.caching import reset_context
        from django.core.signals import request_finished, request_started

        request_finished.connect(reset_context,
                                 dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context,
                                dispatch_uid="request_started_context_reset")

        from django.conf import settings
        contenttype_configuration_error = ImproperlyConfigured(
            "If you're using django.contrib.contenttypes, then you need "
            "to add djangae.contrib.contenttypes to INSTALLED_APPS after "
            "django.contrib.contenttypes.")
        if 'django.contrib.contenttypes' in settings.INSTALLED_APPS:
            from django.db import router, connections
            from django.contrib.contenttypes.models import ContentType
            conn = connections[router.db_for_read(ContentType)]

            if conn.settings_dict.get(
                    "ENGINE") != 'djangae.db.backends.appengine':
                # Don't enforce djangae.contrib.contenttypes if content types are being
                # saved to a different database backend
                return

            if not 'djangae.contrib.contenttypes' in settings.INSTALLED_APPS:
                # Raise error if User is using Django CT, but not Djangae
                raise contenttype_configuration_error
            else:
                if settings.INSTALLED_APPS.index('django.contrib.contenttypes') > \
                        settings.INSTALLED_APPS.index('djangae.contrib.contenttypes'):
                    # Raise error if User is using both Django and Djangae CT, but
                    # Django CT comes after Djangae CT
                    raise contenttype_configuration_error

        from django.core import checks
        from djangae import checks
예제 #44
0
def register_handlers(client):
    from django.core.signals import got_request_exception, request_started, request_finished
    from elasticapm.contrib.django.handlers import exception_handler

    # Connect to Django's internal signal handlers
    got_request_exception.disconnect(dispatch_uid=ERROR_DISPATCH_UID)
    got_request_exception.connect(partial(exception_handler, client),
                                  dispatch_uid=ERROR_DISPATCH_UID,
                                  weak=False)

    request_started.disconnect(dispatch_uid=REQUEST_START_DISPATCH_UID)
    request_started.connect(
        lambda sender, *args, **kwargs: client.begin_transaction("request")
        if _should_start_transaction(client) else None,
        dispatch_uid=REQUEST_START_DISPATCH_UID,
        weak=False,
    )

    request_finished.disconnect(dispatch_uid=REQUEST_FINISH_DISPATCH_UID)
    request_finished.connect(
        lambda sender, **kwargs: client.end_transaction()
        if _should_start_transaction(client) else None,
        dispatch_uid=REQUEST_FINISH_DISPATCH_UID,
        weak=False,
    )

    # If we can import celery, register ourselves as exception handler
    try:
        import celery  # noqa F401
        from elasticapm.contrib.celery import register_exception_tracking

        try:
            register_exception_tracking(client)
        except Exception as e:
            client.logger.exception(
                "Failed installing django-celery hook: %s" % e)
    except ImportError:
        client.logger.debug("Not instrumenting Celery, couldn't import")
예제 #45
0
파일: apps.py 프로젝트: adamchainz/djangae
    def ready(self):
        from djangae.db.backends.appengine.caching import reset_context
        from django.core.signals import request_finished, request_started

        request_finished.connect(reset_context, dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context, dispatch_uid="request_started_context_reset")

        from django.conf import settings
        contenttype_configuration_error = ImproperlyConfigured(
            "If you're using django.contrib.contenttypes, then you need "
            "to add djangae.contrib.contenttypes to INSTALLED_APPS after "
            "django.contrib.contenttypes."
        )
        if 'django.contrib.contenttypes' in settings.INSTALLED_APPS:
            if not 'djangae.contrib.contenttypes' in settings.INSTALLED_APPS:
                # Raise error if User is using Django CT, but not Djangae
                raise contenttype_configuration_error
            else:
                if settings.INSTALLED_APPS.index('django.contrib.contenttypes') > \
                        settings.INSTALLED_APPS.index('djangae.contrib.contenttypes'):
                    # Raise error if User is using both Django and Djangae CT, but
                    # Django CT comes after Djangae CT
                    raise contenttype_configuration_error
예제 #46
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
예제 #47
0
파일: client.py 프로젝트: aetos1918/Django
    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
예제 #48
0
    def __init__(self,
                 model,
                 key='pk',
                 value=None,
                 instances=False,
                 auto_create=False,
                 cache_version='1',
                 *args,
                 **kwargs):
        assert value is not None

        super(ModelDict, self).__init__(*args, **kwargs)

        cls_name = type(self).__name__
        model_name = model.__name__

        self.key = key
        self.value = value

        self.model = model
        self.instances = instances
        self.auto_create = auto_create

        version_suffix = 'py%s' % sys.version_info[0]

        self.remote_cache_key = '%s:%s:%s:%s:%s' % (
            cls_name, model_name, cache_version, self.key, version_suffix)
        self.remote_cache_last_updated_key = '%s.last_updated:%s:%s:%s' % (
            cls_name, model_name, cache_version, self.key)

        request_finished.connect(self._cleanup)
        post_save.connect(self._post_save, sender=model)
        post_delete.connect(self._post_delete, sender=model)

        if has_celery:
            task_postrun.connect(self._cleanup)
예제 #49
0
    def ready(self):
        from djangae.db.backends.appengine.caching import reset_context
        from django.core.signals import request_finished, request_started

        request_finished.connect(reset_context,
                                 dispatch_uid="request_finished_context_reset")
        request_started.connect(reset_context,
                                dispatch_uid="request_started_context_reset")

        from django.conf import settings
        contenttype_configuration_error = ImproperlyConfigured(
            "If you're using django.contrib.contenttypes, then you need "
            "to add djangae.contrib.contenttypes to INSTALLED_APPS after "
            "django.contrib.contenttypes.")
        if 'django.contrib.contenttypes' in settings.INSTALLED_APPS:
            if not 'djangae.contrib.contenttypes' in settings.INSTALLED_APPS:
                # Raise error if User is using Django CT, but not Djangae
                raise contenttype_configuration_error
            else:
                if settings.INSTALLED_APPS.index('django.contrib.contenttypes') > \
                        settings.INSTALLED_APPS.index('djangae.contrib.contenttypes'):
                    # Raise error if User is using both Django and Djangae CT, but
                    # Django CT comes after Djangae CT
                    raise contenttype_configuration_error
예제 #50
0
    def __init__(self, model, key='pk', value=None, instances=False, auto_create=False, *args, **kwargs):
        assert value is not None

        super(ModelDict, self).__init__(*args, **kwargs)

        cls_name = type(self).__name__
        model_name = model.__name__

        self.key = key
        self.value = value

        self.model = model
        self.instances = instances
        self.auto_create = auto_create

        self.remote_cache_key = '%s:%s:%s' % (cls_name, model_name, self.key)
        self.remote_cache_last_updated_key = '%s.last_updated:%s:%s' % (cls_name, model_name, self.key)

        request_finished.connect(self._cleanup)
        post_save.connect(self._post_save, sender=model)
        post_delete.connect(self._post_delete, sender=model)

        if has_celery:
            task_postrun.connect(self._cleanup)
예제 #51
0
def inotify_code_changed():
    """
    Checks for changed code using inotify. After being called
    it blocks until a change event has been fired.
    """
    class EventHandler(pyinotify.ProcessEvent):
        modified_code = None

        def process_default(self, event):
            if event.path.endswith('.mo'):
                EventHandler.modified_code = I18N_MODIFIED
            else:
                EventHandler.modified_code = FILE_MODIFIED

    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm, EventHandler())

    def update_watch(sender=None, **kwargs):
        mask = (pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_ATTRIB
                | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
                | pyinotify.IN_CREATE)
        for path in gen_filenames():
            wm.add_watch(path, mask)

    # New modules may get imported when a request is processed.
    request_finished.connect(update_watch)

    # Block until an event happens.
    update_watch()
    notifier.check_events(timeout=None)
    notifier.read_events()
    notifier.process_events()
    notifier.stop()

    # If we are here the code must have changed.
    return EventHandler.modified_code
예제 #52
0
def inotify_code_changed():
    """
    Checks for changed code using inotify. After being called
    it blocks until a change event has been fired.
    """
    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm)

    def update_watch(sender=None, **kwargs):
        mask = (pyinotify.IN_MODIFY | pyinotify.IN_DELETE | pyinotify.IN_ATTRIB
                | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO
                | pyinotify.IN_CREATE)
        for path in gen_filenames():
            wm.add_watch(path, mask)

    request_finished.connect(update_watch)
    update_watch()

    # Block forever
    notifier.check_events(timeout=None)
    notifier.stop()

    # If we are here the code must have changed.
    return True
예제 #53
0
파일: tests.py 프로젝트: 6r3nt/django
 def setUp(self):
     self.signals = []
     request_started.connect(self.register_started)
     request_finished.connect(self.register_finished)
예제 #54
0
from __future__ import unicode_literals
예제 #55
0
 def __init__(self, *args, **kwargs):
     super(UserOptionManager, self).__init__(*args, **kwargs)
     task_postrun.connect(self.clear_cache)
     request_finished.connect(self.clear_cache)
예제 #56
0
 def __init__(self, field_name, *args, **kwargs):
     super(InstanceMetaManager, self).__init__(*args, **kwargs)
     self.field_name = field_name
     task_postrun.connect(self.clear_cache)
     request_finished.connect(self.clear_cache)
예제 #57
0
파일: manager.py 프로젝트: bgyss/sentry
 def __init__(self, *args, **kwargs):
     super(MetaManager, self).__init__(*args, **kwargs)
     request_finished.connect(self.clear_cache)
예제 #58
0
                        "%s.process_template_response didn't return an "
                        "HttpResponse object. It returned None instead."
                        % (middleware_method.__self__.__class__.__name__)
                    )

            try:
                response = response.render()
            except Exception as e:
                response = self.process_exception_by_middleware(e, request)

        return response

    def process_exception_by_middleware(self, exception, request):
        """
        Pass the exception to the exception middleware. If no middleware
        return a response for this exception, raise it.
        """
        for middleware_method in self._exception_middleware:
            response = middleware_method(request, exception)
            if response:
                return response
        raise


def reset_urlconf(sender, **kwargs):
    """Reset the URLconf after each request is finished."""
    set_urlconf(None)


request_finished.connect(reset_urlconf)
예제 #59
0
파일: tests.py 프로젝트: ArcTanSusan/django
 def tearDown(self):
     request_finished.connect(close_old_connections)