Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Parsing error when POSTing document with created date set
### Description

When POSTing to /api/post_document/ with the 'created' parameter set to a valid date string (such as '2022-15-01'), the document cannot be consumed due to the following error:

`'str' object has no attribute 'utcoffset'`

This is probably related to https://github.com/paperless-ngx/paperless-ngx/issues/2019

### Steps to reproduce

POST any valid file with created date set in the format 'yyyy-MM-dd'

### Webserver logs

```bash
[2023-01-15 12:27:41,307] [INFO] [ocrmypdf._sync] Postprocessing...
[2023-01-15 12:27:42,625] [INFO] [ocrmypdf._pipeline] Optimize ratio: 1.01 savings: 1.0%
[2023-01-15 12:27:42,629] [INFO] [ocrmypdf._sync] Output file is a PDF/A-2B (as expected)
[2023-01-15 12:27:43,122] [WARNING] [paperless.parsing.tesseract] Encountered an error while running OCR: No text was found in the original document. Attempting force OCR to get the text.
[2023-01-15 12:27:43,123] [WARNING] [paperless.parsing.tesseract] Error while getting DPI from image /tmp/paperless/paperless-upload-9pj8ilcb: 'dpi'
[2023-01-15 12:27:43,330] [INFO] [ocrmypdf._pipeline] Input file is not a PDF, checking if it is an image...
[2023-01-15 12:27:43,331] [INFO] [ocrmypdf._pipeline] Input file is an image
[2023-01-15 12:27:43,331] [INFO] [ocrmypdf._pipeline] Input image has no ICC profile, assuming sRGB
[2023-01-15 12:27:43,331] [INFO] [ocrmypdf._pipeline] Image seems valid. Try converting to PDF...
[2023-01-15 12:27:43,340] [INFO] [ocrmypdf._pipeline] Successfully converted to PDF, processing...
[2023-01-15 12:27:44,383] [INFO] [ocrmypdf._pipeline] page is facing ⇩, confidence 1.08 - confidence too low to rotate
[2023-01-15 12:27:58,005] [INFO] [ocrmypdf._sync] Postprocessing...
[2023-01-15 12:27:59,322] [INFO] [ocrmypdf._pipeline] Optimize ratio: 1.01 savings: 1.0%
[2023-01-15 12:27:59,326] [INFO] [ocrmypdf._sync] Output file is a PDF/A-2B (as expected)
[2023-01-15 12:27:59,816] [WARNING] [paperless.parsing.tesseract] No text was found in /tmp/paperless/paperless-upload-9pj8ilcb, the content will be empty.
[2023-01-15 12:28:01,879] [ERROR] [paperless.consumer] The following error occurred while consuming scan_2023_01_15T13_27_21.jpeg: 'str' object has no attribute 'utcoffset'
Traceback (most recent call last):
  File "/usr/src/paperless/src/documents/consumer.py", line 383, in try_consume_file
    document_consumption_finished.send(
  File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/usr/src/paperless/src/documents/signals/handlers.py", line 509, in set_log_entry
    object_repr=document.__str__(),
  File "/usr/src/paperless/src/documents/models.py", line 249, in __str__
    created = datetime.date.isoformat(timezone.localdate(self.created))
  File "/usr/local/lib/python3.9/site-packages/django/utils/timezone.py", line 232, in localdate
    return localtime(value, timezone).date()
  File "/usr/local/lib/python3.9/site-packages/django/utils/timezone.py", line 217, in localtime
    if is_naive(value):
  File "/usr/local/lib/python3.9/site-packages/django/utils/timezone.py", line 269, in is_naive
    return value.utcoffset() is None
AttributeError: 'str' object has no attribute 'utcoffset'
[2023-01-15 12:28:01,940] [ERROR] [celery.app.trace] Task documents.tasks.consume_file[7b89667f-7748-440a-874f-ef22d3fc8bdb] raised unexpected: ConsumerError("scan_2023_01_15T13_27_21.jpeg: The following error occurred while consuming scan_2023_01_15T13_27_21.jpeg: 'str' object has no attribute 'utcoffset'")
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 302, in main_wrap
    raise exc_info[1]
  File "/usr/src/paperless/src/documents/consumer.py", line 383, in try_consume_file
    document_consumption_finished.send(
  File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/usr/src/paperless/src/documents/signals/handlers.py", line 509, in set_log_entry
    object_repr=document.__str__(),
  File "/usr/src/paperless/src/documents/models.py", line 249, in __str__
    created = datetime.date.isoformat(timezone.localdate(self.created))
  File "/usr/local/lib/python3.9/site-packages/django/utils/timezone.py", line 232, in localdate
    return localtime(value, timezone).date()
  File "/usr/local/lib/python3.9/site-packages/django/utils/timezone.py", line 217, in localtime
    if is_naive(value):
  File "/usr/local/lib/python3.9/site-packages/django/utils/timezone.py", line 269, in is_naive
    return value.utcoffset() is None
AttributeError: 'str' object has no attribute 'utcoffset'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 451, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 734, in __protected_call__
    return self.run(*args, **kwargs)
  File "/usr/src/paperless/src/documents/tasks.py", line 179, in consume_file
    document = Consumer().try_consume_file(
  File "/usr/src/paperless/src/documents/consumer.py", line 441, in try_consume_file
    self._fail(
  File "/usr/src/paperless/src/documents/consumer.py", line 91, in _fail
    raise ConsumerError(f"{self.filename}: {log_message or message}") from exception
documents.consumer.ConsumerError: scan_2023_01_15T13_27_21.jpeg: The following error occurred while consuming scan_2023_01_15T13_27_21.jpeg: 'str' object has no attribute 'utcoffset'
```


### Browser logs

_No response_

### Paperless-ngx version

1.11.3

### Host OS

Ubuntu 20.04

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

_No response_

### Other

_No response_


Discussion:
I cant reproduce this, with that invalid date `2022-15-01` you gave I (correctly) get
```
"created": [
        "Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
    ]
```

If I use `2022-01-15` it works fine, perhaps thats what you meant?

Not to mention this exact bug was closed by the linked issue, so perhaps double check youre using the latest version?
> I cant reproduce this, with that invalid date `2022-15-01` you gave I (correctly) get
> 
> ```
> "created": [
>         "Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]."
>     ]
> ```
> 
> If I use `2022-01-15` it works fine, perhaps thats what you meant?
> 
> Not to mention this exact bug was closed by the linked issue, so perhaps double check youre using the latest version?

Yes, sorry, I meant 2022-01-15 (format in code is "yyyy-MM-dd"). I am in fact on the latest version (also shown in the webclient and in the POST response, x-api-version is 1.11.3 and api-version is 2). 
Just tried with Postman and I get the same error: 

![image](https://user-images.githubusercontent.com/79228196/212560457-d00e7c9d-b149-41a9-82f8-145e36c16180.png)

Response headers seem correct. Could this be a migration issue? Is there any way I could check?

EDIT: I just checked the tasks.py file in the running docker and the changes from the pull request are there.
That shows status 200, what was the response output?
The celery task id (some uuid), I saw this was introduced in the changelog in 1.11.3
Hmm, ill double check again later
Thanks, I'll also check with a fresh local docker instance later. 
Aha, I can recreate this, thanks for your patience. Ok I or perhaps our backend team will investigate more when we can
Thanks for your efforts, I appreciate it.

Am I right with the assumption that setting the date via the "created" form data field should overwrite the date which is potentially inferred from the system? I'll also try to look into it.

Resolving patch:
diff --git a/src/documents/tasks.py b/src/documents/tasks.py
--- a/src/documents/tasks.py
+++ b/src/documents/tasks.py
@@ -3,10 +3,10 @@
 import os
 import shutil
 import uuid
-from datetime import datetime
 from pathlib import Path
 from typing import Type
 
+import dateutil.parser
 import tqdm
 from asgiref.sync import async_to_sync
 from celery import shared_task
@@ -105,7 +105,7 @@ def consume_file(
     # More types will be retained through JSON encode/decode
     if override_created is not None and isinstance(override_created, str):
         try:
-            override_created = datetime.fromisoformat(override_created)
+            override_created = dateutil.parser.isoparse(override_created)
         except Exception:
             pass
 


Test patch: