moved delete to seperate file

This commit is contained in:
vabene1111 2020-03-01 12:34:59 +01:00
parent e755068a31
commit abd655ce62
4 changed files with 152 additions and 142 deletions

View File

@ -41,18 +41,18 @@ urlpatterns = [
path('edit/plan/<int:pk>/', edit.MealPlanUpdate.as_view(), name='edit_plan'),
path('edit/ingredient/', edit.edit_ingredients, name='edit_ingredient'),
path('redirect/delete/<slug:name>/<int:pk>/', edit.delete_redirect, name='redirect_delete'),
path('redirect/delete/<slug:name>/<int:pk>/', delete.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'),
path('delete/storage/<int:pk>/', edit.StorageDelete.as_view(), name='delete_storage'),
path('delete/comment/<int:pk>/', edit.CommentDelete.as_view(), name='delete_comment'),
path('delete/recipe-book/<int:pk>/', edit.RecipeBookDelete.as_view(), name='delete_recipe_book'),
path('delete/recipe-book-entry/<int:pk>/', edit.RecipeBookEntryDelete.as_view(), name='delete_recipe_book_entry'),
path('delete/plan/<int:pk>/', edit.MealPlanDelete.as_view(), name='delete_plan'),
path('delete/recipe/<int:pk>/', delete.RecipeDelete.as_view(), name='delete_recipe'),
path('delete/recipe-source/<int:pk>/', delete.RecipeSourceDelete.as_view(), name='delete_recipe_source'),
path('delete/keyword/<int:pk>/', delete.KeywordDelete.as_view(), name='delete_keyword'),
path('delete/sync/<int:pk>/', delete.MonitorDelete.as_view(), name='delete_sync'),
path('delete/import/<int:pk>/', delete.ImportDelete.as_view(), name='delete_import'),
path('delete/storage/<int:pk>/', delete.StorageDelete.as_view(), name='delete_storage'),
path('delete/comment/<int:pk>/', delete.CommentDelete.as_view(), name='delete_comment'),
path('delete/recipe-book/<int:pk>/', delete.RecipeBookDelete.as_view(), name='delete_recipe_book'),
path('delete/recipe-book-entry/<int:pk>/', delete.RecipeBookEntryDelete.as_view(), name='delete_recipe_book_entry'),
path('delete/plan/<int:pk>/', delete.MealPlanDelete.as_view(), name='delete_plan'),
path('data/sync', data.sync, name='data_sync'), # TODO move to generic "new" view
path('data/batch/edit', data.batch_edit, name='data_batch_edit'),

View File

@ -3,4 +3,5 @@ from cookbook.views.api import *
from cookbook.views.data import *
from cookbook.views.edit import *
from cookbook.views.new import *
from cookbook.views.lists import *
from cookbook.views.lists import *
from cookbook.views.delete import *

134
cookbook/views/delete.py Normal file
View File

@ -0,0 +1,134 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.utils.translation import gettext as _
from django.views.generic import DeleteView
from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeBook, \
RecipeBookEntry, MealPlan
from cookbook.provider.dropbox import Dropbox
from cookbook.provider.nextcloud import Nextcloud
# Generic Delete views
def delete_redirect(request, name, pk):
return redirect(('delete_' + name), pk)
class RecipeDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Recipe
success_url = reverse_lazy('index')
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
success_url = reverse_lazy('list_import')
def get_context_data(self, **kwargs):
context = super(ImportDelete, self).get_context_data(**kwargs)
context['title'] = _("Import")
return context
class MonitorDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Sync
success_url = reverse_lazy('data_sync')
def get_context_data(self, **kwargs):
context = super(MonitorDelete, self).get_context_data(**kwargs)
context['title'] = _("Monitor")
return context
class KeywordDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Keyword
success_url = reverse_lazy('list_keyword')
def get_context_data(self, **kwargs):
context = super(KeywordDelete, self).get_context_data(**kwargs)
context['title'] = _("Keyword")
return context
class StorageDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Storage
success_url = reverse_lazy('list_storage')
def get_context_data(self, **kwargs):
context = super(StorageDelete, self).get_context_data(**kwargs)
context['title'] = _("Storage Backend")
return context
class CommentDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Comment
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super(CommentDelete, self).get_context_data(**kwargs)
context['title'] = _("Comment")
return context
class RecipeBookDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = RecipeBook
success_url = reverse_lazy('view_books')
def get_context_data(self, **kwargs):
context = super(RecipeBookDelete, self).get_context_data(**kwargs)
context['title'] = _("Recipe Book")
return context
class RecipeBookEntryDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = RecipeBookEntry
success_url = reverse_lazy('view_books')
def get_context_data(self, **kwargs):
context = super(RecipeBookEntryDelete, self).get_context_data(**kwargs)
context['title'] = _("Bookmarks")
return context
class MealPlanDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = MealPlan
success_url = reverse_lazy('view_plan')
def get_context_data(self, **kwargs):
context = super(MealPlanDelete, self).get_context_data(**kwargs)
context['title'] = _("Meal-Plan")
return context

View File

@ -4,22 +4,20 @@ from io import BytesIO
import simplejson
import simplejson as json
from PIL import Image
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.files import File
from django.db.models import Value, CharField
from django.http import HttpResponseRedirect
from django.shortcuts import redirect, get_object_or_404, render
from django.urls import reverse_lazy, reverse
from django.utils.translation import gettext as _, ngettext
from django.views.generic import UpdateView, DeleteView
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.utils.translation import gettext as _
from django.views.generic import UpdateView
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm, \
MealPlanForm, UnitMergeForm, IngredientMergeForm
from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredient, RecipeBook, \
RecipeBookEntry, MealPlan, Unit, Ingredient
MealPlan, Unit, Ingredient
from cookbook.provider.dropbox import Dropbox
from cookbook.provider.nextcloud import Nextcloud
@ -345,126 +343,3 @@ def edit_ingredients(request):
return render(request, 'forms/ingredients.html', {'units_form': units_form, 'ingredients_form': ingredients_form})
# Generic Delete views
def delete_redirect(request, name, pk):
return redirect(('delete_' + name), pk)
class RecipeDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Recipe
success_url = reverse_lazy('index')
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
success_url = reverse_lazy('list_import')
def get_context_data(self, **kwargs):
context = super(ImportDelete, self).get_context_data(**kwargs)
context['title'] = _("Import")
return context
class MonitorDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Sync
success_url = reverse_lazy('data_sync')
def get_context_data(self, **kwargs):
context = super(MonitorDelete, self).get_context_data(**kwargs)
context['title'] = _("Monitor")
return context
class KeywordDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Keyword
success_url = reverse_lazy('list_keyword')
def get_context_data(self, **kwargs):
context = super(KeywordDelete, self).get_context_data(**kwargs)
context['title'] = _("Keyword")
return context
class StorageDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Storage
success_url = reverse_lazy('list_storage')
def get_context_data(self, **kwargs):
context = super(StorageDelete, self).get_context_data(**kwargs)
context['title'] = _("Storage Backend")
return context
class CommentDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = Comment
success_url = reverse_lazy('index')
def get_context_data(self, **kwargs):
context = super(CommentDelete, self).get_context_data(**kwargs)
context['title'] = _("Comment")
return context
class RecipeBookDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = RecipeBook
success_url = reverse_lazy('view_books')
def get_context_data(self, **kwargs):
context = super(RecipeBookDelete, self).get_context_data(**kwargs)
context['title'] = _("Recipe Book")
return context
class RecipeBookEntryDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = RecipeBookEntry
success_url = reverse_lazy('view_books')
def get_context_data(self, **kwargs):
context = super(RecipeBookEntryDelete, self).get_context_data(**kwargs)
context['title'] = _("Bookmarks")
return context
class MealPlanDelete(LoginRequiredMixin, DeleteView):
template_name = "generic/delete_template.html"
model = MealPlan
success_url = reverse_lazy('view_plan')
def get_context_data(self, **kwargs):
context = super(MealPlanDelete, self).get_context_data(**kwargs)
context['title'] = _("Meal-Plan")
return context