From dbea9c80dafd59ca9dc5fc6581d6e6a749b8bfca Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 24 Dec 2019 00:04:41 +0100 Subject: [PATCH] added renaming for nextcloud/webdav backends --- cookbook/provider/nextcloud.py | 20 ++++++++++++++++++-- cookbook/provider/provider.py | 4 ---- cookbook/views/edit.py | 7 +++++-- requirements.txt | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/cookbook/provider/nextcloud.py b/cookbook/provider/nextcloud.py index e58117276..602d6403c 100644 --- a/cookbook/provider/nextcloud.py +++ b/cookbook/provider/nextcloud.py @@ -26,7 +26,8 @@ class Nextcloud(Provider): import_count = 0 for file in files: path = monitor.path + '/' + file - if not Recipe.objects.filter(file_path=path).exists() and not RecipeImport.objects.filter(file_path=path).exists(): + if not Recipe.objects.filter(file_path=path).exists() and not RecipeImport.objects.filter( + file_path=path).exists(): name = os.path.splitext(file)[0] new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage) new_recipe.save() @@ -51,7 +52,8 @@ class Nextcloud(Provider): data = {'path': recipe.file_path, 'shareType': 3} - r = requests.post(url, headers=headers, auth=HTTPBasicAuth(recipe.storage.username, recipe.storage.password), data=data) + r = requests.post(url, headers=headers, auth=HTTPBasicAuth(recipe.storage.username, recipe.storage.password), + data=data) response_json = r.json() @@ -74,3 +76,17 @@ class Nextcloud(Provider): return element['url'] return Nextcloud.create_share_link(recipe) + + @staticmethod + def rename_file(recipe, new_name): + options = { + 'webdav_hostname': recipe.storage.url + '/remote.php/dav/files/' + recipe.storage.username, + 'webdav_login': recipe.storage.username, + 'webdav_password': recipe.storage.password + } + client = wc.Client(options) + + client.move(recipe.file_path, + os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1]) + + return True diff --git a/cookbook/provider/provider.py b/cookbook/provider/provider.py index 1b674cf1a..746fd0f5c 100644 --- a/cookbook/provider/provider.py +++ b/cookbook/provider/provider.py @@ -14,7 +14,3 @@ class Provider: @staticmethod def rename_file(recipe, new_name): raise Exception('Method not implemented in storage provider') - - @staticmethod - def delete_file(recipe): - raise Exception('Method not implemented in storage provider') diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index 127088481..98dfe326b 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -13,6 +13,7 @@ from django.views.generic import UpdateView, DeleteView from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredients from cookbook.provider.dropbox import Dropbox +from cookbook.provider.nextcloud import Nextcloud @login_required @@ -198,8 +199,10 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView): if not old_recipe.name == self.object.name: if self.object.storage.method == Storage.DROPBOX: Dropbox.rename_file(old_recipe, self.object.name) # TODO central location to handle storage type switches - self.object.file_path = os.path.dirname(self.object.file_path) + '/' + self.object.name + os.path.splitext(self.object.file_path)[1] - # TODO add nextcloud + if self.object.storage.method == Storage.NEXTCLOUD: + Nextcloud.rename_file(old_recipe, self.object.name) + + self.object.file_path = os.path.dirname(self.object.file_path) + '/' + self.object.name + os.path.splitext(self.object.file_path)[1] messages.add_message(self.request, messages.SUCCESS, _('Changes saved!')) return super(RecipeUpdate, self).form_valid(form) diff --git a/requirements.txt b/requirements.txt index c56df72a3..3befe57e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,6 @@ markdown simplejson lxml webdavclient3 -python-dotenv==0.7.1 +python-dotenv==0.10.3 psycopg2 gunicorn==19.7.1