Removed custom form save method, remove unused model methods, restored InvenTree CreateAjaxView, improved part related testing

This commit is contained in:
eeintech 2020-11-04 07:44:06 -05:00
parent 853a821497
commit 142cea0cbb
5 changed files with 14 additions and 45 deletions

View File

@ -333,12 +333,6 @@ class AjaxCreateView(AjaxMixin, CreateView):
- Handles form validation via AJAX POST requests - Handles form validation via AJAX POST requests
""" """
def post_save(self, **kwargs):
"""
Hook for doing something with the created object after it is saved
"""
pass
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
""" Creates form with initial data, and renders JSON response """ """ Creates form with initial data, and renders JSON response """
@ -392,7 +386,6 @@ class AjaxCreateView(AjaxMixin, CreateView):
# Save the object to the database # Save the object to the database
self.object = self.save(self.form) self.object = self.save(self.form)
self.post_save()
# Return the PK of the newly-created object # Return the PK of the newly-created object
data['pk'] = self.object.pk data['pk'] = self.object.pk

View File

@ -154,11 +154,6 @@ class CreatePartRelatedForm(HelperForm):
'part_2': _('Related Part'), 'part_2': _('Related Part'),
} }
def save(self):
""" Disable model saving """
return super(CreatePartRelatedForm, self).save(commit=False)
class EditPartAttachmentForm(HelperForm): class EditPartAttachmentForm(HelperForm):
""" Form for editing a PartAttachment object """ """ Form for editing a PartAttachment object """

View File

@ -1908,24 +1908,3 @@ class PartRelated(models.Model):
'and that the relationship is unique') 'and that the relationship is unique')
raise ValidationError(error_message) raise ValidationError(error_message)
def create_relationship(self, part_1, part_2):
''' Create relationship between two parts '''
validate = self.validate(part_1, part_2)
if validate:
# Add relationship
self.part_1 = part_1
self.part_2 = part_2
self.save()
return validate
@classmethod
def create(cls, part_1, part_2):
''' Create PartRelated object and relationship between two parts '''
related_part = cls()
related_part.create_relationship(part_1, part_2)
return related_part

View File

@ -206,14 +206,27 @@ class PartRelatedTests(PartViewTestCase):
def test_valid_create(self): def test_valid_create(self):
""" test creation of a related part """ """ test creation of a related part """
# Test GET view
response = self.client.get(reverse('part-related-create'), {'part': 1}, response = self.client.get(reverse('part-related-create'), {'part': 1},
HTTP_X_REQUESTED_WITH='XMLHttpRequest') HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
# Test POST view with valid form data
response = self.client.post(reverse('part-related-create'), {'part_1': 1, 'part_2': 2}, response = self.client.post(reverse('part-related-create'), {'part_1': 1, 'part_2': 2},
HTTP_X_REQUESTED_WITH='XMLHttpRequest') HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200) self.assertContains(response, '"form_valid": true', status_code=200)
# Try to create the same relationship with part_1 and part_2 pks reversed
response = self.client.post(reverse('part-related-create'), {'part_1': 2, 'part_2': 1},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertContains(response, '"form_valid": false', status_code=200)
# Try to create part related to itself
response = self.client.post(reverse('part-related-create'), {'part_1': 1, 'part_2': 1},
HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertContains(response, '"form_valid": false', status_code=200)
# Check final count
n = PartRelated.objects.all().count() n = PartRelated.objects.all().count()
self.assertEqual(n, 1) self.assertEqual(n, 1)

View File

@ -128,17 +128,6 @@ class PartRelatedCreate(AjaxCreateView):
return form return form
def post_save(self):
""" Save PartRelated model (POST method does not) """
form = self.get_form()
if form.is_valid():
part_1 = form.cleaned_data['part_1']
part_2 = form.cleaned_data['part_2']
PartRelated.create(part_1, part_2)
class PartRelatedDelete(AjaxDeleteView): class PartRelatedDelete(AjaxDeleteView):
""" View for deleting a PartRelated object """ """ View for deleting a PartRelated object """