added renaming for nextcloud/webdav backends

This commit is contained in:
vabene1111
2019-12-24 00:04:41 +01:00
parent 3b5dd7e51d
commit dbea9c80da
4 changed files with 24 additions and 9 deletions

View File

@ -26,7 +26,8 @@ class Nextcloud(Provider):
import_count = 0 import_count = 0
for file in files: for file in files:
path = monitor.path + '/' + file 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] name = os.path.splitext(file)[0]
new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage) new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage)
new_recipe.save() new_recipe.save()
@ -51,7 +52,8 @@ class Nextcloud(Provider):
data = {'path': recipe.file_path, 'shareType': 3} 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() response_json = r.json()
@ -74,3 +76,17 @@ class Nextcloud(Provider):
return element['url'] return element['url']
return Nextcloud.create_share_link(recipe) 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

View File

@ -14,7 +14,3 @@ class Provider:
@staticmethod @staticmethod
def rename_file(recipe, new_name): def rename_file(recipe, new_name):
raise Exception('Method not implemented in storage provider') raise Exception('Method not implemented in storage provider')
@staticmethod
def delete_file(recipe):
raise Exception('Method not implemented in storage provider')

View File

@ -13,6 +13,7 @@ from django.views.generic import UpdateView, DeleteView
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm
from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredients from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredients
from cookbook.provider.dropbox import Dropbox from cookbook.provider.dropbox import Dropbox
from cookbook.provider.nextcloud import Nextcloud
@login_required @login_required
@ -198,8 +199,10 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView):
if not old_recipe.name == self.object.name: if not old_recipe.name == self.object.name:
if self.object.storage.method == Storage.DROPBOX: if self.object.storage.method == Storage.DROPBOX:
Dropbox.rename_file(old_recipe, self.object.name) # TODO central location to handle storage type switches Dropbox.rename_file(old_recipe, self.object.name) # TODO central location to handle storage type switches
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] 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!')) messages.add_message(self.request, messages.SUCCESS, _('Changes saved!'))
return super(RecipeUpdate, self).form_valid(form) return super(RecipeUpdate, self).form_valid(form)

View File

@ -10,6 +10,6 @@ markdown
simplejson simplejson
lxml lxml
webdavclient3 webdavclient3
python-dotenv==0.7.1 python-dotenv==0.10.3
psycopg2 psycopg2
gunicorn==19.7.1 gunicorn==19.7.1