fix exporting and importing from file
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import mariadb
|
||||
from cement import Controller, ex
|
||||
from kontor_schema import KontorDB
|
||||
|
||||
|
||||
class Database(Controller):
|
||||
@@ -30,9 +29,9 @@ class Database(Controller):
|
||||
}
|
||||
if self.app.pargs.db_file is not None:
|
||||
data['db_file'] = self.app.pargs.db_file
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
db = self.app.kontor_db
|
||||
self.app.log.info(f"export DB to {data['db_file']} as {data['export_type']}")
|
||||
results = kontor_db.export_db(data['export_type'], data['db_file'])
|
||||
results = db.export_db(data['export_type'], data['db_file'])
|
||||
data['results'] = results
|
||||
self.app.render(data, 'export_db.jinja2')
|
||||
|
||||
@@ -43,18 +42,25 @@ class Database(Controller):
|
||||
(['-f', '--file'],
|
||||
{'help': 'file to read data',
|
||||
'action': 'store',
|
||||
'dest': 'db_file'})
|
||||
'dest': 'db_file'}),
|
||||
(['-d', '--delete-first'],
|
||||
{'help': 'delete existing entries before import',
|
||||
'action': 'store_true',
|
||||
'dest': 'delete_first'})
|
||||
],
|
||||
)
|
||||
def import_cmd(self):
|
||||
data = {
|
||||
'db_file': 'data.json',
|
||||
'data_type': 'JSON',
|
||||
'delete_first': False,
|
||||
}
|
||||
if self.app.pargs.db_file is not None:
|
||||
data['db_file'] = self.app.pargs.db_file
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
kontor_db.import_db(data['db_file'], self.app.pargs.dry_run)
|
||||
if self.app.pargs.delete_first is not None:
|
||||
data['delete_first'] = self.app.pargs.delete_first
|
||||
db = self.app.kontor_db
|
||||
db.import_db(data['db_file'], data['delete_first'])
|
||||
|
||||
@ex(
|
||||
help='check the db schema against MetaDataTable and MetaDataColumn'
|
||||
@@ -72,13 +78,13 @@ class Database(Controller):
|
||||
cursor.execute("SHOW TABLES")
|
||||
for (tablename,) in cursor.fetchall():
|
||||
table_list.append(tablename)
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
table_names = kontor_db.get_table_names()
|
||||
db = self.app.kontor_db
|
||||
table_names = db.get_table_names()
|
||||
for table in table_list:
|
||||
if table not in table_names:
|
||||
self.app.log.info(f"{table} is not stored in MetaDataTable")
|
||||
continue
|
||||
meta_data = kontor_db.get_columns(table)
|
||||
meta_data = db.get_columns(table)
|
||||
field_info = self.get_table_field_info(cursor, table)
|
||||
for column in field_info:
|
||||
if column not in meta_data:
|
||||
|
||||
@@ -16,14 +16,14 @@ class Media(Controller):
|
||||
help='update title for mediafiles',
|
||||
)
|
||||
def update_title(self):
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
updates = kontor_db.get_update_list()
|
||||
db = self.app.kontor_db
|
||||
updates = db.get_update_list()
|
||||
self.app.log.info(f"found {len(updates)} links for update")
|
||||
for file_id, url in updates.items():
|
||||
link = VideoLink(url)
|
||||
title = link.get_title()
|
||||
if title is not None:
|
||||
kontor_db.update_entry('media_file', file_id, {'title': title, 'review': 0,})
|
||||
db.update_entry('media_file', file_id, {'title': title, 'review': 0,})
|
||||
|
||||
@ex(
|
||||
label='download',
|
||||
@@ -41,21 +41,21 @@ class Media(Controller):
|
||||
}
|
||||
if self.app.pargs.media_dir is not None:
|
||||
data['media_dir'] = self.app.pargs.media_dir
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
downloads = kontor_db.get_download_list()
|
||||
db = self.app.kontor_db
|
||||
downloads = db.get_download_list()
|
||||
self.app.log.info(f"found {len(downloads)} links for download")
|
||||
for file_id, url in downloads.items():
|
||||
link = VideoLink(url)
|
||||
file_name = link.download(download_dir=data['media_dir'])
|
||||
if file_name is None:
|
||||
kontor_db.update_entry('media_file', file_id, {'file_name': None, 'should_download': 1})
|
||||
db.update_entry('media_file', file_id, {'file_name': None, 'should_download': 1})
|
||||
else:
|
||||
download_file = Path(file_name)
|
||||
download_file.with_name(f"{file_id}{download_file.suffix}")
|
||||
link.file_name = download_file.name
|
||||
link.should_download = 0
|
||||
link.cloud_link = download_file.absolute()
|
||||
kontor_db.update_entry('media_file', file_id,
|
||||
db.update_entry('media_file', file_id,
|
||||
{
|
||||
'file_name': download_file.name,
|
||||
'should_download': 0,
|
||||
@@ -80,8 +80,8 @@ class Media(Controller):
|
||||
if self.app.pargs.dry_run:
|
||||
print(f"add url {data['link_url']} to database")
|
||||
else:
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
result = kontor_db.add_link(self.app.pargs.link)
|
||||
db = self.app.kontor_db
|
||||
result = db.add_link(self.app.pargs.link)
|
||||
self.log.info(result)
|
||||
else:
|
||||
print("no url was given.")
|
||||
@@ -101,5 +101,5 @@ class Media(Controller):
|
||||
}
|
||||
if self.app.pargs.media_dir is not None:
|
||||
data['media_dir'] = self.app.pargs.media_dir
|
||||
kontor_db = KontorDB(self.app.engine)
|
||||
kontor_db.check_files()
|
||||
db = self.app.kontor_db
|
||||
db.check_files()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
from cement import App, TestApp, init_defaults
|
||||
from cement.core.exc import CaughtSignal
|
||||
from kontor_schema.base import Base
|
||||
from kontor_schema import Base, KontorDB
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
@@ -35,6 +35,8 @@ def extend_sqlalchemy(app):
|
||||
Base.metadata.create_all(bind=engine, checkfirst=True)
|
||||
__session__ = sessionmaker(bind=engine)
|
||||
app.extend('engine', engine)
|
||||
kontor_db = KontorDB(engine, app.log)
|
||||
app.extend('kontor_db', kontor_db)
|
||||
|
||||
|
||||
class Kontor(App):
|
||||
|
||||
Reference in New Issue
Block a user