mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Refactor ManufacturerPartCreate form
This commit is contained in:
		| @@ -53,29 +53,23 @@ | ||||
| {{ block.super }} | ||||
|  | ||||
|     $("#manufacturer-part-create").click(function () { | ||||
|         launchModalForm( | ||||
|             "{% url 'manufacturer-part-create' %}", | ||||
|             { | ||||
|                 data: { | ||||
|                     manufacturer: {{ company.id }}, | ||||
|  | ||||
|         constructForm('{% url "api-manufacturer-part-list" %}', { | ||||
|             fields: { | ||||
|                 part: {}, | ||||
|                 manufacturer: { | ||||
|                     value: {{ company.pk }}, | ||||
|                 }, | ||||
|                 success: function() { | ||||
|                     $("#part-table").bootstrapTable("refresh"); | ||||
|                 }, | ||||
|                 secondary: [ | ||||
|                     { | ||||
|                         field: 'part', | ||||
|                         label: '{% trans "New Part" %}', | ||||
|                         title: '{% trans "Create new Part" %}', | ||||
|                         url: "{% url 'part-create' %}" | ||||
|                     }, | ||||
|                     { | ||||
|                         field: 'manufacturer', | ||||
|                         label: '{% trans "New Manufacturer" %}', | ||||
|                         title: '{% trans "Create new Manufacturer" %}', | ||||
|                     }, | ||||
|                 ] | ||||
|             }); | ||||
|                 MPN: {}, | ||||
|                 description: {}, | ||||
|                 link: {}, | ||||
|             }, | ||||
|             method: 'POST', | ||||
|             title: '{% trans "Add Manufacturer Part" %}', | ||||
|             onSuccess: function() {  | ||||
|                 $("#part-table").bootstrapTable("refresh"); | ||||
|             } | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     loadManufacturerPartTable( | ||||
|   | ||||
| @@ -194,45 +194,6 @@ class ManufacturerPartViewTests(CompanyViewTestBase): | ||||
|     Tests for the ManufacturerPart views. | ||||
|     """ | ||||
|  | ||||
|     def test_manufacturer_part_create(self): | ||||
|         """ | ||||
|         Test the ManufacturerPartCreate view. | ||||
|         """ | ||||
|  | ||||
|         url = reverse('manufacturer-part-create') | ||||
|  | ||||
|         # First check that we can GET the form | ||||
|         response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest') | ||||
|         self.assertEqual(response.status_code, 200) | ||||
|  | ||||
|         # How many manufaturer parts are already in the database? | ||||
|         n = ManufacturerPart.objects.all().count() | ||||
|  | ||||
|         data = { | ||||
|             'part': 1, | ||||
|             'manufacturer': 6, | ||||
|         } | ||||
|  | ||||
|         # MPN is required! (form should fail) | ||||
|         (response, errors) = self.post(url, data, valid=False) | ||||
|  | ||||
|         self.assertIsNotNone(errors.get('MPN', None)) | ||||
|  | ||||
|         data['MPN'] = 'TEST-ME-123' | ||||
|  | ||||
|         (response, errors) = self.post(url, data, valid=True) | ||||
|  | ||||
|         # Check that the ManufacturerPart was created! | ||||
|         self.assertEqual(n + 1, ManufacturerPart.objects.all().count()) | ||||
|  | ||||
|         # Try to create duplicate ManufacturerPart | ||||
|         (response, errors) = self.post(url, data, valid=False) | ||||
|  | ||||
|         self.assertIsNotNone(errors.get('__all__', None)) | ||||
|  | ||||
|         # Check that the ManufacturerPart count stayed the same | ||||
|         self.assertEqual(n + 1, ManufacturerPart.objects.all().count()) | ||||
|  | ||||
|     def test_supplier_part_create(self): | ||||
|         """ | ||||
|         Test that the SupplierPartCreate view creates Manufacturer Part. | ||||
|   | ||||
| @@ -38,8 +38,7 @@ company_urls = [ | ||||
| ] | ||||
|  | ||||
| manufacturer_part_urls = [ | ||||
|     url(r'^new/?', views.ManufacturerPartCreate.as_view(), name='manufacturer-part-create'), | ||||
|  | ||||
|      | ||||
|     url(r'^(?P<pk>\d+)/', include([ | ||||
|         url(r'^suppliers/', views.ManufacturerPartDetail.as_view(template_name='company/manufacturer_part_suppliers.html'), name='manufacturer-part-suppliers'), | ||||
|         url('^.*$', views.ManufacturerPartDetail.as_view(template_name='company/manufacturer_part_suppliers.html'), name='manufacturer-part-detail'), | ||||
|   | ||||
| @@ -242,74 +242,6 @@ class ManufacturerPartDetail(DetailView): | ||||
|         return ctx | ||||
|  | ||||
|  | ||||
| class ManufacturerPartCreate(AjaxCreateView): | ||||
|     """ Create view for making new ManufacturerPart """ | ||||
|  | ||||
|     model = ManufacturerPart | ||||
|     form_class = EditManufacturerPartForm | ||||
|     ajax_template_name = 'company/manufacturer_part_create.html' | ||||
|     ajax_form_title = _('Create New Manufacturer Part') | ||||
|     context_object_name = 'part' | ||||
|  | ||||
|     def get_context_data(self): | ||||
|         """ | ||||
|         Supply context data to the form | ||||
|         """ | ||||
|  | ||||
|         ctx = super().get_context_data() | ||||
|  | ||||
|         # Add 'part' object | ||||
|         form = self.get_form() | ||||
|  | ||||
|         part = form['part'].value() | ||||
|  | ||||
|         try: | ||||
|             part = Part.objects.get(pk=part) | ||||
|         except (ValueError, Part.DoesNotExist): | ||||
|             part = None | ||||
|  | ||||
|         ctx['part'] = part | ||||
|  | ||||
|         return ctx | ||||
|  | ||||
|     def get_form(self): | ||||
|         """ Create Form instance to create a new ManufacturerPart object. | ||||
|         Hide some fields if they are not appropriate in context | ||||
|         """ | ||||
|         form = super(AjaxCreateView, self).get_form() | ||||
|  | ||||
|         if form.initial.get('part', None): | ||||
|             # Hide the part field | ||||
|             form.fields['part'].widget = HiddenInput() | ||||
|  | ||||
|         return form | ||||
|  | ||||
|     def get_initial(self): | ||||
|         """ Provide initial data for new ManufacturerPart: | ||||
|  | ||||
|         - If 'manufacturer_id' provided, pre-fill manufacturer field | ||||
|         - If 'part_id' provided, pre-fill part field | ||||
|         """ | ||||
|         initials = super(ManufacturerPartCreate, self).get_initial().copy() | ||||
|  | ||||
|         manufacturer_id = self.get_param('manufacturer') | ||||
|         part_id = self.get_param('part') | ||||
|  | ||||
|         if manufacturer_id: | ||||
|             try: | ||||
|                 initials['manufacturer'] = Company.objects.get(pk=manufacturer_id) | ||||
|             except (ValueError, Company.DoesNotExist): | ||||
|                 pass | ||||
|  | ||||
|         if part_id: | ||||
|             try: | ||||
|                 initials['part'] = Part.objects.get(pk=part_id) | ||||
|             except (ValueError, Part.DoesNotExist): | ||||
|                 pass | ||||
|  | ||||
|         return initials | ||||
|  | ||||
|  | ||||
| class SupplierPartDetail(DetailView): | ||||
|     """ Detail view for SupplierPart """ | ||||
|     model = SupplierPart | ||||
|   | ||||
		Reference in New Issue
	
	Block a user