prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  No Validation or Error when Phases in Dashboard are set in Illogical Order
  URL: https://meinberlin-dev.liqd.net/projekte/module/burgerinnenhaushalt-2-phasen/?mode=list
  user: Project initiator
  expected behaviour: If I have more than one phase in a module, I would expect them to only be able to be set to occur in 1st proposal, 2nd rating, and 3rd voting time slots. If I make a mistake, I hope I am alerted and cannot publish this.
  behaviour: Both 3-phase and 2-phase modules can be published with phases that make no sense in their time line. Users can set up and publish modules with voting phases that occur before rating and proposal phases. There is no validation or error.
  important screensize: any
  device & browser: any
  Comment/Question: moved from mB: https://github.com/liqd/a4-meinberlin/issues/4029
  
  Screenshot?
  2-Phase module published with reverse phases:
  <img width="600" alt="Bildschirmfoto 2021-12-08 um 12 41 47" src="https://user-images.githubusercontent.com/35491681/145202707-b0d39c50-e5a1-476b-9afa-542cc9a85687.png">
  
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `adhocracy4/phases/forms.py`
  Content:
  ```
  1 from django.forms.models import BaseInlineFormSet
  2 from django.utils.translation import gettext_lazy as _
  3 
  4 
  5 class PhaseInlineFormSet(BaseInlineFormSet):
  6     def clean(self):
  7         """
  8         Make sure phases of the same module don't overlap.
  9         """
  10         super().clean()
  11         phase_dates = []
  12         for form in self.forms:
  13             if 'start_date' in form.cleaned_data \
  14                     and 'end_date' in form.cleaned_data \
  15                     and form.cleaned_data['start_date'] is not None \
  16                     and form.cleaned_data['end_date'] is not None:
  17                 start_date = form.cleaned_data['start_date']
  18                 end_date = form.cleaned_data['end_date']
  19                 if phase_dates:
  20                     for phase_date in phase_dates:
  21                         if (start_date < phase_date[1]
  22                                 and phase_date[0] < end_date):
  23                             msg = _('Phases cannot run at the same time '
  24                                     'and must follow after each other.')
  25                             form.add_error('end_date', msg)
  26                 if start_date and end_date:
  27                     phase_dates.append((start_date, end_date))
  28
  ```
  --- 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/adhocracy4/phases/forms.py b/adhocracy4/phases/forms.py
  --- a/adhocracy4/phases/forms.py
  +++ b/adhocracy4/phases/forms.py
  @@ -4,9 +4,7 @@
   
   class PhaseInlineFormSet(BaseInlineFormSet):
       def clean(self):
  -        """
  -        Make sure phases of the same module don't overlap.
  -        """
  +        """Make sure phases of the same module don't overlap."""
           super().clean()
           phase_dates = []
           for form in self.forms:
  @@ -16,6 +14,7 @@
                       and form.cleaned_data['end_date'] is not None:
                   start_date = form.cleaned_data['start_date']
                   end_date = form.cleaned_data['end_date']
  +                weight = form.instance.weight
                   if phase_dates:
                       for phase_date in phase_dates:
                           if (start_date < phase_date[1]
  @@ -23,5 +22,12 @@
                               msg = _('Phases cannot run at the same time '
                                       'and must follow after each other.')
                               form.add_error('end_date', msg)
  +                        if ((start_date < phase_date[0]
  +                                and weight > phase_date[2])
  +                                or (start_date > phase_date[0]
  +                                    and weight < phase_date[2])):
  +                            msg = _('Phases need to be in same order '
  +                                    'as in form.')
  +                            form.add_error('start_date', msg)
                   if start_date and end_date:
  -                    phase_dates.append((start_date, end_date))
  +                    phase_dates.append((start_date, end_date, weight))
  
verification_info: {"golden_diff": "diff --git a/adhocracy4/phases/forms.py b/adhocracy4/phases/forms.py\n--- a/adhocracy4/phases/forms.py\n+++ b/adhocracy4/phases/forms.py\n@@ -4,9 +4,7 @@\n \n class PhaseInlineFormSet(BaseInlineFormSet):\n     def clean(self):\n-        \"\"\"\n-        Make sure phases of the same module don't overlap.\n-        \"\"\"\n+        \"\"\"Make sure phases of the same module don't overlap.\"\"\"\n         super().clean()\n         phase_dates = []\n         for form in self.forms:\n@@ -16,6 +14,7 @@\n                     and form.cleaned_data['end_date'] is not None:\n                 start_date = form.cleaned_data['start_date']\n                 end_date = form.cleaned_data['end_date']\n+                weight = form.instance.weight\n                 if phase_dates:\n                     for phase_date in phase_dates:\n                         if (start_date < phase_date[1]\n@@ -23,5 +22,12 @@\n                             msg = _('Phases cannot run at the same time '\n                                     'and must follow after each other.')\n                             form.add_error('end_date', msg)\n+                        if ((start_date < phase_date[0]\n+                                and weight > phase_date[2])\n+                                or (start_date > phase_date[0]\n+                                    and weight < phase_date[2])):\n+                            msg = _('Phases need to be in same order '\n+                                    'as in form.')\n+                            form.add_error('start_date', msg)\n                 if start_date and end_date:\n-                    phase_dates.append((start_date, end_date))\n+                    phase_dates.append((start_date, end_date, weight))\n", "issue": "No Validation or Error when Phases in Dashboard are set in Illogical Order\nURL: https://meinberlin-dev.liqd.net/projekte/module/burgerinnenhaushalt-2-phasen/?mode=list\r\nuser: Project initiator\r\nexpected behaviour: If I have more than one phase in a module, I would expect them to only be able to be set to occur in 1st proposal, 2nd rating, and 3rd voting time slots. If I make a mistake, I hope I am alerted and cannot publish this.\r\nbehaviour: Both 3-phase and 2-phase modules can be published with phases that make no sense in their time line. Users can set up and publish modules with voting phases that occur before rating and proposal phases. There is no validation or error.\r\nimportant screensize: any\r\ndevice & browser: any\r\nComment/Question: moved from mB: https://github.com/liqd/a4-meinberlin/issues/4029\r\n\r\nScreenshot?\r\n2-Phase module published with reverse phases:\r\n<img width=\"600\" alt=\"Bildschirmfoto 2021-12-08 um 12 41 47\" src=\"https://user-images.githubusercontent.com/35491681/145202707-b0d39c50-e5a1-476b-9afa-542cc9a85687.png\">\r\n\r\n\n", "before_files": [{"content": "from django.forms.models import BaseInlineFormSet\nfrom django.utils.translation import gettext_lazy as _\n\n\nclass PhaseInlineFormSet(BaseInlineFormSet):\n    def clean(self):\n        \"\"\"\n        Make sure phases of the same module don't overlap.\n        \"\"\"\n        super().clean()\n        phase_dates = []\n        for form in self.forms:\n            if 'start_date' in form.cleaned_data \\\n                    and 'end_date' in form.cleaned_data \\\n                    and form.cleaned_data['start_date'] is not None \\\n                    and form.cleaned_data['end_date'] is not None:\n                start_date = form.cleaned_data['start_date']\n                end_date = form.cleaned_data['end_date']\n                if phase_dates:\n                    for phase_date in phase_dates:\n                        if (start_date < phase_date[1]\n                                and phase_date[0] < end_date):\n                            msg = _('Phases cannot run at the same time '\n                                    'and must follow after each other.')\n                            form.add_error('end_date', msg)\n                if start_date and end_date:\n                    phase_dates.append((start_date, end_date))\n", "path": "adhocracy4/phases/forms.py"}], "after_files": [{"content": "from django.forms.models import BaseInlineFormSet\nfrom django.utils.translation import gettext_lazy as _\n\n\nclass PhaseInlineFormSet(BaseInlineFormSet):\n    def clean(self):\n        \"\"\"Make sure phases of the same module don't overlap.\"\"\"\n        super().clean()\n        phase_dates = []\n        for form in self.forms:\n            if 'start_date' in form.cleaned_data \\\n                    and 'end_date' in form.cleaned_data \\\n                    and form.cleaned_data['start_date'] is not None \\\n                    and form.cleaned_data['end_date'] is not None:\n                start_date = form.cleaned_data['start_date']\n                end_date = form.cleaned_data['end_date']\n                weight = form.instance.weight\n                if phase_dates:\n                    for phase_date in phase_dates:\n                        if (start_date < phase_date[1]\n                                and phase_date[0] < end_date):\n                            msg = _('Phases cannot run at the same time '\n                                    'and must follow after each other.')\n                            form.add_error('end_date', msg)\n                        if ((start_date < phase_date[0]\n                                and weight > phase_date[2])\n                                or (start_date > phase_date[0]\n                                    and weight < phase_date[2])):\n                            msg = _('Phases need to be in same order '\n                                    'as in form.')\n                            form.add_error('start_date', msg)\n                if start_date and end_date:\n                    phase_dates.append((start_date, end_date, weight))\n", "path": "adhocracy4/phases/forms.py"}]}