<commit_before>import requests

from django.shortcuts import redirect
from django.http import Http404
from django.views.generic import TemplateView, View
from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.paginator import Paginator
from braces.views import LoginRequiredMixin
from braces.views import (CsrfExemptMixin, JsonRequestResponseMixin)

from .forms import (OrganizationCreateForm, OrganizationEditForm)
from .forms import (OrganizationMemberAddForm)
from libs.ocl import OCLapi, OCLSearch


class OrganizationDetailView(TemplateView):
    """
    Organization details and source search view.
    """

    template_name = "orgs/org_detail.html"

    def get_context_data(self, *args, **kwargs):
        """Gets the org first, then the sources of that org, and then the
        concepts from each of those sources.

        Final context
        -------------
        context['org']
        context['sources']
        context['collections']
        context['members']
        """
        # TODO: Change page so that only one tab loaded at a time

        context = super(OrganizationDetailView, self).get_context_data(*args, **kwargs)

        # Set the organization ID
        org_id = self.kwargs.get('org')

        # Prepare to search the sources and collections in this org
        # NOTE: Both are searched no matter what, but only one accepts search criteria/filters at a time
        res_type = self.request.GET.get('resource_type')
        print 'INPUT PARAMS %s: %s' % (self.request.method, self.request.GET)
        print res_type
        if res_type == 'source':
            source_searcher = OCLSearch(search_type=OCLapi.SOURCE_TYPE, params=self.request.GET)
            collection_searcher = OCLSearch(search_type=OCLapi.COLLECTION_TYPE, parmas={})
        elif res_type == 'collection':
            source_searcher = OCLSearch(search_type=OCLapi.SOURCE_TYPE, params={})
            collection_searcher = OCLSearch(search_type=OCLapi.COLLECTION_TYPE, params=self.request.GET)
        else:
            # Still pass down paging parameters
            source_searcher = OCLSearch(search_type=OCLapi.SOURCE_TYPE, params=self.request.GET)
            collection_searcher = OCLSearch(search_type=OCLapi.COLLECTION_TYPE, params=self.request.GET)

        # Load the org
        api = OCLapi(self.request, debug=True)
        results = api.get('orgs', org_id)
        if results.status_code != 200:
            if results.status_code == 404:
                raise Http404
            else:
                results.raise_for_status()
        org = results.json()

        # Set org about text
        # TODO: Need a more generic method for getting at extras
        if 'extras' in org and isinstance(org['extras'], dict):
            about = org['extras'].get('about', 'No about entry.')
        else:
            # TODO: If user has editing privileges to this org, then prompt them to create an about entry
            about = 'No about entry.'

        # Load the sources in this org
        results = api.get('orgs', org_id, 'sources', params=source_searcher.search_params)
        if results.status_code == requests.codes.not_found:
            num_found = 0
            sources = []
            context['source_page'] = None
        else:
            num_found = int(results.headers['num_found'])
            sources = results.json()
            pg = Paginator(range(num_found), source_searcher.num_per_page)
            context['source_page'] = pg.page(source_searcher.current_page)

        # Set the context for the child sources
        context['source_pagination_url'] = self.request.get_full_path()
        context['sources'] = sources
        context['source_filters'] = source_searcher.get_filters()
        context['source_q'] = source_searcher.get_query()
        # TODO: Sort is not setup correctly to work with both sources and collections
        context['search_sort_options'] = source_searcher.get_sort_options()
        context['search_sort'] = source_searcher.get_sort()

        # Load the collections in this org
        results = api.get('orgs', org_id, 'collections', params=collection_searcher.search_params)
        if results.status_code == requests.codes.not_found:
            collections = []
            num_found = 0
            context['collection_page'] = None
        else:
            collections = results.json()
            num_found = int(results.headers['num_found'])
            pg = Paginator(range(num_found), collection_searcher.num_per_page)
            context['collection_page'] = pg.page(collection_searcher.current_page)

        # Set the context for the child collections
        context['collection_pagination_url'] = self.request.get_full_path()
        context['collections'] = collections
        context['collection_filters'] = collection_searcher.get_filters()

        # Load members of this org
        # TODO: access issue, error if user is not super user??
        members = []
        r = api.get('orgs', org_id, 'members')
        if r.status_code == 200:
            members = r.json()
        elif r.status_code != 404:
            #raise Exception(r.json())
            pass

        # Set the context
        context['org'] = org
        context['members'] = members
        context['about'] = about
        return context


class OrganizationCreateView(LoginRequiredMixin, FormView):
<commit_msg>fixed typo in params parameter<commit_after>import requests

from django.shortcuts import redirect
from django.http import Http404
from django.views.generic import TemplateView, View
from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.core.paginator import Paginator
from braces.views import LoginRequiredMixin
from braces.views import (CsrfExemptMixin, JsonRequestResponseMixin)

from .forms import (OrganizationCreateForm, OrganizationEditForm)
from .forms import (OrganizationMemberAddForm)
from libs.ocl import OCLapi, OCLSearch


class OrganizationDetailView(TemplateView):
    """
    Organization details and source search view.
    """

    template_name = "orgs/org_detail.html"

    def get_context_data(self, *args, **kwargs):
        """Gets the org first, then the sources of that org, and then the
        concepts from each of those sources.

        Final context
        -------------
        context['org']
        context['sources']
        context['collections']
        context['members']
        """
        # TODO: Change page so that only one tab loaded at a time

        context = super(OrganizationDetailView, self).get_context_data(*args, **kwargs)

        # Set the organization ID
        org_id = self.kwargs.get('org')

        # Prepare to search the sources and collections in this org
        # NOTE: Both are searched no matter what, but only one accepts search criteria/filters at a time
        res_type = self.request.GET.get('resource_type')
        print 'INPUT PARAMS %s: %s' % (self.request.method, self.request.GET)
        print res_type
        if res_type == 'source':
            source_searcher = OCLSearch(search_type=OCLapi.SOURCE_TYPE, params=self.request.GET)
            collection_searcher = OCLSearch(search_type=OCLapi.COLLECTION_TYPE, params={})
        elif res_type == 'collection':
            source_searcher = OCLSearch(search_type=OCLapi.SOURCE_TYPE, params={})
            collection_searcher = OCLSearch(search_type=OCLapi.COLLECTION_TYPE, params=self.request.GET)
        else:
            # Still pass down paging parameters
            source_searcher = OCLSearch(search_type=OCLapi.SOURCE_TYPE, params=self.request.GET)
            collection_searcher = OCLSearch(search_type=OCLapi.COLLECTION_TYPE, params=self.request.GET)

        # Load the org
        api = OCLapi(self.request, debug=True)
        results = api.get('orgs', org_id)
        if results.status_code != 200:
            if results.status_code == 404:
                raise Http404
            else:
                results.raise_for_status()
        org = results.json()

        # Set org about text
        # TODO: Need a more generic method for getting at extras
        if 'extras' in org and isinstance(org['extras'], dict):
            about = org['extras'].get('about', 'No about entry.')
        else:
            # TODO: If user has editing privileges to this org, then prompt them to create an about entry
            about = 'No about entry.'

        # Load the sources in this org
        results = api.get('orgs', org_id, 'sources', params=source_searcher.search_params)
        if results.status_code == requests.codes.not_found:
            num_found = 0
            sources = []
            context['source_page'] = 0
        else:
            num_found = int(results.headers['num_found'])
            sources = results.json()
            pg = Paginator(range(num_found), source_searcher.num_per_page)
            context['source_page'] = pg.page(source_searcher.current_page)

        # Set the context for the child sources
        context['source_pagination_url'] = self.request.get_full_path()
        context['sources'] = sources
        context['source_filters'] = source_searcher.get_filters()
        context['source_q'] = source_searcher.get_query()
        # TODO: Sort is not setup correctly to work with both sources and collections
        context['search_sort_options'] = source_searcher.get_sort_options()
        context['search_sort'] = source_searcher.get_sort()

        # Load the collections in this org
        results = api.get('orgs', org_id, 'collections', params=collection_searcher.search_params)
        if results.status_code == requests.codes.not_found:
            collections = []
            num_found = 0
            context['collection_page'] = 0
        else:
            collections = results.json()
            num_found = int(results.headers['num_found'])
            pg = Paginator(range(num_found), collection_searcher.num_per_page)
            context['collection_page'] = pg.page(collection_searcher.current_page)

        # Set the context for the child collections
        context['collection_pagination_url'] = self.request.get_full_path()
        context['collections'] = collections
        context['collection_filters'] = collection_searcher.get_filters()

        # Load members of this org
        # TODO: access issue, error if user is not super user??
        members = []
        r = api.get('orgs', org_id, 'members')
        if r.status_code == 200:
            members = r.json()
        elif r.status_code != 404:
            #raise Exception(r.json())
            pass

        # Set the context
        context['org'] = org
        context['members'] = members
        context['about'] = about
        return context


class OrganizationCreateView(LoginRequiredMixin, FormView):

{'fraction_non_alphanumeric': 0.09331765982457726, 'fraction_numerical': 0.003797811737046749, 'mean_word_length': 2.690357023690357, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5079043', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2984, 'n_words': 812}