-# add the file fields
-for format_ in Book.formats:
- field_name = "%s_file" % format_
- # This weird globals() assignment makes Django migrations comfortable.
- _upload_to = _ebook_upload_to('book/%s/%%s.%s' % (format_, format_))
- _upload_to.__name__ = '_%s_upload_to' % format_
- globals()[_upload_to.__name__] = _upload_to
-
- EbookField(format_, _("%s file" % format_.upper()),
- upload_to=_upload_to,
- storage=bofh_storage,
- max_length=255,
- blank=True,
- default=''
- ).contribute_to_class(Book, field_name)
+ 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 ridero_link(self):
+ return 'https://ridero.eu/%s/books/wl_%s/' % (get_language(), self.slug.replace('-', '_'))
+
+
+def add_file_fields():
+ for format_ in Book.formats:
+ field_name = "%s_file" % format_
+ # This weird globals() assignment makes Django migrations comfortable.
+ _upload_to = _ebook_upload_to('book/%s/%%s.%s' % (format_, format_))
+ _upload_to.__name__ = '_%s_upload_to' % format_
+ globals()[_upload_to.__name__] = _upload_to
+
+ EbookField(
+ format_, _("%s file" % format_.upper()),
+ upload_to=_upload_to,
+ storage=bofh_storage,
+ max_length=255,
+ blank=True,
+ default=''
+ ).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, db_index=True)