From 78abf81b2f9fb834120f9e65d85b14d16979a66e Mon Sep 17 00:00:00 2001 From: Jack Darlington Date: Wed, 1 Mar 2017 22:38:03 +0000 Subject: [PATCH] Fix for editing metadata and uploading for GDrive --- cps/gdriveutils.py | 19 ++++++++++++++++++- cps/helper.py | 26 ++++++++++++++++++++++++++ cps/web.py | 8 +++++--- requirements.txt | 11 +++++++++++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 requirements.txt diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 66afed71..2d2f1ddb 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -21,7 +21,7 @@ Base = declarative_base() # Open session for database connection Session = sessionmaker() Session.configure(bind=engine) -session = Session() +session = scoped_session(Session) class GdriveId(Base): __tablename__='gdrive_ids' @@ -29,16 +29,33 @@ class GdriveId(Base): id = Column(Integer, primary_key=True) gdrive_id = Column(Integer, unique=True) path = Column(String) + __table_args__ = (UniqueConstraint('gdrive_id', 'path', name='_gdrive_path_uc'),) def __repr__(self): return str(self.path) +def migrate(): + for sql in session.execute("select sql from sqlite_master where type='table'"): + if 'CREATE TABLE gdrive_ids' in sql[0]: + currUniqueConstraint='UNIQUE (gdrive_id)' + if currUniqueConstraint in sql[0]: + sql=sql[0].replace(currUniqueConstraint, 'UNIQUE (gdrive_id, path)') + sql=sql.replace(GdriveId.__tablename__, GdriveId.__tablename__+ '2') + session.execute(sql) + session.execute('INSERT INTO gdrive_ids2 (id, gdrive_id, path) SELECT id, gdrive_id, path FROM gdrive_ids;') + session.commit() + session.execute('DROP TABLE %s' % 'gdrive_ids') + session.execute('ALTER TABLE gdrive_ids2 RENAME to gdrive_ids') + break + if not os.path.exists(dbpath): try: Base.metadata.create_all(engine) except Exception: raise +migrate() + def getDrive(gauth=None): if not gauth: gauth=GoogleAuth(settings_file='settings.yaml') diff --git a/cps/helper.py b/cps/helper.py index 54fa1946..2e365c2f 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -28,6 +28,8 @@ import shutil import requests import zipfile from tornado.ioloop import IOLoop +import gdriveutils as gd +import web try: import unidecode @@ -280,6 +282,30 @@ def update_dir_stucture(book_id, calibrepath): book.path = new_authordir + '/' + book.path.split('/')[1] db.session.commit() +def update_dir_structure_gdrive(book_id): + db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort) + book = db.session.query(db.Books).filter(db.Books.id == book_id).first() + + authordir = book.path.split('/')[0] + new_authordir = get_valid_filename(book.authors[0].name) + titledir = book.path.split('/')[1] + new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")" + + if titledir != new_titledir: + print (titledir) + gFile=gd.getFileFromEbooksFolder(web.Gdrive.Instance().drive,os.path.dirname(book.path),titledir) + gFile['title']= new_titledir + gFile.Upload() + book.path = book.path.split('/')[0] + '/' + new_titledir + + if authordir != new_authordir: + gFile=gd.getFileFromEbooksFolder(web.Gdrive.Instance().drive,None,authordir) + gFile['title']= new_authordir + gFile.Upload() + book.path = new_authordir + '/' + book.path.split('/')[1] + + db.session.commit() + class Updater(threading.Thread): def __init__(self): diff --git a/cps/web.py b/cps/web.py index 6b7a329d..ba0a7940 100755 --- a/cps/web.py +++ b/cps/web.py @@ -2443,7 +2443,10 @@ def edit_book(book_id): for author in book.authors: author_names.append(author.name) for b in edited_books_id: - helper.update_dir_stucture(b, config.config_calibre_dir) + if config.config_use_google_drive: + helper.update_dir_structure_gdrive(b) + else: + helper.update_dir_stucture(b, config.config_calibre_dir) if config.config_use_google_drive: updateGdriveCalibreFromLocal() if "detail_view" in to_save: @@ -2538,8 +2541,7 @@ def upload(): for author in db_book.authors: author_names.append(author.name) if config.config_use_google_drive: - if not current_user.role_edit() and not current_user.role_admin(): - updateGdriveCalibreFromLocal() + updateGdriveCalibreFromLocal() cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() if current_user.role_edit() or current_user.role_admin(): return render_title_template('book_edit.html', book=db_book, authors=author_names, cc=cc, diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..10f231bc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +google-api-python-client==1.6.1 +httplib2==0.9.2 +lxml==3.7.2 +oauth2client==4.0.0 +pyasn1==0.1.9 +pyasn1-modules==0.0.8 +PyDrive==1.3.1 +PyYAML==3.12 +rsa==3.4.2 +six==1.10.0 +uritemplate==3.0.0