Repository: digitalfabrik/integreat-cms

Issue:
`UnboundLocalError` in imprint form
### Describe the Bug
<!-- A clear and concise description of what the bug is. -->
The imprint can only be created when it does not yet exist.
Once it exists, it cannot be changed anymore.

### Steps to Reproduce

1. Go to the imprint form
2. Create imprint
3. Change existing imprint translation
4. See error

### Expected Behavior
<!-- A clear and concise description of what you expected to happen. -->
The imprint translation should be changed

### Actual Behavior
<!-- A clear and concise description of what actually happened. -->
`UnboundLocalError at /augsburg/imprint/de/`
`local variable 'imprint' referenced before assignment`
### Additional Information
<!-- Add any other context (e.g. logs, screenshots, etc.) about the problem here. -->

<details>
 <summary>Traceback</summary>

```
Environment:


Request Method: POST
Request URL: http://localhost:8000/augsburg/imprint/de/

Django Version: 3.2.2
Python Version: 3.7.10
Installed Applications:
['cms.apps.CmsConfig',
 'gvz_api.apps.GvzApiConfig',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.messages',
 'django.contrib.sessions',
 'django.contrib.sitemaps',
 'django.contrib.staticfiles',
 'corsheaders',
 'widget_tweaks',
 'easy_thumbnails',
 'filer',
 'mptt',
 'rules.apps.AutodiscoverRulesConfig',
 'linkcheck',
 'django.contrib.admin']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'cms.middleware.timezone_middleware.TimezoneMiddleware']

Traceback (most recent call last):
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/src/cms/decorators.py", line 53, in wrap
    return function(request, *args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/contrib/auth/mixins.py", line 104, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/timo/job/integreat/integreat-cms/src/cms/views/imprint/imprint_view.py", line 226, in post
    imprint=imprint,

Exception Type: UnboundLocalError at /augsburg/imprint/de/
Exception Value: local variable 'imprint' referenced before assignment
```

</details>


Discussion:

Resolving patch:
diff --git a/src/cms/views/imprint/imprint_view.py b/src/cms/views/imprint/imprint_view.py
--- a/src/cms/views/imprint/imprint_view.py
+++ b/src/cms/views/imprint/imprint_view.py
@@ -220,8 +220,8 @@ def post(self, request, *args, **kwargs):
                 },
             )
 
-        if not imprint_instance:
-            imprint = ImprintPage.objects.create(region=region)
+        imprint = imprint_instance or ImprintPage.objects.create(region=region)
+
         imprint_translation = imprint_translation_form.save(
             imprint=imprint,
             user=request.user,


Test patch: