prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  IntegrityError in language tree
  I just found a bug causing an `IntegrityError` in the language tree. The error can be reproduced reliably in the current state of the develop branch.
  
  Steps to reproduce:
  - In the network admin view:
    - Create a new region
    - Create at least two languages (in the following steps, we assume the two languages to be German and Englisch, works with any other languages as well)
  - In the region view (in the region we just created):
    - Create a new language node for the base language (German in this example)
    - **Bug occurs in the next steps, therefore I provide a more precise description of the following steps:** in the language tree view, click on "create language tree node"
    - Choose "English" as language, "German" as source language, check the checkbox for language activation
    - click on "save", a success message should show up
    - click on "save" again without changing any form fields
    - now the form fields should have the following contents:
      - language: "English"
      - source language: "German"
      - activate language: is checked (`true`)
    - change language field to "German", as all languages can be chosen again
    - now the form fields should have the following contents:
      - language: "German"
      - source language: "German"
      - activate language: is checked (`true`)
    - click on "save" again
    - `IntegrityError` occurs
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `backend/cms/views/language_tree/language_tree_node.py`
  Content:
  ```
  1 """
  2 
  3 Returns:
  4     [type]: [description]
  5 """
  6 
  7 from django.contrib import messages
  8 from django.contrib.auth.decorators import login_required
  9 from django.contrib.auth.mixins import PermissionRequiredMixin
  10 from django.utils.translation import ugettext as _
  11 from django.utils.decorators import method_decorator
  12 from django.views.generic import TemplateView
  13 from django.shortcuts import render, redirect
  14 
  15 from .language_tree_node_form import LanguageTreeNodeForm
  16 from ...models import Language, LanguageTreeNode, Site
  17 from ...decorators import region_permission_required
  18 
  19 
  20 @method_decorator(login_required, name='dispatch')
  21 @method_decorator(region_permission_required, name='dispatch')
  22 class LanguageTreeNodeView(PermissionRequiredMixin, TemplateView):
  23     permission_required = 'cms.manage_language_tree'
  24     raise_exception = True
  25 
  26     template_name = 'language_tree/tree_node.html'
  27     base_context = {'current_menu_item': 'language_tree'}
  28 
  29     def get(self, request, *args, **kwargs):
  30         language_tree_node_id = self.kwargs.get('language_tree_node_id')
  31         # limit possible parents to nodes of current region
  32         parent_queryset = Site.get_current_site(request).language_tree_nodes
  33         # limit possible languages to those which are not yet included in the tree
  34         language_queryset = Language.objects.exclude(
  35             language_tree_nodes__in=parent_queryset.exclude(id=language_tree_node_id)
  36         )
  37         if language_tree_node_id:
  38             language_tree_node = LanguageTreeNode.objects.get(id=language_tree_node_id)
  39             children = language_tree_node.get_descendants(include_self=True)
  40             parent_queryset = parent_queryset.difference(children)
  41             form = LanguageTreeNodeForm(initial={
  42                 'language': language_tree_node.language,
  43                 'parent': language_tree_node.parent,
  44                 'active': language_tree_node.active,
  45             })
  46         else:
  47             form = LanguageTreeNodeForm()
  48         form.fields['parent'].queryset = parent_queryset
  49         form.fields['language'].queryset = language_queryset
  50         return render(request, self.template_name, {
  51             'form': form, **self.base_context})
  52 
  53     def post(self, request, site_slug, language_tree_node_id=None):
  54         # TODO: error handling
  55         form = LanguageTreeNodeForm(data=request.POST, site_slug=site_slug)
  56         if form.is_valid():
  57             if language_tree_node_id:
  58                 form.save_language_node(
  59                     language_tree_node_id=language_tree_node_id,
  60                 )
  61                 messages.success(request, _('Language tree node was saved successfully.'))
  62             else:
  63                 language_tree_node = form.save_language_node()
  64                 messages.success(request, _('Language tree node was created successfully.'))
  65                 return redirect('edit_language_tree_node', **{
  66                     'language_tree_node_id': language_tree_node.id,
  67                     'site_slug': site_slug,
  68                 })
  69             # TODO: improve messages
  70         else:
  71             messages.error(request, _('Errors have occurred.'))
  72 
  73         return render(request, self.template_name, {
  74             'form': form, **self.base_context})
  75
  ```
  --- 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/backend/cms/views/language_tree/language_tree_node.py b/backend/cms/views/language_tree/language_tree_node.py
  --- a/backend/cms/views/language_tree/language_tree_node.py
  +++ b/backend/cms/views/language_tree/language_tree_node.py
  @@ -55,17 +55,17 @@
           form = LanguageTreeNodeForm(data=request.POST, site_slug=site_slug)
           if form.is_valid():
               if language_tree_node_id:
  -                form.save_language_node(
  +                language_tree_node = form.save_language_node(
                       language_tree_node_id=language_tree_node_id,
                   )
                   messages.success(request, _('Language tree node was saved successfully.'))
               else:
                   language_tree_node = form.save_language_node()
                   messages.success(request, _('Language tree node was created successfully.'))
  -                return redirect('edit_language_tree_node', **{
  -                    'language_tree_node_id': language_tree_node.id,
  -                    'site_slug': site_slug,
  -                })
  +            return redirect('edit_language_tree_node', **{
  +                'language_tree_node_id': language_tree_node.id,
  +                'site_slug': site_slug,
  +            })
               # TODO: improve messages
           else:
               messages.error(request, _('Errors have occurred.'))
  
verification_info: {"golden_diff": "diff --git a/backend/cms/views/language_tree/language_tree_node.py b/backend/cms/views/language_tree/language_tree_node.py\n--- a/backend/cms/views/language_tree/language_tree_node.py\n+++ b/backend/cms/views/language_tree/language_tree_node.py\n@@ -55,17 +55,17 @@\n         form = LanguageTreeNodeForm(data=request.POST, site_slug=site_slug)\n         if form.is_valid():\n             if language_tree_node_id:\n-                form.save_language_node(\n+                language_tree_node = form.save_language_node(\n                     language_tree_node_id=language_tree_node_id,\n                 )\n                 messages.success(request, _('Language tree node was saved successfully.'))\n             else:\n                 language_tree_node = form.save_language_node()\n                 messages.success(request, _('Language tree node was created successfully.'))\n-                return redirect('edit_language_tree_node', **{\n-                    'language_tree_node_id': language_tree_node.id,\n-                    'site_slug': site_slug,\n-                })\n+            return redirect('edit_language_tree_node', **{\n+                'language_tree_node_id': language_tree_node.id,\n+                'site_slug': site_slug,\n+            })\n             # TODO: improve messages\n         else:\n             messages.error(request, _('Errors have occurred.'))\n", "issue": "IntegrityError in language tree\nI just found a bug causing an `IntegrityError` in the language tree. The error can be reproduced reliably in the current state of the develop branch.\r\n\r\nSteps to reproduce:\r\n- In the network admin view:\r\n  - Create a new region\r\n  - Create at least two languages (in the following steps, we assume the two languages to be German and Englisch, works with any other languages as well)\r\n- In the region view (in the region we just created):\r\n  - Create a new language node for the base language (German in this example)\r\n  - **Bug occurs in the next steps, therefore I provide a more precise description of the following steps:** in the language tree view, click on \"create language tree node\"\r\n  - Choose \"English\" as language, \"German\" as source language, check the checkbox for language activation\r\n  - click on \"save\", a success message should show up\r\n  - click on \"save\" again without changing any form fields\r\n  - now the form fields should have the following contents:\r\n    - language: \"English\"\r\n    - source language: \"German\"\r\n    - activate language: is checked (`true`)\r\n  - change language field to \"German\", as all languages can be chosen again\r\n  - now the form fields should have the following contents:\r\n    - language: \"German\"\r\n    - source language: \"German\"\r\n    - activate language: is checked (`true`)\r\n  - click on \"save\" again\r\n  - `IntegrityError` occurs\n", "before_files": [{"content": "\"\"\"\n\nReturns:\n    [type]: [description]\n\"\"\"\n\nfrom django.contrib import messages\nfrom django.contrib.auth.decorators import login_required\nfrom django.contrib.auth.mixins import PermissionRequiredMixin\nfrom django.utils.translation import ugettext as _\nfrom django.utils.decorators import method_decorator\nfrom django.views.generic import TemplateView\nfrom django.shortcuts import render, redirect\n\nfrom .language_tree_node_form import LanguageTreeNodeForm\nfrom ...models import Language, LanguageTreeNode, Site\nfrom ...decorators import region_permission_required\n\n\n@method_decorator(login_required, name='dispatch')\n@method_decorator(region_permission_required, name='dispatch')\nclass LanguageTreeNodeView(PermissionRequiredMixin, TemplateView):\n    permission_required = 'cms.manage_language_tree'\n    raise_exception = True\n\n    template_name = 'language_tree/tree_node.html'\n    base_context = {'current_menu_item': 'language_tree'}\n\n    def get(self, request, *args, **kwargs):\n        language_tree_node_id = self.kwargs.get('language_tree_node_id')\n        # limit possible parents to nodes of current region\n        parent_queryset = Site.get_current_site(request).language_tree_nodes\n        # limit possible languages to those which are not yet included in the tree\n        language_queryset = Language.objects.exclude(\n            language_tree_nodes__in=parent_queryset.exclude(id=language_tree_node_id)\n        )\n        if language_tree_node_id:\n            language_tree_node = LanguageTreeNode.objects.get(id=language_tree_node_id)\n            children = language_tree_node.get_descendants(include_self=True)\n            parent_queryset = parent_queryset.difference(children)\n            form = LanguageTreeNodeForm(initial={\n                'language': language_tree_node.language,\n                'parent': language_tree_node.parent,\n                'active': language_tree_node.active,\n            })\n        else:\n            form = LanguageTreeNodeForm()\n        form.fields['parent'].queryset = parent_queryset\n        form.fields['language'].queryset = language_queryset\n        return render(request, self.template_name, {\n            'form': form, **self.base_context})\n\n    def post(self, request, site_slug, language_tree_node_id=None):\n        # TODO: error handling\n        form = LanguageTreeNodeForm(data=request.POST, site_slug=site_slug)\n        if form.is_valid():\n            if language_tree_node_id:\n                form.save_language_node(\n                    language_tree_node_id=language_tree_node_id,\n                )\n                messages.success(request, _('Language tree node was saved successfully.'))\n            else:\n                language_tree_node = form.save_language_node()\n                messages.success(request, _('Language tree node was created successfully.'))\n                return redirect('edit_language_tree_node', **{\n                    'language_tree_node_id': language_tree_node.id,\n                    'site_slug': site_slug,\n                })\n            # TODO: improve messages\n        else:\n            messages.error(request, _('Errors have occurred.'))\n\n        return render(request, self.template_name, {\n            'form': form, **self.base_context})\n", "path": "backend/cms/views/language_tree/language_tree_node.py"}], "after_files": [{"content": "\"\"\"\n\nReturns:\n    [type]: [description]\n\"\"\"\n\nfrom django.contrib import messages\nfrom django.contrib.auth.decorators import login_required\nfrom django.contrib.auth.mixins import PermissionRequiredMixin\nfrom django.utils.translation import ugettext as _\nfrom django.utils.decorators import method_decorator\nfrom django.views.generic import TemplateView\nfrom django.shortcuts import render, redirect\n\nfrom .language_tree_node_form import LanguageTreeNodeForm\nfrom ...models import Language, LanguageTreeNode, Site\nfrom ...decorators import region_permission_required\n\n\n@method_decorator(login_required, name='dispatch')\n@method_decorator(region_permission_required, name='dispatch')\nclass LanguageTreeNodeView(PermissionRequiredMixin, TemplateView):\n    permission_required = 'cms.manage_language_tree'\n    raise_exception = True\n\n    template_name = 'language_tree/tree_node.html'\n    base_context = {'current_menu_item': 'language_tree'}\n\n    def get(self, request, *args, **kwargs):\n        language_tree_node_id = self.kwargs.get('language_tree_node_id')\n        # limit possible parents to nodes of current region\n        parent_queryset = Site.get_current_site(request).language_tree_nodes\n        # limit possible languages to those which are not yet included in the tree\n        language_queryset = Language.objects.exclude(\n            language_tree_nodes__in=parent_queryset.exclude(id=language_tree_node_id)\n        )\n        if language_tree_node_id:\n            language_tree_node = LanguageTreeNode.objects.get(id=language_tree_node_id)\n            children = language_tree_node.get_descendants(include_self=True)\n            parent_queryset = parent_queryset.difference(children)\n            form = LanguageTreeNodeForm(initial={\n                'language': language_tree_node.language,\n                'parent': language_tree_node.parent,\n                'active': language_tree_node.active,\n            })\n        else:\n            form = LanguageTreeNodeForm()\n        form.fields['parent'].queryset = parent_queryset\n        form.fields['language'].queryset = language_queryset\n        return render(request, self.template_name, {\n            'form': form, **self.base_context})\n\n    def post(self, request, site_slug, language_tree_node_id=None):\n        # TODO: error handling\n        form = LanguageTreeNodeForm(data=request.POST, site_slug=site_slug)\n        if form.is_valid():\n            if language_tree_node_id:\n                language_tree_node = form.save_language_node(\n                    language_tree_node_id=language_tree_node_id,\n                )\n                messages.success(request, _('Language tree node was saved successfully.'))\n            else:\n                language_tree_node = form.save_language_node()\n                messages.success(request, _('Language tree node was created successfully.'))\n            return redirect('edit_language_tree_node', **{\n                'language_tree_node_id': language_tree_node.id,\n                'site_slug': site_slug,\n            })\n            # TODO: improve messages\n        else:\n            messages.error(request, _('Errors have occurred.'))\n\n        return render(request, self.template_name, {\n            'form': form, **self.base_context})\n", "path": "backend/cms/views/language_tree/language_tree_node.py"}]}