A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Merge branch 'master' of github.com:danielquinn/paperless into ENH_filename_date_parsing
Joshua Taillon730daa3d6d6d
diff --cc docs/guesswork.rstindex 54982e96c,54982e96c..4c2a67595--- a/docs/guesswork.rst+++ b/docs/guesswork.rst@@@ -43,6 -43,6 +43,16 @@@ These however wouldn't work* ``Some Company Name, Invoice 2016-01-01, money, invoices.pdf``* ``Another Company- Letter of Reference.jpg``++Do I have to be so strict about naming?++---------------------------------------++Rather than using the strict document naming rules, one can also set the option++``PAPERLESS_FILENAME_DATE_ORDER`` in ``paperless.conf`` to any date order++that is accepted by dateparser_. Doing so will cause ``paperless`` to default++to any date format that is found in the title, instead of a date pulled from++the document's text, without requiring the strict formatting of the document++filename as described above.++++.. _dateparser: https://github.com/scrapinghub/dateparser/blob/v0.7.0/docs/usage.rst#settings.. _guesswork-content:diff --cc src/documents/parsers.pyindex 6edd1b053,1f60b1479..fec6ed67c--- a/src/documents/parsers.py+++ b/src/documents/parsers.py@@@ -36,6 -36,8 +40,9 @@@ class DocumentParser"""SCRATCH = settings.SCRATCH_DIR+ DATE_ORDER = settings.DATE_ORDER++ FILENAME_DATE_ORDER = settings.FILENAME_DATE_ORDER+ OPTIPNG = settings.OPTIPNG_BINARYdef __init__(self, path):self.document_path = path@@@ -59,7 -74,52 +79,75 @@@"""Returns the date of the document."""- raise NotImplementedError()+++ def __parser__(ds, date_order):++ """++ Call dateparser.parse with a particular date ordering++ """++ return dateparser.parse(ds,++ settings={"DATE_ORDER": date_order,++ "PREFER_DAY_OF_MONTH": "first",++ "RETURN_AS_TIMEZONE_AWARE":++ True})+ date = None+ date_string = None+++ next_year = timezone.now().year + 5 # Arbitrary 5 year future limit++ title = os.path.basename(self.document_path)++++ # if filename date parsing is enabled, search there first:++ if self.FILENAME_DATE_ORDER:++ self.log("info", "Checking document title for date")++ for m in re.finditer(DATE_REGEX, title):++ date_string = m.group(0)++++ try:++ date = __parser__(date_string, self.FILENAME_DATE_ORDER)++ except TypeError:++ # Skip all matches that do not parse to a proper date++ continue++++ if date is not None and next_year > date.year > 1900:++ self.log("info",++ "Detected document date {} based on string {} "++ "from document title"++ "".format(date.isoformat(), date_string))++ return date+++ try:++ # getting text after checking filename will save time if only++ # looking at the filename instead of the whole text+ text = self.get_text()+ except ParseError:+ return None+- next_year = timezone.now().year + 5 # Arbitrary 5 year future limit-- # Iterate through all regex matches and try to parse the date++ # Iterate through all regex matches in text and try to parse the date+ for m in re.finditer(DATE_REGEX, text):-+ date_string = m.group(0)++ try:- date = dateparser.parse(- date_string,- settings={- "DATE_ORDER": self.DATE_ORDER,- "PREFER_DAY_OF_MONTH": "first",- "RETURN_AS_TIMEZONE_AWARE": True- }- )++ date = __parser__(date_string, self.DATE_ORDER)+ except TypeError:+ # Skip all matches that do not parse to a proper date+ continue++ if date is not None and next_year > date.year > 1900:+ break+ else:+ date = None++ if date is not None:+ self.log(+ "info",+ "Detected document date {} based on string {}".format(+ date.isoformat(),+ date_string+ )+ )+ else:+ self.log("info", "Unable to detect date for document")++ return datedef log(self, level, message):getattr(self.logger, level)(message, extra={diff --cc src/paperless/settings.pyindex fb722bf14,97226ef44..3583ca03a--- a/src/paperless/settings.py+++ b/src/paperless/settings.py@@@ -292,4 -306,9 +306,10 @@@ FY_END = os.getenv("PAPERLESS_FINANCIAL# Specify the default date order (for autodetected dates)DATE_ORDER = os.getenv("PAPERLESS_DATE_ORDER", "DMY")+FILENAME_DATE_ORDER = os.getenv("PAPERLESS_FILENAME_DATE_ORDER")++ # Specify for how many years a correspondent is considered recent. Recent+ # correspondents will be shown in a separate "Recent correspondents" filter as+ # well. Set to 0 to disable this filter.+ PAPERLESS_RECENT_CORRESPONDENT_YEARS = int(os.getenv(+ "PAPERLESS_RECENT_CORRESPONDENT_YEARS", 0))diff --cc src/paperless_tesseract/tests/test_date.pyindex 7e6129e29,15fed1a37..918052abe--- a/src/paperless_tesseract/tests/test_date.py+++ b/src/paperless_tesseract/tests/test_date.py@@@ -422,75 -382,44 +422,114 @@@ class TestDate(TestCase)self.assertEqual(document._is_ocred(), True)self.assertEqual(document.get_date(),- datetime.datetime(2017, 12, 31, 0, 0, tzinfo=tz.tzutc())+ datetime.datetime(2017, 12, 31, 0, 0,+ tzinfo=tz.gettz(settings.TIME_ZONE))+ )++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_filename_date_1_pdf(self):+ input_file = os.path.join(self.SAMPLE_FILES,+ "tests_date_in_filename_2018-03-20_1.pdf")+ document = RasterisedDocumentParser(input_file)+ document.FILENAME_DATE_ORDER = 'YMD'+ document.get_text()+ date = document.get_date()+ self.assertEqual(document._is_ocred(), True)+ self.assertEqual(+ date,+ datetime.datetime(2018, 3, 20, 0, 0,+ tzinfo=tz.gettz(settings.TIME_ZONE))+ )++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_filename_date_1_png(self):+ input_file = os.path.join(self.SAMPLE_FILES,+ "tests_date_in_filename_2018-03-20_1.png")+ document = RasterisedDocumentParser(input_file)+ document.FILENAME_DATE_ORDER = 'YMD'+ date = document.get_date()+ self.assertEqual(document._is_ocred(), False)+ self.assertEqual(+ date,+ datetime.datetime(2018, 3, 20, 0, 0,+ tzinfo=tz.gettz(settings.TIME_ZONE))+ )++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_filename_date_2_pdf(self):+ input_file = os.path.join(self.SAMPLE_FILES,+ "2013-12-11_tests_date_in_filename_2.pdf")+ document = RasterisedDocumentParser(input_file)+ document.FILENAME_DATE_ORDER = 'YMD'+ date = document.get_date()+ self.assertEqual(document._is_ocred(), True)+ self.assertEqual(+ date,+ datetime.datetime(2013, 12, 11, 0, 0,+ tzinfo=tz.gettz(settings.TIME_ZONE))+ )++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_filename_date_2_png(self):+ input_file = os.path.join(self.SAMPLE_FILES,+ "2013-12-11_tests_date_in_filename_2.png")+ document = RasterisedDocumentParser(input_file)+ document.FILENAME_DATE_ORDER = 'YMD'+ date = document.get_date()+ self.assertEqual(document._is_ocred(), False)+ self.assertEqual(+ date,+ datetime.datetime(2013, 12, 11, 0, 0,+ tzinfo=tz.gettz(settings.TIME_ZONE)))++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",+ return_value="01-07-0590 00:00:00"+ )+ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_crazy_date_past(self, *args):+ document = RasterisedDocumentParser("/dev/null")+ document.get_text()+ self.assertIsNone(document.get_date())++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",+ return_value="01-07-2350 00:00:00"+ )+ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_crazy_date_future(self, *args):+ document = RasterisedDocumentParser("/dev/null")+ document.get_text()+ self.assertIsNone(document.get_date())++ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.get_text",+ return_value="01-07-0590 00:00:00"+ )+ @mock.patch(+ "paperless_tesseract.parsers.RasterisedDocumentParser.SCRATCH",+ SCRATCH+ )+ def test_crazy_date_past(self, *args):+ document = RasterisedDocumentParser("/dev/null")+ document.get_text()+ self.assertIsNone(document.get_date())