From a681d2cef4e4aa81c53107b229e2c737ab851d38 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 4 Jun 2018 07:44:14 +0100 Subject: [PATCH] Refactor the code for grabbing titles. --- sourcing/model.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/sourcing/model.py b/sourcing/model.py index 9b452a6..6ccf03f 100644 --- a/sourcing/model.py +++ b/sourcing/model.py @@ -266,20 +266,30 @@ class XanaLink(Item): else: return "link: " + self.hashid - @classmethod - def get_all_titles(cls): - titles = {} - for link in (obj.parse() for obj in cls.query): - if link['type'] != 'title': - continue - facet1, facet2 = link['facets'] - link_type, _, ident = facet1[0].partition(': ') - item = Item.from_external(ident) + def item_and_title(self, home=None): + link = self.parse() + if link['type'] != 'title': + return - ident2, start, length = parse_span(facet2[0]) - source_of_title = SourceDoc.from_external(ident2) - if source_of_title: - titles[item] = source_of_title.text[start:length + start] + facet1, facet2 = link['facets'] + link_type, _, ident = facet1[0].partition(': ') + item = Item.from_external(ident, home) + + ident2, start, length = parse_span(facet2[0]) + source_of_title = SourceDoc.from_external(ident2, home) + if source_of_title: + return(item, source_of_title.text[start:length + start]) + + @classmethod + def get_all_titles(cls, home=None): + titles = {} + for link in cls.query: + ret = link.item_and_title(home) + if ret is None: + continue + item, title = ret + titles[item] = title + print(link.hashid, title) return titles def snippet(self):