<commit_before>#!/usr/bin/env python
# Licensed to Cloudera, Inc. under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  Cloudera, Inc. licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from crequest.middleware import CrequestMiddleware


def default_organization():
  from useradmin.models import Organization
  default_organization, created = Organization.objects.get_or_create(name='default', domain='default')
  return default_organization


def get_user_request_organization():
  request = CrequestMiddleware.get_request()
  return request.user.organization if request and hasattr(request, 'user') and request.user.is_authenticated() else default_organization()


def _fitered_queryset(queryset, by_owner=False):
  request = CrequestMiddleware.get_request()

  # Avoid infinite recursion on very first retrieval of the user
  if request and hasattr(request, 'user') and hasattr(request.user, '_wrapped') and type(request.user._wrapped) is not object and request.user.is_authenticated():
    if by_owner:
      filters = {'owner__organization': request.user.organization}
    else:
      filters = {'organization': request.user.organization}

    queryset = queryset.filter(**filters)

  return queryset<commit_msg>HUE-8530 [organization] Avoid filtering querysets when off<commit_after>#!/usr/bin/env python
# Licensed to Cloudera, Inc. under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  Cloudera, Inc. licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from crequest.middleware import CrequestMiddleware

from desktop.conf import ENABLE_ORGANIZATIONS


def default_organization():
  from useradmin.models import Organization
  default_organization, created = Organization.objects.get_or_create(name='default', domain='default')
  return default_organization


def get_user_request_organization():
  request = CrequestMiddleware.get_request()
  return request.user.organization if request and hasattr(request, 'user') and request.user.is_authenticated() else default_organization()


def _fitered_queryset(queryset, by_owner=False):
  request = CrequestMiddleware.get_request()

  # Avoid infinite recursion on very first retrieval of the user
  if ENABLE_ORGANIZATIONS.get() and \
      request and hasattr(request, 'user') and hasattr(request.user, '_wrapped') and type(request.user._wrapped) is not object and \
      request.user.is_authenticated():
    if by_owner:
      filters = {'owner__organization': request.user.organization}
    else:
      filters = {'organization': request.user.organization}

    queryset = queryset.filter(**filters)

  return queryset
{'fraction_non_alphanumeric': 0.07829274679235403, 'fraction_numerical': 0.003142183817753339, 'mean_word_length': 4.959438377535101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2130073', 'n_tokens_mistral': 1029, 'n_tokens_neox': 983, 'n_words': 381}