Refactor the code for grabbing titles.

This commit is contained in:
Edward Betts 2018-06-04 07:44:14 +01:00
parent c7ef45d126
commit a681d2cef4

View file

@ -266,20 +266,30 @@ class XanaLink(Item):
else: else:
return "link: " + self.hashid return "link: " + self.hashid
@classmethod def item_and_title(self, home=None):
def get_all_titles(cls): link = self.parse()
titles = {}
for link in (obj.parse() for obj in cls.query):
if link['type'] != 'title': if link['type'] != 'title':
continue return
facet1, facet2 = link['facets'] facet1, facet2 = link['facets']
link_type, _, ident = facet1[0].partition(': ') link_type, _, ident = facet1[0].partition(': ')
item = Item.from_external(ident) item = Item.from_external(ident, home)
ident2, start, length = parse_span(facet2[0]) ident2, start, length = parse_span(facet2[0])
source_of_title = SourceDoc.from_external(ident2) source_of_title = SourceDoc.from_external(ident2, home)
if source_of_title: if source_of_title:
titles[item] = source_of_title.text[start:length + start] 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 return titles
def snippet(self): def snippet(self):