diff options
Diffstat (limited to 'app/books/models.py')
-rw-r--r-- | app/books/models.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/app/books/models.py b/app/books/models.py index ef6a9b6..2aecaef 100644 --- a/app/books/models.py +++ b/app/books/models.py @@ -1,11 +1,16 @@ +import os import datetime +from PIL import Image from django.db import models +from django.db.models.signals import post_save +from django.dispatch import receiver from django.urls import reverse from django.apps import apps from django.utils.html import format_html from django.conf import settings from django.template.defaultfilters import slugify +from photos.utils import resize_image from utils.widgets import markdown_to_html @@ -51,6 +56,10 @@ class Book(models.Model): def get_image_url(self): return '%sbook-covers/%s' % (settings.IMAGES_URL, self.image.name.split('/')[-1]) + def get_small_image_url(self): + filename, file_extension = os.path.splitext(self.image.path) + return "%s/book-covers/%s_small%s" % (settings.IMAGES_URL, filename.split('/')[-1], file_extension) + def get_rating(self): return int(self.rating) @@ -60,14 +69,14 @@ class Book(models.Model): @property def get_previous_admin_url(self): - n = self.get_previous_by_pub_date() + n = self.get_previous_by_read_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]) + return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=[self.get_next_by_read_date().pk]) except model.DoesNotExist: return '' @@ -82,6 +91,15 @@ class Book(models.Model): super(Book, self).save() +@receiver(post_save, sender=Book) +def post_save_events(sender, update_fields, created, instance, **kwargs): + base_path = "%s/%s/" % (settings.MEDIA_ROOT, "/".join(str(i) for i in instance.image.name.split('/')[:-1])) + filename, file_extension = os.path.splitext(instance.image.path) + img = Image.open(instance.image.path) + resize_image(img, None, 160, 78, base_path, "%s_tn%s" % (filename.split('/')[-1], file_extension)) + resize_image(img, None, 650, 78, base_path, "%s_small%s" % (filename.split('/')[-1], file_extension)) + + class BookHighlight(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE) page = models.PositiveSmallIntegerField(null=True) |