<commit_before>from __future__ import absolute_import

import logging

from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect
from django.views.generic import View

from sentry.models import Organization, Project, Team
from sentry.web.helpers import get_login_url, render_to_response


class OrganizationMixin(object):
    def get_active_organization(self, request, organization_id=None,
                                access=None):
        """
        Returns the currently active organization for the request or None
        if no organization.
        """
        active_organization = None

        is_implicit = organization_id is None

        if is_implicit:
            organization_id = request.session.get('activeorg')

        if organization_id:
            try:
                organization_id = int(organization_id)
            except (TypeError, ValueError):
                if not is_implicit:
                    return None

        if organization_id is not None:
            if request.user.is_superuser:
                try:
                    active_organization = Organization.objects.get_from_cache(
                        id=organization_id,
                    )
                except Organization.DoesNotExist:
                    logging.info('Active organization [%s] not found',
                                 organization_id)
                    return None

        if active_organization is None:
            organizations = Organization.objects.get_for_user(
                user=request.user,
                access=access,
            )

        if active_organization is None and organization_id:
            try:
                active_organization = (
                    o for o in organizations
                    if o.id == organization_id
                ).next()
            except StopIteration:
                logging.info('Active organization [%s] not found in scope',
                             organization_id)
                if is_implicit:
                    del request.session['activeorg']
                active_organization = None

        if active_organization is None:
            if not is_implicit:
                return None

            print(organizations)
            try:
                active_organization = organizations[0]
            except IndexError:
                logging.info('User is not a member of any organizations')
                pass

        return active_organization

    def get_active_team(self, request, team_slug, access=None):
        """
        Returns the currently selected team for the request or None
        if no match.
        """
        if request.user.is_superuser:
            try:
                return Team.objects.get_from_cache(slug=team_slug)
            except Team.DoesNotExist:
                return None

        team_list = Team.objects.get_for_user(
            user=request.user,<commit_msg>Remove print<commit_after>from __future__ import absolute_import

import logging

from django.core.context_processors import csrf
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_protect
from django.views.generic import View

from sentry.models import Organization, Project, Team
from sentry.web.helpers import get_login_url, render_to_response


class OrganizationMixin(object):
    def get_active_organization(self, request, organization_id=None,
                                access=None):
        """
        Returns the currently active organization for the request or None
        if no organization.
        """
        active_organization = None

        is_implicit = organization_id is None

        if is_implicit:
            organization_id = request.session.get('activeorg')

        if organization_id:
            try:
                organization_id = int(organization_id)
            except (TypeError, ValueError):
                if not is_implicit:
                    return None

        if organization_id is not None:
            if request.user.is_superuser:
                try:
                    active_organization = Organization.objects.get_from_cache(
                        id=organization_id,
                    )
                except Organization.DoesNotExist:
                    logging.info('Active organization [%s] not found',
                                 organization_id)
                    return None

        if active_organization is None:
            organizations = Organization.objects.get_for_user(
                user=request.user,
                access=access,
            )

        if active_organization is None and organization_id:
            try:
                active_organization = (
                    o for o in organizations
                    if o.id == organization_id
                ).next()
            except StopIteration:
                logging.info('Active organization [%s] not found in scope',
                             organization_id)
                if is_implicit:
                    del request.session['activeorg']
                active_organization = None

        if active_organization is None:
            if not is_implicit:
                return None

            try:
                active_organization = organizations[0]
            except IndexError:
                logging.info('User is not a member of any organizations')
                pass

        return active_organization

    def get_active_team(self, request, team_slug, access=None):
        """
        Returns the currently selected team for the request or None
        if no match.
        """
        if request.user.is_superuser:
            try:
                return Team.objects.get_from_cache(slug=team_slug)
            except Team.DoesNotExist:
                return None

        team_list = Team.objects.get_for_user(
            user=request.user,
{'fraction_non_alphanumeric': 0.053617571059431525, 'fraction_numerical': 0.00032299741602067185, 'mean_word_length': 1.6682464454976302, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5654633', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1398, 'n_words': 403}