4b5bf654966b44952d98908f7b3a63e095558b5a
[redakcja.git] / src / depot / models.py
1 import json
2 import os
3 import tempfile
4 import traceback
5 import zipfile
6 from datetime import datetime
7 from django.conf import settings
8 from django.db import models
9 from django.utils.timezone import now
10 from librarian.cover import make_cover
11 from librarian.builders import EpubBuilder, MobiBuilder
12 from .publishers.legimi import Legimi
13 from .publishers.woblink import Woblink
14
15
16 class Package(models.Model):
17     created_at = models.DateTimeField(auto_now_add=True)
18     placed_at = models.DateTimeField(null=True, blank=True)
19     finished_at = models.DateTimeField(null=True, blank=True)
20     definition_json = models.TextField(blank=True)
21     books = models.ManyToManyField('documents.Book')
22     status_json = models.TextField(blank=True)
23     logo = models.FileField(blank=True, upload_to='depot/logo')
24     file = models.FileField(blank=True, upload_to='depot/package/')
25
26     def save(self, *args, **kwargs):
27         try:
28             self.set_status(self.get_status())
29         except:
30             pass
31
32         try:
33             self.set_definition(self.get_definition())
34         except:
35             pass
36
37         super().save(*args, **kwargs)
38
39     def get_status(self):
40         return json.loads(self.status_json)
41
42     def set_status(self, status):
43         self.status_json = json.dumps(status, indent=4, ensure_ascii=False)
44
45     def get_definition(self):
46         return json.loads(self.definition_json)
47
48     def set_definition(self, definition):
49         self.definition_json = json.dumps(definition, indent=4, ensure_ascii=False)
50
51     def build(self):
52         f = tempfile.NamedTemporaryFile(prefix='depot-', suffix='.zip', mode='wb', delete=False)
53         book_count = self.books.all().count()
54         with zipfile.ZipFile(f, 'w') as z:
55             for i, book in enumerate(self.books.all()):
56                 print(f'{i}/{book_count} {book.slug}')
57                 self.build_for(book, z)
58         f.close()
59         with open(f.name, 'rb') as ff:
60             self.file.save('package-{}.zip'.format(datetime.now().isoformat(timespec='seconds')), ff)
61         os.unlink(f.name)
62
63     def build_for(self, book, z):
64         wldoc2 = book.wldocument(librarian2=True)
65         slug = wldoc2.meta.url.slug
66         for item in self.get_definition():
67             wldoc = book.wldocument()
68             wldoc2 = book.wldocument(librarian2=True)
69             base_url = 'file://' + book.gallery_path() + '/'
70
71             ext = item['type']
72
73             if item['type'] == 'cover':
74                 kwargs = {}
75                 if self.logo:
76                     kwargs['cover_logo'] = self.logo.path
77                 for k in 'format', 'width', 'height', 'cover_class':
78                     if k in item:
79                         kwargs[k] = item[k]
80                 cover = make_cover(wldoc.book_info, **kwargs)
81                 output = cover.output_file()
82                 ext = cover.ext()
83
84             elif item['type'] == 'pdf':
85                 cover_kwargs = {}
86                 if 'cover_class' in item:
87                     cover_kwargs['cover_class'] = item['cover_class']
88                 if self.logo:
89                     cover_kwargs['cover_logo'] = self.logo.path
90                 cover = lambda *args, **kwargs: make_cover(*args, **kwargs, **cover_kwargs)
91                 output = wldoc.as_pdf(cover=cover, base_url=base_url)
92
93             elif item['type'] == 'epub':
94                 cover_kwargs = {}
95                 if 'cover_class' in item:
96                     cover_kwargs['cover_class'] = item['cover_class']
97                 if self.logo:
98                     cover_kwargs['cover_logo'] = self.logo.path
99                 cover = lambda *args, **kwargs: make_cover(*args, **kwargs, **cover_kwargs)
100
101                 output = EpubBuilder(
102                     cover=cover,
103                     base_url=base_url,
104                     fundraising=item.get('fundraising', []),
105                 ).build(wldoc2)
106
107             elif item['type'] == 'mobi':
108                 output = MobiBuilder(
109                     cover=cover,
110                     base_url=base_url,
111                     fundraising=item.get('fundraising', []),
112                 ).build(wldoc2)
113
114             fname = f'{slug}/{slug}.'
115             if 'slug' in item:
116                 fname += item['slug'] + '.'
117             fname += ext
118
119             z.writestr(
120                 fname,
121                 output.get_bytes()
122             )
123
124
125 class ShopBookPublish(models.Model):
126     book = models.ForeignKey('documents.Book', models.CASCADE)
127     user = models.ForeignKey(settings.AUTH_USER_MODEL, models.SET_NULL, null=True)
128     shop = models.ForeignKey('Shop', models.SET_NULL, null=True)
129     created_at = models.DateTimeField()
130     started_at = models.DateTimeField(null=True, blank=True)
131     finished_at = models.DateTimeField(null=True, blank=True)
132     status = models.PositiveSmallIntegerField(choices=[
133         (0, 'queued'),
134         (10, 'running'),
135         (100, 'done'),
136         (110, 'error'),
137     ], default=0)
138     error = models.TextField(blank=True)
139
140     @classmethod
141     def create_for(cls, book, user, shop):
142         book.assert_publishable()
143         changes = book.get_current_changes(publishable=True)
144         me = cls.objects.create(book=book, user=user, shop=shop, created_at=now())
145         for change in changes:
146             me.shopchunkpublish_set.create(change=change)
147         return me
148
149     def publish(self):
150         self.status = 10
151         self.started_at = now()
152         self.save(update_fields=['status', 'started_at'])
153         try:
154             changes = [
155                 p.change for p in
156                 self.shopchunkpublish_set.order_by('change__chunk__number')
157             ]
158
159             self.shop.publish(self.book, changes=changes)
160
161         except Exception:
162             self.status = 110
163             self.error = traceback.format_exc()
164         else:
165             self.status = 100
166             self.error = ''
167         self.finished_at = now()
168         self.save(update_fields=['status', 'finished_at', 'error'])
169
170
171 class ShopChunkPublish(models.Model):
172     book_publish = models.ForeignKey(ShopBookPublish, models.CASCADE)
173     change = models.ForeignKey('documents.ChunkChange', models.CASCADE)
174
175
176 class Shop(models.Model):
177     name = models.CharField(max_length=255)
178     shop = models.CharField(max_length=32, choices=[
179         ('legimi', 'Legimi'),
180         ('woblink', 'Woblink'),
181     ])
182     username = models.CharField(max_length=255)
183     password = models.CharField(max_length=255)
184     publisher_handle = models.CharField(max_length=255, blank=True)
185     description_add = models.TextField(blank=True)
186
187     def __str__(self):
188         return self.shop
189
190     def get_texts(self):
191         return [t.text for t in self.mediainserttext_set.all()]
192
193     def get_price(self, words, pages):
194         price_obj = self.pricelevel_set.exclude(
195             min_pages__gt=pages
196         ).exclude(
197             min_words__gt=words
198         ).order_by('-price').first()
199         if price_obj is None:
200             return None
201         return price_obj.price
202
203     def get_publisher(self):
204         if self.shop == 'legimi':
205             pub_class = Legimi
206         elif self.shop == 'woblink':
207             pub_class = Woblink
208         return pub_class(self.username, self.password, self.publisher_handle)
209
210     def publish(self, book, changes):
211         self.get_publisher().send_book(
212             self, book, changes=changes,
213         )
214
215     def can_publish(self, book):
216         return self.get_publisher().can_publish(self, book)
217
218     def get_last(self, book):
219         return self.shopbookpublish_set.filter(book=book).order_by('-created_at').first()
220
221
222 class PriceLevel(models.Model):
223     shop = models.ForeignKey(Shop, models.CASCADE)
224     min_pages = models.IntegerField(null=True, blank=True)
225     min_words = models.IntegerField(null=True, blank=True)
226     price = models.IntegerField()
227
228     class Meta:
229         ordering = ('price',)
230
231
232 class MediaInsertText(models.Model):
233     shop = models.ForeignKey(Shop, models.CASCADE)
234     ordering = models.IntegerField()
235     text = models.TextField()
236
237     class Meta:
238         ordering = ('ordering',)