Minor fixes.
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 18 Mar 2019 09:46:23 +0000 (10:46 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 18 Mar 2019 09:46:23 +0000 (10:46 +0100)
src/basicauth.py
src/catalogue/views.py
src/oai/handlers.py
src/reporting/utils.py
src/search/index.py

index 3635727..dc7aceb 100644 (file)
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
 #############################################################################
 # from http://djangosnippets.org/snippets/243/
 
 #############################################################################
 # from http://djangosnippets.org/snippets/243/
 
@@ -29,7 +28,7 @@ def view_or_basicauth(view, request, test_func, realm="", *args, **kwargs):
             # NOTE: We are only support basic authentication for now.
             #
             if auth[0].lower() == "basic":
             # NOTE: We are only support basic authentication for now.
             #
             if auth[0].lower() == "basic":
-                uname, passwd = base64.b64decode(auth[1]).split(':')
+                uname, passwd = base64.b64decode(auth[1].encode('utf-8')).decode('utf-8').split(':')
                 user = authenticate(username=uname, password=passwd)
                 if user is not None:
                     if user.is_active:
                 user = authenticate(username=uname, password=passwd)
                 if user is not None:
                     if user.is_active:
index 5beb6dc..2c9f5dd 100644 (file)
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
@@ -512,7 +511,7 @@ def collections(request):
     objects = Collection.objects.all()
 
     if len(objects) > 3:
     objects = Collection.objects.all()
 
     if len(objects) > 3:
-        best = random.sample(objects, 3)
+        best = random.sample(list(objects), 3)
     else:
         best = objects
 
     else:
         best = objects
 
index 779406d..b2f2559 100644 (file)
@@ -98,12 +98,12 @@ class Catalogue(common.ResumptionOAIPMH):
         identifier = self.slug_to_identifier(book.slug)
         if isinstance(book, Book):
             # setSpec = map(self.tag_to_setspec, book.tags.filter(category__in=self.TAG_CATEGORIES))
         identifier = self.slug_to_identifier(book.slug)
         if isinstance(book, Book):
             # setSpec = map(self.tag_to_setspec, book.tags.filter(category__in=self.TAG_CATEGORIES))
-            header = common.Header(identifier, make_time_naive(book.changed_at), [], False)
+            header = common.Header(None, identifier, make_time_naive(book.changed_at), [], False)
             if not headers_only:
                 meta = common.Metadata(self.metadata(book))
             about = None
         elif isinstance(book, Deleted):
             if not headers_only:
                 meta = common.Metadata(self.metadata(book))
             about = None
         elif isinstance(book, Deleted):
-            header = common.Header(identifier, make_time_naive(book.deleted_at), [], True)
+            header = common.Header(None, identifier, make_time_naive(book.deleted_at), [], True)
             if not headers_only:
                 meta = common.Metadata({})
             about = None
             if not headers_only:
                 meta = common.Metadata({})
             about = None
index 955f7d9..ef4b1d0 100755 (executable)
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
@@ -72,7 +71,7 @@ def render_to_csv(output_path, template, context=None, add_files=None):
     makedirs(os.path.dirname(output_path))
 
     rendered = render_to_string(template, context)
     makedirs(os.path.dirname(output_path))
 
     rendered = render_to_string(template, context)
-    with open(output_path, 'w') as csv_file:
+    with open(output_path, 'wb') as csv_file:
         csv_file.write(rendered.encode('utf-8'))
 
 
         csv_file.write(rendered.encode('utf-8'))
 
 
@@ -111,7 +110,7 @@ def generated_file_view(file_name, mime_type, send_name=None, signals=None):
 
             response = HttpResponse(content_type=mime_type)
             response['Content-Disposition'] = 'attachment; filename=%s' % name
 
             response = HttpResponse(content_type=mime_type)
             response['Content-Disposition'] = 'attachment; filename=%s' % name
-            with open(file_path) as f:
+            with open(file_path, 'rb') as f:
                 for chunk in read_chunks(f):
                     response.write(chunk)
             return response
                 for chunk in read_chunks(f):
                     response.write(chunk)
             return response
index 7120893..9f87b99 100644 (file)
@@ -634,7 +634,10 @@ class SearchResult(object):
     def get_book(self):
         if self._book is not None:
             return self._book
     def get_book(self):
         if self._book is not None:
             return self._book
-        self._book = catalogue.models.Book.objects.get(id=self.book_id)
+        try:
+            self._book = catalogue.models.Book.objects.get(id=self.book_id)
+        except catalogue.models.Book.DoesNotExist:
+            self._book = None
         return self._book
 
     book = property(get_book)
         return self._book
 
     book = property(get_book)
@@ -746,13 +749,17 @@ class SearchResult(object):
                     books[r.book_id] = r
         return books.values()
 
                     books[r.book_id] = r
         return books.values()
 
+    def get_sort_key(self):
+        return (-self.score,
+                self.published_date,
+                self.book.sort_key_author if self.book else '',
+                self.book.sort_key if self.book else '')
+
     def __lt__(self, other):
     def __lt__(self, other):
-        return (-self.score, self.published_date, self.book.sort_key_author, self.book.sort_key) > \
-               (-other.score, other.published_date, other.book.sort_key_author, other.book.sort_key)
+        return self.get_sort_key() > other.get_sort_key()
 
     def __eq__(self, other):
 
     def __eq__(self, other):
-        return (self.score, self.published_date, self.book.sort_key_author, self.book.sort_key) == \
-               (other.score, other.published_date, other.book.sort_key_author, other.book.sort_key)
+        return self.get_sort_key() == other.get_sort_key()
 
     def __len__(self):
         return len(self.hits)
 
     def __len__(self):
         return len(self.hits)