<commit_before>from django.db.models import Count
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User 
from django.core.urlresolvers import reverse 
from django.http import HttpResponseRedirect, Http404 
from django.shortcuts import render_to_response, get_object_or_404 
from django.template import RequestContext 


from grid.forms import ElementForm, FeatureForm, GridForm, GridPackageForm
from grid.models import Element, Feature, Grid, GridPackage
from package.models import Package
from package.forms import PackageForm
from package.views import repo_data_for_js

def grids(request, template_name="grid/grids.html"):
    # annotations providing bad counts
    #grids = Grid.objects.annotate(gridpackage_count=Count('gridpackage'), feature_count=Count('feature'))
    return render_to_response(
        template_name, {
            'grids': Grid.objects.all(),
        }, context_instance = RequestContext(request)
    )

def grid_detail(request, slug, template_name="grid/grid_detail.html"):
    grid = get_object_or_404(Grid, slug=slug)
    features = grid.feature_set.all()

    gp = grid.gridpackage_set.select_related('gridpackage', 'package__repo', 'package__category')
    grid_packages = gp.annotate(usage_count=Count('package__usage')).order_by('-usage_count', 'package')

    # Horrifying two-level dict due to needing to use hash() function later
    element_map = {}
    elements = Element.objects.all() \
                .filter(feature__in=features,
                        grid_package__in=grid_packages)
    for element in elements:
        element_map.setdefault(element.feature_id, {})
        element_map[element.feature_id][element.grid_package_id] = element

    return render_to_response(
        template_name, {
            'grid': grid,
            'features': features,
            'grid_packages': grid_packages,
            'elements': element_map,
        }, context_instance = RequestContext(request)
    )

def grid_detail_feature(request, slug, feature_id, bogus_slug, template_name="grid/grid_detail_feature.html"):
    grid = get_object_or_404(Grid, slug=slug)
    features = grid.feature_set.filter(id=feature_id)
    if not features.count():
        raise Http404
    grid_packages = grid.gridpackage_set.select_related('gridpackage')

    # Horrifying two-level dict due to needing to use hash() function later
    element_map = {}
    elements = Element.objects.all() \
                .filter(feature__in=features,
                        grid_package__in=grid_packages)
    for element in elements:
        element_map.setdefault(element.feature_id, {})
        element_map[element.feature_id][element.grid_package_id] = element


    return render_to_response(
        template_name, {
            'grid': grid,
            'feature': features[0],
            'grid_packages': grid_packages,
            'elements': element_map,
        }, context_instance = RequestContext(request)
    )

@login_required
def add_grid(request, template_name="grid/add_grid.html"):

    new_grid = Grid()
    form = GridForm(request.POST or None, instance=new_grid)    

    if form.is_valid(): 
        new_grid = form.save()
        return HttpResponseRedirect(reverse('grid', kwargs={'slug':new_grid.slug}))

    return render_to_response(template_name, { 
        'form': form
        },
        context_instance=RequestContext(request))
        
@login_required
def edit_grid(request, slug, template_name="grid/edit_grid.html"):

    grid = get_object_or_404(Grid, slug=slug)<commit_msg>factor building grid element map<commit_after>from django.db.models import Count
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User 
from django.core.urlresolvers import reverse 
from django.http import HttpResponseRedirect, Http404 
from django.shortcuts import render_to_response, get_object_or_404 
from django.template import RequestContext 


from grid.forms import ElementForm, FeatureForm, GridForm, GridPackageForm
from grid.models import Element, Feature, Grid, GridPackage
from package.models import Package
from package.forms import PackageForm
from package.views import repo_data_for_js

def build_element_map(elements):
    # Horrifying two-level dict due to needing to use hash() function later
    element_map = {}
    for element in elements:
        element_map.setdefault(element.feature_id, {})
        element_map[element.feature_id][element.grid_package_id] = element
    return element_map

def grids(request, template_name="grid/grids.html"):
    # annotations providing bad counts
    #grids = Grid.objects.annotate(gridpackage_count=Count('gridpackage'), feature_count=Count('feature'))
    return render_to_response(
        template_name, {
            'grids': Grid.objects.all(),
        }, context_instance = RequestContext(request)
    )

def grid_detail(request, slug, template_name="grid/grid_detail.html"):
    grid = get_object_or_404(Grid, slug=slug)
    features = grid.feature_set.all()

    gp = grid.gridpackage_set.select_related('gridpackage', 'package__repo', 'package__category')
    grid_packages = gp.annotate(usage_count=Count('package__usage')).order_by('-usage_count', 'package')

    elements = Element.objects.all() \
                .filter(feature__in=features,
                        grid_package__in=grid_packages)

    element_map = build_element_map(elements)

    return render_to_response(
        template_name, {
            'grid': grid,
            'features': features,
            'grid_packages': grid_packages,
            'elements': element_map,
        }, context_instance = RequestContext(request)
    )

def grid_detail_feature(request, slug, feature_id, bogus_slug, template_name="grid/grid_detail_feature.html"):
    grid = get_object_or_404(Grid, slug=slug)
    features = grid.feature_set.filter(id=feature_id)
    if not features.count():
        raise Http404
    grid_packages = grid.gridpackage_set.select_related('gridpackage')

    elements = Element.objects.all() \
                .filter(feature__in=features,
                        grid_package__in=grid_packages)

    element_map = build_element_map(elements)

    return render_to_response(
        template_name, {
            'grid': grid,
            'feature': features[0],
            'grid_packages': grid_packages,
            'elements': element_map,
        }, context_instance = RequestContext(request)
    )

@login_required
def add_grid(request, template_name="grid/add_grid.html"):

    new_grid = Grid()
    form = GridForm(request.POST or None, instance=new_grid)    

    if form.is_valid(): 
        new_grid = form.save()
        return HttpResponseRedirect(reverse('grid', kwargs={'slug':new_grid.slug}))

    return render_to_response(template_name, { 
        'form': form
        },
        context_instance=RequestContext(request))
        
@login_required
def edit_grid(request, slug, template_name="grid/edit_grid.html"):

    grid = get_object_or_404(Grid, slug=slug)
{'fraction_non_alphanumeric': 0.09623784697759617, 'fraction_numerical': 0.005354375088065379, 'mean_word_length': 3.603112840466926, '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': '4694527', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1999, 'n_words': 414}