Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Some .eml files fail to be saved due to naive datetime
### Description

When consuming some .eml files, it may fail due to naive datetime.

![image](https://user-images.githubusercontent.com/68612932/210041579-09433d1e-0dc1-4d1f-91f2-96cb90f41bb3.png)

### Steps to reproduce

1. Go to Paperless-ngx Dashboard
2. Download the file below and change the extension to `.eml` (`.eml` is unsupported on GitHub)
3. Import file to Paperless-ngx
4. Error

[Shadow Tactics - Blades of the Shogun - Aiko's Choice (modified).txt](https://github.com/paperless-ngx/paperless-ngx/files/10323319/Shadow.Tactics.-.Blades.of.the.Shogun.-.Aiko.s.Choice.modified.txt)

_I have modified the file to replace identifying information. The issue still remains._

### Webserver logs

```bash
[2022-12-30 00:19:41,321] [INFO] [paperless.consumer] Consuming Shadow Tactics - Blades of the Shogun - Aiko's Choice (modified).eml
[2022-12-30 00:19:41,341] [DEBUG] [paperless.consumer] Detected mime type: message/rfc822
[2022-12-30 00:19:42,129] [DEBUG] [paperless.consumer] Parser: MailDocumentParser
[2022-12-30 00:19:42,138] [DEBUG] [paperless.consumer] Parsing Shadow Tactics - Blades of the Shogun - Aiko's Choice (modified).eml...
[2022-12-30 00:19:42,142] [INFO] [paperless.parsing.mail] Sending content to Tika server
[2022-12-30 00:19:42,207] [INFO] [paperless.parsing.mail] Converting mail to PDF
[2022-12-30 00:19:47,456] [INFO] [paperless.parsing.mail] Converting html to PDF
[2022-12-30 00:19:50,752] [DEBUG] [paperless.consumer] Generating thumbnail for Shadow Tactics - Blades of the Shogun - Aiko's Choice (modified).eml...
[2022-12-30 00:19:50,763] [DEBUG] [paperless.parsing] Execute: convert -density 300 -scale 500x5000> -alpha remove -strip -auto-orient /tmp/paperless/paperless-4z55zg_9/merged.pdf[0] /tmp/paperless/paperless-4z55zg_9/convert.webp
[2022-12-30 00:20:01,289] [DEBUG] [paperless.consumer] Saving record to database
[2022-12-30 00:20:01,291] [DEBUG] [paperless.consumer] Creation date from parse_date: 2022-11-08 08:22:07
[2022-12-30 00:20:03,742] [ERROR] [paperless.consumer] The following error occurred while consuming Shadow Tactics - Blades of the Shogun - Aiko's Choice (modified).eml: localtime() cannot be applied to a naive datetime
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 218, in localtime
    raise ValueError("localtime() cannot be applied to a naive datetime")
ValueError: localtime() cannot be applied to a naive datetime
[2022-12-30 00:20:03,776] [DEBUG] [paperless.parsing.mail] Deleting directory /tmp/paperless/paperless-4z55zg_9```
```


### Browser logs

_No response_

### Paperless-ngx version

1.11.0

### Host OS

Synology DSM `x86_64`

### Installation method

Docker - official image

### Browser

Firefox

### Configuration changes

_No response_

### Other

I imported ~10 files and this was the only one that didn't work.


Discussion:
@p-h-a-i-l  Can you look at this?
yes, thanks for the sample file, will look into it asap
I think where the date is assigned, something like this:
```python

    if is_naive(mail.date):
        self.date = make_aware(mail.date)
    else:
        self.date = mail.date
```

They might always be naive, I'm not sure

Resolving patch:
diff --git a/src/paperless_mail/parsers.py b/src/paperless_mail/parsers.py
--- a/src/paperless_mail/parsers.py
+++ b/src/paperless_mail/parsers.py
@@ -8,6 +8,8 @@
 from bleach import clean
 from bleach import linkify
 from django.conf import settings
+from django.utils.timezone import is_naive
+from django.utils.timezone import make_aware
 from documents.parsers import DocumentParser
 from documents.parsers import make_thumbnail_from_pdf
 from documents.parsers import ParseError
@@ -135,7 +137,11 @@ def strip_text(text: str):
 
         self.text += f"\n\n{strip_text(mail.text)}"
 
-        self.date = mail.date
+        if is_naive(mail.date):
+            self.date = make_aware(mail.date)
+        else:
+            self.date = mail.date
+
         self.archive_path = self.generate_pdf(document_path)
 
     def tika_parse(self, html: str):


Test patch: