Repository: liqd/a4-opin

Issue:
Community debate module error messages
**expected behaviour:** Error message only shown for document upload if file is incorrect
**behaviour:** Error message is also shown when title is not added




Discussion:
I tested this on dev. The error message for the image is not shown when the title is not added, UNLESS an image with the wrong attirbutes induced the error message before, then the error message for the wrong image attributes just won't go away. So these are two issues in one, I guess. Also: someone taking the title out that was already there, then adding an image with wrong attirbutes is probably a rare edge case. And this is the only way that this would happen.

If only the title is missing, the page produces an HTML-browser-based error pop-up
**Browser Error Message for Missing Title:**
![image](https://user-images.githubusercontent.com/17978375/157619749-d1527448-bdf1-4b1f-b240-77f7156f31e9.png)


**Error Message for Document Upload:**
![image](https://user-images.githubusercontent.com/17978375/157616826-6ce0eec6-d550-4293-9302-eec43defda97.png)

@hello-kathi Do you want more than the browser-produced error message for the title and description fields as well, i.e. one that looks like the error message of the document-upload or are these error messages enough?


@sabinammm I think it is enough, what do you think?
@sabinammm this issue is concerning th community debate 'document upload' not the image upload in the project dashboard here is a link https://opin-dev.liqd.net/en/communitydebate/create/module/community-debate-9/ sorry should have been in original issue
The erroneous error is related to the validator here https://github.com/liqd/a4-opin/blob/6a273bdce95d29e3a47b307805e895b93d98765a/euth/communitydebate/models.py#L57
Which comes from here: https://github.com/liqd/a4-opin/blob/6a273bdce95d29e3a47b307805e895b93d98765a/euth/contrib/validators.py#L7

In addition:
In this form, images and documents that are correctly added, also disappear/are not saved if there is another error.
I also tried my luck in finding sth. There are a few weird things.
1. when I put a pdb into the validator, it gets through there twice. The first time, there is a filetype (`'application/pdf'`), which is also in the mimetypes. But the second time, it is empty and results in the thrown error (`'application/x-empty'`)
2. The fileupload with the proper errors does work for the OfflineEvents. But using the pdb there, just results in a `BdbQuit`.
Maybe the answer is somewhere in that stacktrace?
```
Traceback (most recent call last):
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/contrib/auth/mixins.py", line 104, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/katharina/a4-opin/euth/offlinephases/views.py", line 85, in post
    if form.is_valid() and upload_forms.is_valid():
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/forms/formsets.py", line 321, in is_valid
    self.errors
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/forms/formsets.py", line 304, in errors
    self.full_clean()
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/forms/formsets.py", line 361, in full_clean
    form_errors = form.errors
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/forms/forms.py", line 170, in errors
    self.full_clean()
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/forms/forms.py", line 374, in full_clean
    self._post_clean()
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/forms/models.py", line 413, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1229, in full_clean
    self.clean_fields(exclude=exclude)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/db/models/base.py", line 1271, in clean_fields
    setattr(self, f.attname, f.clean(raw_value, self))
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 671, in clean
    self.run_validators(value)
  File "/home/katharina/a4-opin/venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 623, in run_validators
    v(value)
  File "/home/katharina/a4-opin/euth/contrib/validators.py", line 18, in validate_file_type_and_size
    filetype = magic.from_buffer(upload.read(), mime=True)
  File "/home/katharina/a4-opin/euth/contrib/validators.py", line 18, in validate_file_type_and_size
    filetype = magic.from_buffer(upload.read(), mime=True)
  File "/usr/lib/python3.8/bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "/usr/lib/python3.8/bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit

Exception Type: BdbQuit at /en/dashboard/offlineevents/create/project/community-debate/
Exception Value: 
```
But that's a mystery for Monday!

Resolving patch:
diff --git a/euth/communitydebate/views.py b/euth/communitydebate/views.py
--- a/euth/communitydebate/views.py
+++ b/euth/communitydebate/views.py
@@ -95,8 +95,9 @@ def post(self, request, *args, **kwargs):
                                        'successfully created'))
                 return response
 
-        upload_forms = forms.TopicFileUploadFormset(request.POST,
-                                                    request.FILES)
+        else:
+            upload_forms = forms.TopicFileUploadFormset(request.POST,
+                                                        request.FILES)
         return render(request, self.template_name,
                       self.get_context_data(upload_forms=upload_forms))
 


Test patch: