<commit_before># -*- coding: utf-8 -*-
import itertools
import httplib as http
import logging
import math
import urllib

from modularodm import Q
from modularodm.exceptions import NoResultsFound
from flask import request

from framework import utils, sentry
from framework.auth.decorators import must_be_logged_in
from framework.auth.forms import SignInForm, ForgotPasswordForm
from framework.exceptions import HTTPError
from framework.flask import redirect  # VOL-aware redirect
from framework.forms import utils as form_utils
from framework.routing import proxy_url
from website.institutions.views import view_institution

from website.models import Guid
from website.models import Node, Institution
from website.project import new_bookmark_collection
from website.settings import INSTITUTION_DISPLAY_NODE_THRESHOLD
from website.util import permissions

logger = logging.getLogger(__name__)


def _render_node(node, auth=None):
    """

    :param node:
    :return:

    """
    perm = None
    # NOTE: auth.user may be None if viewing public project while not
    # logged in
    if auth and auth.user and node.get_permissions(auth.user):
        perm_list = node.get_permissions(auth.user)
        perm = permissions.reduce_permissions(perm_list)

    return {
        'title': node.title,
        'id': node._primary_key,
        'url': node.url,
        'api_url': node.api_url,
        'primary': node.primary,
        'date_modified': utils.iso8601format(node.date_modified),
        'category': node.category,
        'permissions': perm,  # A string, e.g. 'admin', or None,
        'archiving': node.archiving,
        'is_retracted': node.is_retracted,
        'is_registration': node.is_registration,
    }


def _render_nodes(nodes, auth=None, show_path=False):
    """

    :param nodes:
    :return:
    """
    ret = {
        'nodes': [
            _render_node(node, auth)
            for node in nodes
        ],
        'show_path': show_path
    }
    return ret


def index():
    try:
        #TODO : make this way more robust
        institution = Institution.find_one(Q('domains', 'eq', request.host.lower()))
        inst_dict = view_institution(institution._id)
        inst_dict.update({
            'home': False,
            'institution': True,
            'redirect_url': '/institutions/{}/'.format(institution._id)
        })

        return inst_dict
    except NoResultsFound:
        pass

    all_institutions = [inst for inst in Institution.find().sort('name')]
    dashboard_institutions = [
        {'id': inst._id, 'name': inst.name, 'logo_path': inst.logo_path_rounded_corners}
        for inst in all_institutions
        if len(
            Node.find_by_institutions(inst, query=(
                Q('is_public', 'eq', True) &
                Q('is_folder', 'ne', True) &
                Q('is_deleted', 'ne', True) &
                Q('parent_node', 'eq', None) &
                Q('is_registration', 'eq', False)
            ))
        ) >= INSTITUTION_DISPLAY_NODE_THRESHOLD
    ]

    return {
        'home': True,
        'dashboard_institutions': dashboard_institutions
    }


def find_bookmark_collection(user):
    bookmark_collection = Node.find(Q('is_bookmark_collection', 'eq', True) & Q('contributors', 'eq', user._id))
    if bookmark_collection.count() == 0:
        new_bookmark_collection(user)
    return bookmark_collection[0]


@must_be_logged_in
def dashboard(auth):
    return redirect('/')


@must_be_logged_in
def my_projects(auth):<commit_msg>Remove expensive node query from index view<commit_after># -*- coding: utf-8 -*-
import itertools
import httplib as http
import logging
import math
import urllib

from modularodm import Q
from modularodm.exceptions import NoResultsFound
from flask import request

from framework import utils, sentry
from framework.auth.decorators import must_be_logged_in
from framework.auth.forms import SignInForm, ForgotPasswordForm
from framework.exceptions import HTTPError
from framework.flask import redirect  # VOL-aware redirect
from framework.forms import utils as form_utils
from framework.routing import proxy_url
from website.institutions.views import view_institution

from website.models import Guid
from website.models import Node, Institution
from website.project import new_bookmark_collection
from website.util import permissions

logger = logging.getLogger(__name__)


def _render_node(node, auth=None):
    """

    :param node:
    :return:

    """
    perm = None
    # NOTE: auth.user may be None if viewing public project while not
    # logged in
    if auth and auth.user and node.get_permissions(auth.user):
        perm_list = node.get_permissions(auth.user)
        perm = permissions.reduce_permissions(perm_list)

    return {
        'title': node.title,
        'id': node._primary_key,
        'url': node.url,
        'api_url': node.api_url,
        'primary': node.primary,
        'date_modified': utils.iso8601format(node.date_modified),
        'category': node.category,
        'permissions': perm,  # A string, e.g. 'admin', or None,
        'archiving': node.archiving,
        'is_retracted': node.is_retracted,
        'is_registration': node.is_registration,
    }


def _render_nodes(nodes, auth=None, show_path=False):
    """

    :param nodes:
    :return:
    """
    ret = {
        'nodes': [
            _render_node(node, auth)
            for node in nodes
        ],
        'show_path': show_path
    }
    return ret


def index():
    try:
        #TODO : make this way more robust
        institution = Institution.find_one(Q('domains', 'eq', request.host.lower()))
        inst_dict = view_institution(institution._id)
        inst_dict.update({
            'home': False,
            'institution': True,
            'redirect_url': '/institutions/{}/'.format(institution._id)
        })

        return inst_dict
    except NoResultsFound:
        pass

    all_institutions = Institution.find().sort('name')
    dashboard_institutions = [
        {'id': inst._id, 'name': inst.name, 'logo_path': inst.logo_path_rounded_corners}
        for inst in all_institutions
    ]

    return {
        'home': True,
        'dashboard_institutions': dashboard_institutions
    }


def find_bookmark_collection(user):
    bookmark_collection = Node.find(Q('is_bookmark_collection', 'eq', True) & Q('contributors', 'eq', user._id))
    if bookmark_collection.count() == 0:
        new_bookmark_collection(user)
    return bookmark_collection[0]


@must_be_logged_in
def dashboard(auth):
    return redirect('/')


@must_be_logged_in
def my_projects(auth):
{'fraction_non_alphanumeric': 0.10228476320169466, 'fraction_numerical': 0.0021183234982599486, 'mean_word_length': 2.991545893719807, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3900492', 'n_tokens_mistral': 2020, 'n_tokens_neox': 1962, 'n_words': 495}