コード例 #1
0
    def _convert_key(self, key, level=6):
        parts = key.split('__')
        for part in parts:
            if part in self.IGNORE:
                parts.remove(part)
        
        klass = self.model
        for part in parts:
            index = parts.index(part) + 1
            part = part.replace('-', '')
            try:
                field = klass._meta.get_field_by_name(part)[0]
            except models.fields.FieldDoesNotExist, e:
                mapper = getattr(klass, 'FIELD_MAP', {})
                if part not in mapper:
                    log.warning('Field not found: %s -> %s' % (part, klass))
                    raise e

                mapped = mapper.get(part)
                new = key.replace(part, mapped)
                if mapped.find('__') == 0:
                    new = key.replace(part, klass.get_field_mapper(key=part))
                caller = utils.get_caller(level=level)
                log.debug('[%s] Field mapped: %s => %s' % (caller, key, new))
                return new

            # The order of these checks is important
            if getattr(field, 'rel', False):
                klass = field.rel.to
            elif hasattr(field, 'model'):
                klass = field.model
            else:
                raise Exception
コード例 #2
0
    def _inject(self, *args, **kwargs):
        found = False
        def check(query, seek):
            for child in getattr(query, 'children', []):
                if isinstance(child, models.Q):
                    return check(query=child)
                else:
                    d = dict((child,))
                    for key in d:
                        if not not ~key.find(seek):
                            return True
            return False

        if ENABLE_INJECTION:
            mapping = getattr(self.model, 'INJECTION_MAP', {})
            for seek in mapping:
                replace = mapping.get(seek)

                for arg in args:
                    found = check(query=arg, seek=seek)
                    if found:
                        break

                for key in kwargs:
                    if not not ~key.find(seek):
                        found = True
                        break

                if not found:
                    value = self.model.get_injection_value(field=seek)

                    if replace.find('__') == 0:
                        key = '%s%s' % (self.model.get_injection_prefix(), replace)
                    else:
                        key = replace

                    # Kwargs have a higher priority, only use args if they were used instead
                    if args and not kwargs:
                        args += (models.Q(**{key: value}),)
                    else:
                        kwargs[key] = value
                    
                    caller = utils.get_caller(level=5)
                    log.debug('[%s] Query injected: %s = %s' % (caller, key, value))

        return args, kwargs