external delete button

This commit is contained in:
vabene1111 2019-12-25 15:54:23 +01:00
parent 4b0164a676
commit e2301c0c3a
7 changed files with 84 additions and 31 deletions

View File

@ -27,7 +27,7 @@ class Dropbox(Provider):
try:
recipes = r.json()
except ValueError:
log_entry = SyncLog(status='ERROR', msg=str(r), monitor=monitor)
log_entry = SyncLog(status='ERROR', msg=str(r), sync=monitor)
log_entry.save()
return r
@ -105,3 +105,20 @@ class Dropbox(Provider):
r = requests.post(url, headers=headers, data=json.dumps(data))
return r.json()
@staticmethod
def delete_file(recipe):
url = "https://api.dropboxapi.com/2/files/delete_v2"
headers = {
"Authorization": "Bearer " + recipe.storage.token,
"Content-Type": "application/json"
}
data = {
"path": recipe.file_path
}
r = requests.post(url, headers=headers, data=json.dumps(data))
return r.json()

View File

@ -12,13 +12,17 @@ from cookbook.provider.provider import Provider
class Nextcloud(Provider):
@staticmethod
def import_all(monitor):
def get_client(storage):
options = {
'webdav_hostname': monitor.storage.url + '/remote.php/dav/files/' + monitor.storage.username,
'webdav_login': monitor.storage.username,
'webdav_password': monitor.storage.password
'webdav_hostname': storage.url + '/remote.php/dav/files/' + storage.username,
'webdav_login': storage.username,
'webdav_password': storage.password
}
client = wc.Client(options)
return wc.Client(options)
@staticmethod
def import_all(monitor):
client = Nextcloud.get_client(monitor.storage)
files = client.list(monitor.path)
files.pop(0) # remove first element because its the folder itself
@ -79,14 +83,17 @@ class Nextcloud(Provider):
@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 = Nextcloud.get_client(recipe.storage)
client.move(recipe.file_path,
os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1])
return True
@staticmethod
def delete_file(recipe):
client = Nextcloud.get_client(recipe.storage)
client.clean(recipe.file_path)
return True

View File

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

View File

@ -40,25 +40,29 @@
{% if view_url %}
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
{% endif %}
{% if form.instance.storage %}
<a href="{% url 'delete_recipe_source' form.instance.pk %}" class="btn btn-warning"><i
class="fas fa-exclamation-triangle"></i> {% trans 'Delete original file' %}</a>
{% endif %}
</form>
<script>
function selectText(node) {
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
} else {
console.warn("Could not select text in node: Unsupported browser.");
}
}
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
} else {
console.warn("Could not select text in node: Unsupported browser.");
}
}
//converts multiselct in recipe edit to searchable multiselect
//shitty solution that needs to be redone at some point

View File

@ -26,6 +26,9 @@
{% if view_url %}
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
{% endif %}
{% if delete_external_url %}
<a href="{{ delete_external_url }}" class="btn btn-warning"><i class="fas fa-exclamation-triangle"></i> {% trans 'Delete original file' %}</a>
{% endif %}
</form>
{% endblock %}

View File

@ -38,6 +38,7 @@ urlpatterns = [
path('redirect/delete/<slug:name>/<int:pk>/', edit.delete_redirect, name='redirect_delete'),
path('delete/recipe/<int:pk>/', edit.RecipeDelete.as_view(), name='delete_recipe'),
path('delete/recipe-source/<int:pk>/', edit.RecipeSourceDelete.as_view(), name='delete_recipe_source'),
path('delete/keyword/<int:pk>/', edit.KeywordDelete.as_view(), name='delete_keyword'),
path('delete/sync/<int:pk>/', edit.MonitorDelete.as_view(), name='delete_sync'),
path('delete/import/<int:pk>/', edit.ImportDelete.as_view(), name='delete_import'),

View File

@ -237,6 +237,8 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView):
context = super(RecipeUpdate, self).get_context_data(**kwargs)
context['title'] = _("Recipe")
context['view_url'] = reverse('view_recipe', args=[self.object.pk])
if self.object.storage:
context['delete_external_url'] = reverse('delete_recipe_source', args=[self.object.pk])
return context
@ -251,17 +253,32 @@ class RecipeDelete(LoginRequiredMixin, DeleteView):
model = Recipe
success_url = reverse_lazy('index')
def form_valid(self, form):
self.object = form.save(commit=False)
return super(RecipeDelete, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(RecipeDelete, self).get_context_data(**kwargs)
context['title'] = _("Recipe")
return context
class RecipeSourceDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Recipe
success_url = reverse_lazy('index')
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.storage.method == Storage.DROPBOX:
Dropbox.delete_file(self.object) # TODO central location to handle storage type switches
if self.object.storage.method == Storage.NEXTCLOUD:
Nextcloud.delete_file(self.object)
return super(RecipeSourceDelete, self).delete(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(RecipeSourceDelete, self).get_context_data(**kwargs)
context['title'] = _("Recipe")
return context
class ImportDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = RecipeImport