v1 of books. You can see a list of books.

This commit is contained in:
Zergling_man 2021-10-24 23:59:35 +11:00
parent 6792242926
commit 09637c478b
12 changed files with 144 additions and 3 deletions

0
books/__init__.py Normal file
View File

3
books/admin.py Normal file
View File

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

6
books/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class BooksConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'books'

View File

@ -0,0 +1,34 @@
# Generated by Django 3.2.8 on 2021-10-24 12:41
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('added', models.DateField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Chapter',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('number', models.IntegerField(default=0)),
('title', models.CharField(max_length=200)),
('contents', models.TextField()),
('added', models.DateField(auto_now_add=True)),
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='books.book')),
],
),
]

View File

23
books/models.py Normal file
View File

@ -0,0 +1,23 @@
from django.db import models
from datetime import date
# Create your models here.
class Book(models.Model):
title=models.CharField(max_length=200)
added=models.DateField(auto_now_add=True)
@property
def last_updated(self):
return Chapter.objects.filter(book=self).order_by('added')[0].added
def __str__(self):
return self.title
class Chapter(models.Model):
book=models.ForeignKey(Book, on_delete=models.CASCADE)
number=models.IntegerField(default=0)
title=models.CharField(max_length=200)
contents=models.TextField()
added=models.DateField(auto_now_add=True)
def __str__(self):
return self.title or self.book.title+' '+self.number

3
books/tests.py Normal file
View File

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

19
books/urls.py Normal file
View File

@ -0,0 +1,19 @@
from django.urls import path, re_path, include
from . import views
urlpatterns=[
path('',views.index,name='bidex'),
]
"""
path('books/',include('books.urls')),
path('contact',views.contact,name='contact'),
path('specs',views.specs,name='specs'),
path('songs',views.songs,name='songs'),
path('nerdshope',views.nerdshope,name='shop'),
path('teapot',views.teapot,name='teapot'),
path('toask',views.articles,name='articles'),
re_path('r/(.*)',views.redirect,name='redirect'),
path('md.rss',views.md,name='md'),
re_path('(.*)',views.autopage,name='everything'),
"""

50
books/views.py Normal file
View File

@ -0,0 +1,50 @@
from django.shortcuts import render
from . import models
from django.template import loader
import functools
def rerender(funky):
@functools.wraps(funky)
def wrap(req,*args,**kwargs):
with open('main/utils/navs') as b:
navs=parsecat(b,4)
namednavs=[{'id':blah[0], 'link':blah[1], 'text':blah[2], 'align':int(blah[3])} for blah in navs['']]
out=funky(*args,**kwargs)
temp=loader.get_template('main/nav.html')
out['nav']=temp.render({"navs":namednavs},None)
return render(req, 'main/temp.html', out)
return wrap
def parsecat(file,n):
if not isinstance(file,str):
# Assume open handle
stuff=file.read()
else:
with open(f'main/pages/{file}') as b:
stuff=b.read()
cats=stuff.split('\n\n')
out={}
head=''
out['']=[]
for cat in cats:
if ':' in cat[:cat.index('\n')]:
# We have a category name
head,cat=cat.split('\n',1)
head=head[:-1]
out[head]=[]
else: head=''
for line in cat.split('\n'):
lin=line.split(',',n-1)
pad=n-len(lin)
lin.extend(['']*pad)
out[head].append(lin)
if out['']==[]: del(out[''])
return out
@rerender
def index():
books=models.Book.objects.all()
#Make table I guess, for now just get data up there
contents='\n'.join([f'{book.title} - {book.last_updated}' for book in books])
return {'title':'Booklist','content':contents+"<br/>You can't actually read books yet, I'm working on it.",'date':'2021/10/24'}

View File

@ -1,4 +1,4 @@
from django.urls import path, re_path
from django.urls import path, re_path, include
from . import views
urlpatterns=[

View File

@ -34,6 +34,7 @@ ALLOWED_HOSTS = [n.strip() for n in conf['ALLOWED_HOSTS'].split(',')]
INSTALLED_APPS = [
'main.apps.MainConfig',
'books.apps.BooksConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
@ -78,7 +79,8 @@ WSGI_APPLICATION = 'rakka.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'djongo',
'ENGINE': 'django.db.backends.sqlite3',
#'ENGINE': 'djongo',
'NAME': conf['DBNAME'],
}
}

View File

@ -18,6 +18,7 @@ from django.urls import include, path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('admin', admin.site.urls),
path('books/',include('books.urls')),
path('', include('main.urls')),
]