Best Python code snippet using localstack_python
delete.py
Source:delete.py
1from django.contrib import messages2from django.db import models3from django.db.models import ProtectedError4from django.http import HttpResponseRedirect5from django.shortcuts import get_object_or_404, render6from django.urls import reverse, reverse_lazy7from django.utils.translation import gettext as _8from django.views.generic import DeleteView9from cookbook.helper.permission_helper import GroupRequiredMixin, OwnerRequiredMixin, group_required10from cookbook.models import (Comment, InviteLink, MealPlan, Recipe, RecipeBook, RecipeBookEntry,11 RecipeImport, Storage, Sync, UserSpace, Space)12from cookbook.provider.dropbox import Dropbox13from cookbook.provider.local import Local14from cookbook.provider.nextcloud import Nextcloud15class RecipeDelete(GroupRequiredMixin, DeleteView):16 groups_required = ['user']17 template_name = "generic/delete_template.html"18 model = Recipe19 success_url = reverse_lazy('index')20 def delete(self, request, *args, **kwargs):21 self.object = self.get_object()22 # TODO make this more generic so that all delete functions benefit from this23 if self.get_context_data()['protected_objects']:24 return render(request, template_name=self.template_name, context=self.get_context_data())25 success_url = self.get_success_url()26 self.object.delete()27 return HttpResponseRedirect(success_url)28 def get_context_data(self, **kwargs):29 context = super(RecipeDelete, self).get_context_data(**kwargs)30 context['title'] = _("Recipe")31 # TODO make this more generic so that all delete functions benefit from this32 self.object = self.get_object()33 context['protected_objects'] = []34 context['cascading_objects'] = []35 context['set_null_objects'] = []36 for x in self.object._meta.get_fields():37 try:38 related = x.related_model.objects.filter(**{x.field.name: self.object})39 if related.exists() and x.on_delete == models.PROTECT:40 context['protected_objects'].append(related)41 if related.exists() and x.on_delete == models.CASCADE:42 context['cascading_objects'].append(related)43 if related.exists() and x.on_delete == models.SET_NULL:44 context['set_null_objects'].append(related)45 except AttributeError:46 pass47 return context48@group_required('user')49def delete_recipe_source(request, pk):50 recipe = get_object_or_404(Recipe, pk=pk, space=request.space)51 if recipe.storage.method == Storage.DROPBOX:52 # TODO central location to handle storage type switches53 Dropbox.delete_file(recipe)54 if recipe.storage.method == Storage.NEXTCLOUD:55 Nextcloud.delete_file(recipe)56 if recipe.storage.method == Storage.LOCAL:57 Local.delete_file(recipe)58 recipe.storage = None59 recipe.file_path = ''60 recipe.file_uid = ''61 recipe.save()62 return HttpResponseRedirect(reverse('edit_recipe', args=[recipe.pk]))63class RecipeImportDelete(GroupRequiredMixin, DeleteView):64 groups_required = ['user']65 template_name = "generic/delete_template.html"66 model = RecipeImport67 success_url = reverse_lazy('list_recipe_import')68 def get_context_data(self, **kwargs):69 context = super(RecipeImportDelete, self).get_context_data(**kwargs)70 context['title'] = _("Import")71 return context72class SyncDelete(GroupRequiredMixin, DeleteView):73 groups_required = ['admin']74 template_name = "generic/delete_template.html"75 model = Sync76 success_url = reverse_lazy('data_sync')77 def get_context_data(self, **kwargs):78 context = super(SyncDelete, self).get_context_data(**kwargs)79 context['title'] = _("Monitor")80 return context81# class KeywordDelete(GroupRequiredMixin, DeleteView):82# groups_required = ['user']83# template_name = "generic/delete_template.html"84# model = Keyword85# success_url = reverse_lazy('list_keyword')86# def get_context_data(self, **kwargs):87# context = super(KeywordDelete, self).get_context_data(**kwargs)88# context['title'] = _("Keyword")89# return context90class StorageDelete(GroupRequiredMixin, DeleteView):91 groups_required = ['admin']92 template_name = "generic/delete_template.html"93 model = Storage94 success_url = reverse_lazy('list_storage')95 def get_context_data(self, **kwargs):96 context = super(StorageDelete, self).get_context_data(**kwargs)97 context['title'] = _("Storage Backend")98 return context99 def post(self, request, *args, **kwargs):100 try:101 return self.delete(request, *args, **kwargs)102 except ProtectedError:103 messages.add_message(104 request,105 messages.WARNING,106 _('Could not delete this storage backend as it is used in at least one monitor.') # noqa: E501107 )108 return HttpResponseRedirect(reverse('list_storage'))109class CommentDelete(OwnerRequiredMixin, DeleteView):110 template_name = "generic/delete_template.html"111 model = Comment112 success_url = reverse_lazy('index')113 def get_context_data(self, **kwargs):114 context = super(CommentDelete, self).get_context_data(**kwargs)115 context['title'] = _("Comment")116 return context117class RecipeBookDelete(OwnerRequiredMixin, DeleteView):118 template_name = "generic/delete_template.html"119 model = RecipeBook120 success_url = reverse_lazy('view_books')121 def get_context_data(self, **kwargs):122 context = super(RecipeBookDelete, self).get_context_data(**kwargs)123 context['title'] = _("Recipe Book")124 return context125class RecipeBookEntryDelete(OwnerRequiredMixin, DeleteView):126 groups_required = ['user']127 template_name = "generic/delete_template.html"128 model = RecipeBookEntry129 success_url = reverse_lazy('view_books')130 def get_context_data(self, **kwargs):131 context = super(RecipeBookEntryDelete, self).get_context_data(**kwargs)132 context['title'] = _("Bookmarks")133 return context134class MealPlanDelete(OwnerRequiredMixin, DeleteView):135 template_name = "generic/delete_template.html"136 model = MealPlan137 success_url = reverse_lazy('view_plan')138 def get_context_data(self, **kwargs):139 context = super(MealPlanDelete, self).get_context_data(**kwargs)140 context['title'] = _("Meal-Plan")141 return context142class InviteLinkDelete(OwnerRequiredMixin, DeleteView):143 template_name = "generic/delete_template.html"144 model = InviteLink145 success_url = reverse_lazy('list_invite_link')146 def get_context_data(self, **kwargs):147 context = super(InviteLinkDelete, self).get_context_data(**kwargs)148 context['title'] = _("Invite Link")149 return context150class UserSpaceDelete(OwnerRequiredMixin, DeleteView):151 template_name = "generic/delete_template.html"152 model = UserSpace153 success_url = reverse_lazy('view_space_overview')154 def get_context_data(self, **kwargs):155 context = super(UserSpaceDelete, self).get_context_data(**kwargs)156 context['title'] = _("Space Membership")157 return context158class SpaceDelete(OwnerRequiredMixin, DeleteView):159 template_name = "generic/delete_template.html"160 model = Space161 success_url = reverse_lazy('view_space_overview')162 def delete(self, request, *args, **kwargs):163 self.object = self.get_object()164 self.object.safe_delete()165 return HttpResponseRedirect(self.get_success_url())166 def get_context_data(self, **kwargs):167 context = super(SpaceDelete, self).get_context_data(**kwargs)168 context['title'] = _("Space")...
test_delete.py
Source:test_delete.py
1"""This unit test suite tests the application's "delete" command."""2import pytest3from app.cli import delete4@pytest.fixture5def delete_template(click_runner):6 """Runs the delete command from the click runner"""7 return click_runner(delete)8@pytest.mark.command9@pytest.mark.delete10def test_clone_without_template(delete_template):11 """An error is thrown when clone is called without a template"""12 response = delete_template([])13 assert response.exit_code == 214 assert "Missing option '--template'" in response.output15@pytest.mark.command16@pytest.mark.delete17def test_clone_displays_success_message(delete_template, mocker):18 """The message sent to the client from remove_template is displayed"""19 message = 'delete display message'20 mocker.patch('app.cli.remove_template', return_value={'msg': message})21 response = delete_template(["-t", "flask-app"])22 assert response.exit_code == 0...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!