from django.db import models from django.utils.encoding import force_text from django.conf import settings from django.template.defaultfilters import slugify from taggit.managers import TaggableManager class Book(models.Model): title = models.CharField(max_length=200) author_name = models.CharField(max_length=200) slug = models.CharField(max_length=50) year_pub = models.CharField(max_length=4, blank=True, null=True) read_date = models.DateTimeField() isbn = models.CharField(max_length=100, blank=True, null=True) body_html = models.TextField(null=True, blank=True) url = models.CharField(max_length=200, blank=True, null=True) tags = TaggableManager() 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) enable_comments = models.BooleanField(default=False) image = models.FileField(upload_to='book-covers/', null=True, blank=True) class Meta: ordering = ('-read_date',) def __str__(self): return self.title def get_image_url(self): return '/media/%s' % (self.image) 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): self.slug = slugify(self.title[:50]) super(Book, self).save() class BookHighlight(models.Model): book = models.ForeignKey(Book) page = models.PositiveSmallIntegerField(null=True) location = models.PositiveSmallIntegerField(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) class BookNote(BookHighlight): pass