Repository: digitalfabrik/integreat-cms

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


Discussion:
Using the same approach until the step when changing the language field to "German", I also can select nodes in the language trees of other regions as source language. This doesn't lead to a crash, but shows an error message. Is this the desired behaviour or another bug (probably with the same cause)?

Resolving patch:
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 @@ def post(self, request, site_slug, language_tree_node_id=None):
         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.'))


Test patch: