Skip to content

Commit 4ff4805

Browse files
authored
fix: restrict rfc bulkload to those with a Document object (#10289)
* fix: restrict rfc bulkload to those with a Document object * fix: only query Document once * test: update test to match check for existing Document
1 parent 50653e9 commit 4ff4805

2 files changed

Lines changed: 38 additions & 28 deletions

File tree

ietf/sync/tests_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from django.test import override_settings
77
from ietf import settings
8+
from ietf.doc.factories import RfcFactory
89
from ietf.doc.storage_utils import exists_in_storage, retrieve_str
910
from ietf.sync.utils import build_from_file_content, load_rfcs_into_blobdb, rsync_helper
1011
from ietf.utils.test_utils import TestCase
@@ -59,6 +60,7 @@ def test_load_rfcs_into_blobdb(self):
5960
rfc_path = Path(faux_rfc_path)
6061
(rfc_path / "prerelease").mkdir()
6162
for num in [12345, 54321]:
63+
RfcFactory(rfc_number=num)
6264
for ext in settings.RFC_FILE_TYPES + ("json",):
6365
with (rfc_path / f"rfc{num}.{ext}").open("w") as f:
6466
f.write(ext)

ietf/sync/utils.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from pathlib import Path
77

88
from django.conf import settings
9-
109
from ietf.utils import log
10+
from ietf.doc.models import Document
1111
from ietf.doc.storage_utils import AlreadyExistsError, store_bytes
1212

1313

@@ -26,17 +26,42 @@ def build_from_file_content(rfc_numbers: list[int]) -> str:
2626

2727
def load_rfcs_into_blobdb(numbers: list[int]):
2828
types_to_load = settings.RFC_FILE_TYPES + ("json",)
29+
rfc_docs = Document.objects.filter(type="rfc", rfc_number__in=numbers).values_list("rfc_number", flat=True)
2930
for num in numbers:
30-
for ext in types_to_load:
31-
fs_path = Path(settings.RFC_PATH) / f"rfc{num}.{ext}"
32-
if fs_path.is_file():
33-
with fs_path.open("rb") as f:
31+
if num in rfc_docs:
32+
for ext in types_to_load:
33+
fs_path = Path(settings.RFC_PATH) / f"rfc{num}.{ext}"
34+
if fs_path.is_file():
35+
with fs_path.open("rb") as f:
36+
bytes = f.read()
37+
mtime = fs_path.stat().st_mtime
38+
try:
39+
store_bytes(
40+
kind="rfc",
41+
name=f"{ext}/rfc{num}.{ext}",
42+
content=bytes,
43+
allow_overwrite=False, # Intentionally not allowing overwrite.
44+
doc_name=f"rfc{num}",
45+
doc_rev=None,
46+
# Not setting content_type
47+
mtime=datetime.datetime.fromtimestamp(
48+
mtime, tz=datetime.UTC
49+
),
50+
)
51+
except AlreadyExistsError as e:
52+
log.log(str(e))
53+
54+
# store the not-prepped xml
55+
name = f"rfc{num}.notprepped.xml"
56+
source = Path(settings.RFC_PATH) / "prerelease" / name
57+
if source.is_file():
58+
with open(source, "rb") as f:
3459
bytes = f.read()
35-
mtime = fs_path.stat().st_mtime
60+
mtime = source.stat().st_mtime
3661
try:
3762
store_bytes(
3863
kind="rfc",
39-
name=f"{ext}/rfc{num}.{ext}",
64+
name=f"notprepped/{name}",
4065
content=bytes,
4166
allow_overwrite=False, # Intentionally not allowing overwrite.
4267
doc_name=f"rfc{num}",
@@ -46,24 +71,7 @@ def load_rfcs_into_blobdb(numbers: list[int]):
4671
)
4772
except AlreadyExistsError as e:
4873
log.log(str(e))
49-
50-
# store the not-prepped xml
51-
name = f"rfc{num}.notprepped.xml"
52-
source = Path(settings.RFC_PATH) / "prerelease" / name
53-
if source.is_file():
54-
with open(source, "rb") as f:
55-
bytes = f.read()
56-
mtime = source.stat().st_mtime
57-
try:
58-
store_bytes(
59-
kind="rfc",
60-
name=f"notprepped/{name}",
61-
content=bytes,
62-
allow_overwrite=False, # Intentionally not allowing overwrite.
63-
doc_name=f"rfc{num}",
64-
doc_rev=None,
65-
# Not setting content_type
66-
mtime=datetime.datetime.fromtimestamp(mtime, tz=datetime.UTC),
67-
)
68-
except AlreadyExistsError as e:
69-
log.log(str(e))
74+
else:
75+
log.log(
76+
f"Skipping loading rfc{num} into blobdb as no matching Document exists"
77+
)

0 commit comments

Comments
 (0)