From ccdfdb7ab5b7a4c134a60e7440ffe193cb290362 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 6 Mar 2020 13:37:48 +0000 Subject: [PATCH 1/1] Workaround for API problem, minor fixes. --- src/catalogue/api/urls.py | 8 ++++---- src/catalogue/api/views.py | 12 ++++++++++++ src/lesmianator/management/commands/lesmianator.py | 5 ++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/catalogue/api/urls.py b/src/catalogue/api/urls.py index 2e5a9beb4..4e0d937c1 100644 --- a/src/catalogue/api/urls.py +++ b/src/catalogue/api/urls.py @@ -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'), diff --git a/src/catalogue/api/views.py b/src/catalogue/api/views.py index 89fafd0d2..335a30161 100644 --- a/src/catalogue/api/views.py +++ b/src/catalogue/api/views.py @@ -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( diff --git a/src/lesmianator/management/commands/lesmianator.py b/src/lesmianator/management/commands/lesmianator.py index bcad60189..7d9558d0f 100644 --- a/src/lesmianator/management/commands/lesmianator.py +++ b/src/lesmianator/management/commands/lesmianator.py @@ -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) -- 2.20.1