Skip to content

Commit

Permalink
optimised directory listing query
Browse files Browse the repository at this point in the history
  • Loading branch information
ajurna authored and ajurna committed May 22, 2020
1 parent e4a1812 commit 6f8a7bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cbreader/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
'silk.middleware.SilkyMiddleware',
]

SILK_ENABLED = True
SILK_ENABLED = True

SILKY_PYTHON_PROFILER = True
15 changes: 11 additions & 4 deletions comic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from django.db import models
from django.db.transaction import atomic
from django.utils.http import urlsafe_base64_encode
from PyPDF4 import PdfFileReader
import PyPDF4
import PyPDF4.utils


from comic import rarfile

Expand All @@ -31,6 +33,9 @@ class Directory(models.Model):
parent = models.ForeignKey("Directory", null=True, blank=True, on_delete=models.CASCADE)
selector = models.UUIDField(unique=True, default=uuid.uuid4, db_index=True)

class Meta:
ordering = ['name']

def __str__(self):
return "Directory: {0}; {1}".format(self.name, self.parent)

Expand Down Expand Up @@ -263,10 +268,12 @@ def process_comic_book(comic_file_name, directory=False):
cbx = zipfile.ZipFile(comic_full_path)
except zipfile.BadZipFile:
cbx = None
pdf_file = None
if not cbx:
pdf_file = PdfFileReader(comic_full_path)
else:
pdf_file = None
try:
pdf_file = PyPDF4.PdfFileReader(comic_full_path)
except PyPDF4.utils.PyPdfError:
pass
if not pdf_file and not cbx:
return comic_file_name

Expand Down
18 changes: 12 additions & 6 deletions comic/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict
from os import listdir, path

from django.db.models import Count, Q
from django.db.transaction import atomic
from django.utils.http import urlsafe_base64_encode

Expand Down Expand Up @@ -94,7 +95,7 @@ def populate_directory(self, directory, user):
self.icon = "fa-folder-open"
self.selector = urlsafe_base64_encode(directory.selector.bytes)
self.location = "/comic/{0}/".format(self.selector)
self.label = generate_dir_status(user, directory)
self.label = generate_dir_status(directory.total, directory.total_read)
self.type = "directory"

def populate_comic(self, comic, user):
Expand Down Expand Up @@ -140,6 +141,12 @@ def generate_directory(user, directory=False):
else:
dir_list_obj = Directory.objects.filter(name__in=dir_list, parent__isnull=True)
file_list_obj = ComicBook.objects.filter(file_name__in=file_list, directory__isnull=True)

dir_list_obj = dir_list_obj.annotate(total=Count('comicbook'),
total_read=Count('comicbook__comicstatus',
Q(comicbook__comicstatus__finished=True,
comicbook__comicstatus__user=user)))

for directory_obj in dir_list_obj:
df = DirFile()
df.populate_directory(directory_obj, user)
Expand All @@ -157,6 +164,8 @@ def generate_directory(user, directory=False):
else:
directory_obj = Directory(name=directory_name)
directory_obj.save()
directory_obj.total = 0
directory_obj.total_read = 0
df = DirFile()
df.populate_directory(directory_obj, user)
files.append(df)
Expand Down Expand Up @@ -185,17 +194,14 @@ def generate_label(book, status):
return label_text


def generate_dir_status(user, directory):
cb_list = ComicBook.objects.filter(directory=directory)
total = cb_list.count()
total_read = ComicStatus.objects.filter(user=user, comic__in=cb_list, finished=True).count()
def generate_dir_status(total, total_read):
if total == 0:
return '<center><span class="label label-default">Empty</span></center>'
elif total == total_read:
return '<center><span class="label label-success">All Read</span></center>'
elif total_read == 0:
return '<center><span class="label label-default">Unread</span></center>'
return '<center><span class="label label-primary">{0}/{1}</span></center>'.format(total_read, total)
return f'<center><span class="label label-primary">{total_read}/{total}</span></center>'


def get_ordered_dir_list(folder):
Expand Down

0 comments on commit 6f8a7bd

Please sign in to comment.