Esempio n. 1
0
def pass_event_context(sender=None, body=None, **kwargs):
    if body is None:
        return

    event_context = get_event_context()
    if event_context:
        body['kwargs']['event_context'] = event_context
Esempio n. 2
0
def protected_url(value, field):
    if settings.DEBUG:
        return value.url
    context = get_event_context()
    return encode_protected_url(value.instance,
                                field,
                                user_uuid=context['user_uuid'])
Esempio n. 3
0
def protected_url(value, field):
    context = get_event_context()
    if not settings.USE_PROTECTED_URL:
        return value.url
    return encode_protected_url(value.instance,
                                field,
                                user_uuid=context['user_uuid'])
Esempio n. 4
0
    def compile_context(self, **kwargs):
        # Get a list of fields here in order to be sure all models already loaded.
        if not hasattr(self, 'fields'):
            self.fields = {
                k: self.get_field_model(v)
                for k, v in self.__class__.__dict__.items()
                if not k.startswith('_')
                and not isinstance(v, types.FunctionType) and k != 'Meta'
            }

        missed = set(self.fields.keys()) - set(
            self.get_nullable_fields()) - set(kwargs.keys())
        if missed:
            raise LoggerError("Missed fields in event context: %s" %
                              ', '.join(missed))

        context = {}

        event_context = get_event_context()
        if event_context:
            context.update(event_context)
            username = event_context.get('user_username')
            if 'user' in self.fields and username:
                logger.warning(
                    "User is passed directly to event context. "
                    "Currently authenticated user %s is ignored.", username)

        for entity_name, entity in six.iteritems(kwargs):
            if entity_name in self.fields:
                entity_class = self.fields[entity_name]
                if entity is None and entity_name in self.get_nullable_fields(
                ):
                    continue
                if not isinstance(entity, entity_class):
                    raise LoggerError(
                        "Field '%s' must be an instance of %s but %s received"
                        % (entity_name, entity_class.__name__,
                           entity.__class__.__name__))
            else:
                logger.error(
                    "Field '%s' cannot be used in logging context for %s",
                    entity_name, self.__class__.__name__)
                continue

            if isinstance(entity, LoggableMixin):
                context.update(entity._get_log_context(entity_name))
            elif isinstance(
                    entity,
                (int, float, six.string_types, dict, tuple, list, bool)):
                context[entity_name] = entity
            elif entity is None:
                pass
            else:
                context[entity_name] = six.text_type(entity)
                logger.warning("Cannot properly serialize '%s' context field. "
                               "Must be inherited from LoggableMixin." %
                               entity_name)

        return context
Esempio n. 5
0
def pass_event_context(sender=None, body=None, **kwargs):
    if body is None:
        return

    event_context = get_event_context()
    if event_context:
        # kwargs is the second item in body tuple with index equal 1.
        # See also http://docs.celeryproject.org/en/v4.1.0/internals/protocol.html#version-2
        body[1]['event_context'] = event_context
Esempio n. 6
0
def protected_url(value, field):
    context = get_event_context()
    if settings.USE_PROTECTED_URL:
        url = value.url
        if (
            settings.CONVERT_MEDIA_URLS_TO_MASTERMIND_NETLOC
        ):  # If using s3-compatible storage
            url = s3_to_waldur_media_url(url, context['request'])
        return url
    return encode_protected_url(value.instance, field, user_uuid=context['user_uuid'])