-
Notifications
You must be signed in to change notification settings - Fork 17
/
models.py
129 lines (107 loc) · 4.56 KB
/
models.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from django.db import models
import os
import spine_detection
from django.core.files.base import File
import scrap_book
import random
def bookshelf_image_path(instance, filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(base_name)
new_filename = 'bookshelf'
new_filename = "{new_filename}{ext}".format(
new_filename=new_filename,
ext=ext
)
return "bookshelfs/{new_filename}".format(
new_filename=new_filename
)
def spine_drawn_bookshelf_image_path(instance, filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(base_name)
new_filename = 'spine-drawn-bookshelf'
final_filname = "{new_filename}{ext}".format(
new_filename=new_filename,
ext=ext
)
return "spine-drawn-bookshelfs/{final_filname}".format(
final_filname=final_filname
)
def spine_image_path(instance, filepath):
base_name = os.path.basename(filepath)
name, ext = os.path.splitext(base_name)
new_filename = 'spine'
final_filname = "{new_filename}{ext}".format(
new_filename=new_filename,
ext=ext
)
return "spines/{final_filname}".format(final_filname=final_filname)
class Bookshelf(models.Model):
image = models.ImageField(upload_to=bookshelf_image_path)
spine_line_drawn_image = models.ImageField(
upload_to=spine_drawn_bookshelf_image_path, null=True, blank=True)
def save(self, *args, **kwargs):
'''
Saves an image with spine lines drawn on it
'''
if self.id is None:
processed_image, extension = spine_detection.draw_spine_lines(self.image)
self.spine_line_drawn_image.save(
"image.{extension}".format(extension=extension.lower()),
File(processed_image),
save=False
)
super(Bookshelf, self).save(*args, **kwargs)
# Creates and saves cropped spines to database
spine_images = spine_detection.get_spines(self.image)
for spine_image in spine_images:
spine = Spine.objects.create(bookshelf=self)
spine.image.save(
"image.{extension}".format(extension=extension.lower()),
File(spine_image),
save=True
)
def __str__(self):
return str(self.id)
class Spine(models.Model):
image = models.ImageField(upload_to=spine_image_path, null=True, blank=True)
bookshelf = models.ForeignKey(Bookshelf, on_delete=models.CASCADE)
def __str__(self):
return "{book_id} : {spine_number}".format(
book_id=self.bookshelf.id,
spine_number=self.id
)
class Book(models.Model):
title = models.CharField(max_length=500, null=False)
author = models.CharField(max_length=500, null=True, blank=True)
price = models.CharField(max_length=20, null=True, blank=True)
rating = models.CharField(max_length=20, null=True, blank=True)
description = models.TextField(null=True, blank=True)
publisher = models.CharField(max_length=500, null=True, blank=True)
isbn_10 = models.CharField(max_length=20, null=True, blank=True)
isbn_13 = models.CharField(max_length=20, null=True, blank=True)
total_pages = models.CharField(max_length=10, null=True, blank=True)
genre = models.CharField(max_length=500, null=True, blank=True)
dimensions = models.CharField(max_length=500, null=True, blank=True)
book_cover_url = models.CharField(max_length=32656232365, null=True, blank=True)
def __str__(self):
return "{id}. {book_title}".format(id=self.id, book_title=self.title)
def save(self, *args, **kwargs):
# fetch book cover image URL and isbn then save the object
if self.id is None:
self.title = str(self.title).title()
bookInfo = scrap_book.get_book_info(self.title)
self.title = bookInfo.title
self.author = bookInfo.author
# TODO: Add actual price
self.price = "Rs. " + str(random.randint(100, 500)) + ".00"
self.rating = bookInfo.rating
self.description = bookInfo.description
self.publisher = bookInfo.publisher
self.isbn_10 = bookInfo.isbn_10
self.isbn_13 = bookInfo.isbn_13
self.total_pages = bookInfo.total_pages
self.genre = bookInfo.genre
# TODO: Add actual dimensions
self.dimensions = "19.7 x 13 x 2.2 cm"
self.book_cover_url = bookInfo.image_url
super(Book, self).save(*args, **kwargs)