adding bookshelf mgmt base

adam
aundus 2020-12-02 18:21:29 -05:00
commit 4a4145f7a2
31 changed files with 586 additions and 0 deletions

View File

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Bookshelf, Book
admin.site.register(Bookshelf)
admin.site.register(Book)

View File

@ -0,0 +1,26 @@
# Generated by Django 3.1.3 on 2020-11-30 19:10
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Bookshelf',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bookshelf', models.CharField(max_length=255)),
('downloads', models.IntegerField(default=0)),
('release_date', models.DateField(null=True)),
],
options={
'db_table': 'bookshelves',
},
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 3.1.3 on 2020-11-30 20:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mgmt', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='bookshelf',
name='id',
),
migrations.AlterField(
model_name='bookshelf',
name='bookshelf',
field=models.CharField(max_length=255, primary_key=True, serialize=False),
),
]

View File

@ -0,0 +1,48 @@
# Generated by Django 3.1.3 on 2020-12-01 21:34
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('mgmt', '0002_auto_20201130_2017'),
]
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, default='', max_length=255, null=True)),
('aliases', models.CharField(blank=True, default='', max_length=255, null=True)),
('birth_year', models.IntegerField(null=True)),
('death_year', models.IntegerField(null=True)),
('wikipedia_url', models.URLField(max_length=500)),
],
),
migrations.CreateModel(
name='Book',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('book_id', models.IntegerField()),
('repo_name', models.CharField(blank=True, max_length=255, null=True)),
('title', models.CharField(db_index=True, default='', max_length=1000)),
('language', models.CharField(db_index=True, default='en', max_length=5)),
('description', models.TextField(blank=True, default='', null=True)),
('gutenberg_type', models.CharField(blank=True, default='', max_length=255, null=True)),
('gutenberg_bookshelf', models.CharField(default='', max_length=1000)),
('subjects', models.CharField(default='', max_length=1000)),
('full_text', models.TextField(blank=True, default='', null=True)),
('num_downloads', models.IntegerField(default=0)),
('added', models.DateTimeField(auto_now_add=True, null=True)),
('updated', models.DateTimeField(auto_now_add=True, null=True)),
('yaml', models.TextField(default='', null=True)),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='mgmt.author')),
],
options={
'db_table': 'books',
},
),
]

View File

@ -0,0 +1,106 @@
# Generated by Django 3.1.3 on 2020-12-01 21:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mgmt', '0003_author_book'),
]
operations = [
migrations.RemoveField(
model_name='book',
name='added',
),
migrations.RemoveField(
model_name='book',
name='author',
),
migrations.RemoveField(
model_name='book',
name='book_id',
),
migrations.RemoveField(
model_name='book',
name='description',
),
migrations.RemoveField(
model_name='book',
name='full_text',
),
migrations.RemoveField(
model_name='book',
name='gutenberg_bookshelf',
),
migrations.RemoveField(
model_name='book',
name='gutenberg_type',
),
migrations.RemoveField(
model_name='book',
name='language',
),
migrations.RemoveField(
model_name='book',
name='num_downloads',
),
migrations.RemoveField(
model_name='book',
name='repo_name',
),
migrations.RemoveField(
model_name='book',
name='subjects',
),
migrations.RemoveField(
model_name='book',
name='updated',
),
migrations.RemoveField(
model_name='book',
name='yaml',
),
migrations.AddField(
model_name='book',
name='copyrighted',
field=models.IntegerField(default=0, null=True),
),
migrations.AddField(
model_name='book',
name='downloads',
field=models.IntegerField(default=0, null=True),
),
migrations.AddField(
model_name='book',
name='filemask',
field=models.CharField(default='', max_length=240),
preserve_default=False,
),
migrations.AddField(
model_name='book',
name='gutindex',
field=models.TextField(blank=True, default='', null=True),
),
migrations.AddField(
model_name='book',
name='nonfiling',
field=models.IntegerField(default=0),
),
migrations.AddField(
model_name='book',
name='release_date',
field=models.DateField(null=True),
),
migrations.AddField(
model_name='book',
name='updatemode',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name='book',
name='title',
field=models.TextField(blank=True, default='', max_length=255, null=True),
),
]

View File

@ -0,0 +1,28 @@
# Generated by Django 3.1.3 on 2020-12-01 21:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mgmt', '0004_auto_20201201_2147'),
]
operations = [
migrations.RemoveField(
model_name='book',
name='id',
),
migrations.AlterField(
model_name='book',
name='gutindex',
field=models.TextField(default='1', primary_key=True, serialize=False),
preserve_default=False,
),
migrations.AlterField(
model_name='book',
name='updatemode',
field=models.IntegerField(default=0, null=True),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.1.3 on 2020-12-01 21:52
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('mgmt', '0005_auto_20201201_2149'),
]
operations = [
migrations.AlterModelTable(
name='author',
table='authors',
),
]

View File

@ -0,0 +1,16 @@
# Generated by Django 3.1.3 on 2020-12-01 22:03
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('mgmt', '0006_auto_20201201_2152'),
]
operations = [
migrations.DeleteModel(
name='Author',
),
]

View File

@ -0,0 +1,117 @@
from django.db import models
from django.utils import timezone as tz
# logger = logging.getLogger(__name__)
# gh_org = 'GITenberg'
# def smart_truncate(content, length=100, suffix='...'):
# if len(content) <= length:
# return content
# else:
# return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix
'''
class Author(models.Model):
name = models.CharField(max_length=255, default="", null=True, blank=True)
aliases = models.CharField(max_length=255, default="", null=True, blank=True)
birth_year = models.IntegerField(null=True)
death_year = models.IntegerField(null=True)
wikipedia_url = models.URLField(max_length=500)
class Meta:
db_table = "authors"
'''
class Bookshelf(models.Model):
bookshelf = models.CharField(max_length=255, null=False, primary_key=True)
downloads = models.IntegerField(default=0)
release_date = models.DateField(null=True)
class Meta:
db_table = "bookshelves"
class Book(models.Model):
copyrighted = models.IntegerField(null=True, default=0)
updatemode = models.IntegerField(null=True, default=0)
release_date = models.DateField(null=True)
filemask = models.CharField(max_length=240)
gutindex = models.TextField(primary_key=True)
downloads = models.IntegerField(null=True, default=0)
title = models.TextField(max_length=255, default="", null=True, blank=True)
nonfiling = models.IntegerField(default=0)
'''
# using a custom save method in order to update the "updated" timestamp when specific fields are updated
def save(self, *args, **kwargs):
if self.pk: # if object already exists in db
old_model = Book.objects.get(pk=self.pk)
# This is the list of fields that, when modified, should update the "updated" timestamp
fields = ["title", "language", "description", "author", "gutenberg_type", "gutenberg_bookshelf", "subjects", "full_text"]
for field in fields:
# If one of the fields was modified, update the timestamp
if getattr(old_model, field, None) != getattr(self, field, None):
self.updated = tz.now()
super(Book, self).save(*args, **kwargs) # call the inherited save method
def __unicode__(self):
return self.repo_name
@property
def title_short(self):
return smart_truncate(self.title, 65)
@property
def subjects_str(self):
return self.subjects.replace(";", ", ")
@property
def author_first_last(self):
if self.author is None:
return None
else:
return " ".join(self.author.name.split(", ")[::-1])
@property
def description_short(self):
return smart_truncate(self.description, 300)
@property
def repo_url(self):
return 'https://github.com/{}/{}'.format(gh_org,self.repo_name)
@property
def issues_url(self):
return 'https://github.com/{}/{}/issues'.format(gh_org,self.repo_name)
@property
def downloads_url(self):
return 'https://github.com/{}/{}/releases'.format(gh_org,self.repo_name)
@property
def pg_url(self):
return 'https://www.gutenberg.org/ebooks/{}'.format(self.book_id)
@property
def cover_url(self):
existing_covers = list(Cover.objects.filter(book=self))
for cover in existing_covers:
if cover.default_cover and cover.file and hasattr(cover.file, "url"):
return cover.file.url
#No cover is set as default, so return the first cover that has a url
if len(existing_covers) > 0:
for cover in existing_covers:
if cover.file and hasattr(cover.file, "url"):
return cover.file.url
return None
_pandata=None
def metadata(self):
if not self._pandata:
self._pandata=Pandata()
self._pandata.load(self.yaml)
return self._pandata.metadata
'''
class Meta:
db_table = "books"

View File

@ -0,0 +1,16 @@
"""
ASGI config for bookshelf_management project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookshelf_management.settings')
application = get_asgi_application()

View File

@ -0,0 +1,125 @@
"""
Django settings for bookshelf_management project.
Generated by 'django-admin startproject' using Django 3.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '&@xb%y*a-v56sap7_5&+kdtsdck68h-9&r6*u#3!a2ey@+i76$'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["localhost"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'bookshelf_management.apps.mgmt'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'bookshelf_management.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'bookshelf_management.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'gutenberg',
'USER': 'adamundus',
'PASSWORD': 'database',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'

View File

@ -0,0 +1,21 @@
"""bookshelf_management URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]

View File

@ -0,0 +1,16 @@
"""
WSGI config for bookshelf_management project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookshelf_management.settings')
application = get_wsgi_application()

0
db.sqlite3 Normal file
View File

22
manage.py Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookshelf_management.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()