<commit_before>"""This module provides the views for the explorer interface."""

import json, nbformat
from bson import ObjectId
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from mpcontribs.rest.rester import MPContribsRester
from mpcontribs.rest.views import get_endpoint
from mpcontribs.builder import export_notebook
from webtzite.models import RegisteredUser

def index(request):
    ctx = RequestContext(request)
    fields = ['identifiers', 'projects', 'cids']
    ctx['fields'] = fields
    if request.user.is_authenticated():
        user = RegisteredUser.objects.get(username=request.user.username)
        with MPContribsRester(user.api_key, endpoint=get_endpoint(request)) as mpr:

            if request.method == 'GET':
                options = dict((field, set()) for field in fields)
                docs = mpr.query_contributions()
                if docs:
                    for doc in docs:
                        options[fields[0]].add(str(doc['mp_cat_id']))
                        options[fields[1]].add(str(doc['project']))
                        options[fields[2]].add(str(doc['_id']))
                else:
                    ctx.update({'alert': 'No contributions available!'})
                options = dict((k, list(v)) for k,v in options.iteritems())
                selection = dict((field, []) for field in fields)

            elif request.method == 'POST':
                projection_keys = ['mp_cat_id', 'project']
                options, selection = (
                    dict(
                        (field, [str(el) for el in json.loads(
                            request.POST['_'.join([prefix, field])]
                        )]) for field in fields
                    ) for prefix in ['options', 'selection']
                )

                mode = request.POST['submit']
                if mode == 'Find':
                    criteria = {}
                    for idx, key in enumerate(projection_keys):
                        if selection[fields[idx]]:
                            criteria.update({
                                key: {'$in': selection[fields[idx]]}
                            })
                    docs = mpr.query_contributions(criteria=criteria, limit=10)
                    ctx['urls'] = [mpr.get_card(doc['_id'], embed=False) for doc in docs]
                elif mode == 'Show':
                    if selection[fields[2]]:
                        docs = mpr.query_contributions(
                            criteria={'_id': {'$in': selection[fields[2]]}}
                        )
                        ctx['urls'] = [mpr.get_card(doc['_id'], embed=False) for doc in docs]
                    else:
                        ctx.update({'alert': 'Enter a contribution identifier!'})

        ctx['options'] = options
        ctx['selection'] = selection
    else:
        ctx.update({'alert': 'Please log in!'})
    return render_to_response("mpcontribs_explorer_index.html", ctx)

def contribution(request, collection, cid):<commit_msg>disable explorer for the time being<commit_after>"""This module provides the views for the explorer interface."""

import json, nbformat
from bson import ObjectId
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from mpcontribs.rest.rester import MPContribsRester
from mpcontribs.rest.views import get_endpoint
from mpcontribs.builder import export_notebook
from webtzite.models import RegisteredUser

def index(request):
    ctx = RequestContext(request)
    fields = ['identifiers', 'projects', 'cids']
    ctx['fields'] = fields
    if request.user.is_authenticated():
        user = RegisteredUser.objects.get(username=request.user.username)
        ctx['alert'] = 'Under Construction'
        #with MPContribsRester(user.api_key, endpoint=get_endpoint(request)) as mpr:

        #    if request.method == 'GET':
        #        options = dict((field, set()) for field in fields)
        #        docs = mpr.query_contributions()
        #        if docs:
        #            for doc in docs:
        #                options[fields[0]].add(str(doc['mp_cat_id']))
        #                options[fields[1]].add(str(doc['project']))
        #                options[fields[2]].add(str(doc['_id']))
        #        else:
        #            ctx.update({'alert': 'No contributions available!'})
        #        options = dict((k, list(v)) for k,v in options.iteritems())
        #        selection = dict((field, []) for field in fields)

        #    elif request.method == 'POST':
        #        projection_keys = ['mp_cat_id', 'project']
        #        options, selection = (
        #            dict(
        #                (field, [str(el) for el in json.loads(
        #                    request.POST['_'.join([prefix, field])]
        #                )]) for field in fields
        #            ) for prefix in ['options', 'selection']
        #        )

        #        mode = request.POST['submit']
        #        if mode == 'Find':
        #            criteria = {}
        #            for idx, key in enumerate(projection_keys):
        #                if selection[fields[idx]]:
        #                    criteria.update({
        #                        key: {'$in': selection[fields[idx]]}
        #                    })
        #            docs = mpr.query_contributions(criteria=criteria, limit=10)
        #            ctx['urls'] = [mpr.get_card(doc['_id'], embed=False) for doc in docs]
        #        elif mode == 'Show':
        #            if selection[fields[2]]:
        #                docs = mpr.query_contributions(
        #                    criteria={'_id': {'$in': selection[fields[2]]}}
        #                )
        #                ctx['urls'] = [mpr.get_card(doc['_id'], embed=False) for doc in docs]
        #            else:
        #                ctx.update({'alert': 'Enter a contribution identifier!'})

        #ctx['options'] = options
        #ctx['selection'] = selection
    else:
        ctx.update({'alert': 'Please log in!'})
    return render_to_response("mpcontribs_explorer_index.html", ctx)

def contribution(request, collection, cid):
{'fraction_non_alphanumeric': 0.12024714828897339, 'fraction_numerical': 0.002217997465145754, 'mean_word_length': 1.768859649122807, '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': '942197', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1634, 'n_words': 395}