feat: app inside project with custom command

This commit is contained in:
eneller
2025-03-16 15:31:45 +01:00
parent 52998fa280
commit 0589148b6f
14 changed files with 73 additions and 21 deletions

0
src/core/__init__.py Normal file
View File

3
src/core/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
src/core/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CoreConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "core"

View File

View File

View File

@@ -0,0 +1,20 @@
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from epub2go.convert import get_all_books, Book, allbooks_url
import pickle, os
class Command(BaseCommand):
help = "Download the Book List"
filepath = os.path.join(settings.MEDIA_ROOT, 'books.pkl')
def handle(self, *args, **options):
try:
books = get_all_books()
except:
raise CommandError('Failed to get Book list from %s', allbooks_url)
try:
with open(self.filepath, 'wb') as file:
pickle.dump(books,file)
except:
raise CommandError(f'Failed to save pickle file. Did you create {settings.MEDIA_ROOT} ?')

View File

3
src/core/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
src/core/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

7
src/core/urls.py Normal file
View File

@@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]

21
src/core/views.py Normal file
View File

@@ -0,0 +1,21 @@
from django.shortcuts import render
from django.http import HttpRequest, HttpResponse
from epub2go.convert import get_all_books, Book
def index(request: HttpRequest):
title = 'epub2go'
targetParam = request.GET.get('t', None)
books = get_all_books()
if targetParam is not None:
getEpub(targetParam)
return render(request, 'index.html', locals())
def getEpub(param):
print(param)
# TODO validate / sanitize input
# TODO check for existing file and age
# TODO download
# TODO redirect to loading page
# TODO redirect to download page
raise NotImplementedError

View File

@@ -14,6 +14,7 @@ from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
PROJ_DIR = BASE_DIR / 'epub2go_web'
# Quick-start development settings - unsuitable for production
@@ -37,6 +38,7 @@ INSTALLED_APPS = [
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"core",
]
MIDDLEWARE = [
@@ -55,7 +57,7 @@ TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
"epub2go_web/templates/"
PROJ_DIR / "templates/"
],
"APP_DIRS": True,
"OPTIONS": {
@@ -119,9 +121,12 @@ USE_TZ = True
STATIC_URL = "static/"
STATICFILES_DIRS = [
BASE_DIR / "epub2go_web/static",
PROJ_DIR / "static/",
]
MEDIA_URL = "media/"
MEDIA_ROOT = PROJ_DIR / "media/"
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

View File

@@ -12,7 +12,7 @@
<a href="javascript:void(window.open('./?t='+encodeURIComponent(window.location.toString())))" title="Add this to your bookmarks"> <!--TODO fix domain part as variable-->
<img src="{% static 'bookmark.svg' %}" alt="Bookmarklet" class="header-icon">
</a>
<a href="https://github.com/eneller/epub2go.py">
<a href="https://github.com/eneller/epub2go-web">
<img src="{% static 'github.svg' %}" alt="GitHub" class="header-icon">
</a></div>
<search>

View File

@@ -16,29 +16,13 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render, redirect
from epub2go.convert import get_all_books, GBConvert, Book
def root(request:HttpRequest):
title = 'epub2go'
targetParam = request.GET.get('t', None)
books = get_all_books()
if targetParam is not None:
getEpub(targetParam)
return render(request, 'index.html', locals())
urlpatterns = [
path("admin/", admin.site.urls),
path('',root, name='root')
path('',include('core.urls'))
]
def getEpub(param):
print(param)
# TODO validate / sanitize input
# TODO check for existing file and age
# TODO download
# TODO redirect to loading page
# TODO redirect to download page
raise NotImplementedError