Delete on gdrive working

Moving author on gdrive working
bugfix delete on normal folders
This commit is contained in:
OzzieIsaacs 2018-07-14 13:48:51 +02:00
parent a8040ad3fa
commit 31b703db62
3 changed files with 60 additions and 36 deletions

View File

@ -255,9 +255,30 @@ def copyDriveFileRemote(drive, origin_file_id, copy_title):
print ('An error occurred: %s' % error) print ('An error occurred: %s' % error)
return None return None
def moveGdriveFolderRemote(origin_file, target_folder):
drive = getDrive(Gdrive.Instance().drive)
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
gFileTargetDir = getFileFromEbooksFolder(None, target_folder)
if not gFileTargetDir:
# Folder is not exisiting, create, and move folder
gFileTargetDir = drive.CreateFile(
{'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
"mimeType": "application/vnd.google-apps.folder"})
gFileTargetDir.Upload()
# Move the file to the new folder
drive.auth.service.files().update(fileId=origin_file['id'],
addParents=gFileTargetDir['id'],
removeParents=previous_parents,
fields='id, parents').execute()
# if previous_parents has no childs anymore, delete originfileparent
# is not working correctly, because of slow update on gdrive -> could cause trouble in gdrive.db
# (nonexisting folder has id)
# children = drive.auth.service.children().list(folderId=previous_parents).execute()
# if not len(children['items']):
# drive.auth.service.files().delete(fileId=previous_parents).execute()
def downloadFile(path, filename, output): def downloadFile(path, filename, output):
# drive = getDrive(drive)
f = getFileFromEbooksFolder(path, filename) f = getFileFromEbooksFolder(path, filename)
f.GetContentFile(output) f.GetContentFile(output)
@ -413,7 +434,7 @@ def getChangeById (drive, change_id):
change = drive.auth.service.changes().get(changeId=change_id).execute() change = drive.auth.service.changes().get(changeId=change_id).execute()
return change return change
except (errors.HttpError) as error: except (errors.HttpError) as error:
app.logger.info(error.message) web.app.logger.info(error.message)
return None return None
# Deletes the local hashes database to force search for new folder names # Deletes the local hashes database to force search for new folder names
@ -434,3 +455,8 @@ def updateDatabaseOnEdit(ID,newPath):
if storedPathName: if storedPathName:
storedPathName.path = newPath storedPathName.path = newPath
session.commit() session.commit()
# Deletes the hashes in database of deleted book
def deleteDatabaseEntry(ID):
session.query(GdriveId).filter(GdriveId.gdrive_id == ID).delete()
session.commit()

View File

@ -365,36 +365,24 @@ def update_dir_structure_gdrive(book_id):
if authordir != new_authordir: if authordir != new_authordir:
gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir)
# gFileDirOrig = gd.getFileFromEbooksFolder(None, authordir)
if gFile: if gFile:
# check if authordir exisits gd.moveGdriveFolderRemote(gFile,new_authordir)
gFileDirOrig = gd.getFileFromEbooksFolder(None, authordir)
if gFileDirOrig:
gFile['parents'].append({"id": gFileDirOrig['id']})
gFile.Upload()
else:
# Folder is not exisiting
#parent = drive.CreateFile({'title': authordir, 'parents': [{"kind": "drive#fileLink", 'id': root folder id}],
# "mimeType": "application/vnd.google-apps.folder"})
parent.Upload()
# gFile['title'] = new_authordir
# gFile.Upload()
book.path = new_authordir + '/' + book.path.split('/')[1] book.path = new_authordir + '/' + book.path.split('/')[1]
gd.updateDatabaseOnEdit(gFile['id'], book.path) gd.updateDatabaseOnEdit(gFile['id'], book.path)
# Todo last element from parent folder moved to different folder, what to do with parent folder?
# parent folder affected
else: else:
error = _(u'File %s not found on gdrive' % authordir) # file not found error = _(u'File %s not found on gdrive' % authordir) # file not found
return error return error
# ToDo: Implement delete book on gdrive
def delete_book_gdrive(book):
# delete book and path of book in gdrive.db
# delete book and path of book on gdrive
#gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir)
#gFile.Trash()
pass
def delete_book_gdrive(book):
error= False
gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path),book.path.split('/')[1])
if gFile:
gd.deleteDatabaseEntry(gFile['id'])
gFile.Trash()
else:
error =_(u'Path %s not found on gdrive' % book.path) # file not found
return error
################################## External interface ################################## External interface
@ -406,9 +394,9 @@ def update_dir_stucture(book_id, calibrepath):
def delete_book(book, calibrepath): def delete_book(book, calibrepath):
if ub.config.config_use_google_drive: if ub.config.config_use_google_drive:
return delete_book_file(book, calibrepath)
else:
return delete_book_gdrive(book) return delete_book_gdrive(book)
else:
return delete_book_file(book, calibrepath)
################################## ##################################

View File

@ -1568,7 +1568,7 @@ def on_received_watch_confirmation():
db.setup_db() db.setup_db()
except Exception as e: except Exception as e:
app.logger.info(e.message) app.logger.info(e.message)
# app.logger.exception(e) app.logger.exception(e)
updateMetaData() updateMetaData()
return '' return ''
@ -3293,40 +3293,50 @@ def upload():
if db_language is not None: if db_language is not None:
db_book.languages.append(db_language) db_book.languages.append(db_language)
db_data = db.Data(db_book, meta.extension.upper()[1:], file_size, data_name) db_data = db.Data(db_book, meta.extension.upper()[1:], file_size, data_name)
input_tags = tags.split(',')
input_tags = list(map(lambda it: it.strip(), input_tags))
if input_tags[0] !="":
modify_database_object(input_tags, db_book.tags, db.Tags, db.session, 'tags')
db_book.data.append(db_data) db_book.data.append(db_data)
db.session.add(db_book) db.session.add(db_book)
db.session.flush() # flush content get db_book.id avalible db.session.flush() # flush content get db_book.id avalible
# add comment # add comment
book_id = db_book.id
upload_comment = Markup(meta.description).unescape() upload_comment = Markup(meta.description).unescape()
if upload_comment != "": if upload_comment != "":
db.session.add(db.Comments(upload_comment, db_book.id)) db.session.add(db.Comments(upload_comment, book_id))
input_tags = tags.split(',')
input_tags = list(map(lambda it: it.strip(), input_tags))
if input_tags[0] !="":
modify_database_object(input_tags, db_book.tags, db.Tags, db.session, 'tags')
db.session.commit() db.session.commit()
book = db.session.query(db.Books) \
.filter(db.Books.id == book_id).filter(common_filters()).first()
if config.config_use_google_drive: if config.config_use_google_drive:
gdriveutils.updateGdriveCalibreFromLocal() gdriveutils.updateGdriveCalibreFromLocal()
error = helper.update_dir_stucture(db_book.id, config.config_calibre_dir)
error = helper.update_dir_stucture(book.id, config.config_calibre_dir)
# ToDo: Handle error # ToDo: Handle error
if error: if error:
pass pass
if db_language is not None: # display Full name instead of iso639.part3 if db_language is not None: # display Full name instead of iso639.part3
db_book.languages[0].language_name = _(meta.languages) book.languages[0].language_name = _(meta.languages)
author_names = [] author_names = []
for author in db_book.authors: for author in db_book.authors:
author_names.append(author.name) author_names.append(author.name)
if len(request.files.getlist("btn-upload")) < 2: if len(request.files.getlist("btn-upload")) < 2:
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort)
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() 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(): 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,title=_(u"edit metadata")) return render_title_template('book_edit.html', book=book, authors=author_names, cc=cc,title=_(u"edit metadata"))
book_in_shelfs = [] book_in_shelfs = []
return render_title_template('detail.html', entry=db_book, cc=cc, title=db_book.title, books_shelfs=book_in_shelfs, ) return render_title_template('detail.html', entry=book, cc=cc, title=book.title, books_shelfs=book_in_shelfs, )
return redirect(url_for("index")) return redirect(url_for("index"))
else: else:
return redirect(url_for("index")) return redirect(url_for("index"))