prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Error in disaggregation view
  The PGView for disaggregation is incorrect. It includes data from all updates rather than just approved updates.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `akvo/rsr/models/result/indicator_period_aggregation.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 
  3 # Akvo Reporting is covered by the GNU Affero General Public License.
  4 # See more details in the license.txt file located at the root folder of the Akvo RSR module.
  5 # For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
  6 
  7 from django.db import models
  8 
  9 from django_pgviews import view as pg
  10 
  11 
  12 ACTUAL_VALUE_SQL = """
  13     SELECT
  14         -- row_number() OVER... creates an artificial "pk" column, without which Django will protest
  15         row_number() OVER (ORDER BY period.id) AS id,
  16         period.id AS period_id,
  17         indicator.measure as measure,
  18         sum((update.value) :: DECIMAL(20,2)) AS value,
  19         sum((update.numerator) :: DECIMAL(20,2)) AS numerator,
  20         sum((update.denominator) :: DECIMAL(20,2)) AS denominator
  21     FROM
  22         rsr_indicatorperiod period,
  23         rsr_indicator indicator,
  24         rsr_indicatorperioddata update
  25     WHERE
  26         (
  27             (((indicator.id = period.indicator_id) AND
  28             (period.id = update.period_id)) AND
  29             ((update.status) :: TEXT = 'A' :: TEXT)) AND
  30             ((update.value) :: TEXT ~ '^\d+\.?\d{0,2}$' :: TEXT OR update.value IS NULL)
  31         )
  32     GROUP BY period.id, indicator.measure;
  33 """
  34 
  35 
  36 class PeriodActualValue(pg.View):
  37     # on_delete=models.DO_NOTHING is needed to prevent problems with PG trying to delete views' data
  38     period = models.ForeignKey('IndicatorPeriod', on_delete=models.DO_NOTHING)
  39     measure = models.CharField(max_length=1)
  40     value = models.IntegerField()
  41     numerator = models.IntegerField()
  42     denominator = models.IntegerField()
  43 
  44     sql = ACTUAL_VALUE_SQL
  45 
  46     class Meta:
  47         app_label = 'rsr'
  48         db_table = 'rsr_indicator_period_actual_value'
  49         managed = False
  50 
  51 
  52 DISAGG_SQL = """
  53     WITH aggregated_disaggs AS (
  54         SELECT
  55             dimension_id,
  56             sum(("value") :: DECIMAL(20,2)) AS value,
  57             sum((numerator) :: DECIMAL(20,2)) AS numerator,
  58             sum((denominator) :: DECIMAL(20,2)) AS denominator
  59         FROM
  60             rsr_disaggregation
  61         GROUP BY
  62             dimension_id
  63     ),
  64     period_disaggs AS (
  65         SELECT DISTINCT
  66             indicator.id AS indicator_id,
  67             period.id AS period_id,
  68             dimension.name AS dimension_name,
  69             dimension.value AS dimension_value,
  70             agg.value,
  71             agg.numerator,
  72             agg.denominator
  73         FROM
  74             rsr_indicator indicator,
  75             rsr_indicatorperiod period,
  76             rsr_indicatorperioddata update,
  77             aggregated_disaggs agg,
  78             rsr_indicatordimension dimension
  79         WHERE
  80             indicator.id = period.indicator_id AND
  81             period.id = update.period_id AND
  82             indicator.id = dimension.indicator_id AND
  83             dimension.id = agg.dimension_id
  84     )
  85     SELECT
  86         row_number() OVER (ORDER BY indicator_id) AS id,
  87         *
  88     FROM period_disaggs
  89 """
  90 
  91 
  92 class PeriodDisaggregation(pg.View):
  93     indicator = models.ForeignKey('Indicator', on_delete=models.DO_NOTHING)
  94     period = models.ForeignKey('IndicatorPeriod', on_delete=models.DO_NOTHING)
  95     dimension_name = models.CharField(max_length=100)
  96     dimension_value = models.CharField(max_length=100)
  97     value = models.IntegerField()
  98     numerator = models.IntegerField()
  99     denominator = models.IntegerField()
  100 
  101     sql = DISAGG_SQL
  102 
  103     class Meta:
  104         app_label = 'rsr'
  105         db_table = 'rsr_indicator_period_disaggregation'
  106         managed = False
  107
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/akvo/rsr/models/result/indicator_period_aggregation.py b/akvo/rsr/models/result/indicator_period_aggregation.py
  --- a/akvo/rsr/models/result/indicator_period_aggregation.py
  +++ b/akvo/rsr/models/result/indicator_period_aggregation.py
  @@ -52,12 +52,16 @@
   DISAGG_SQL = """
       WITH aggregated_disaggs AS (
           SELECT
  -            dimension_id,
  -            sum(("value") :: DECIMAL(20,2)) AS value,
  -            sum((numerator) :: DECIMAL(20,2)) AS numerator,
  -            sum((denominator) :: DECIMAL(20,2)) AS denominator
  +            disagg.dimension_id AS dimension_id,
  +            sum((disagg.value) :: DECIMAL(20,2)) AS value,
  +            sum((disagg.numerator) :: DECIMAL(20,2)) AS numerator,
  +            sum((disagg.denominator) :: DECIMAL(20,2)) AS denominator
           FROM
  -            rsr_disaggregation
  +            rsr_disaggregation disagg,
  +            rsr_indicatorperioddata "update"
  +        WHERE
  +            update.status = 'A' AND
  +            disagg.update_id = update.id
           GROUP BY
               dimension_id
       ),
  
verification_info: {"golden_diff": "diff --git a/akvo/rsr/models/result/indicator_period_aggregation.py b/akvo/rsr/models/result/indicator_period_aggregation.py\n--- a/akvo/rsr/models/result/indicator_period_aggregation.py\n+++ b/akvo/rsr/models/result/indicator_period_aggregation.py\n@@ -52,12 +52,16 @@\n DISAGG_SQL = \"\"\"\n     WITH aggregated_disaggs AS (\n         SELECT\n-            dimension_id,\n-            sum((\"value\") :: DECIMAL(20,2)) AS value,\n-            sum((numerator) :: DECIMAL(20,2)) AS numerator,\n-            sum((denominator) :: DECIMAL(20,2)) AS denominator\n+            disagg.dimension_id AS dimension_id,\n+            sum((disagg.value) :: DECIMAL(20,2)) AS value,\n+            sum((disagg.numerator) :: DECIMAL(20,2)) AS numerator,\n+            sum((disagg.denominator) :: DECIMAL(20,2)) AS denominator\n         FROM\n-            rsr_disaggregation\n+            rsr_disaggregation disagg,\n+            rsr_indicatorperioddata \"update\"\n+        WHERE\n+            update.status = 'A' AND\n+            disagg.update_id = update.id\n         GROUP BY\n             dimension_id\n     ),\n", "issue": "Error in disaggregation view\nThe PGView for disaggregation is incorrect. It includes data from all updates rather than just approved updates.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Akvo Reporting is covered by the GNU Affero General Public License.\n# See more details in the license.txt file located at the root folder of the Akvo RSR module.\n# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\nfrom django.db import models\n\nfrom django_pgviews import view as pg\n\n\nACTUAL_VALUE_SQL = \"\"\"\n    SELECT\n        -- row_number() OVER... creates an artificial \"pk\" column, without which Django will protest\n        row_number() OVER (ORDER BY period.id) AS id,\n        period.id AS period_id,\n        indicator.measure as measure,\n        sum((update.value) :: DECIMAL(20,2)) AS value,\n        sum((update.numerator) :: DECIMAL(20,2)) AS numerator,\n        sum((update.denominator) :: DECIMAL(20,2)) AS denominator\n    FROM\n        rsr_indicatorperiod period,\n        rsr_indicator indicator,\n        rsr_indicatorperioddata update\n    WHERE\n        (\n            (((indicator.id = period.indicator_id) AND\n            (period.id = update.period_id)) AND\n            ((update.status) :: TEXT = 'A' :: TEXT)) AND\n            ((update.value) :: TEXT ~ '^\\d+\\.?\\d{0,2}$' :: TEXT OR update.value IS NULL)\n        )\n    GROUP BY period.id, indicator.measure;\n\"\"\"\n\n\nclass PeriodActualValue(pg.View):\n    # on_delete=models.DO_NOTHING is needed to prevent problems with PG trying to delete views' data\n    period = models.ForeignKey('IndicatorPeriod', on_delete=models.DO_NOTHING)\n    measure = models.CharField(max_length=1)\n    value = models.IntegerField()\n    numerator = models.IntegerField()\n    denominator = models.IntegerField()\n\n    sql = ACTUAL_VALUE_SQL\n\n    class Meta:\n        app_label = 'rsr'\n        db_table = 'rsr_indicator_period_actual_value'\n        managed = False\n\n\nDISAGG_SQL = \"\"\"\n    WITH aggregated_disaggs AS (\n        SELECT\n            dimension_id,\n            sum((\"value\") :: DECIMAL(20,2)) AS value,\n            sum((numerator) :: DECIMAL(20,2)) AS numerator,\n            sum((denominator) :: DECIMAL(20,2)) AS denominator\n        FROM\n            rsr_disaggregation\n        GROUP BY\n            dimension_id\n    ),\n    period_disaggs AS (\n        SELECT DISTINCT\n            indicator.id AS indicator_id,\n            period.id AS period_id,\n            dimension.name AS dimension_name,\n            dimension.value AS dimension_value,\n            agg.value,\n            agg.numerator,\n            agg.denominator\n        FROM\n            rsr_indicator indicator,\n            rsr_indicatorperiod period,\n            rsr_indicatorperioddata update,\n            aggregated_disaggs agg,\n            rsr_indicatordimension dimension\n        WHERE\n            indicator.id = period.indicator_id AND\n            period.id = update.period_id AND\n            indicator.id = dimension.indicator_id AND\n            dimension.id = agg.dimension_id\n    )\n    SELECT\n        row_number() OVER (ORDER BY indicator_id) AS id,\n        *\n    FROM period_disaggs\n\"\"\"\n\n\nclass PeriodDisaggregation(pg.View):\n    indicator = models.ForeignKey('Indicator', on_delete=models.DO_NOTHING)\n    period = models.ForeignKey('IndicatorPeriod', on_delete=models.DO_NOTHING)\n    dimension_name = models.CharField(max_length=100)\n    dimension_value = models.CharField(max_length=100)\n    value = models.IntegerField()\n    numerator = models.IntegerField()\n    denominator = models.IntegerField()\n\n    sql = DISAGG_SQL\n\n    class Meta:\n        app_label = 'rsr'\n        db_table = 'rsr_indicator_period_disaggregation'\n        managed = False\n", "path": "akvo/rsr/models/result/indicator_period_aggregation.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Akvo Reporting is covered by the GNU Affero General Public License.\n# See more details in the license.txt file located at the root folder of the Akvo RSR module.\n# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\nfrom django.db import models\n\nfrom django_pgviews import view as pg\n\n\nACTUAL_VALUE_SQL = \"\"\"\n    SELECT\n        -- row_number() OVER... creates an artificial \"pk\" column, without which Django will protest\n        row_number() OVER (ORDER BY period.id) AS id,\n        period.id AS period_id,\n        indicator.measure as measure,\n        sum((update.value) :: DECIMAL(20,2)) AS value,\n        sum((update.numerator) :: DECIMAL(20,2)) AS numerator,\n        sum((update.denominator) :: DECIMAL(20,2)) AS denominator\n    FROM\n        rsr_indicatorperiod period,\n        rsr_indicator indicator,\n        rsr_indicatorperioddata update\n    WHERE\n        (\n            (((indicator.id = period.indicator_id) AND\n            (period.id = update.period_id)) AND\n            ((update.status) :: TEXT = 'A' :: TEXT)) AND\n            ((update.value) :: TEXT ~ '^\\d+\\.?\\d{0,2}$' :: TEXT OR update.value IS NULL)\n        )\n    GROUP BY period.id, indicator.measure;\n\"\"\"\n\n\nclass PeriodActualValue(pg.View):\n    # on_delete=models.DO_NOTHING is needed to prevent problems with PG trying to delete views' data\n    period = models.ForeignKey('IndicatorPeriod', on_delete=models.DO_NOTHING)\n    measure = models.CharField(max_length=1)\n    value = models.IntegerField()\n    numerator = models.IntegerField()\n    denominator = models.IntegerField()\n\n    sql = ACTUAL_VALUE_SQL\n\n    class Meta:\n        app_label = 'rsr'\n        db_table = 'rsr_indicator_period_actual_value'\n        managed = False\n\n\nDISAGG_SQL = \"\"\"\n    WITH aggregated_disaggs AS (\n        SELECT\n            disagg.dimension_id AS dimension_id,\n            sum((disagg.value) :: DECIMAL(20,2)) AS value,\n            sum((disagg.numerator) :: DECIMAL(20,2)) AS numerator,\n            sum((disagg.denominator) :: DECIMAL(20,2)) AS denominator\n        FROM\n            rsr_disaggregation disagg,\n            rsr_indicatorperioddata \"update\"\n        WHERE\n            update.status = 'A' AND\n            disagg.update_id = update.id\n        GROUP BY\n            dimension_id\n    ),\n    period_disaggs AS (\n        SELECT DISTINCT\n            indicator.id AS indicator_id,\n            period.id AS period_id,\n            dimension.name AS dimension_name,\n            dimension.value AS dimension_value,\n            agg.value,\n            agg.numerator,\n            agg.denominator\n        FROM\n            rsr_indicator indicator,\n            rsr_indicatorperiod period,\n            rsr_indicatorperioddata update,\n            aggregated_disaggs agg,\n            rsr_indicatordimension dimension\n        WHERE\n            indicator.id = period.indicator_id AND\n            period.id = update.period_id AND\n            indicator.id = dimension.indicator_id AND\n            dimension.id = agg.dimension_id\n    )\n    SELECT\n        row_number() OVER (ORDER BY indicator_id) AS id,\n        *\n    FROM period_disaggs\n\"\"\"\n\n\nclass PeriodDisaggregation(pg.View):\n    indicator = models.ForeignKey('Indicator', on_delete=models.DO_NOTHING)\n    period = models.ForeignKey('IndicatorPeriod', on_delete=models.DO_NOTHING)\n    dimension_name = models.CharField(max_length=100)\n    dimension_value = models.CharField(max_length=100)\n    value = models.IntegerField()\n    numerator = models.IntegerField()\n    denominator = models.IntegerField()\n\n    sql = DISAGG_SQL\n\n    class Meta:\n        app_label = 'rsr'\n        db_table = 'rsr_indicator_period_disaggregation'\n        managed = False\n", "path": "akvo/rsr/models/result/indicator_period_aggregation.py"}]}