From 15e4960e33a5f3259b2c9b351a8c5f384f9f747a Mon Sep 17 00:00:00 2001 From: th3r00t Date: Wed, 5 Feb 2020 13:11:46 -0500 Subject: [PATCH] ui gets v# from config --- src/backend/lib/storage.py | 28 +++++++++++++-- src/backend/pyShelf_MakeCollections.py | 23 +++++++++++++ src/backend/pyShelf_ScanLibrary.py | 1 - src/interface/migrations/0004_collections.py | 25 ++++++++++++++ src/interface/models.py | 9 +++++ src/interface/templates/index.html | 2 +- src/interface/templates/search.html | 2 +- src/interface/views.py | 36 ++++++++++++++++---- 8 files changed, 114 insertions(+), 12 deletions(-) create mode 100755 src/backend/pyShelf_MakeCollections.py create mode 100644 src/interface/migrations/0004_collections.py diff --git a/src/backend/lib/storage.py b/src/backend/lib/storage.py index e193b88..5358a79 100755 --- a/src/backend/lib/storage.py +++ b/src/backend/lib/storage.py @@ -1,9 +1,7 @@ #!/usr/bin/python import sqlite3 - import psycopg2 from psycopg2 import Error - from .config import Config # db_pointer = Config().catalogue_db @@ -102,3 +100,29 @@ class Storage: """ self.db.close() return True + + def make_collections(self): + _q = "SELECT id,file_name FROM books" + self.cursor.execute(_q) + _set = self.cursor.fetchall() + for book in _set: + path = self.config.book_path+'/' + _collections = [] + _pathing = book[1].split(path)[1].split('/') + _pathing.pop(0);_pathing.pop(-1) + for _p in _pathing: + _s = _p.replace("'","") + _q_x = """ + SELECT id FROM collections where collection='%s' AND book_id_id=%s + """%(_s,book[0]) + try: + self.cursor.execute(_q_x) + if len(self.cursor.fetchall()) < 1: + self.cursor.execute( + """INSERT INTO collections (collection, book_id_id) VALUES ('%s',%s)"""%(_s,book[0]) + ) + except Exception as e: + print(e) + _collections.append(_p) + self.db.commit() + self.close() diff --git a/src/backend/pyShelf_MakeCollections.py b/src/backend/pyShelf_MakeCollections.py new file mode 100755 index 0000000..0f16549 --- /dev/null +++ b/src/backend/pyShelf_MakeCollections.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +import os +import sys +import time + +from .lib.config import Config +from .lib.library import Catalogue +from .lib.pyShelf import InitFiles +from .lib.storage import Storage + +sys.path.append(os.path.abspath(".")) + + +def MakeCollections(root): + _t1 = time.time() + config = Config(root) # Get configuration settings + # InitFiles(config.file_array) # Initialize file system + _storage = Storage(config) + _storage.make_collections() + _t2 = time.time() + scan_time = round(_t2 - _t1) + print("Collections Made.") + print("Time %s seconds" % scan_time) diff --git a/src/backend/pyShelf_ScanLibrary.py b/src/backend/pyShelf_ScanLibrary.py index 9acbf3d..84b62e5 100755 --- a/src/backend/pyShelf_ScanLibrary.py +++ b/src/backend/pyShelf_ScanLibrary.py @@ -19,7 +19,6 @@ def execute_scan(root): _t1 = time.time() config = Config(root) # Get configuration settings InitFiles(config.file_array) # Initialize file system - Storage(config).check_ownership() catalogue = Catalogue(config) # Open the Catalogue catalogue.import_books() diff --git a/src/interface/migrations/0004_collections.py b/src/interface/migrations/0004_collections.py new file mode 100644 index 0000000..7f590c9 --- /dev/null +++ b/src/interface/migrations/0004_collections.py @@ -0,0 +1,25 @@ +# Generated by Django 3.0.2 on 2020-02-04 20:22 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('interface', '0003_auto_20200101_0447'), + ] + + operations = [ + migrations.CreateModel( + name='Collections', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('collection', models.CharField(max_length=255)), + ('book_id', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='interface.Books')), + ], + options={ + 'db_table': 'collections', + }, + ), + ] diff --git a/src/interface/models.py b/src/interface/models.py index 929eed2..f172da6 100755 --- a/src/interface/models.py +++ b/src/interface/models.py @@ -30,6 +30,15 @@ class Books(models.Model): progress = models.IntegerField(null=True) file_name = models.CharField(max_length=255, null=False) +class Collections(models.Model): + + class Meta: + db_table = "collections" + def __str__(self): + return self.collection + collection = models.CharField(max_length=255) + book_id = models.ForeignKey(Books, on_delete=models.PROTECT) + def get_absolute_url(self): """Returns the url to access a particular instance of MyModelName.""" return reverse("model-detail-view", args=[str(self.id)]) diff --git a/src/interface/templates/index.html b/src/interface/templates/index.html index a7fd6be..9e4fc64 100755 --- a/src/interface/templates/index.html +++ b/src/interface/templates/index.html @@ -18,7 +18,7 @@
-

pyShelf 0.4.1

+

pyShelf {{Version}}

"An elegant tool... for a more civilized age."

diff --git a/src/interface/templates/search.html b/src/interface/templates/search.html index 55fd543..2146508 100644 --- a/src/interface/templates/search.html +++ b/src/interface/templates/search.html @@ -18,7 +18,7 @@
-

pyShelf 0.4.1

+

pyShelf {{Version}}

"An elegant tool... for a more civilized age."

diff --git a/src/interface/views.py b/src/interface/views.py index 8b5c82f..1cc8a19 100755 --- a/src/interface/views.py +++ b/src/interface/views.py @@ -6,17 +6,22 @@ from django.db import models from django.http import JsonResponse from django.shortcuts import HttpResponse, render #render_to_response from django.utils.text import slugify - +from pathlib import Path from .models import Books +from backend.lib.config import Config - +config = Config(Path('../')) def index(request): """ Return template index """ _set = 1 return render( - request, "index.html", {"Books": book_set(20, _set), "Set": str(_set)} + request, "index.html", { + "Books": book_set(20, _set), + "Set": str(_set), + "Version": config.VERSION + } ) @@ -29,7 +34,11 @@ def next_page(request, bookset): except Exception: _set = 1 return render( - request, "index.html", {"Books": book_set(None, _set), "Set": str(_set)} + request, "index.html", { + "Books": book_set(None, _set), + "Set": str(_set), + "Version": config.VERSION + } ) @@ -46,7 +55,11 @@ def prev_page(request, bookset): except Exception: _set = 1 return render( - request, "index.html", {"Books": book_set(None, _set), "Set": str(_set)} + request, "index.html", { + "Books": book_set(None, _set), + "Set": str(_set), + "Version": config.VERSION + } ) @@ -56,7 +69,10 @@ def search(request, query=None, _set=1, _limit=None): """ _set = int(_set) if query is None: - return render(request, "index.html", {"Books": None}) + return render(request, "index.html", { + "Books": None, + "Version": config.VERSION + }) if _limit is None: _limit = 20 ## TODO set to user defaults if _set < 1: @@ -69,7 +85,13 @@ def search(request, query=None, _set=1, _limit=None): return render( request, "search.html", - {"Books": _r, "Query": query, "Set": _set, "len_results": search_len}, + { + "Books": _r, + "Query": query, + "Set": _set, + "len_results": search_len, + "Version": config.VERSION + }, )