summaryrefslogtreecommitdiff
path: root/app/books/models.py
blob: f31edcedcf2d21d8db5cd7095aee88fea4f6257e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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

# http://freewisdom.org/projects/python-markdown/
import markdown

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)
    image = models.FileField(upload_to='book-covers/', null=True, blank=True)

    class Meta:
        ordering = ('-read_date',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return "/books/%s" % (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)

    def admin_thumbnail(self):
        return force_text('<a href=""><img src="%s" width="100" style="width:100px"></a>' % (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.markdown(self.body_markdown, extensions=['extra'], safe_mode=False)
        self.slug = slugify(self.title[:50])
        super(Book, self).save()

#class Book

class BookHighlight(models.Model):
    book = models.ForeignKey(Book)
    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.markdown(self.body_markdown, extensions=['extra'], safe_mode=False)


class BookNote(BookHighlight):
    pass