Best Python code snippet using Kiwi_python
views.py
Source:views.py
...50 context_data = {51 "plan_id": plan_id, # used for UI conditionals52 "test_cases": test_cases,53 "form": form,54 "disabled_cases": get_disabled_test_cases_count(test_cases),55 "is_cloning": is_cloning,56 }57 return render(request, self.template_name, context_data)58 def post(self, request):59 form = NewRunForm(data=request.POST)60 form.populate(request.POST.get("plan"))61 if form.is_valid():62 test_run = form.save()63 # copy all of the selected properties into the test run64 for prop in EnvironmentProperty.objects.filter(65 environment__in=form.cleaned_data["environment"]66 ):67 test_run.property_set.create(name=prop.name, value=prop.value)68 loop = 169 for case in form.cleaned_data["case"]:70 try:71 tcp = TestCasePlan.objects.get(72 plan=form.cleaned_data["plan"], case=case73 )74 sortkey = tcp.sortkey75 except ObjectDoesNotExist:76 sortkey = loop * 1077 test_run.create_execution(78 case=case,79 assignee=form.cleaned_data["default_tester"],80 sortkey=sortkey,81 matrix_type=form.cleaned_data["matrix_type"],82 )83 loop += 184 return HttpResponseRedirect(85 reverse(86 "testruns-get",87 args=[88 test_run.pk,89 ],90 )91 )92 test_cases = (93 TestCase.objects.filter(pk__in=request.POST.getlist("case"))94 .select_related("author", "case_status", "category", "priority")95 .order_by("pk")96 )97 context_data = {98 "plan_id": request.POST.get("plan"),99 "test_cases": test_cases,100 "form": form,101 "disabled_cases": get_disabled_test_cases_count(test_cases),102 }103 return render(request, self.template_name, context_data)104@method_decorator(permission_required("testruns.view_testrun"), name="dispatch")105class SearchTestRunView(TemplateView):106 template_name = "testruns/search.html"107 def get_context_data(self, **kwargs):108 form = SearchRunForm(self.request.GET)109 form.populate(product_id=self.request.GET.get("product"))110 return {111 "form": form,112 }113@method_decorator(114 object_permission_required(115 "testruns.view_testrun", (TestRun, "pk", "pk"), accept_global_perms=True116 ),117 name="dispatch",118)119class GetTestRunView(DetailView):120 template_name = "testruns/get.html"121 http_method_names = ["get"]122 model = TestRun123 def get_context_data(self, **kwargs):124 context = super().get_context_data(**kwargs)125 context["execution_statuses"] = TestExecutionStatus.objects.order_by(126 "-weight", "name"127 )128 context["confirmed_statuses"] = TestCaseStatus.objects.filter(is_confirmed=True)129 context["link_form"] = LinkReferenceForm()130 context["bug_trackers"] = BugSystem.objects.all()131 context["comment_form"] = SimpleCommentForm()132 context["OBJECT_MENU_ITEMS"] = [133 (134 "...",135 [136 (137 _("Edit"),138 reverse("testruns-edit", args=[self.object.pk]),139 ),140 (141 _("Clone"),142 reverse("testruns-clone", args=[self.object.pk]),143 ),144 (145 _("History"),146 f"/admin/testruns/testrun/{self.object.pk}/history/",147 ),148 ("-", "-"),149 (150 _("Object permissions"),151 reverse(152 "admin:testruns_testrun_permissions",153 args=[self.object.pk],154 ),155 ),156 ("-", "-"),157 (158 _("Delete"),159 reverse(160 "admin:testruns_testrun_delete",161 args=[self.object.pk],162 ),163 ),164 ],165 )166 ]167 return context168@method_decorator(169 object_permission_required(170 "testruns.change_testrun", (TestRun, "pk", "pk"), accept_global_perms=True171 ),172 name="dispatch",173)174class EditTestRunView(UpdateView):175 model = TestRun176 template_name = "testruns/mutable.html"177 form_class = NewRunForm178 def get_form(self, form_class=None):179 form = super().get_form(form_class)180 form.populate(self.object.plan_id)181 return form182 def get_context_data(self, **kwargs):183 context = super().get_context_data(**kwargs)184 context["plan_id"] = self.object.plan185 return context186 def get_initial(self):187 return {188 "manager": self.object.manager,189 "default_tester": self.object.default_tester,190 }191@method_decorator(permission_required("testruns.add_testrun"), name="dispatch")192class CloneTestRunView(NewTestRunView):193 # note: post is handled directly by NewTestRunView194 # b/c <form action> points to testruns-new URL195 http_method_names = ["get"]196 def get(self, request, pk): # pylint: disable=arguments-differ197 test_run = get_object_or_404(TestRun, pk=pk)198 request.GET._mutable = True # pylint: disable=protected-access199 request.GET["p"] = test_run.plan_id200 request.GET.setlist(201 "c", test_run.executions.all().values_list("case", flat=True)202 )203 form_initial = {204 "summary": _("Clone of ") + test_run.summary,205 "notes": test_run.notes,206 "manager": test_run.manager,207 "build": test_run.build_id,208 "default_tester": test_run.default_tester,209 "plan": test_run.plan_id,210 }211 return super().get(request, form_initial=form_initial, is_cloning=True)212def get_disabled_test_cases_count(test_cases):213 return test_cases.filter(case_status__is_confirmed=False).count()214@method_decorator(215 object_permission_required(216 "testruns.view_environment", (Environment, "pk", "pk"), accept_global_perms=True217 ),218 name="dispatch",219)220class GetEnvironment(DetailView):221 template_name = "testruns/environment.html"222 http_method_names = ["get"]223 model = Environment224 def get_context_data(self, **kwargs):225 context = super().get_context_data(**kwargs)226 context["OBJECT_MENU_ITEMS"] = [...
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!!