external delete button
This commit is contained in:
@ -27,7 +27,7 @@ class Dropbox(Provider):
|
|||||||
try:
|
try:
|
||||||
recipes = r.json()
|
recipes = r.json()
|
||||||
except ValueError:
|
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()
|
log_entry.save()
|
||||||
return r
|
return r
|
||||||
|
|
||||||
@ -105,3 +105,20 @@ class Dropbox(Provider):
|
|||||||
r = requests.post(url, headers=headers, data=json.dumps(data))
|
r = requests.post(url, headers=headers, data=json.dumps(data))
|
||||||
|
|
||||||
return r.json()
|
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()
|
||||||
|
@ -12,13 +12,17 @@ from cookbook.provider.provider import Provider
|
|||||||
class Nextcloud(Provider):
|
class Nextcloud(Provider):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def import_all(monitor):
|
def get_client(storage):
|
||||||
options = {
|
options = {
|
||||||
'webdav_hostname': monitor.storage.url + '/remote.php/dav/files/' + monitor.storage.username,
|
'webdav_hostname': storage.url + '/remote.php/dav/files/' + storage.username,
|
||||||
'webdav_login': monitor.storage.username,
|
'webdav_login': storage.username,
|
||||||
'webdav_password': monitor.storage.password
|
'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 = client.list(monitor.path)
|
||||||
files.pop(0) # remove first element because its the folder itself
|
files.pop(0) # remove first element because its the folder itself
|
||||||
@ -79,14 +83,17 @@ class Nextcloud(Provider):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def rename_file(recipe, new_name):
|
def rename_file(recipe, new_name):
|
||||||
options = {
|
client = Nextcloud.get_client(recipe.storage)
|
||||||
'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,
|
client.move(recipe.file_path,
|
||||||
os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1])
|
os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def delete_file(recipe):
|
||||||
|
client = Nextcloud.get_client(recipe.storage)
|
||||||
|
|
||||||
|
client.clean(recipe.file_path)
|
||||||
|
|
||||||
|
return True
|
||||||
|
@ -14,3 +14,7 @@ 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, new_name):
|
||||||
|
raise Exception('Method not implemented in storage provider')
|
||||||
|
@ -40,6 +40,10 @@
|
|||||||
{% if view_url %}
|
{% if view_url %}
|
||||||
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
|
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
|
||||||
{% endif %}
|
{% 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>
|
</form>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
@ -58,7 +62,7 @@
|
|||||||
} else {
|
} else {
|
||||||
console.warn("Could not select text in node: Unsupported browser.");
|
console.warn("Could not select text in node: Unsupported browser.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//converts multiselct in recipe edit to searchable multiselect
|
//converts multiselct in recipe edit to searchable multiselect
|
||||||
//shitty solution that needs to be redone at some point
|
//shitty solution that needs to be redone at some point
|
||||||
|
@ -26,6 +26,9 @@
|
|||||||
{% if view_url %}
|
{% if view_url %}
|
||||||
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
|
<a href="{{ view_url }}" class="btn btn-info">{% trans 'View' %} <i class="far fa-eye"></i></a>
|
||||||
{% endif %}
|
{% 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>
|
</form>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
@ -38,6 +38,7 @@ urlpatterns = [
|
|||||||
path('redirect/delete/<slug:name>/<int:pk>/', edit.delete_redirect, name='redirect_delete'),
|
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/<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/keyword/<int:pk>/', edit.KeywordDelete.as_view(), name='delete_keyword'),
|
||||||
path('delete/sync/<int:pk>/', edit.MonitorDelete.as_view(), name='delete_sync'),
|
path('delete/sync/<int:pk>/', edit.MonitorDelete.as_view(), name='delete_sync'),
|
||||||
path('delete/import/<int:pk>/', edit.ImportDelete.as_view(), name='delete_import'),
|
path('delete/import/<int:pk>/', edit.ImportDelete.as_view(), name='delete_import'),
|
||||||
|
@ -237,6 +237,8 @@ class RecipeUpdate(LoginRequiredMixin, UpdateView):
|
|||||||
context = super(RecipeUpdate, self).get_context_data(**kwargs)
|
context = super(RecipeUpdate, self).get_context_data(**kwargs)
|
||||||
context['title'] = _("Recipe")
|
context['title'] = _("Recipe")
|
||||||
context['view_url'] = reverse('view_recipe', args=[self.object.pk])
|
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
|
return context
|
||||||
|
|
||||||
|
|
||||||
@ -251,17 +253,32 @@ class RecipeDelete(LoginRequiredMixin, DeleteView):
|
|||||||
model = Recipe
|
model = Recipe
|
||||||
success_url = reverse_lazy('index')
|
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):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(RecipeDelete, self).get_context_data(**kwargs)
|
context = super(RecipeDelete, self).get_context_data(**kwargs)
|
||||||
context['title'] = _("Recipe")
|
context['title'] = _("Recipe")
|
||||||
return context
|
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):
|
class ImportDelete(LoginRequiredMixin, DeleteView):
|
||||||
template_name = "generic/delete_template.html"
|
template_name = "generic/delete_template.html"
|
||||||
model = RecipeImport
|
model = RecipeImport
|
||||||
|
Reference in New Issue
Block a user