Skip to content

Commit a677a70

Browse files
committed
Refines Bill Fenner's regex based search through documents for references.
Populates RelatedDocument with relations for references for each type draft Document. Replaces these reference relationships with updated copies on draft submission. Note to deployer: There is a script to run in patches/fill_in_references.py that does the work of bringing the database up to date. It takes around 10 minutes to complete on a fast development laptop. fixes bug ietf-tools#1173 - Legacy-Id: 6622
2 parents 0ff1e5d + e309ff9 commit a677a70

4 files changed

Lines changed: 306542 additions & 1 deletion

File tree

ietf/doc/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from ietf.utils import markup_txt
77
from ietf.doc.models import *
88

9+
from ietf.utils import draft
10+
911
def get_state_types(doc):
1012
res = []
1113

@@ -290,3 +292,47 @@ def update_telechat(request, doc, by, new_telechat_date, new_returning_item=None
290292
e.desc = "Removed telechat returning item indication"
291293

292294
e.save()
295+
296+
def rebuild_reference_relations(doc):
297+
if doc.type.slug != 'draft':
298+
return None
299+
300+
if doc.get_state_slug() == 'rfc':
301+
filename=os.path.join(settings.RFC_PATH,doc.canonical_name()+".txt")
302+
else:
303+
filename=os.path.join(settings.INTERNET_DRAFT_PATH,doc.filename_with_rev())
304+
305+
try:
306+
refs = draft.Draft(draft._gettext(filename), filename).get_refs()
307+
except IOError as e:
308+
return { 'errors': ["%s :%s" % (e.strerror, filename)] }
309+
310+
doc.relateddocument_set.filter(relationship__slug__in=['refnorm','refinfo','refold','refunk']).delete()
311+
312+
warnings = []
313+
errors = []
314+
unfound = set()
315+
for ( ref, refType ) in refs.iteritems():
316+
refdoc = DocAlias.objects.filter( name=ref )
317+
count = refdoc.count()
318+
if count == 0:
319+
unfound.add( "%s" % ref )
320+
continue
321+
elif count > 1:
322+
errors.append("Too many DocAlias objects found for %s"%ref)
323+
else:
324+
# Don't add references to ourself
325+
if doc != refdoc[0].document:
326+
RelatedDocument.objects.get_or_create( source=doc, target=refdoc[ 0 ], relationship=DocRelationshipName.objects.get( slug='ref%s' % refType ) )
327+
if unfound:
328+
warnings.append('There were %d references with no matching DocAlias'%len(unfound))
329+
330+
ret = {}
331+
if errors:
332+
ret['errors']=errors
333+
if warnings:
334+
ret['warnings']=warnings
335+
if unfound:
336+
ret['unfound']=list(unfound)
337+
338+
return ret

0 commit comments

Comments
 (0)