+ def book_info(self, publishable=True):
+ try:
+ book_xml = self.materialize(publishable=publishable)
+ except self.NoTextError:
+ pass
+ else:
+ from librarian.dcparser import BookInfo
+ from librarian import NoDublinCore, ParseError, ValidationError
+ try:
+ return BookInfo.from_bytes(book_xml.encode('utf-8'))
+ except (self.NoTextError, ParseError, NoDublinCore, ValidationError):
+ return None
+
+ def refresh_dc_cache(self):
+ update = {
+ 'dc_slug': None,
+ 'dc_cover_image': None,
+ }
+
+ info = self.book_info()
+ if info is not None:
+ update['dc_slug'] = info.url.slug
+ if info.cover_source:
+ try:
+ image = Image.objects.get(pk=int(info.cover_source.rstrip('/').rsplit('/', 1)[-1]))
+ except:
+ pass
+ else:
+ if info.cover_source == image.get_full_url():
+ update['dc_cover_image'] = image
+ Book.objects.filter(pk=self.pk).update(**update)
+