X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/0bd6d7d8e87e1d538661b6167d0a5d99428dfc39..6f313f26ab28a868801a725ee986341d68ca3856:/src/catalogue/models/book.py diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index d3e2a7756..0ed97161b 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -92,7 +92,7 @@ class Book(models.Model): pass class Meta: - ordering = ('sort_key',) + ordering = ('sort_key_author', 'sort_key') verbose_name = _('book') verbose_name_plural = _('books') app_label = 'catalogue' @@ -576,6 +576,15 @@ class Book(models.Model): else: return None + def update_popularity(self): + count = self.tags.filter(category='set').values('user').order_by('user').distinct().count() + try: + pop = self.popularity + pop.count = count + pop.save() + except BookPopularity.DoesNotExist: + BookPopularity.objects.create(book=self, count=count) + def add_file_fields(): for format_ in Book.formats: @@ -595,3 +604,8 @@ def add_file_fields(): ).contribute_to_class(Book, field_name) add_file_fields() + + +class BookPopularity(models.Model): + book = models.OneToOneField(Book, related_name='popularity') + count = models.IntegerField(default=0)