class ProblemDataView(TitleMixin, ProblemManagerMixin): template_name = 'problem/data.html' def get_title(self): return _('Editing data for {0}').format(self.object.name) def get_content_title(self): return mark_safe(escape(_('Editing data for %s')) % ( format_html('<a href="{1}">{0}</a>', self.object.name, reverse('problem_detail', args=[self.object.code])))) def get_data_form(self, post=False): return ProblemDataForm(data=self.request.POST if post else None, prefix='problem-data', files=self.request.FILES if post else None, instance=ProblemData.objects.get_or_create(problem=self.object)[0]) def get_case_formset(self, files, post=False): return ProblemCaseFormSet(data=self.request.POST if post else None, prefix='cases', valid_files=files, queryset=ProblemTestCase.objects.filter(dataset_id=self.object.pk).order_by('order')) def get_valid_files(self, data, post=False): try: if post and 'problem-data-zipfile-clear' in self.request.POST: return [] elif post and 'problem-data-zipfile' in self.request.FILES: return ZipFile(self.request.FILES['problem-data-zipfile']).namelist() elif data.zipfile: return ZipFile(data.zipfile.path).namelist() except BadZipfile: return [] return [] def get_context_data(self, **kwargs): context = super(ProblemDataView, self).get_context_data(**kwargs) if 'data_form' not in context: context['data_form'] = self.get_data_form() valid_files = context['valid_files'] = self.get_valid_files(context['data_form'].instance) context['data_form'].zip_valid = valid_files is not False context['cases_formset'] = self.get_case_formset(valid_files) context['valid_files_json'] = mark_safe(json.dumps(context['valid_files'])) context['valid_files'] = set(context['valid_files']) context['all_case_forms'] = chain(context['cases_formset'], [context['cases_formset'].empty_form]) return context def post(self, request, *args, **kwargs): self.object = problem = self.get_object() data_form = self.get_data_form(post=True) valid_files = self.get_valid_files(data_form.instance, post=True) data_form.zip_valid = valid_files is not False cases_formset = self.get_case_formset(valid_files, post=True) if data_form.is_valid() and cases_formset.is_valid(): data = data_form.save() for case in cases_formset.save(commit=False): case.dataset_id = problem.id case.save() for case in cases_formset.deleted_objects: case.delete() ProblemDataCompiler.generate(problem, data, problem.cases.order_by('order'), valid_files) return HttpResponseRedirect(request.get_full_path())
def post(self, request, *args, **kwargs): self.object = problem = self.get_object() data_form = self.get_data_form(post=True) valid_files = self.get_valid_files(data_form.instance, post=True) data_form.zip_valid = valid_files is not False cases_formset = self.get_case_formset(valid_files, post=True) if data_form.is_valid() and cases_formset.is_valid(): data = data_form.save() for case in cases_formset.save(commit=False): case.dataset_id = problem.id case.save() for case in cases_formset.deleted_objects: case.delete() ProblemDataCompiler.generate(problem, data, problem.cases.order_by('order'), valid_files) return HttpResponseRedirect(request.get_full_path()) return self.render_to_response(self.get_context_data(data_form=data_form, cases_formset=cases_formset, valid_files=valid_files))