+ def get_current_etag(self):
+ import pkg_resources
+ librarian_version = pkg_resources.get_distribution("librarian").version
+ return librarian_version
+
+ def schedule_stale(self, queryset=None):
+ """Schedule building this format for all the books where etag is stale."""
+ # If there is not ETag field, bail. That's true for xml file field.
+ if not hasattr(self.model, f'{self.attname}_etag'):
+ return
+
+ etag = self.get_current_etag()
+ if queryset is None:
+ queryset = self.model.objects.all()
+
+ if self.format_name in EBOOK_FORMATS_WITHOUT_CHILDREN + ['html']:
+ queryset = queryset.filter(children=None)
+
+ queryset = queryset.exclude(**{
+ f'{self.etag_field_name}__in': [
+ etag, f'{etag}{ETAG_SCHEDULED_SUFFIX}'
+ ]
+ })
+ for obj in queryset:
+ fieldfile = getattr(obj, self.attname)
+ priority = EBOOK_REBUILD_PRIORITY if fieldfile else EBOOK_BUILD_PRIORITY
+ fieldfile.build_delay(priority=priority)
+
+ @classmethod
+ def schedule_all_stale(cls):
+ """Schedules all stale ebooks of all formats to rebuild."""
+ for field in cls.registry:
+ field.schedule_stale()
+
+