From 09637c478b78ddd260c04e235ca2a0eb581465e5 Mon Sep 17 00:00:00 2001 From: Zergling_man Date: Sun, 24 Oct 2021 23:59:35 +1100 Subject: [PATCH] v1 of books. You can see a list of books. --- books/__init__.py | 0 books/admin.py | 3 ++ books/apps.py | 6 ++++ books/migrations/0001_initial.py | 34 ++++++++++++++++++++++ books/migrations/__init__.py | 0 books/models.py | 23 +++++++++++++++ books/tests.py | 3 ++ books/urls.py | 19 ++++++++++++ books/views.py | 50 ++++++++++++++++++++++++++++++++ main/urls.py | 2 +- rakka/settings.py | 4 ++- rakka/urls.py | 3 +- 12 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 books/__init__.py create mode 100644 books/admin.py create mode 100644 books/apps.py create mode 100644 books/migrations/0001_initial.py create mode 100644 books/migrations/__init__.py create mode 100644 books/models.py create mode 100644 books/tests.py create mode 100644 books/urls.py create mode 100644 books/views.py diff --git a/books/__init__.py b/books/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/books/admin.py b/books/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/books/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/books/apps.py b/books/apps.py new file mode 100644 index 0000000..a53388c --- /dev/null +++ b/books/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BooksConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'books' diff --git a/books/migrations/0001_initial.py b/books/migrations/0001_initial.py new file mode 100644 index 0000000..b0b86dd --- /dev/null +++ b/books/migrations/0001_initial.py @@ -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')), + ], + ), + ] diff --git a/books/migrations/__init__.py b/books/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/books/models.py b/books/models.py new file mode 100644 index 0000000..54dccf1 --- /dev/null +++ b/books/models.py @@ -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 \ No newline at end of file diff --git a/books/tests.py b/books/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/books/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/books/urls.py b/books/urls.py new file mode 100644 index 0000000..2703166 --- /dev/null +++ b/books/urls.py @@ -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'), +""" \ No newline at end of file diff --git a/books/views.py b/books/views.py new file mode 100644 index 0000000..22fb490 --- /dev/null +++ b/books/views.py @@ -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+"
You can't actually read books yet, I'm working on it.",'date':'2021/10/24'} \ No newline at end of file diff --git a/main/urls.py b/main/urls.py index 7b642a6..e4e42a2 100644 --- a/main/urls.py +++ b/main/urls.py @@ -1,4 +1,4 @@ -from django.urls import path, re_path +from django.urls import path, re_path, include from . import views urlpatterns=[ diff --git a/rakka/settings.py b/rakka/settings.py index a1192d2..6dd9a97 100644 --- a/rakka/settings.py +++ b/rakka/settings.py @@ -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'], } } diff --git a/rakka/urls.py b/rakka/urls.py index 37bcd9b..b08569f 100644 --- a/rakka/urls.py +++ b/rakka/urls.py @@ -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')), ]