def create_mathematical_object(request): if request.method == 'POST': mathematical_object_form = forms.MathematicalObjectForm(request.POST) if mathematical_object_form.is_valid(): new_instance = mathematical_object_form.save() return redirect('front:mathematical_object', pk=new_instance.pk) else: mathematical_object_form = forms.MathematicalObjectForm() return render(request, "front/mathematical_object_creation.html", {'form': mathematical_object_form})
def create_mathematical_object(self, with_function=False, with_name=False, with_latex=None, description=None): representation = 'cmathobj' + get_random_characters() if with_latex: representation = with_latex type = 'S' data = { 'latex': representation, 'type': type, } if with_function: function = create_function(self) data.update({'functions': [function.id]}) if with_name: name = create_name(self) data.update({'names': [name.id]}) if description: data.update({'description': description}) mathematical_object_form = forms.MathematicalObjectForm(data=data) self.assertTrue(mathematical_object_form.is_valid()) response = self.client.post(reverse('front:mathematical_object_creation'), data=mathematical_object_form.data, format='json') self.assertTrue(status.HTTP_302_FOUND) id = re.findall(r'\d+', response.url)[-1] return models.MathematicalObject.objects.get(pk=id)
def __create_test_data(self, with_description=None): utils.log_as(self, utils.UserType.STAFF) func = utils.create_function(self) name = utils.create_name(self) mathematical_object_1 = utils.create_mathematical_object(self) representation = 'createtestdata' object_type = 'S' data = { 'latex': representation, 'type': object_type, 'functions': [func.id], 'names': [name.id], 'related': [mathematical_object_1.id], } if with_description: data.update({'description': with_description}) mathematical_object_form = forms.MathematicalObjectForm(data=data) self.assertTrue(mathematical_object_form.is_valid()) self.client.post(reverse('front:mathematical_object_creation'), mathematical_object_form.data, format='json') mathematical_object_2 = models.MathematicalObject.objects.exclude(pk=mathematical_object_1.id).first() return [mathematical_object_1, mathematical_object_2], func, name
def test_create_full_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) func = utils.create_function(self) name = utils.create_name(self) mathematical_object_1 = utils.create_mathematical_object(self) representation = 'testcreatefullmathematicalobject' object_type = 'S' description = 'test_create_full_mathematical_object' mathematical_object_form = forms.MathematicalObjectForm(data={ 'latex': representation, 'type': object_type, 'functions': [func.id], 'names': [name.id], 'related': [mathematical_object_1.id], 'description': description }) self.assertTrue(mathematical_object_form.is_valid()) response = self.client.post(reverse('front:mathematical_object_creation'), mathematical_object_form.data, format='json') self.assertTrue(status.HTTP_302_FOUND) self.assertEqual(models.MathematicalObject.objects.count(), 2) mathematical_object_2 = models.MathematicalObject.objects.exclude(pk=mathematical_object_1.id)[:1].get() self.assertEqual(mathematical_object_2.get_content(), description)
def edit_mathematical_object(request, pk): mathematical_object_instance = get_object_or_404(models.MathematicalObject, pk=pk) if request.method == 'POST': mathematical_object_form = forms.MathematicalObjectForm( request.POST, initial={ 'description': mathematical_object_instance.get_content() }, instance=mathematical_object_instance) if mathematical_object_form.is_valid(): instance = mathematical_object_form.save() return redirect('front:mathematical_object', pk=instance.pk) mathematical_object_form = forms.MathematicalObjectForm( initial={'description': mathematical_object_instance.get_content()}, instance=mathematical_object_instance) return render(request, "front/mathematical_object_creation.html", {'form': mathematical_object_form})
def test_create_mathematical_object_with_invalid_latex(self): utils.log_as(self, utils.UserType.STAFF) representation = '^' mathematical_object_form = forms.MathematicalObjectForm(data={ 'latex': representation, 'type': 'S' }) self.assertFalse(mathematical_object_form.is_valid()) response = self.client.post(reverse('front:mathematical_object_creation'), data=mathematical_object_form.data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(models.MathematicalObject.objects.count(), 0)
def test_create_small_mathematical_object(self): utils.log_as(self, utils.UserType.STAFF) representation = 'testcreatesmallmathematicalobject' type2 = 'S' mathematical_object_form = forms.MathematicalObjectForm(data={ 'latex': representation, 'type': type2 }) self.assertTrue(mathematical_object_form.is_valid()) response = self.client.post(reverse('front:mathematical_object_creation'), data=mathematical_object_form.data, format='json') self.assertTrue(status.HTTP_302_FOUND) self.assertEqual(models.MathematicalObject.objects.count(), 1) created_object = models.MathematicalObject.objects.all()[:1].get() self.assertRedirects(response, reverse('front:mathematical_object', kwargs={'pk': created_object.pk})) self.assertEqual(created_object.latex, representation) self.assertEqual(created_object.type, type2)
def test_change_latex(self): utils.log_as(self, utils.UserType.STAFF) objects, func, name = self.__create_test_data() mathematical_object_2 = objects[1] new_latex = 'testadddescription' data = { 'latex': new_latex, 'type': mathematical_object_2.type, 'related': [m.pk for m in mathematical_object_2.related.all()], 'functions': [f.pk for f in mathematical_object_2.functions.all()], 'names': [n.pk for n in mathematical_object_2.names.all()] } mathematical_object_form = forms.MathematicalObjectForm(data) self.assertTrue(mathematical_object_form.is_valid()) self.client.post(reverse('front:mathematical_object_edition', kwargs={'pk': mathematical_object_2.pk}), mathematical_object_form.data, format='json') mathematical_object_2.refresh_from_db() self.assertEqual(mathematical_object_2.latex, new_latex) self.assertFalse(mathematical_object_2.get_content())
def test_create_partial_mathematical_object_with_related(self): utils.log_as(self, utils.UserType.STAFF) mathematical_object_1 = utils.create_mathematical_object(self, with_name=True, with_function=True) representation = 'testcreatefullmathematicalobject' type2 = 'S' mathematical_object_form = forms.MathematicalObjectForm(data={ 'latex': representation, 'type': type2, 'related': [mathematical_object_1.id] }) self.assertTrue(mathematical_object_form.is_valid()) response = self.client.post(reverse('front:mathematical_object_creation'), mathematical_object_form.data, format='json') self.assertTrue(status.HTTP_302_FOUND) self.assertEqual(models.MathematicalObject.objects.count(), 2) mathematical_object_2 = models.MathematicalObject.objects.exclude(pk=mathematical_object_1.id)[:1].get() self.assertRedirects(response, reverse('front:mathematical_object', kwargs={'pk': mathematical_object_2.pk})) self.assertEqual(mathematical_object_2.latex, representation) self.assertEqual(mathematical_object_2.type, type2) result = mathematical_object_1.related.get(pk=mathematical_object_2.id) self.assertEqual(result.latex, representation)
def test_create_partial_mathematical_object_with_name(self): utils.log_as(self, utils.UserType.STAFF) name = utils.create_name(self) representation = 'testcreatepartialmathematicalobjectwithname' type2 = 'S' mathematical_object_form = forms.MathematicalObjectForm(data={ 'latex': representation, 'type': type2, 'names': [name.id] }) self.assertTrue(mathematical_object_form.is_valid()) response = self.client.post(reverse('front:mathematical_object_creation'), mathematical_object_form.data, format='json') self.assertTrue(status.HTTP_302_FOUND) self.assertEqual(models.MathematicalObject.objects.count(), 1) created_object = models.MathematicalObject.objects.all()[:1].get() self.assertRedirects(response, reverse('front:mathematical_object', kwargs={'pk': created_object.pk})) self.assertEqual(created_object.latex, representation) self.assertEqual(created_object.type, type2) self.assertEqual(created_object.names.count(), 1) retrieved_name = models.Name.objects.get(id__in=created_object.names.all()) self.assertEqual(name.name, retrieved_name.name) self.assertEqual(created_object.functions.count(), 0)