From 31b703db62f225faf3ea97f8bb33660bfac7800f Mon Sep 17 00:00:00 2001 From: OzzieIsaacs Date: Sat, 14 Jul 2018 13:48:51 +0200 Subject: [PATCH] Delete on gdrive working Moving author on gdrive working bugfix delete on normal folders --- cps/gdriveutils.py | 30 ++++++++++++++++++++++++++++-- cps/helper.py | 36 ++++++++++++------------------------ cps/web.py | 30 ++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 69c6c091..776cdb61 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -255,9 +255,30 @@ def copyDriveFileRemote(drive, origin_file_id, copy_title): print ('An error occurred: %s' % error) 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): - # drive = getDrive(drive) f = getFileFromEbooksFolder(path, filename) f.GetContentFile(output) @@ -413,7 +434,7 @@ def getChangeById (drive, change_id): change = drive.auth.service.changes().get(changeId=change_id).execute() return change except (errors.HttpError) as error: - app.logger.info(error.message) + web.app.logger.info(error.message) return None # Deletes the local hashes database to force search for new folder names @@ -434,3 +455,8 @@ def updateDatabaseOnEdit(ID,newPath): if storedPathName: storedPathName.path = newPath session.commit() + +# Deletes the hashes in database of deleted book +def deleteDatabaseEntry(ID): + session.query(GdriveId).filter(GdriveId.gdrive_id == ID).delete() + session.commit() diff --git a/cps/helper.py b/cps/helper.py index f70c92bf..79a8a252 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -365,36 +365,24 @@ def update_dir_structure_gdrive(book_id): if authordir != new_authordir: gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) - # gFileDirOrig = gd.getFileFromEbooksFolder(None, authordir) if gFile: - # check if authordir exisits - 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() + gd.moveGdriveFolderRemote(gFile,new_authordir) book.path = new_authordir + '/' + book.path.split('/')[1] 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: error = _(u'File %s not found on gdrive' % authordir) # file not found 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 @@ -406,9 +394,9 @@ def update_dir_stucture(book_id, calibrepath): def delete_book(book, calibrepath): if ub.config.config_use_google_drive: - return delete_book_file(book, calibrepath) - else: return delete_book_gdrive(book) + else: + return delete_book_file(book, calibrepath) ################################## diff --git a/cps/web.py b/cps/web.py index c788ac9c..3aec6e2c 100755 --- a/cps/web.py +++ b/cps/web.py @@ -1568,7 +1568,7 @@ def on_received_watch_confirmation(): db.setup_db() except Exception as e: app.logger.info(e.message) - # app.logger.exception(e) + app.logger.exception(e) updateMetaData() return '' @@ -3293,40 +3293,50 @@ def upload(): if db_language is not None: db_book.languages.append(db_language) 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.session.add(db_book) db.session.flush() # flush content get db_book.id avalible # add comment + book_id = db_book.id upload_comment = Markup(meta.description).unescape() if upload_comment != "": - db.session.add(db.Comments(upload_comment, db_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.add(db.Comments(upload_comment, book_id)) 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: 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 if error: pass + 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 = [] for author in db_book.authors: author_names.append(author.name) 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() 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 = [] - 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")) else: return redirect(url_for("index"))