GitHub PullRequestReviewCommentEvent at 2014-04-07 17:49:23
Repository: commcare-hq
Actor: esoergel
Payload: {"comment":{"url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/11355838","id":11355838,"diff_hunk":"@@ -16,29 +18,165 @@\n from corehq.elastic import es_query\n from custom.m4change.constants import BOOKING_FORMS, FOLLOW_UP_FORMS, BOOKED_AND_UNBOOKED_DELIVERY_FORMS, IMMUNIZATION_FORMS\n from custom.m4change.models import McctStatus\n+from custom.m4change.reports import get_location_hierarchy_by_id\n from custom.m4change.utils import get_case_by_id, get_user_by_id, get_property, get_form_ids_by_status\n from custom.m4change.constants import EMPTY_FIELD\n \n \n-class McctProjectReview(CustomProjectReport, ElasticProjectInspectionReport, ProjectReport,\n-                        ProjectReportParametersMixin, MultiFormDrilldownMixin, DatespanMixin):\n-    name = 'mCCT Project Review Page'\n-    slug = 'mcct_project_review_page'\n-    emailable = False\n-    exportable = True\n-    asynchronous = True\n-    ajax_pagination = True\n-    include_inactive = True\n+CASE_FORM_SCRIPT_FILTER_NAMESPACES = BOOKED_AND_UNBOOKED_DELIVERY_FORMS + IMMUNIZATION_FORMS\n+\n+\n+def _get_date_range(range):\n+    if range is not None:\n+        dates = str(range).split(_(\" to \"))\n+        return (dates[0], dates[1])\n+    return None\n+\n+\n+def _get_report_query(start_date, end_date, filtered_case_ids):\n+    return {\n+        \"query\": {\n+            \"bool\": {\n+                \"must\": [\n+                    {\"range\": {\"form.meta.timeEnd\": {\"from\": start_date, \"to\": end_date, \"include_upper\": False}}},\n+                    {\"term\": {\"doc_type\": \"xforminstance\"}},\n+                ],\n+                \"should\": [\n+                    {\"terms\": {\"xmlns.exact\": CASE_FORM_SCRIPT_FILTER_NAMESPACES}},\n+                    {\"range\": {\"form.visits\": {\"gte\": 1, \"lte\": 4}}}\n+                ]\n+            }\n+        },\n+        \"filter\": {\n+            \"and\": [\n+                {\"not\": {\"missing\": {\"field\": \"form.case.@case_id\"}}},\n+                {\"terms\": {\"form.case.@case_id\": filtered_case_ids}}\n+            ]\n+        }\n+    }\n+\n+\n+def calculate_form_data(self, form):\n+    try:\n+        case_id = form[\"form\"][\"case\"][\"@case_id\"]\n+        case = get_case_by_id(case_id)\n+    except KeyError:\n+        case = EMPTY_FIELD\n+\n+    amount_due = EMPTY_FIELD\n+    service_type = EMPTY_FIELD\n+    visits = form[\"form\"].get(\"visits\")\n+    form_id = form[\"_id\"]\n+    location_name = EMPTY_FIELD\n+    location_parent_name = EMPTY_FIELD\n+    location_id = None\n+\n+    if case is not EMPTY_FIELD:\n+        user_id = get_property(case, \"user_id\", EMPTY_FIELD)\n+        user = get_user_by_id(user_id)\n+        location_id = get_commtrack_location_id(user, Domain.get_by_name(self.domain))\n+    if location_id is not None:\n+        location = Location.get(location_id)\n+        location_name = location.name\n+        location_parent = location.parent\n+        location_parent_name = location_parent.name if location_parent is not None else EMPTY_FIELD\n+\n+    if form[\"xmlns\"] in IMMUNIZATION_FORMS:\n+        service_type = _(\"Immunization\")\n+        amount_due = 1000\n+    elif form[\"xmlns\"] in BOOKED_AND_UNBOOKED_DELIVERY_FORMS:\n+        service_type = _(\"Delivery\")\n+        amount_due = 2000\n+    elif visits == \"1\":\n+        service_type = _(\"First Antenatal\")\n+        amount_due = 1000\n+    elif visits == \"2\":\n+        service_type = _(\"Second Antenatal\")\n+        amount_due = 300\n+    elif visits == \"3\":\n+        service_type = _(\"Third Antenatal\")\n+        amount_due = 300\n+    elif visits == \"4\":\n+        service_type = _(\"Fourth Antenatal\")\n+        amount_due = 400\n+\n+    return {'case': case, 'service_type': service_type, 'location_name': location_name,\n+            'location_parent_name': location_parent_name, 'amount_due': amount_due, 'form_id': form_id}\n+\n+\n+class BaseReport(CustomProjectReport, ElasticProjectInspectionReport, ProjectReport,\n+                 ProjectReportParametersMixin, MultiFormDrilldownMixin, DatespanMixin):\n+\n+        emailable = False\n+        exportable = True\n+        asynchronous = True\n+        ajax_pagination = True\n+        include_inactive = True\n+\n+        fields = [\n+            AsyncLocationFilter,\n+            'custom.m4change.fields.DateRangeField',\n+            'custom.m4change.fields.CaseSearchField'\n+        ]\n+\n+        base_template = 'reports/report.html'\n+        report_template_path = 'reports/selectTemplate.html'\n+        filter_users_field_class = StrongFilterUsersField\n+\n+        @property\n+        def es_results(self):\n+            if not getattr(self, 'es_response', None):\n+                self.es_query()\n+            return self.es_response\n+\n+        def _get_filtered_cases(self, start_date, end_date):\n+            location_id = self.request.GET.get(\"location_id\")\n+            location_ids = get_location_hierarchy_by_id(location_id, self.domain)\n+            domain = Domain.get_by_name(self.domain)\n+            all_users = CommCareUser.by_domain(self.domain)\n+            matching_user_ids = []\n+            for user in all_users:\n+                if hasattr(user, \"user_data\") and user.user_data.get(\"CCT\", None) == \"true\":\n+                    user_location_id = get_commtrack_location_id(user, domain)\n+                    if user_location_id in location_ids:\n+                        matching_user_ids.append(user._id)\n+\n+            query = {\n+                \"query\": {\n+                    \"range\": {\n+                        \"modified_on\": {\n+                            \"from\": start_date,\n+                            \"to\": end_date,\n+                            \"include_upper\": False\n+                        }\n+                    }\n+                },\n+                \"filter\": {\n+                    \"and\": [\n+                        {\"terms\": {\"user_id\": matching_user_ids}}\n+                    ]\n+                }\n+            }\n \n-    fields = [\n-        'custom.m4change.fields.DateRangeField',\n-        'custom.m4change.fields.CaseSearchField'\n-    ]\n+            case_search = self.request.GET.get(\"case_search\", \"\")\n+            if len(case_search) > 0:\n+                query[\"filter\"][\"and\"].append({\n+                    \"regexp\": {\n+                        \"name.exact\": \".*?%s.*?\" % case_search\n+                    }","path":"custom/m4change/reports/mcct_project_review.py","position":171,"original_position":171,"commit_id":"6800f67adfefd57acb49b0986d1f8e0d6ec26002","original_commit_id":"6800f67adfefd57acb49b0986d1f8e0d6ec26002","user":{"login":"esoergel","id":2367539,"avatar_url":"https://avatars.githubusercontent.com/u/2367539?","gravatar_id":"3ffda11b7953b849e1d97fd2c17f0e59","url":"https://api.github.com/users/esoergel","html_url":"https://github.com/esoergel","followers_url":"https://api.github.com/users/esoergel/followers","following_url":"https://api.github.com/users/esoergel/following{/other_user}","gists_url":"https://api.github.com/users/esoergel/gists{/gist_id}","starred_url":"https://api.github.com/users/esoergel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/esoergel/subscriptions","organizations_url":"https://api.github.com/users/esoergel/orgs","repos_url":"https://api.github.com/users/esoergel/repos","events_url":"https://api.github.com/users/esoergel/events{/privacy}","received_events_url":"https://api.github.com/users/esoergel/received_events","type":"User","site_admin":false},"body":"Oh, I didn't realize a lot of this was a refactor of previous code.  My comment still stands, but I don't think it's imperative that we fix this.","created_at":"2014-04-07T17:49:23Z","updated_at":"2014-04-07T17:49:23Z","html_url":"https://github.com/dimagi/commcare-hq/pull/2903#discussion_r11355838","pull_request_url":"https://api.github.com/repos/dimagi/commcare-hq/pulls/2903","_links":{"self":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/comments/11355838"},"html":{"href":"https://github.com/dimagi/commcare-hq/pull/2903#discussion_r11355838"},"pull_request":{"href":"https://api.github.com/repos/dimagi/commcare-hq/pulls/2903"}}}}