From 3d10081846f227514c6aa1b7ef4d6dbbea78754a Mon Sep 17 00:00:00 2001 From: eneller Date: Wed, 19 Mar 2025 22:04:23 +0100 Subject: [PATCH] feat: error handling --- pyproject.toml | 1 + src/core/views.py | 27 +++++++++++++++------------ src/epub2go_web/static/styles.css | 11 +++++------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 388a7f7..8e8669e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ version = "0.1.0" description = "Web Interface to epub2go.py" readme = "README.md" requires-python = ">=3.12" +# TODO enable auto-reload using https://github.com/adamchainz/django-browser-reload dependencies = [ "celery>=5.4.0", "django>=5.1.6", diff --git a/src/core/views.py b/src/core/views.py index c453186..d2944ac 100644 --- a/src/core/views.py +++ b/src/core/views.py @@ -1,5 +1,5 @@ from django.shortcuts import render -from django.http import HttpRequest, HttpResponse, FileResponse +from django.http import HttpRequest, HttpResponse, FileResponse, HttpResponseBadRequest from django.conf import settings from celery import shared_task @@ -24,19 +24,22 @@ def index(request: HttpRequest): } targetParam = request.GET.get('t', None) - if validateUrl(targetParam): - fpath = getEpub(targetParam) - fname = os.path.basename(fpath) - file = open(fpath, 'rb') - response = FileResponse(file) - response['Content-Type'] = 'application/octet-stream' - response['Content-Disposition'] = f'attachment; filename="{fname}"' - return response - - return render(request, 'index.html', context) + if targetParam: + if validateUrl(targetParam): + # download file + fpath = getEpub(targetParam) + fname = os.path.basename(fpath) + file = open(fpath, 'rb') + response = FileResponse(file) + response['Content-Type'] = 'application/octet-stream' + response['Content-Disposition'] = f'attachment; filename="{fname}"' + return response + else: return HttpResponseBadRequest('Input URL invalid.') + else: + # return base view + return render(request, 'index.html', context) def validateUrl(param)->bool : - if not param: return False netloc = urlparse(param).netloc if(netloc == gbnetloc): return True diff --git a/src/epub2go_web/static/styles.css b/src/epub2go_web/static/styles.css index e984e6c..4152491 100644 --- a/src/epub2go_web/static/styles.css +++ b/src/epub2go_web/static/styles.css @@ -5,8 +5,8 @@ body{ line-height:1.6; font-size:18px; color:#444; - padding:0 - 10px} + padding:0 10px; +} h1,h2,h3{ line-height:1.2 @@ -17,7 +17,8 @@ h1,h2,h3{ --white:#faf0e673; } body{ - background-color: var(--white) + background-color: var(--white); + font-family: serif; } header{ text-align: center; @@ -36,13 +37,11 @@ tr:hover{ background-color: #DDDDDD; transition: all 2ms; } -.inline-icon{ +.inline-icon, .header-icon{ vertical-align: middle; height: 1em; } .header-icon{ - vertical-align: middle; - height: 1em; padding: .5em; } a:hover, a:any-link{