from __future__ import absolute_import
from __future__ import unicode_literals

from dateutil.relativedelta import relativedelta
from custom.icds_reports.const import AGG_CCS_RECORD_DELIVERY_TABLE
from custom.icds_reports.utils.aggregation_helpers import BaseICDSAggregationHelper, month_formatter


class DeliveryFormsAggregationHelper(BaseICDSAggregationHelper):
    ucr_data_source_id = 'static-dashboard_delivery_forms'
    aggregate_parent_table = AGG_CCS_RECORD_DELIVERY_TABLE
    aggregate_child_table_prefix = 'icds_db_delivery_form_'

    def aggregation_query(self):
        month = self.month.replace(day=1)
        tablename = self.generate_child_tablename(month)
        current_month_start = month_formatter(self.month)
        next_month_start = month_formatter(self.month + relativedelta(months=1))

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "current_month_start": current_month_start,
            "next_month_start": next_month_start,
        }

        return """
        INSERT INTO "{tablename}" (
          case_id, state_id, month, latest_time_end_processed, breastfed_at_birth, valid_visits,
          where_born
        ) (
          SELECT
            DISTINCT case_load_ccs_record0 AS case_id,
            %(state_id)s AS state_id,
            %(month)s::DATE AS month,
            LAST_VALUE(timeend) over w AS latest_time_end_processed,
            LAST_VALUE(breastfed_at_birth) over w as breastfed_at_birth,
            SUM(CASE WHEN (unscheduled_visit=0 AND days_visit_late < 8) OR
            (timeend::DATE - next_visit) < 8 THEN 1 ELSE 0 END) OVER w as valid_visits,
            where_born
          FROM "{ucr_tablename}"
          WHERE state_id = %(state_id)s AND
                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND
                case_load_ccs_record0 IS NOT NULL
          WINDOW w AS (
            PARTITION BY case_load_ccs_record0
            ORDER BY timeend RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
          )
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=tablename
        ), query_params
@@ -1585,16 +1585,30 @@
 %3C 8) OR%0A
+              
         
@@ -1661,16 +1661,33 @@
 SE 0 END
+%0A                
 ) OVER w

Indent the query