def post(self, request, *args, **kwargs): """ Handles POST requests, instantiating a form instance with the passed POST variables and then checked for validity. """ form = self.get_form() if form.is_valid(): self.form_valid(form) self.object.save() # Automatically create Realism question for new category question = Question(category=self.object, type='realism', question='Is this a possible combination of variables?', options={'Possible': 1, 'Outlier': 2, 'Impossible': 0}) question.save() # Automatically create Algorithm for new question algo_name = 'Algo for %s question with ID: %d' % (question.type, question.pk) algorithm = Algorithm(question=question, name=algo_name) algorithm.save() if 'submit-form-and-add-another' in request.POST: return HttpResponseRedirect( reverse('dashboard:category_create', args=[get_contract_pk(self.request)])) return HttpResponseRedirect(self.get_success_url()) else: self.object = None return self.form_invalid(form)
def post(self, request, *args, **kwargs): """ Handles POST requests, instantiating a form instance with the passed POST variables and then checked for validity. """ form = CategoryForm(request.POST, instance=self.get_object()) if form.is_valid(): self.form_valid(form) # process categorical vars categorical_vars_ids = self.object.get_categorical_vars().values_list('id', flat=True) new_categorical_vars_ids = [int(x) for x in request.POST.getlist('categorical_variables')] deleted_categorical_vars_ids = list(set(categorical_vars_ids) - set(new_categorical_vars_ids)) added_categorical_vars_ids = list(set(new_categorical_vars_ids) - set(categorical_vars_ids)) for id in deleted_categorical_vars_ids: CategoricalVariable.objects.get(pk=id).delete() for id in added_categorical_vars_ids: categorical_var = CategoricalVariable.objects.get(pk=id) categorical_var.pk = None categorical_var.id = None categorical_var.category = self.object categorical_var.save() # process numerical vars numerical_vars_ids = self.object.get_numerical_vars().values_list('id', flat=True) new_numerical_vars_ids = [int(x) for x in request.POST.getlist('numerical_variables')] deleted_numerical_vars_ids = list(set(numerical_vars_ids) - set(new_numerical_vars_ids)) added_numerical_vars_ids = list(set(new_numerical_vars_ids) - set(numerical_vars_ids)) for id in deleted_numerical_vars_ids: NumericalVariable.objects.get(pk=id).delete() for id in added_numerical_vars_ids: numerical_var = NumericalVariable.objects.get(pk=id) numerical_var.pk = None numerical_var.id = None numerical_var.category = self.object numerical_var.save() # process questions questions_ids = self.object.get_usual_questions().values_list('id', flat=True) new_questions_ids = [int(x) for x in request.POST.getlist('questions')] deleted_questions_ids = list(set(questions_ids) - set(new_questions_ids)) added_questions_ids = list(set(new_questions_ids) - set(questions_ids)) for id in deleted_questions_ids: Question.objects.get(pk=id).delete() for id in added_questions_ids: question = Question.objects.get(pk=id) question.pk = None question.category = self.object question.save() # Automatically create Algorithm for new question algo_name = 'Algo for %s question with ID: %d' % (question.type, question.pk) algorithm = Algorithm(question=question, name=algo_name) algorithm.save() if 'submit-form-and-add-another' in request.POST: return HttpResponseRedirect( reverse('dashboard:category_create', args=[get_contract_pk(self.request)])) return HttpResponseRedirect(self.get_success_url()) else: self.object = None return self.form_invalid(form)