diff --git a/cookbook/provider/dropbox.py b/cookbook/provider/dropbox.py index 8236144c2..c058d82ec 100644 --- a/cookbook/provider/dropbox.py +++ b/cookbook/provider/dropbox.py @@ -34,7 +34,8 @@ class Dropbox(Provider): import_count = 0 for recipe in recipes['entries']: # TODO check if has_more is set and import that as well path = recipe['path_lower'] - 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(recipe['name'])[0] new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage, file_uid=recipe['id']) new_recipe.save() @@ -75,7 +76,7 @@ class Dropbox(Provider): } data = { - "path": recipe.file_uid + "path": recipe.file_path, } r = requests.post(url, headers=headers, data=json.dumps(data)) @@ -86,3 +87,21 @@ class Dropbox(Provider): response = Dropbox.create_share_link(recipe) return response['url'] + + @staticmethod + def rename_file(recipe, new_name): + url = "https://api.dropboxapi.com/2/files/move_v2" + + headers = { + "Authorization": "Bearer " + recipe.storage.token, + "Content-Type": "application/json" + } + + data = { + "from_path": recipe.file_path, + "to_path": os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1] + } + + r = requests.post(url, headers=headers, data=json.dumps(data)) + + return r.json() diff --git a/cookbook/provider/provider.py b/cookbook/provider/provider.py index 2ce69796b..1b674cf1a 100644 --- a/cookbook/provider/provider.py +++ b/cookbook/provider/provider.py @@ -10,3 +10,11 @@ class Provider: @staticmethod def get_share_link(recipe): raise Exception('Method not implemented in storage 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 d15a4cf60..b5aa2415d 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -11,6 +11,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 @login_required @@ -191,6 +192,14 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView): template_name = "generic/edit_template.html" def form_valid(self, form): + self.object = form.save(commit=False) + old_recipe = Recipe.objects.get(pk=self.object.pk) + 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 + messages.add_message(self.request, messages.SUCCESS, _('Changes saved!')) return super(RecipeUpdate, self).form_valid(form)