Repository: paperless-ngx/paperless-ngx
Pull request: Bugfix document exporter document dates are off by one day #267
<!--
Note: All PRs with code changes should be targeted to the `dev` branch, pure documentation changes can target `main`
-->

## Proposed change

I added a test to describe the expectation for a filename if you have a timezone different then UTC, to simulate a bug described in #267. The test failed so I made it pass. 

Even while I'm happy to fix a bug, I'm confused. paperless-ng already had this issue (see https://github.com/jonaswinkler/paperless-ng/issues/331) and basically I reverted the production code change (see https://github.com/jonaswinkler/paperless-ng/commit/623893b528f3cd688627b59eabf67f45d439f05d). 
All tests of the suite still are passing so it seems I haven't broken something.

Fixes #267

## Type of change

<!--
What type of change does your PR introduce to Paperless-ngx?
NOTE: Please check only one box!
-->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Other (please explain)

## Checklist:

- [x] I have read & agree with the [contributing guidelines](https://github.com/paperless-ngx/paperless-ngx/blob/main/CONTRIBUTING.md).
- [ ] If applicable, I have tested my code for new features & regressions on both mobile & desktop devices, using the latest version of major browsers.
- [x] If applicable, I have checked that all tests pass, see [documentation](https://paperless-ngx.readthedocs.io/en/latest/extending.html#back-end-development).
- [x] I have run all `pre-commit` hooks, see [documentation](https://paperless-ngx.readthedocs.io/en/latest/extending.html#code-formatting-with-pre-commit-hooks).
- [ ] I have made corresponding changes to the documentation as needed.
- [x] I have checked my modifications for any breaking changes.


Problem statement:
[BUG] document_exporter document dates are off by one day
**Describe the bug**
When running the document_exporter script with the "-f" flag, all documents (and thumbnails) are off by one day.

**To Reproduce**
Steps to reproduce the behavior:
1. Running paperless-ngx on a raspi4 within a docker-container (The OS is set to the timezone "Europe/Berlin" and also within docker-compose PAPERLESS_TIME_ZONE is also set to Europe/Berlin). PAPERLESS_FILENAME_FORMAT is set to `{correspondent}/{document_type}/{created_year}/{created_year}{created_month}{created_day}_{title}_{tag_list}`
2. Consume a document as set the date to e.g. 11.03.2022
3. Check that the document is correctly stored with 20220311_xxx.pdf as filename
4. Run the document_exporter script the document (and the thumbnail) is saved as 20220310_xxx.pdf

**Expected behavior**
After running the document_exporter script the document is correctly saved as 20220311_xxx.pdf

**Relevant information**
 - RaspberryPiOS 64bit
 - Chrome
 - 1.6.0 (but was already with 1.5.0
 - docker

Hints from issue discussion:
Thanks for reporting. Possibly related: #224 
I ran into that yesterday as well. Its a Synology NAS, `TZ` is set to `Europe/Berlin` (if important). One hint regarding #224: It is unrelated to whether the date was auto-detected or not. I can simply set a date myself, the Filename will always be off by one day.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This should still be fixed I think. More soon then later, it is very annoying for users I fear 
The same problem is also present in the `document_renamer`. I used the default filename format previously (e.g. 000000123.pdf) and changed it to something else. After that I ran the document_renamer script and it populated all placeholders correctly, except for the {created_day} being off one day on all my ~800 documents.
Just updated to 1.7.0, didn't fix it. So it seems to be a different issue as #224 which was fixed in this release.
When editing the document using the web ui, the filename has the correct date, however after running the `document_renamer` it's off by one day again.

Commit message: always use datetime.date.isoformat to generate filename

fixes #267

Signed-off-by: Oliver Lippert <info@trusty.codes>

Patch to review:
diff --git a/src/documents/models.py b/src/documents/models.py
index a1a7873255c..8f531e401b0 100644
--- a/src/documents/models.py
+++ b/src/documents/models.py
@@ -11,7 +11,6 @@
 from django.contrib.auth.models import User
 from django.db import models
 from django.utils import timezone
-from django.utils.timezone import is_aware
 from django.utils.translation import gettext_lazy as _
 from documents.parsers import get_default_file_extension
 
@@ -210,10 +209,8 @@ class Meta:
         verbose_name_plural = _("documents")
 
     def __str__(self):
-        if is_aware(self.created):
-            created = timezone.localdate(self.created).isoformat()
-        else:
-            created = datetime.date.isoformat(self.created)
+        created = datetime.date.isoformat(self.created)
+
         if self.correspondent and self.title:
             return f"{created} {self.correspondent} {self.title}"
         else:
diff --git a/src/documents/tests/test_document_model.py b/src/documents/tests/test_document_model.py
index a99d6dd18e0..bea8b6b91e3 100644
--- a/src/documents/tests/test_document_model.py
+++ b/src/documents/tests/test_document_model.py
@@ -3,6 +3,7 @@
 from pathlib import Path
 from unittest import mock
 
+import pytz
 from django.test import override_settings
 from django.test import TestCase
 from django.utils import timezone
@@ -55,6 +56,24 @@ def test_file_name(self):
         )
         self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")
 
+    def test_file_name_with_timezone(self):
+
+        doc = Document(
+            mime_type="application/pdf",
+            title="test",
+            created=timezone.datetime(
+                2020,
+                12,
+                25,
+                0,
+                0,
+                0,
+                0,
+                pytz.timezone("Europe/Berlin"),
+            ),
+        )
+        self.assertEqual(doc.get_public_filename(), "2020-12-25 test.pdf")
+
     def test_file_name_jpg(self):
 
         doc = Document(


Code review comments:
diff_hunk: @@ -3,6 +3,7 @@
 from pathlib import Path
 from unittest import mock
 
+import pytz
path: src/documents/tests/test_document_model.py
text: @user1:
Based on the [Django 4 release notes](https://docs.djangoproject.com/en/4.0/releases/4.0/) using `pytz` is now deprecated.  This should be simple enough to switch over to `zoneinfo`.  See the file `test_consumer.py` for how to support the multiple Python versions

@author:
@user1 thanks for the hint. Done :)