Repository: digitalfabrik/integreat-cms

Issue:
User creation not working
### Describe the Bug
<!-- A clear and concise description of what the bug is. -->
Due to a problem in the user form's logging, an error occurs when new users are created and a new role is assigned in the creation process

### Steps to Reproduce

1. Go to the user management
2. Create new user and select at least one role
4. See error

### Expected Behavior
<!-- A clear and concise description of what you expected to happen. -->
The user including a corresponding user profile should be created

### Actual Behavior
<!-- A clear and concise description of what actually happened. -->
An error occurs and the user is created without a user profile
```
RelatedObjectDoesNotExist at /augsburg/users/new

User has no profile.
```
### Additional Information
<!-- Add any other context (e.g. logs, screenshots, etc.) about the problem here. -->

<details>

Environment:


Request Method: POST
Request URL: http://localhost:8000/augsburg/users/new

Django Version: 3.1.8
Python Version: 3.7.10
Installed Applications:
['cms.apps.CmsConfig',
 'gvz_api.apps.GvzApiConfig',
 'django.contrib.admin',
 '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']
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 85, 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/users/region_user_view.py", line 100, in post
    user = region_user_form.save()
  File "/home/timo/job/integreat/integreat-cms/src/cms/forms/users/user_form.py", line 96, in save
    logger.info("%r was assigned to %r", role, user.profile)
  File "/home/timo/job/integreat/integreat-cms/.venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 424, in __get__
    self.related.get_accessor_name()

Exception Type: RelatedObjectDoesNotExist at /augsburg/users/new
Exception Value: User has no profile.

</details>


Discussion:

Resolving patch:
diff --git a/src/cms/forms/users/user_form.py b/src/cms/forms/users/user_form.py
--- a/src/cms/forms/users/user_form.py
+++ b/src/cms/forms/users/user_form.py
@@ -92,7 +92,10 @@ def save(self, *args, **kwargs):
         # assign all selected roles which the user does not have already
         for role in set(self.cleaned_data["roles"]) - set(user.groups.all()):
             role.user_set.add(user)
-            logger.info("%r was assigned to %r", role, user.profile)
+            if hasattr(user, "profile"):
+                logger.info("%r was assigned to %r", role, user.profile)
+            else:
+                logger.info("%r was assigned to %r", role, user)
 
         # remove all unselected roles which the user had before
         for role in set(user.groups.all()) - set(self.cleaned_data["roles"]):


Test patch: