mirror of
https://github.com/ajurna/cbwebreader.git
synced 2025-12-06 06:17:17 +00:00
code cleanup.
moved process_comic_book to static method.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from django.contrib import admin
|
||||
from comic.models import Setting, ComicBook, ComicPage
|
||||
|
||||
# Register your models here.
|
||||
|
||||
@admin.register(Setting)
|
||||
class SettingAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'value')
|
||||
@@ -11,6 +11,7 @@ class SettingAdmin(admin.ModelAdmin):
|
||||
class ComicBookAdmin(admin.ModelAdmin):
|
||||
list_display = ('file_name', 'last_read_page')
|
||||
|
||||
|
||||
@admin.register(ComicPage)
|
||||
class ComicPageAdmin(admin.ModelAdmin):
|
||||
list_display = ('Comic', 'index', 'page_file_name', 'content_type')
|
||||
|
||||
@@ -78,7 +78,9 @@ class ComicBook(models.Model):
|
||||
out.next_path = comic_path
|
||||
|
||||
return out
|
||||
def nav_get_prev_comic(self, comic_path):
|
||||
|
||||
@staticmethod
|
||||
def nav_get_prev_comic(comic_path):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR').value
|
||||
comic_path = urlsafe_base64_decode(comic_path)
|
||||
directory, comic = path.split(comic_path)
|
||||
@@ -96,14 +98,16 @@ class ComicBook(models.Model):
|
||||
try:
|
||||
book = ComicBook.objects.get(file_name=prev_comic)
|
||||
except ComicBook.DoesNotExist:
|
||||
book = process_comic_book(base_dir, comic_path, prev_comic)
|
||||
book = ComicBook.process_comic_book(base_dir, comic_path, prev_comic)
|
||||
index = ComicPage.objects.filter(Comic=book).count() - 1
|
||||
comic_path = urlsafe_base64_encode(comic_path)
|
||||
else:
|
||||
comic_path = urlsafe_base64_encode(directory)
|
||||
index = -1
|
||||
return comic_path, index
|
||||
def nav_get_next_comic(self, comic_path):
|
||||
|
||||
@staticmethod
|
||||
def nav_get_next_comic(comic_path):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR')
|
||||
comic_path = urlsafe_base64_decode(comic_path)
|
||||
directory, comic = path.split(comic_path)
|
||||
@@ -118,20 +122,9 @@ class ComicBook(models.Model):
|
||||
comic_path = urlsafe_base64_encode(directory)
|
||||
index = -1
|
||||
return comic_path, index
|
||||
class Comic:
|
||||
def __init__(self):
|
||||
self.name = ''
|
||||
self.index = 0
|
||||
|
||||
def pages(self):
|
||||
out = []
|
||||
for item in ComicPage.objects.filter(Comic=self).order_by('index'):
|
||||
i = self.Comic()
|
||||
i.name = item.page_file_name
|
||||
i.index = item.index
|
||||
out.append(i)
|
||||
return out
|
||||
def process_comic_book(base_dir, comic_path, comic_file_name):
|
||||
@staticmethod
|
||||
def process_comic_book(base_dir, comic_path, comic_file_name):
|
||||
try:
|
||||
cbx = rarfile.RarFile(path.join(base_dir, comic_path))
|
||||
except rarfile.BadRarFile:
|
||||
@@ -176,11 +169,23 @@ def process_comic_book(base_dir, comic_path, comic_file_name):
|
||||
|
||||
return book
|
||||
|
||||
class Comic:
|
||||
def __init__(self):
|
||||
self.name = ''
|
||||
self.index = 0
|
||||
|
||||
def pages(self):
|
||||
out = []
|
||||
for item in ComicPage.objects.filter(Comic=self).order_by('index'):
|
||||
i = self.Comic()
|
||||
i.name = item.page_file_name
|
||||
i.index = item.index
|
||||
out.append(i)
|
||||
return out
|
||||
|
||||
|
||||
class ComicPage(models.Model):
|
||||
Comic = models.ForeignKey(ComicBook)
|
||||
index = models.IntegerField()
|
||||
page_file_name = models.CharField(max_length=100, unique=False)
|
||||
content_type = models.CharField(max_length=30)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -7,5 +7,4 @@ urlpatterns = [
|
||||
url(r'^(?P<comic_path>[\w]+)/$', views.comic_list, name='comic_list'),
|
||||
url(r'^read/(?P<comic_path>[\w]+)/(?P<page>[0-9]+)/$', views.read_comic, name='read_comic'),
|
||||
url(r'^read/(?P<comic_path>[\w]+)/(?P<page>[0-9]+)/img$', views.get_image, name='get_image'),
|
||||
|
||||
]
|
||||
@@ -10,19 +10,10 @@ class Breadcrumb:
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
class DirFile:
|
||||
def __init__(self):
|
||||
self.name = ''
|
||||
self.isdir = False
|
||||
self.icon = ''
|
||||
self.iscb = False
|
||||
self.location = ''
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def generate_breadcrumbs(comic_path):
|
||||
output = [Breadcrumb()]
|
||||
@@ -41,6 +32,18 @@ def generate_breadcrumbs(comic_path):
|
||||
return output
|
||||
|
||||
|
||||
class DirFile:
|
||||
def __init__(self):
|
||||
self.name = ''
|
||||
self.isdir = False
|
||||
self.icon = ''
|
||||
self.iscb = False
|
||||
self.location = ''
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
def generate_directory(base_dir, comic_path):
|
||||
files = []
|
||||
for fn in os.listdir(path.join(base_dir, comic_path)):
|
||||
|
||||
@@ -3,11 +3,12 @@ from django.template import RequestContext
|
||||
from django.utils.http import urlsafe_base64_decode
|
||||
from django.shortcuts import render
|
||||
|
||||
from comic.models import Setting, ComicBook, process_comic_book
|
||||
from comic.models import Setting, ComicBook
|
||||
from util import generate_breadcrumbs, generate_directory
|
||||
|
||||
from os import path
|
||||
|
||||
|
||||
def comic_list(request, comic_path=''):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR').value
|
||||
comic_path = urlsafe_base64_decode(comic_path)
|
||||
@@ -29,7 +30,7 @@ def read_comic(request, comic_path, page):
|
||||
try:
|
||||
book = ComicBook.objects.get(file_name=comic_file_name)
|
||||
except ComicBook.DoesNotExist:
|
||||
book = process_comic_book(base_dir, decoded_path, comic_file_name)
|
||||
book = ComicBook.process_comic_book(base_dir, decoded_path, comic_file_name)
|
||||
book.last_read_page = page
|
||||
book.save()
|
||||
context = RequestContext(request, {
|
||||
@@ -41,7 +42,7 @@ def read_comic(request, comic_path, page):
|
||||
return render(request, 'comic/read_comic.html', context)
|
||||
|
||||
|
||||
def get_image(request, comic_path, page):
|
||||
def get_image(_, comic_path, page):
|
||||
base_dir = Setting.objects.get(name='BASE_DIR').value
|
||||
page = int(page)
|
||||
decoded_path = urlsafe_base64_decode(comic_path)
|
||||
@@ -49,7 +50,7 @@ def get_image(request, comic_path, page):
|
||||
try:
|
||||
book = ComicBook.objects.get(file_name=comic_file_name)
|
||||
except ComicBook.DoesNotExist:
|
||||
book = process_comic_book(base_dir, decoded_path, comic_file_name)
|
||||
book = ComicBook.process_comic_book(base_dir, decoded_path, comic_file_name)
|
||||
full_path = path.join(base_dir, decoded_path)
|
||||
img, content = book.get_image(full_path, page)
|
||||
return HttpResponse(img.read(), content_type=content)
|
||||
|
||||
Reference in New Issue
Block a user