fix: test file existence using metadata (#8292)
* fix: test file existance using metadata * fix: use Path more * fix: don't read the file to see if it exists * fix: more conservative error handling * chore: remove unused import
This commit is contained in:
parent
f76137eaae
commit
b39b80fe1a
|
@ -4,7 +4,6 @@
|
|||
|
||||
import datetime
|
||||
import logging
|
||||
import io
|
||||
import os
|
||||
|
||||
import django.db
|
||||
|
@ -530,16 +529,27 @@ class DocumentInfo(models.Model):
|
|||
def replaced_by(self):
|
||||
return set([ r.document for r in self.related_that("replaces") ])
|
||||
|
||||
def text(self, size = -1):
|
||||
def _text_path(self):
|
||||
path = self.get_file_name()
|
||||
root, ext = os.path.splitext(path)
|
||||
txtpath = root+'.txt'
|
||||
if ext != '.txt' and os.path.exists(txtpath):
|
||||
path = txtpath
|
||||
return path
|
||||
|
||||
def text_exists(self):
|
||||
path = Path(self._text_path())
|
||||
return path.exists()
|
||||
|
||||
def text(self, size = -1):
|
||||
path = Path(self._text_path())
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
with io.open(path, 'rb') as file:
|
||||
with path.open('rb') as file:
|
||||
raw = file.read(size)
|
||||
except IOError:
|
||||
except IOError as e:
|
||||
log.log(f"Error reading text for {path}: {e}")
|
||||
return None
|
||||
text = None
|
||||
try:
|
||||
|
|
|
@ -3318,3 +3318,18 @@ class InvestigateTests(TestCase):
|
|||
self.assertEqual(r.status_code, 200)
|
||||
q = PyQuery(r.content)
|
||||
self.assertEqual(len(q("#id_name_fragment.is-invalid")), 1)
|
||||
|
||||
class LogIOErrorTests(TestCase):
|
||||
|
||||
def test_doc_text_io_error(self):
|
||||
|
||||
d = IndividualDraftFactory()
|
||||
|
||||
with mock.patch("ietf.doc.models.Path") as path_cls_mock:
|
||||
with mock.patch("ietf.doc.models.log.log") as log_mock:
|
||||
path_cls_mock.return_value.exists.return_value = True
|
||||
path_cls_mock.return_value.open.return_value.__enter__.return_value.read.side_effect = IOError("Bad things happened")
|
||||
text = d.text()
|
||||
self.assertIsNone(text)
|
||||
self.assertTrue(log_mock.called)
|
||||
self.assertIn("Bad things happened", log_mock.call_args[0][0])
|
||||
|
|
|
@ -1081,7 +1081,7 @@ def build_file_urls(doc: Union[Document, DocHistory]):
|
|||
label = "plain text" if t == "txt" else t
|
||||
file_urls.append((label, base + doc.name + "-" + doc.rev + "." + t))
|
||||
|
||||
if doc.text():
|
||||
if doc.text_exists():
|
||||
file_urls.append(("htmlized", urlreverse('ietf.doc.views_doc.document_html', kwargs=dict(name=doc.name, rev=doc.rev))))
|
||||
file_urls.append(("pdfized", urlreverse('ietf.doc.views_doc.document_pdfized', kwargs=dict(name=doc.name, rev=doc.rev))))
|
||||
file_urls.append(("bibtex", urlreverse('ietf.doc.views_doc.document_bibtex',kwargs=dict(name=doc.name,rev=doc.rev))))
|
||||
|
|
Loading…
Reference in a new issue