from django.utils.functional import cached_property

from memoized import memoized

from custom.inddex.food import FoodData
from custom.inddex.ucr_data import FoodCaseData
from custom.inddex.utils import BaseGapsSummaryReport


class MasterDataFileSummaryReport(BaseGapsSummaryReport):
    title = 'Output 1 - Master Data File'
    name = title
    slug = 'output_1_master_data_file'
    export_only = False
    show_filters = True
    report_comment = 'This output includes all data that appears in the output files as well as background ' \
                     'data that are used to perform calculations that appear in the outputs.'

    @property
    @memoized
    def data_providers(self):
        return [
            MasterDataFileData(config=self.report_config),
        ]


class MasterDataFileData:
    title = 'Master Data'
    slug = 'master_data'

    def __init__(self, config):
        self.config = config

    @cached_property
    def food_data(self):
        return FoodData(
            self.config['domain'],
            FoodCaseData(self.config).get_data(),
        )

    @property
    def headers(self):
        return self.food_data.headers

    @property
    def rows(self):
        return self.food_data.rows

from django.utils.functional import cached_property

from corehq.apps.reports.datatables import DataTablesColumn, DataTablesHeader
from corehq.apps.reports.generic import GenericTabularReport
from corehq.apps.reports.standard import CustomProjectReport, DatespanMixin
from custom.inddex.filters import (
    CaseOwnersFilter,
    DateRangeFilter,
    GapTypeFilter,
    RecallStatusFilter,
)
from custom.inddex.food import FoodData
from custom.inddex.ucr_data import FoodCaseData


class MasterDataFileSummaryReport(DatespanMixin, CustomProjectReport, GenericTabularReport):
    title = 'Output 1 - Master Data File'
    name = title
    slug = 'output_1_master_data_file'
    export_only = False
    report_comment = 'This output includes all data that appears in the output files as well as background ' \
                     'data that are used to perform calculations that appear in the outputs.'

    @property
    def fields(self):
        return [CaseOwnersFilter, DateRangeFilter, GapTypeFilter, RecallStatusFilter]

    @property
    def headers(self):
        return DataTablesHeader(
            *(DataTablesColumn(header) for header in self._food_data.headers)
        )

    @property
    def rows(self):
        return self._food_data.rows

    @cached_property
    def _food_data(self):
        return FoodData(
            self.domain,
            FoodCaseData({
                'domain': self.domain,
                'startdate': str(self.datespan.startdate),
                'enddate': str(self.datespan.enddate),
                'case_owners': self.request.GET.get('case_owners') or '',
                'gap_type': self.request.GET.get('gap_type') or '',
                'recall_status': self.request.GET.get('recall_status') or '',
            }).get_data(),
        )

Make it a normal report
Make it a normal report

No need to override a bunch of stuff, assemble filters and configs
through inheritance, use a custom template, etcetera

  from django.utils.functional import cached_property
  
- from memoized import memoized
- 
+ from corehq.apps.reports.datatables import DataTablesColumn, DataTablesHeader
+ from corehq.apps.reports.generic import GenericTabularReport
+ from corehq.apps.reports.standard import CustomProjectReport, DatespanMixin
+ from custom.inddex.filters import (
+     CaseOwnersFilter,
+     DateRangeFilter,
+     GapTypeFilter,
+     RecallStatusFilter,
+ )
  from custom.inddex.food import FoodData
  from custom.inddex.ucr_data import FoodCaseData
- from custom.inddex.utils import BaseGapsSummaryReport
  
  
- class MasterDataFileSummaryReport(BaseGapsSummaryReport):
+ class MasterDataFileSummaryReport(DatespanMixin, CustomProjectReport, GenericTabularReport):
      title = 'Output 1 - Master Data File'
      name = title
      slug = 'output_1_master_data_file'
      export_only = False
-     show_filters = True
      report_comment = 'This output includes all data that appears in the output files as well as background ' \
                       'data that are used to perform calculations that appear in the outputs.'
  
      @property
+     def fields(self):
+         return [CaseOwnersFilter, DateRangeFilter, GapTypeFilter, RecallStatusFilter]
-     @memoized
-     def data_providers(self):
-         return [
-             MasterDataFileData(config=self.report_config),
-         ]
  
- 
- class MasterDataFileData:
-     title = 'Master Data'
-     slug = 'master_data'
- 
-     def __init__(self, config):
-         self.config = config
- 
-     @cached_property
+     @property
+     def headers(self):
+         return DataTablesHeader(
+             *(DataTablesColumn(header) for header in self._food_data.headers)
-     def food_data(self):
-         return FoodData(
-             self.config['domain'],
-             FoodCaseData(self.config).get_data(),
          )
  
      @property
-     def headers(self):
+     def rows(self):
-         return self.food_data.headers
+         return self._food_data.rows
  
-     @property
+     @cached_property
-     def rows(self):
-         return self.food_data.rows
+     def _food_data(self):
+         return FoodData(
+             self.domain,
+             FoodCaseData({
+                 'domain': self.domain,
+                 'startdate': str(self.datespan.startdate),
+                 'enddate': str(self.datespan.enddate),
+                 'case_owners': self.request.GET.get('case_owners') or '',
+                 'gap_type': self.request.GET.get('gap_type') or '',
+                 'recall_status': self.request.GET.get('recall_status') or '',
+             }).get_data(),
+         )
  
Make it a normal report
## Code Before:
from django.utils.functional import cached_property

from memoized import memoized

from custom.inddex.food import FoodData
from custom.inddex.ucr_data import FoodCaseData
from custom.inddex.utils import BaseGapsSummaryReport


class MasterDataFileSummaryReport(BaseGapsSummaryReport):
    title = 'Output 1 - Master Data File'
    name = title
    slug = 'output_1_master_data_file'
    export_only = False
    show_filters = True
    report_comment = 'This output includes all data that appears in the output files as well as background ' \
                     'data that are used to perform calculations that appear in the outputs.'

    @property
    @memoized
    def data_providers(self):
        return [
            MasterDataFileData(config=self.report_config),
        ]


class MasterDataFileData:
    title = 'Master Data'
    slug = 'master_data'

    def __init__(self, config):
        self.config = config

    @cached_property
    def food_data(self):
        return FoodData(
            self.config['domain'],
            FoodCaseData(self.config).get_data(),
        )

    @property
    def headers(self):
        return self.food_data.headers

    @property
    def rows(self):
        return self.food_data.rows

## Instruction:
Make it a normal report
## Code After:
from django.utils.functional import cached_property

from corehq.apps.reports.datatables import DataTablesColumn, DataTablesHeader
from corehq.apps.reports.generic import GenericTabularReport
from corehq.apps.reports.standard import CustomProjectReport, DatespanMixin
from custom.inddex.filters import (
    CaseOwnersFilter,
    DateRangeFilter,
    GapTypeFilter,
    RecallStatusFilter,
)
from custom.inddex.food import FoodData
from custom.inddex.ucr_data import FoodCaseData


class MasterDataFileSummaryReport(DatespanMixin, CustomProjectReport, GenericTabularReport):
    title = 'Output 1 - Master Data File'
    name = title
    slug = 'output_1_master_data_file'
    export_only = False
    report_comment = 'This output includes all data that appears in the output files as well as background ' \
                     'data that are used to perform calculations that appear in the outputs.'

    @property
    def fields(self):
        return [CaseOwnersFilter, DateRangeFilter, GapTypeFilter, RecallStatusFilter]

    @property
    def headers(self):
        return DataTablesHeader(
            *(DataTablesColumn(header) for header in self._food_data.headers)
        )

    @property
    def rows(self):
        return self._food_data.rows

    @cached_property
    def _food_data(self):
        return FoodData(
            self.domain,
            FoodCaseData({
                'domain': self.domain,
                'startdate': str(self.datespan.startdate),
                'enddate': str(self.datespan.enddate),
                'case_owners': self.request.GET.get('case_owners') or '',
                'gap_type': self.request.GET.get('gap_type') or '',
                'recall_status': self.request.GET.get('recall_status') or '',
            }).get_data(),
        )
