<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


import datetime
import time

from django.db.models.query import RawQuerySet, RawQuerySet
from django.template import RequestContext
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.http import Http404, HttpResponseServerError, HttpResponseRedirect, HttpResponse
from django import forms
from django.contrib.auth.models import User
from generic.models import Dashboard, Module, ModuleParams, StaticModuleContent
from django.db.models import Count
from django.views.decorators.cache import cache_control
from .utils import copy_dashboard, get_dates

def generic_row(request, model=None, pk=None, partial_row='generic/partials/partial_row.html', selectable=True):
    if not (model and pk):
        return HttpResponseServerError
    object = get_object_or_404(model, pk=pk)
    return render_to_response(partial_row, {'object':object, 'selectable':selectable},
        context_instance=RequestContext(request))

def generic(request,
            model=None,
            queryset=None,
            template_object_name='object_list',
            base_template='generic/base.html',
            partial_base='generic/partials/partial_base.html',
            partial_header='generic/partials/partial_header.html',
            partial_row='generic/partials/partial_row.html',
            paginator_template='generic/partials/pagination.html',
            results_title='Results',
            paginated=True,
            selectable=True,
            objects_per_page=25,
            columns=[('object', False, '')],
            sort_column='',
            sort_ascending=True,
            filter_forms=[],
            action_forms=[],
            needs_date=False,
            dates={},
            **kwargs):
    # model parameter is required
    if not model:
        return HttpResponseServerError

    # querysets can be calls to a function for dynamic, run-time retrieval
    if callable(queryset):
        if needs_date:
            queryset = queryset(request=request, dates=dates)
        else:
            queryset = queryset(request=request)

    # the default list is either a queryset parameter, or all
    # objects from the model parameter
    object_list = queryset if queryset is not None else model.objects.all()

    # dynamically create a form class to represent the list of selected results,
    # for performing actions
    class ResultsForm(forms.Form):
        results = forms.ModelMultipleChoiceField(queryset=object_list, widget=forms.CheckboxSelectMultiple())

    class_dict = {}
    action_form_instances = []
    for action_class in action_forms:
        form_instance = action_class(**{'request':request})
        fully_qualified_class_name = "%s.%s" % (form_instance.__module__, form_instance.__class__.__name__)
        # we need both a dictionary of action forms (for looking up actions performed)
        # and a list of tuple for rendering within the template in a particular order
        class_dict[fully_qualified_class_name] = action_class
        action_form_instances.append((fully_qualified_class_name, form_instance,))

    filter_form_instances = []
    for filter_class in filter_forms:
        form_instance = filter_class(**{'request':request})
        filter_form_instances.append(form_instance)

    # define some defaults
    response_template = base_template
    page = 1
    selected = False
    status_message = ''
    status_message_type = ''

    FILTER_REQUEST_KEY = "%s_filter_request" % request.path

    filtered_list = object_list

    if request.method == 'POST':
        # check for previous filters in the case of a post,
        # as other actions will be driven from this
        # filtered list
        filter_request_post = request.session.setdefault(FILTER_REQUEST_KEY, None)
        if filter_request_post:
            for form_class in filter_forms:
                form_instance = form_class(filter_request_post, request=request)
                if form_instance.is_valid():
                    filtered_list = form_instance.filter(request, filtered_list)

        page_action = request.POST.get('page_action', '')
        sort_action = request.POST.get('sort_action', '')
        sort_column = request.POST.get('sort_column', '')
        sort_ascending = request.POST.get('sort_ascending', 'True')
        sort_ascending = (sort_ascending == 'True')
        action_taken = request.POST.get('action', '')

        for column_name, sortable, sort_param, sorter in columns:
            if sortable and sort_param == sort_column:
                filtered_list = sorter.sort(sort_column, filtered_list, sort_ascending)

        if sort_action:
            # we already have to sort regardless, but
            # if this action was just a sort, we're done 
            # (and should avoid re-filtering)
            pass

        elif page_action:
            try:
                page = int(request.POST.get('page_num', '1'))
            except ValueError:
                pass
        elif action_taken:
            everything_selected = request.POST.get('select_everything', None)
            results = []
            if everything_selected:
                results = filtered_list
            else:
                resultsform = ResultsForm(request.POST)
                if resultsform.is_valid():
                    results = resultsform.cleaned_data['results']
            action_class = class_dict[action_taken]
            action_instance = action_class(request.POST, request=request)
            if action_instance.is_valid():
                status_message, status_message_type = action_instance.perform(request, results)
        else:
            # it's a new filter, re-start from the object list
            # and filter down on the new set of forms
            filtered_list = object_list
            for form_class in filter_forms:
                form_instance = form_class(request.POST, request=request)
                if form_instance.is_valid():
                    filtered_list = form_instance.filter(request, filtered_list)
            selected = True
            # store the request filters in the session
            request.session[FILTER_REQUEST_KEY] = request.POST

        response_template = partial_base
    else:
        # reset the filter key, if there was a previous one it should be 
        # cleared out
        request.session[FILTER_REQUEST_KEY] = None
        # calls to this view can define a default sorting order,
        # if it's an initial GET request we should perform this sort here
        if sort_column:
            for column_name, sortable, sort_param, sorter in columns:
                if sortable and sort_param == sort_column:
                    filtered_list = sorter.sort(sort_column, filtered_list, sort_ascending)

    if hasattr(filtered_list, 'count') and callable(filtered_list.count):
        total = filtered_list.count()
    else:
        total = len(filtered_list)
    paginator = None
    ranges = []
    if paginated:
        paginator = Paginator(filtered_list, objects_per_page)
        # If page request is out of range, deliver last page of results.
        try:
            filtered_list = paginator.page(page).object_list
        except (EmptyPage, InvalidPage):
            filtered_list = paginator.page(paginator.num_pages).object_list
            page = paginator.num_pages
        if paginator.num_pages > 10:<commit_msg>minor fix to finding count efficiently<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


import datetime
import time

from django.db.models.query import RawQuerySet, RawQuerySet
from django.template import RequestContext
from django.shortcuts import redirect, get_object_or_404, render_to_response
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.http import Http404, HttpResponseServerError, HttpResponseRedirect, HttpResponse
from django import forms
from django.contrib.auth.models import User
from generic.models import Dashboard, Module, ModuleParams, StaticModuleContent
from django.db.models import Count
from django.views.decorators.cache import cache_control
from .utils import copy_dashboard, get_dates

def generic_row(request, model=None, pk=None, partial_row='generic/partials/partial_row.html', selectable=True):
    if not (model and pk):
        return HttpResponseServerError
    object = get_object_or_404(model, pk=pk)
    return render_to_response(partial_row, {'object':object, 'selectable':selectable},
        context_instance=RequestContext(request))

def generic(request,
            model=None,
            queryset=None,
            template_object_name='object_list',
            base_template='generic/base.html',
            partial_base='generic/partials/partial_base.html',
            partial_header='generic/partials/partial_header.html',
            partial_row='generic/partials/partial_row.html',
            paginator_template='generic/partials/pagination.html',
            results_title='Results',
            paginated=True,
            selectable=True,
            objects_per_page=25,
            columns=[('object', False, '')],
            sort_column='',
            sort_ascending=True,
            filter_forms=[],
            action_forms=[],
            needs_date=False,
            dates={},
            **kwargs):
    # model parameter is required
    if not model:
        return HttpResponseServerError

    # querysets can be calls to a function for dynamic, run-time retrieval
    if callable(queryset):
        if needs_date:
            queryset = queryset(request=request, dates=dates)
        else:
            queryset = queryset(request=request)

    # the default list is either a queryset parameter, or all
    # objects from the model parameter
    object_list = queryset if queryset is not None else model.objects.all()

    # dynamically create a form class to represent the list of selected results,
    # for performing actions
    class ResultsForm(forms.Form):
        results = forms.ModelMultipleChoiceField(queryset=object_list, widget=forms.CheckboxSelectMultiple())

    class_dict = {}
    action_form_instances = []
    for action_class in action_forms:
        form_instance = action_class(**{'request':request})
        fully_qualified_class_name = "%s.%s" % (form_instance.__module__, form_instance.__class__.__name__)
        # we need both a dictionary of action forms (for looking up actions performed)
        # and a list of tuple for rendering within the template in a particular order
        class_dict[fully_qualified_class_name] = action_class
        action_form_instances.append((fully_qualified_class_name, form_instance,))

    filter_form_instances = []
    for filter_class in filter_forms:
        form_instance = filter_class(**{'request':request})
        filter_form_instances.append(form_instance)

    # define some defaults
    response_template = base_template
    page = 1
    selected = False
    status_message = ''
    status_message_type = ''

    FILTER_REQUEST_KEY = "%s_filter_request" % request.path

    filtered_list = object_list

    if request.method == 'POST':
        # check for previous filters in the case of a post,
        # as other actions will be driven from this
        # filtered list
        filter_request_post = request.session.setdefault(FILTER_REQUEST_KEY, None)
        if filter_request_post:
            for form_class in filter_forms:
                form_instance = form_class(filter_request_post, request=request)
                if form_instance.is_valid():
                    filtered_list = form_instance.filter(request, filtered_list)

        page_action = request.POST.get('page_action', '')
        sort_action = request.POST.get('sort_action', '')
        sort_column = request.POST.get('sort_column', '')
        sort_ascending = request.POST.get('sort_ascending', 'True')
        sort_ascending = (sort_ascending == 'True')
        action_taken = request.POST.get('action', '')

        for column_name, sortable, sort_param, sorter in columns:
            if sortable and sort_param == sort_column:
                filtered_list = sorter.sort(sort_column, filtered_list, sort_ascending)

        if sort_action:
            # we already have to sort regardless, but
            # if this action was just a sort, we're done 
            # (and should avoid re-filtering)
            pass

        elif page_action:
            try:
                page = int(request.POST.get('page_num', '1'))
            except ValueError:
                pass
        elif action_taken:
            everything_selected = request.POST.get('select_everything', None)
            results = []
            if everything_selected:
                results = filtered_list
            else:
                resultsform = ResultsForm(request.POST)
                if resultsform.is_valid():
                    results = resultsform.cleaned_data['results']
            action_class = class_dict[action_taken]
            action_instance = action_class(request.POST, request=request)
            if action_instance.is_valid():
                status_message, status_message_type = action_instance.perform(request, results)
        else:
            # it's a new filter, re-start from the object list
            # and filter down on the new set of forms
            filtered_list = object_list
            for form_class in filter_forms:
                form_instance = form_class(request.POST, request=request)
                if form_instance.is_valid():
                    filtered_list = form_instance.filter(request, filtered_list)
            selected = True
            # store the request filters in the session
            request.session[FILTER_REQUEST_KEY] = request.POST

        response_template = partial_base
    else:
        # reset the filter key, if there was a previous one it should be 
        # cleared out
        request.session[FILTER_REQUEST_KEY] = None
        # calls to this view can define a default sorting order,
        # if it's an initial GET request we should perform this sort here
        if sort_column:
            for column_name, sortable, sort_param, sorter in columns:
                if sortable and sort_param == sort_column:
                    filtered_list = sorter.sort(sort_column, filtered_list, sort_ascending)

    if hasattr(filtered_list, 'count') and callable(filtered_list.count):
        try:
            total = filtered_list.count()
        except TypeError:
            total = len(filtered_list)
    else:
        total = len(filtered_list)
    paginator = None
    ranges = []
    if paginated:
        paginator = Paginator(filtered_list, objects_per_page)
        # If page request is out of range, deliver last page of results.
        try:
            filtered_list = paginator.page(page).object_list
        except (EmptyPage, InvalidPage):
            filtered_list = paginator.page(paginator.num_pages).object_list
            page = paginator.num_pages
        if paginator.num_pages > 10:
{'fraction_non_alphanumeric': 0.0764872521246459, 'fraction_numerical': 0.0023716977402990975, 'mean_word_length': 2.617731172545281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1151435', 'n_tokens_mistral': 4048, 'n_tokens_neox': 3909, 'n_words': 1069}