from django.db import models from django.urls import reverse from django.utils.encoding import force_text from django.conf import settings from django.template.defaultfilters import slugify from utils.widgets import markdown_to_html def get_upload_path(self, filename): return "images/book-covers/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename) class Book(models.Model): title = models.CharField(max_length=200) author_name = models.CharField(max_length=200) slug = models.CharField(max_length=50) read_date = models.DateTimeField() isbn = models.CharField(max_length=100, blank=True, null=True) body_markdown = models.TextField(null=True, blank=True) body_html = models.TextField(null=True, blank=True) read_in = models.TextField(null=True, blank=True) url = models.CharField(max_length=200, blank=True, null=True) pages = models.CharField(max_length=5, blank=True, null=True) publish_date = models.CharField(max_length=40, blank=True, null=True) publish_place = models.CharField(max_length=100, blank=True, null=True) openlib_url = models.CharField(max_length=400, blank=True, null=True) RATINGS = ( ('1', "1 Star"), ('2', "2 Stars"), ('3', "3 Stars"), ('4', "4 Stars"), ('5', "5 Stars"), ) rating = models.CharField(max_length=1, choices=RATINGS, null=True, blank=True) enable_comments = models.BooleanField(default=False) is_public = models.BooleanField(default=True) image = models.FileField(upload_to=get_upload_path, null=True, blank=True) class Meta: ordering = ('-read_date',) def __str__(self): return self.title def get_absolute_url(self): return reverse("books:detail", kwargs={"slug": self.slug}) def get_image_url(self): return '%sbook-covers/%s.jpg' % (settings.IMAGES_URL, self.slug) def get_rating(self): return int(self.rating) @property def ratings_range(cls): return range(1, 6) @property def get_previous_admin_url(self): n = self.get_previous_by_pub_date() return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] ) @property def get_next_admin_url(self): model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name) try: return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[self.get_next_by_pub_date().pk] ) except model.DoesNotExist: return '' def admin_thumbnail(self): return force_text('' % (self.get_image_url())) admin_thumbnail.allow_tags = True admin_thumbnail.short_description = 'Thumbnail' def save(self, *args, **kwargs): if self.body_markdown: self.body_html = markdown_to_html(self.body_markdown) self.slug = slugify(self.title[:50]) super(Book, self).save() class BookHighlight(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE) page = models.PositiveSmallIntegerField(null=True) location = models.CharField(max_length=200, blank=True, null=True) date_added = models.DateTimeField() body_markdown = models.TextField() class Meta: ordering = ('-date_added',) def __str__(self): return "%s%s" % (self.book.title, self.id) def body_html(self): return markdown_to_html(self.body_markdown) class BookNote(BookHighlight): pass