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

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