X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/33af401eb4abe15a37d0147807d06a9b19eaa802..3b0dd45f80df3512dabe75506e635a0f6e3a87e3:/src/catalogue/models/book.py?ds=sidebyside diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 01f02816a..2371103e7 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -226,6 +226,18 @@ class Book(models.Model): def isbn_mobi(self): return self.get_extra_info_json().get('isbn_mobi') + def is_accessible_to(self, user): + if not self.preview: + return True + if not user.is_authenticated: + return False + Membership = apps.get_model('club', 'Membership') + if Membership.is_active_for(user): + return True + Funding = apps.get_model('funding', 'Funding') + if Funding.objects.filter(user=user, offer__book=self): + return True + return False def save(self, force_insert=False, force_update=False, **kwargs): from sortify import sortify @@ -738,6 +750,8 @@ class Book(models.Model): def update_references(self): from references.models import Entity, Reference master = self.get_master() + if master is None: + master = [] found = set() for i, sec in enumerate(master): for ref in sec.findall('.//ref'): @@ -829,7 +843,7 @@ class Book(models.Model): def related_themes(self): return Tag.objects.usage_for_queryset( Fragment.objects.filter(models.Q(book=self) | models.Q(book__ancestor=self)), - counts=True).filter(category='theme') + counts=True).filter(category='theme').order_by('-count') def parent_cover_changed(self): """Called when parent book's cover image is changed."""