'Returns the currently active organization for the request or None
if no organization.'

def get_active_organization(self, request, organization_slug=None):

    active_organization = getattr(self, '_active_org', None)
    cached_active_org = (active_organization and (active_organization[0].slug == organization_slug) and (active_organization[1] == request.user))
    if cached_active_org:
        return active_organization[0]
    active_organization = None
    is_implicit = (organization_slug is None)
    if is_implicit:
        organization_slug = request.session.get('activeorg')
    if (organization_slug is not None):
        if request.is_superuser():
            try:
                active_organization = Organization.objects.get_from_cache(slug=organization_slug)
                if (active_organization.status != OrganizationStatus.VISIBLE):
                    raise Organization.DoesNotExist
            except Organization.DoesNotExist:
                logger.info('Active    organization    [%s]    not    found', organization_slug)
    if (active_organization is None):
        organizations = Organization.objects.get_for_user(user=request.user)
    if ((active_organization is None) and organization_slug):
        try:
            active_organization = six.next((o for o in organizations if (o.slug == organization_slug)))
        except StopIteration:
            logger.info('Active    organization    [%s]    not    found    in    scope', organization_slug)
            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:
            logger.info('User    is    not    a    member    of    any    organizations')
            pass
    if (active_organization and self._is_org_member(request.user, active_organization)):
        if (active_organization.slug != request.session.get('activeorg')):
            request.session['activeorg'] = active_organization.slug
    self._active_org = (active_organization, request.user)
    return active_organization
