X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/ae60b2a3949e96357477cc04f90fd0873cee8a92..1ca64302e8447c9715ab42dd1a33075f312e6059:/src/catalogue/models/book.py?ds=inline diff --git a/src/catalogue/models/book.py b/src/catalogue/models/book.py index 5194fe1d6..f954e1a90 100644 --- a/src/catalogue/models/book.py +++ b/src/catalogue/models/book.py @@ -544,6 +544,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: @@ -563,3 +572,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)