woblink last steps
[redakcja.git] / src / depot / publishers / woblink.py
index 00dca5e..d7d55ea 100644 (file)
@@ -8,7 +8,7 @@ from django.utils.html import escape, format_html
 from django.utils.safestring import mark_safe
 from librarian.builders.html import SnippetHtmlBuilder
 from librarian.functions import lang_code_3to2
-from catalogue.models import Author, Thema
+from catalogue.models import Audience, Author, Thema
 from .. import models
 from .base import BasePublisher
 from .woblink_constants import WOBLINK_CATEGORIES
@@ -124,11 +124,15 @@ class Woblink(BasePublisher):
     STEP1_URL = BASE_URL + 'catalog/edit/%s'
     STEP2_URL = BASE_URL + 'catalog/edit/%s/2'
     STEP3_URL = BASE_URL + 'catalog/edit/%s/3'
+    STEP4_URL = BASE_URL + 'catalog/edit/%s/4'
+    STEP5_URL = BASE_URL + 'catalog/edit/%s/5'
     UPLOAD_URL = BASE_URL + 'file/upload-%s'
     JOB_STATUS_URL = BASE_URL + 'task/status'
     GENERATE_DEMO_URL = BASE_URL + 'task/run/generate-%s-demo/%s/%d'
     CHECK_DEMO_URL = BASE_URL + 'task/run/check-%s-demo/%s'
 
+    SEARCH_CATALOGUE_URL = BASE_URL + '{category}/autocomplete/{term}'
+
     ROLE_AUTHOR = 1
     ROLE_TRANSLATOR = 4
 
@@ -148,6 +152,28 @@ class Woblink(BasePublisher):
             data=data,
         )
 
+    def search_catalogue(self, category, term):
+        return self.session.get(
+            self.SEARCH_CATALOGUE_URL.format(category=category, term=term)
+        ).json()
+
+    def search_author_catalogue(self, term):
+        return [
+            {
+                'id': item['autId'],
+                'text': item['autFullname']
+            }
+            for item in self.search_catalogue('author', term)
+        ]
+    def search_series_catalogue(self, term):
+        return [
+            {
+                'id': item['id'],
+                'text': item['name']
+            }
+            for item in self.search_catalogue('series', term)
+        ]
+        
     def get_isbn(self, meta, errors=None):
         if not meta.isbn_epub:
             if errors is not None:
@@ -219,25 +245,35 @@ class Woblink(BasePublisher):
         return category_ids
 
     def get_series(self, meta, errors=None):
-        pass
+        return list(Audience.objects.filter(code__in=meta.audiences).exclude(
+            woblink=None).values_list('woblink', flat=True))
 
     def get_abstract(self, wldoc, errors=None, description_add=None):
         description = self.get_description(wldoc, description_add)
         parts = description.split('\n', 1)
-        if len(parts) == 1 or len(parts[0]) > 200:
-            p1 = description[:200].rsplit(' ', 1)[0]
-            p2 = description[len(p1):]
-            p1 += '…'
-            p2 = '…' + p2
+        if len(parts) == 1 or len(parts[0]) > 240:
+            # No newline found here.
+            # Try to find last sentence end..
+            parts = re.split(r' \.', description[240::-1], 1)
+            if len(parts) == 2:
+                p1 = parts[1][::-1] + '.'
+                p2 = description[len(p1) + 1:]
+            else:
+                # No sentence end found.
+                # Just find a space.
+                p1 = description[:240].rsplit(' ', 1)[0]
+                p2 = description[len(p1) + 1:]
+                p1 += '…'
+                p2 = '…' + p2
             parts = [p1, p2]
 
         m = re.search(r'<[^>]+$', parts[0])
         if m is not None:
-            parts[0] = parts[:-len(m.group(0))]
+            parts[0] = parts[0][:-len(m.group(0))]
             parts[1] = m.group(0) + parts[1]
 
         opened = []
-        for tag in re.findall(r'<[^>]+[^/>]>', parts[0]):
+        for tag in re.findall(r'<[^>]*[^/>]>', parts[0]):
             if tag[1] == '/':
                 opened.pop()
             else:
@@ -254,7 +290,12 @@ class Woblink(BasePublisher):
         return lang_code_3to2(meta.language)
 
     def get_price(self, shop, wldoc, errors=None):
-        stats = wldoc.get_statistics()['total']
+        try:
+            stats = wldoc.get_statistics()['total']
+        except:
+            if errors:
+                errors.append(NoPrice(shop))
+            return 0
         words = stats['words_with_fn']
         pages = stats['chars_with_fn'] / 1800
         price = shop.get_price(words, pages)
@@ -270,6 +311,7 @@ class Woblink(BasePublisher):
         d = {
             'warnings': [],
             'errors': [],
+            'info': [],
         }
         errors = []
         book_data = self.get_book_data(shop, wldoc, errors)
@@ -281,11 +323,15 @@ class Woblink(BasePublisher):
             errlist.append(error.as_html())
 
         if book_data.get('genres'):
-            d['comment'] = format_html(
+            d['info'].append(format_html(
                 'W kategoriach: {cat} ({price} zł)',
                 cat=', '.join(self.describe_category(g) for g in book_data['genres']),
-                price=book_data['price']
-            )
+                price=book_data['price'],
+            ))
+        d['info'].append(mark_safe(
+            '<strong>' + book_data['abstract']['header'] +
+            '</strong><br/>' + book_data['abstract']['rest']
+        ))
 
         return d
 
@@ -339,6 +385,8 @@ class Woblink(BasePublisher):
             book.woblink_id, wldoc, book.gallery_path(),
             fundraising=texts
         )
+        self.edit_step4(book.woblink_id, book_data)
+        self.edit_step5(book.woblink_id, book_data)
 
     def get_book_data(self, shop, wldoc, errors=None):
         return {
@@ -351,6 +399,7 @@ class Woblink(BasePublisher):
             "lang2code": self.get_lang2code(wldoc.meta, errors=errors),
             "genres": self.get_genres(wldoc.meta, errors=errors),
             "price": self.get_price(shop, wldoc, errors=errors),
+            "series": self.get_series(wldoc.meta, errors=errors),
         }
 
     def with_form_name(self, data, name):
@@ -371,6 +420,14 @@ class Woblink(BasePublisher):
             for (author_type, author_id) in data['authors']
         ]
 
+        series_data = [
+            {
+                'PublicationId': woblink_id,
+                'SeriesId': series_id,
+            }
+            for series_id in data['series']
+        ]
+
         d = {
             'pubTitle': book_data['title'],
             'npwAuthorHasPublications': json.dumps(authors_data),
@@ -378,8 +435,7 @@ class Woblink(BasePublisher):
             'pubNote': data['abstract']['rest'],
             'pubCulture': data['lang2code'],
             'npwPublicationHasAwards': '[]',
-            'npwPublicationHasSeriess': '[]', # TODO
-                # "[{\"Id\":6153,\"PublicationId\":73876,\"SeriesId\":1615,\"Tome\":null}]"
+            'npwPublicationHasSeriess': json.dumps(series_data),
         }
         d = self.with_form_name(d, 'EditPublicationStep1')
         d['roles'] = [author_type for (author_type, author_id) in data['authors']]
@@ -402,8 +458,8 @@ class Woblink(BasePublisher):
                 if legacy is None:
                     legacy = WOBLINK_CATEGORIES[p].get('legacy')
             else:
-                gd.setdefault(p, {})
-                ds[p]['isMain'] = True
+                gd.setdefault(g, {})
+                gd[g]['isMain'] = True
         gd = [
             {
                 "pubId": woblink_id,
@@ -423,7 +479,7 @@ class Woblink(BasePublisher):
     def edit_step3(self, woblink_id, book_data):
         d = {
             'pubBasePrice': book_data['price'],
-            'pubPremiereDate': '2023-08-09', #date.today().isoformat(),
+            'pubPremiereDate': date.today().isoformat(),
             'pubIsLicenseIndefinite': '1',
             'pubFileFormat': 'epub+mobi',
             'pubIsAcs': '0',
@@ -432,6 +488,16 @@ class Woblink(BasePublisher):
         d = self.with_form_name(d, 'EditPublicationStep3')
         return self.session.post(self.STEP3_URL % woblink_id, data=d)
 
+    def edit_step4(self, woblink_id, book_data):
+        d = {}
+        d = self.with_form_name(d, 'EditPublicationStep4')
+        return self.session.post(self.STEP4_URL % woblink_id, data=d)
+
+    def edit_step5(self, woblink_id, book_data):
+        d = {}
+        d = self.with_form_name(d, 'EditPublicationStep5')
+        return self.session.post(self.STEP5_URL % woblink_id, data=d)
+
     def wait_for_job(self, job_id):
         while True:
             response = self.session.post(