<commit_before>from StringIO import StringIO
import json
from datetime import datetime
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext, Context
from django.template.loader import render_to_string
import pytz
from tastypie.http import HttpBadRequest
from corehq.apps.reports import util
from dimagi.utils.modules import to_function
from tempfile import NamedTemporaryFile
from couchexport.models import Format
from couchexport.export import export_from_tables
from couchexport.shortcuts import export_response

class HQReport(object):
    name = ""
    slug = ""
    base_slug = None # for instance, 'reports' or 'manage'

    description = ""
    report_partial = None
    title = None
    headers = None
    rows = None
    individual = None
    case_type = None
    group = None
    form = None,
    datespan = None
    show_time_notice = False
    fields = []
    exportable = False

    asynchronous = False
    template_name = None

    base_template_name = "reports/report_base.html"
    async_base_template_name = "reports/async/default.html"

    def __init__(self, domain, request, base_context = None):
        base_context = base_context or {}
        if not self.name or not self.slug:
            raise NotImplementedError
        self.domain = domain
        self.request = request

        try:
            self.timezone = util.get_timezone(self.request.couch_user.user_id, domain)
        except AttributeError:
            self.timezone = util.get_timezone(None, domain)

        if not self.rows:
            self.rows = []

        if not self.asynchronous:
            self.async_base_template_name = self.base_template_name

        self.context = base_context
        self.context.update(name = self.name,
                            slug = self.slug,
                            description = self.description,
                            template_name = self.get_template(),
                            exportable = self.exportable,
                            export_path = self.request.get_full_path().replace('/custom/', '/export/'),
                            export_formats = Format.VALID_FORMATS,
                            async_report = self.asynchronous,
                            report_base = self.async_base_template_name
        )

    def build_selector_form(self):
        """
        Get custom field info and use it to construct a set of field form HTML to grab the right inputs.
        """
        if not self.fields: return
        field_classes = []
        for f in self.fields:
            klass = to_function(f)
            field_classes.append(klass(self.request, self.domain, self.timezone))
        self.context['custom_fields'] = [{"field": f.render(), "slug": f.slug} for f in field_classes]

    def get_report_context(self):
        # circular import
        from .util import report_context
        self.build_selector_form()
        self.context.update(report_context(self.domain,
                                        report_partial = self.report_partial,
                                        title = self.name,
                                        headers = self.headers,
                                        rows = self.rows,
                                        individual = self.individual,
                                        case_type = self.case_type,
                                        group = self.group,
                                        form = None, #self.form,<commit_msg>modified HQReport to work with admin / non domain dependent reports<commit_after>from StringIO import StringIO
import json
from datetime import datetime
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext, Context
from django.template.loader import render_to_string
import pytz
from tastypie.http import HttpBadRequest
from corehq.apps.reports import util
from dimagi.utils.modules import to_function
from tempfile import NamedTemporaryFile
from couchexport.models import Format
from couchexport.export import export_from_tables
from couchexport.shortcuts import export_response

class HQReport(object):
    name = ""
    slug = ""
    base_slug = None # for instance, 'reports' or 'manage'

    description = ""
    report_partial = None
    title = None
    headers = None
    rows = None
    individual = None
    case_type = None
    group = None
    form = None,
    datespan = None
    show_time_notice = False
    fields = []
    exportable = False
    is_admin_report = False

    asynchronous = False
    template_name = None

    base_template_name = "reports/report_base.html"
    async_base_template_name = "reports/async/default.html"

    def __init__(self, domain, request, base_context = None):
        if request.method == 'POST':
            self.asynchronous = False
        base_context = base_context or {}
        if not self.name or not self.slug:
            raise NotImplementedError
        self.domain = domain
        self.request = request
        if not self.domain:
            self.timezone = pytz.utc
        else:
            try:
                self.timezone = util.get_timezone(self.request.couch_user.user_id, domain)
            except AttributeError:
                self.timezone = util.get_timezone(None, domain)

        if not self.rows:
            self.rows = []

        if not self.asynchronous:
            self.async_base_template_name = self.base_template_name

        self.context = base_context
        self.context.update(name = self.name,
                            slug = self.slug,
                            description = self.description,
                            template_name = self.get_template(),
                            exportable = self.exportable,
                            export_path = self.request.get_full_path().replace('/custom/', '/export/'),
                            export_formats = Format.VALID_FORMATS,
                            async_report = self.asynchronous,
                            report_base = self.async_base_template_name,
                            is_admin_report = self.is_admin_report
        )

    def build_selector_form(self):
        """
        Get custom field info and use it to construct a set of field form HTML to grab the right inputs.
        """
        if not self.fields: return
        field_classes = []
        for f in self.fields:
            klass = to_function(f)
            field_classes.append(klass(self.request, self.domain, self.timezone))
        self.context['custom_fields'] = [{"field": f.render(), "slug": f.slug} for f in field_classes]

    def get_report_context(self):
        # circular import
        from .util import report_context
        self.build_selector_form()
        self.context.update(report_context(self.domain,
                                        report_partial = self.report_partial,
                                        title = self.name,
                                        headers = self.headers,
                                        rows = self.rows,
                                        individual = self.individual,
                                        case_type = self.case_type,
                                        group = self.group,
                                        form = None, #self.form,
{'fraction_non_alphanumeric': 0.06936493143389695, 'fraction_numerical': 0.0, 'mean_word_length': 1.7894541403639064, 'pattern_counts': {'":': 4, '<': 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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1658009', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1845, 'n_words': 495}