Fixed database access issues, and installer question list

This commit is contained in:
Mike Young
2019-12-31 21:13:52 -05:00
parent a19401c1aa
commit da133c4197
8 changed files with 37 additions and 23 deletions

1
importBooks vendored
View File

@@ -3,6 +3,7 @@
import pathlib
import sys
from src.backend.lib.storage import Storage
from src.backend.pyShelf_ScanLibrary import execute_scan
PRG_PATH = pathlib.Path.cwd()

16
installer vendored
View File

@@ -152,7 +152,7 @@ class SystemInstaller:
wsgiport = r["answer"]
nginx_conf_str = """
# pyshelf_nginx.conf
upstream django {server %s:%s;}
upstream django {server unix:///tmp/pyshelf_wsgi.sock;}
server {
listen %s;
server_name %s;
@@ -166,8 +166,6 @@ class SystemInstaller:
location / {uwsgi_pass django; include %s/uwsgi_params;}
}
""" % (
hostname,
wsgiport,
port,
hostname,
root,
@@ -195,11 +193,10 @@ class SystemInstaller:
master=True
pidfile=/tmp/pyShelf.pid
vacuum=True
socket=%s:%s
socket=/tmp/pyshelf_wsgi.sock
chmod-socket=777
""" % (
root,
hostname,
wsgiport
)
with open(_fp, "w") as write_file:
write_file.write(wsgi_conf_str)
@@ -223,7 +220,7 @@ installer = sysinstall.bin
install_answers = TerminalDisplay().installer()
for key in install_answers:
config[key["name"]] = key["answer"]
config["USER"] = os.environ["USER"]
# Write configuration
Configuration().write_file(config)
@@ -300,8 +297,9 @@ if RequiredServices().db_server_found(req) is False:
)
install_status = os.system(cmd)
for r in install_answers:
if r["name"] == "USER": sql_user = r["answer"]
elif r["name"] == "PASSWORD": sql_pass = r["answer"]
if r["name"] == "PASSWORD": sql_pass = r["answer"]
sql_user = config["USER"]
db_name = "pyshelf"
psql_cmd = """
CREATE DATABASE %s;

View File

@@ -44,13 +44,6 @@ class TerminalDisplay:
"answer": None,
"default": "5432",
},
{
"message": ' Input your PostgreSQL user name\n Enter for default "pyshelf" > ',
"options": "pyshelf",
"name": "USER",
"answer": None,
"default": "pyshelf",
},
{
"message": ' Input your PostgreSQL password\n Enter for default "pyshelf" > ',
"options": "pyshelf",

View File

@@ -141,7 +141,7 @@ class Catalogue:
"""
Calls storage system, gets list of books stored and compares against files on disk
"""
db = Storage(self.db_pointer, self.config)
db = Storage(self.config)
stored = db.book_paths_list()
db.close()
if self.books is None:
@@ -162,7 +162,7 @@ class Catalogue:
Iterates over list and inserts new books into database.
"""
book_list = self.compare_shelf_current()
db = Storage(self.db_pointer, self.config)
db = Storage(self.config)
for book in book_list:
book = self.process_book(book)
extracted = self.extract_metadata(book)

View File

@@ -12,8 +12,7 @@ from .config import Config
class Storage:
"""Contains all methods for system storage"""
def __init__(self, db_pointer, config):
# self.db_file = db_pointer
def __init__(self, config):
self.sql = config.catalogue_db
self.user = config.user
self.password = config.password
@@ -22,8 +21,27 @@ class Storage:
self.db = psycopg2.connect(
database=self.sql, user=self.user, password=self.password, host=self.db_host
)
self.config = config
self.cursor = self.db.cursor()
# self.create_tables()
def check_ownership(self, table=None):
if table is None:
table = "books"
_q = "SELECT * FROM books"
try:
self.cursor.execute(_q)
except Exception as e:
breakpoint()
if e.pgcode == "42501":
_q = """ALTER TABLE public.books OWNER to pyshelf;"""
self.close()
set_perms = Storage(self.config)
try:
set_perms.cursor.execute(_q)
set_perms.close()
except Exception as e:
print(e)
set_perms.close()
def create_tables(self):
"""Create table structure"""

View File

@@ -6,6 +6,7 @@ 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("."))
@@ -18,6 +19,8 @@ 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()
_t2 = time.time()

View File

@@ -107,8 +107,8 @@ DATABASES = {
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": CONFIG.user,
"PASSWORD": CONFIG.password,
"NAME": CONFIG.DATABASE,
"PASSWORD": CONFIG.PASSWORD,
}
}

View File

@@ -18,6 +18,7 @@ class Books(models.Model):
class Meta:
db_table = "books"
managed = False
def __str__(self):
return self.title