<commit_before>from datetime import datetime
from django.utils.timesince import timesince
from django.core.urlresolvers import reverse
from avocado.store.forms import ReportForm, SessionReportForm
from restlib import http, resources
from serrano.http import ExcelResponse

__all__ = ('ReportResource', 'SessionReportResource', 'ReportResourceCollection',
    'ReportRedirectResource')

class ReportResource(resources.ModelResource):
    model = 'avocado.Report'

    fields = (':pk', 'name', 'description', 'modified', 'timesince',
        'has_changed', 'unique_count', 'url', 'report_url')

    default_for_related = False

    middleware = (
        'serrano.api.middleware.NeverCache',
    ) + resources.Resource.middleware

    @classmethod
    def unique_count(self, obj):
        return obj.scope.count

    @classmethod
    def timesince(self, obj):
        if obj.modified:
            return '%s ago' % timesince(obj.modified)

    @classmethod
    def url(self, obj):
        return reverse('api:reports:read', args=[obj.pk])

    @classmethod
    def report_url(self, obj):
        return reverse('report-redirect', args=[obj.pk])

    @classmethod
    def queryset(self, request):
        return self.model._default_manager.filter(user=request.user,
            session=False).order_by('-modified')

    def _export_csv(self, request, inst):
        context = {'user': request.user}

        # fetch the report cache from the session, default to a new dict with
        # a few defaults. if a new dict is used, this implies that this a
        # report has not been resolved yet this session.
        cache = request.session.get(inst.REPORT_CACHE_KEY, {
            'timestamp': None,
            'page_num': 1,
            'per_page': 10,
            'offset': 0,
            'unique': None,
            'count': None,
            'datakey': inst.get_datakey(request)
        })

        # test if the cache is still valid, then attempt to fetch the requested
        # page from cache
        timestamp = cache['timestamp']
        queryset, unique, count = inst.get_queryset(timestamp, **context)

        rows = inst._execute_raw_query(queryset)
        iterator = inst.perspective.format(rows, 'csv')
        header = inst.perspective.get_columns_as_fields()
        name = 'report-' + datetime.now().strftime('%Y-%m-%d-%H,%M,%S')

        return ExcelResponse(list(iterator), name, header)

    def _GET(self, request, inst):
        "The interface for resolving a report, i.e. running a query."
        user = request.user

        if not inst.has_permission(user):
            return http.FORBIDDEN

        format_type = request.GET.get('f', None)

        # XXX: hack
        if format_type == 'csv':
            return self._export_csv(request, inst)

        page_num = request.GET.get('p', None)
        per_page = request.GET.get('n', None)

        count = unique = None

        # define the default context for use by ``get_queryset``
        # TODO can this be defined elsewhere? only scope depends on this, but
        # the user object has to propagate down from the view
        context = {'user': user}

        # fetch the report cache from the session, default to a new dict with
        # a few defaults. if a new dict is used, this implies that this a
        # report has not been resolved yet this session.
        cache = request.session.get(inst.REPORT_CACHE_KEY, {
            'timestamp': None,
            'page_num': 1,
            'per_page': 10,
            'offset': 0,
            'unique': None,
            'count': None,
            'datakey': inst.get_datakey(request)
        })

        # acts as reference to compare to so the resp can be determined
        old_cache = cache.copy()


        # test if the cache is still valid, then attempt to fetch the requested
        # page from cache
        timestamp = cache['timestamp']

        if inst.cache_is_valid(timestamp):
            # only update the cache if there are values specified for either arg
            if page_num:
                cache['page_num'] = int(page_num)
            if per_page:
                cache['per_page'] = int(per_page)

            rows = inst.get_page_from_cache(cache)

            # ``rows`` will only be None if no cache was found. attempt to
            # update the cache by running a partial query
            if rows is None:
                # since the cache is not invalid, the counts do not have to be run
                queryset, unique, count = inst.get_queryset(timestamp, **context)
                cache['timestamp'] = datetime.now()

                rows = inst.update_cache(cache, queryset);

        # when the cache becomes invalid, the cache must be refreshed
        else:
            queryset, unique, count = inst.get_queryset(timestamp, **context)

            cache.update({
                'timestamp': datetime.now(),
                'page_num': 1,
                'offset': 0,
            })

            if count is not None:
                cache['count'] = count
                if unique is not None:
                    cache['unique'] = unique

            rows = inst.refresh_cache(cache, queryset)


        request.session[inst.REPORT_CACHE_KEY] = cache

        # the response is composed of a few different data that is dependent on
        # various conditions. the only required data is the ``rows`` which will
        # always be needed since all other components act on determing the rows
        # to be returned
        resp = {
            'rows': list(inst.perspective.format(rows, 'html')),
        }

        if inst.name:
            resp['name'] = inst.name

        if inst.description:
            resp['description'] = inst.description

        # a *no change* requests implies the page has been requested statically
        # and the whole response object must be provided
        resp.update({
            'per_page': cache['per_page'],
            'count': cache['count'],
            'unique': cache['unique'],
        })

        paginator, page = inst.paginator_and_page(cache)

        if paginator.num_pages > 1:
            resp.update({
                'pages': {
                    'page': page.number,
                    'pages': page.page_links(),<commit_msg>Update instance variable name Why?<commit_after>from datetime import datetime
from django.utils.timesince import timesince
from django.core.urlresolvers import reverse
from avocado.store.forms import ReportForm, SessionReportForm
from restlib import http, resources
from serrano.http import ExcelResponse

__all__ = ('ReportResource', 'SessionReportResource', 'ReportResourceCollection',
    'ReportRedirectResource')

class ReportResource(resources.ModelResource):
    model = 'avocado.Report'

    fields = (':pk', 'name', 'description', 'modified', 'timesince',
        'has_changed', 'unique_count', 'url', 'report_url')

    default_for_related = False

    middleware = (
        'serrano.api.middleware.NeverCache',
    ) + resources.Resource.middleware

    @classmethod
    def unique_count(self, obj):
        return obj.scope.count

    @classmethod
    def timesince(self, obj):
        if obj.modified:
            return '%s ago' % timesince(obj.modified)

    @classmethod
    def url(self, obj):
        return reverse('api:reports:read', args=[obj.pk])

    @classmethod
    def report_url(self, obj):
        return reverse('report-redirect', args=[obj.pk])

    @classmethod
    def queryset(self, request):
        return self.model._default_manager.filter(user=request.user,
            session=False).order_by('-modified')

    def _export_csv(self, request, inst):
        context = {'user': request.user}

        # fetch the report cache from the session, default to a new dict with
        # a few defaults. if a new dict is used, this implies that this a
        # report has not been resolved yet this session.
        cache = request.session.get(inst.REPORT_CACHE_KEY, {
            'timestamp': None,
            'page_num': 1,
            'per_page': 10,
            'offset': 0,
            'unique': None,
            'count': None,
            'datakey': inst.get_datakey(request)
        })

        # test if the cache is still valid, then attempt to fetch the requested
        # page from cache
        timestamp = cache['timestamp']
        queryset, unique, count = inst.get_queryset(timestamp, **context)

        rows = inst._execute_raw_query(queryset)
        iterator = inst.perspective.format(rows, 'csv')
        header = inst.perspective.get_columns_as_fields()
        name = 'report-' + datetime.now().strftime('%Y-%m-%d-%H,%M,%S')

        return ExcelResponse(list(iterator), name, header)

    def _GET(self, request, instance):
        "The interface for resolving a report, i.e. running a query."
        user = request.user

        if not instance.has_permission(user):
            return http.FORBIDDEN

        format_type = request.GET.get('format', None)

        # XXX: hack
        if format_type == 'csv':
            return self._export_csv(request, instance)

        page_num = request.GET.get('page', None)
        per_page = request.GET.get('size', None)

        count = unique = None

        # define the default context for use by ``get_queryset``
        # TODO can this be defined elsewhere? only scope depends on this, but
        # the user object has to propagate down from the view
        context = {'user': user}

        # fetch the report cache from the session, default to a new dict with
        # a few defaults. if a new dict is used, this implies that this a
        # report has not been resolved yet this session.
        cache = request.session.get(instance.REPORT_CACHE_KEY, {
            'timestamp': None,
            'page_num': 1,
            'per_page': 10,
            'offset': 0,
            'unique': None,
            'count': None,
            'datakey': instance.get_datakey(request)
        })

        # test if the cache is still valid, then attempt to fetch the requested
        # page from cache
        timestamp = cache['timestamp']

        if instance.cache_is_valid(timestamp):
            # only update the cache if there are values specified for either arg
            if page_num:
                cache['page_num'] = int(page_num)
            if per_page:
                cache['per_page'] = int(per_page)

            rows = instance.get_page_from_cache(cache)

            # ``rows`` will only be None if no cache was found. attempt to
            # update the cache by running a partial query
            if rows is None:
                # since the cache is not invalid, the counts do not have to be run
                queryset, unique, count = instance.get_queryset(timestamp, **context)
                cache['timestamp'] = datetime.now()

                rows = instance.update_cache(cache, queryset);

        # when the cache becomes invalid, the cache must be refreshed
        else:
            queryset, unique, count = instance.get_queryset(timestamp, **context)

            cache.update({
                'timestamp': datetime.now(),
                'page_num': 1,
                'offset': 0,
            })

            if count is not None:
                cache['count'] = count
                if unique is not None:
                    cache['unique'] = unique

            rows = instance.refresh_cache(cache, queryset)


        request.session[instance.REPORT_CACHE_KEY] = cache

        # the response is composed of a few different data that is dependent on
        # various conditions. the only required data is the ``rows`` which will
        # always be needed since all other components act on determing the rows
        # to be returned
        resp = {
            'rows': list(instance.perspective.format(rows, 'html')),
        }

        if instance.name:
            resp['name'] = instance.name

        if instance.description:
            resp['description'] = instance.description

        # a *no change* requests implies the page has been requested statically
        # and the whole response object must be provided
        resp.update({
            'per_page': cache['per_page'],
            'count': cache['count'],
            'unique': cache['unique'],
        })

        paginator, page = instance.paginator_and_page(cache)

        if paginator.num_pages > 1:
            resp.update({
                'pages': {
                    'page': page.number,
                    'pages': page.page_links(),
{'fraction_non_alphanumeric': 0.09740932642487046, 'fraction_numerical': 0.001753686727779992, 'mean_word_length': 2.119343610144207, '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': '1187111', 'n_tokens_mistral': 3385, 'n_tokens_neox': 3242, 'n_words': 1073}