Allow multiple sources.
[redakcja.git] / src / depot / publishers / legimi.py
index dbfd71e..da22245 100644 (file)
@@ -129,13 +129,17 @@ class Legimi(BasePublisher):
                 'Password': self.password,
             })
 
                 'Password': self.password,
             })
 
-    def can_publish(self, shop, book):
-        meta = book.wldocument(librarian2=True).meta
+    def can_publish(self, site, book):
         d = {
             'errors': [],
             'warnings': [],
             'info': []
         }
         d = {
             'errors': [],
             'warnings': [],
             'info': []
         }
+        try:
+            meta = book.wldocument(librarian2=True).meta
+        except:
+            d['errors'].append('Nieprawidłowy dokument.')
+            return d
         thema = self.get_thema(meta)
         if thema:
             d['info'].append(mark_safe(
         thema = self.get_thema(meta)
         if thema:
             d['info'].append(mark_safe(
@@ -178,12 +182,15 @@ class Legimi(BasePublisher):
         )
         return thema
 
         )
         return thema
 
-    def send_book(self, shop, book, changes=None):
+    def send_book(self, site_book_publish, changes=None):
+        site_book = site_book_publish.site_book
+        site = site_book.site
+        book = site_book.book
         wlbook = book.wldocument(librarian2=True, changes=changes)
         meta = wlbook.meta
 
         cover = LabelMarquiseCover(meta, width=1200).output_file()
         wlbook = book.wldocument(librarian2=True, changes=changes)
         meta = wlbook.meta
 
         cover = LabelMarquiseCover(meta, width=1200).output_file()
-        texts = shop.get_texts()
+        texts = site.get_texts()
         epub_file = EpubBuilder(
             cover=MarquiseCover,
             fundraising=texts,
         epub_file = EpubBuilder(
             cover=MarquiseCover,
             fundraising=texts,
@@ -206,7 +213,7 @@ class Legimi(BasePublisher):
             'Isbn': '',
             'LanguageLocale': lang_code_3to2(meta.language),
 
             'Isbn': '',
             'LanguageLocale': lang_code_3to2(meta.language),
 
-            'Description': self.get_description(wlbook, shop.description_add),
+            'Description': self.get_description(wlbook, site.description_add),
         }
         if meta.isbn_html:
             isbn = meta.isbn_html
         }
         if meta.isbn_html:
             isbn = meta.isbn_html
@@ -243,22 +250,22 @@ class Legimi(BasePublisher):
             'BookMobi.Name': mobi_data['name'],
         })
 
             'BookMobi.Name': mobi_data['name'],
         })
 
-        if book.legimi_id:
+        if site_book.external_id:
             self.edit(
             self.edit(
-                book.legimi_id,
+                site_book.external_id,
                 book_data
             )
             self.edit_files(
                 book_data
             )
             self.edit_files(
-                book.legimi_id,
+                site_book.external_id,
                 files_data
             )
         else:
             legimi_id = self.create_book(book_data, files_data)
             if legimi_id:
                 files_data
             )
         else:
             legimi_id = self.create_book(book_data, files_data)
             if legimi_id:
-                book.legimi_id = legimi_id
-                book.save(update_fields=['legimi_id'])
+                site_book.external_id = legimi_id
+                site_book.save(update_fields=['external_id'])
 
 
-        self.edit_sale(book)
+        self.edit_sale(site_book)
 
     def get_genre(self, wlbook):
         if wlbook.meta.legimi and wlbook.meta.legimi in self.CATEGORIES:
 
     def get_genre(self, wlbook):
         if wlbook.meta.legimi and wlbook.meta.legimi in self.CATEGORIES:
@@ -353,10 +360,11 @@ class Legimi(BasePublisher):
             data=current
         )
 
             data=current
         )
 
-    def edit_sale(self, book):
-        assert book.legimi_id
+    def edit_sale(self, site_book):
+        book = site_book.book
+        assert site_book.external_id
 
 
-        words = book.wldocument().get_statistics()['total']['words_with_fn']
+        words = book.wldocument(librarian2=True).get_statistics()['total']['words_with_fn']
 
         price = settings.LEGIMI_SMALL_PRICE
         if words > settings.LEGIMI_SMALL_WORDS:
 
         price = settings.LEGIMI_SMALL_PRICE
         if words > settings.LEGIMI_SMALL_WORDS:
@@ -366,7 +374,7 @@ class Legimi(BasePublisher):
 
         data = {
             'ValidationTrue': 'true',
 
         data = {
             'ValidationTrue': 'true',
-            'Id': book.legimi_id,
+            'Id': site_book.external_id,
             'SalesPromotionId': "0",
             'IsLibraryPass': "False",
             'OriginalEnterToTheMarketType': "No",
             'SalesPromotionId': "0",
             'IsLibraryPass': "False",
             'OriginalEnterToTheMarketType': "No",
@@ -385,6 +393,6 @@ class Legimi(BasePublisher):
         }
 
         self.session.post(
         }
 
         self.session.post(
-            self.EDIT_SALE_URL % book.legimi_id,
+            self.EDIT_SALE_URL % site_book.external_id,
             data=data
         )
             data=data
         )