Workaround for API problem, minor fixes.
authorRadek Czajka <rczajka@rczajka.pl>
Fri, 6 Mar 2020 13:37:48 +0000 (13:37 +0000)
committerRadek Czajka <rczajka@rczajka.pl>
Fri, 6 Mar 2020 13:42:56 +0000 (13:42 +0000)
src/catalogue/api/urls.py
src/catalogue/api/views.py
src/lesmianator/management/commands/lesmianator.py

index 2e5a9be..4e0d937 100644 (file)
@@ -21,16 +21,16 @@ urlpatterns = [
 
     re_path(tags_re + r'books/' + paginate_re,
             piwik_track_view(views.BookList.as_view()),
-            name='catalogue_api_book_list'),
+            {"filename": "books"}, name='catalogue_api_book_list'),
     re_path(tags_re + r'parent_books/' + paginate_re,
             piwik_track_view(views.BookList.as_view()),
-            {"top_level": True}, name='catalogue_api_parent_book_list'),
+            {"filename": "parent_books", "top_level": True}, name='catalogue_api_parent_book_list'),
     re_path(tags_re + r'audiobooks/' + paginate_re,
             piwik_track_view(views.BookList.as_view()),
-            {"audiobooks": True}, name='catalogue_api_audiobook_list'),
+            {"filename": "audiobooks", "audiobooks": True}, name='catalogue_api_audiobook_list'),
     re_path(tags_re + r'daisy/' + paginate_re,
             piwik_track_view(views.BookList.as_view()),
-            {"daisy": True}, name='catalogue_api_daisy_list'),
+            {"filename": "daisy", "daisy": True}, name='catalogue_api_daisy_list'),
     re_path(r'^recommended/' + paginate_re,
             piwik_track_view(views.BookList.as_view()),
             {"recommended": True}, name='catalogue_api_recommended_list'),
index 89fafd0..335a301 100644 (file)
@@ -2,6 +2,8 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 import json
+import os.path
+from django.conf import settings
 from django.http import Http404, HttpResponse
 from django.utils.decorators import method_decorator
 from django.views.decorators.cache import never_cache
@@ -42,6 +44,16 @@ class BookList(ListAPIView):
     queryset = Book.objects.none()  # Required for DjangoModelPermissions
     serializer_class = serializers.BookListSerializer
 
+    def get(self, request, filename=None, **kwargs):
+        if filename and 'count' not in request.query_params:
+            try:
+                with open(os.path.join(settings.MEDIA_ROOT, 'api', '%s.%s' % (filename, request.accepted_renderer.format)), 'rb') as f:
+                    content = f.read()
+                return HttpResponse(content, content_type=request.accepted_media_type)
+            except:
+                pass
+        return super().get(request, filename=filename, **kwargs)
+
     def get_queryset(self):
         try:
             tags, ancestors = read_tags(
index bcad601..7d9558d 100644 (file)
@@ -75,7 +75,7 @@ class Command(BaseCommand):
 
             processed += 1
             last_word = ''
-            text = str(m.group(1), 'utf-8').lower()
+            text = m.group(1).lower()
             for letter in text:
                 mydict = lesmianator.setdefault(last_word, {})
                 mydict.setdefault(letter, 0)
@@ -91,12 +91,11 @@ class Command(BaseCommand):
             return
 
         try:
-            dump(lesmianator, open(path, 'w'))
+            dump(lesmianator, open(path, 'wb'))
         except IOError:
             print(self.style.ERROR("Couldn't write to $s" % path))
             return
 
-        dump(lesmianator, open(path, 'w'))
         if verbose >= 1:
             print("%d processed, %d skipped" % (processed, skipped))
             print("Results dumped to %s" % path)