mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-26 10:57:40 +00:00 
			
		
		
		
	Replace existing django form views with API request
This commit is contained in:
		| @@ -103,6 +103,7 @@ dynamic_javascript_urls = [ | ||||
|     url(r'^order.js', DynamicJsView.as_view(template_name='js/order.js'), name='order.js'), | ||||
|     url(r'^part.js', DynamicJsView.as_view(template_name='js/part.js'), name='part.js'), | ||||
|     url(r'^label.js', DynamicJsView.as_view(template_name='js/label.js'), name='label.js'), | ||||
|     url(r'^report.js', DynamicJsView.as_view(template_name='js/report.js'), name='report.js'), | ||||
|     url(r'^stock.js', DynamicJsView.as_view(template_name='js/stock.js'), name='stock.js'), | ||||
|     url(r'^table_filters.js', DynamicJsView.as_view(template_name='js/table_filters.js'), name='table_filters.js'), | ||||
| ] | ||||
|   | ||||
| @@ -394,12 +394,7 @@ $('#stock-uninstall').click(function() { | ||||
| }); | ||||
|  | ||||
| $("#stock-test-report").click(function() { | ||||
|     launchModalForm( | ||||
|         "{% url 'stock-item-test-report-select' item.id %}", | ||||
|         { | ||||
|             follow: true, | ||||
|         } | ||||
|     ); | ||||
|     printTestReports([{{ item.pk }}]); | ||||
| }); | ||||
|  | ||||
| $("#print-label").click(function() { | ||||
|   | ||||
| @@ -29,8 +29,6 @@ stock_item_detail_urls = [ | ||||
|  | ||||
|     url(r'^add_tracking/', views.StockItemTrackingCreate.as_view(), name='stock-tracking-create'), | ||||
|  | ||||
|     url(r'^test-report-select/', views.StockItemTestReportSelect.as_view(), name='stock-item-test-report-select'), | ||||
|  | ||||
|     url(r'^test/', views.StockItemDetail.as_view(template_name='stock/item_tests.html'), name='stock-item-test-results'), | ||||
|     url(r'^children/', views.StockItemDetail.as_view(template_name='stock/item_childs.html'), name='stock-item-children'), | ||||
|     url(r'^attachments/', views.StockItemDetail.as_view(template_name='stock/item_attachments.html'), name='stock-item-attachments'), | ||||
| @@ -62,8 +60,6 @@ stock_urls = [ | ||||
|  | ||||
|     url(r'^item/uninstall/', views.StockItemUninstall.as_view(), name='stock-item-uninstall'), | ||||
|  | ||||
|     url(r'^item/test-report-download/', views.StockItemTestReportDownload.as_view(), name='stock-item-test-report-download'), | ||||
|  | ||||
|     # URLs for StockItem attachments | ||||
|     url(r'^item/attachment/', include([ | ||||
|         url(r'^new/', views.StockItemAttachmentCreate.as_view(), name='stock-item-attachment-create'), | ||||
|   | ||||
| @@ -30,7 +30,6 @@ from datetime import datetime, timedelta | ||||
|  | ||||
| from company.models import Company, SupplierPart | ||||
| from part.models import Part | ||||
| from report.models import TestReport | ||||
| from .models import StockItem, StockLocation, StockItemTracking, StockItemAttachment, StockItemTestResult | ||||
|  | ||||
| import common.settings | ||||
| @@ -410,92 +409,6 @@ class StockItemTestResultDelete(AjaxDeleteView): | ||||
|     role_required = 'stock.delete' | ||||
|  | ||||
|  | ||||
| class StockItemTestReportSelect(AjaxView): | ||||
|     """ | ||||
|     View for selecting a TestReport template, | ||||
|     and generating a TestReport as a PDF. | ||||
|     """ | ||||
|  | ||||
|     model = StockItem | ||||
|     ajax_form_title = _("Select Test Report Template") | ||||
|     role_required = 'stock.view' | ||||
|  | ||||
|     def get_form(self): | ||||
|  | ||||
|         stock_item = StockItem.objects.get(pk=self.kwargs['pk']) | ||||
|         form = StockForms.TestReportFormatForm(stock_item) | ||||
|  | ||||
|         return form | ||||
|  | ||||
|     def get_initial(self): | ||||
|  | ||||
|         initials = super().get_initial() | ||||
|  | ||||
|         form = self.get_form() | ||||
|         options = form.fields['template'].queryset | ||||
|  | ||||
|         # If only a single template is available, pre-select it | ||||
|         if options.count() == 1: | ||||
|             initials['template'] = options[0] | ||||
|  | ||||
|         return initials | ||||
|  | ||||
|     def post(self, request, *args, **kwargs): | ||||
|  | ||||
|         template_id = request.POST.get('template', None) | ||||
|  | ||||
|         try: | ||||
|             template = TestReport.objects.get(pk=template_id) | ||||
|         except (ValueError, TestReport.DoesNoteExist): | ||||
|             raise ValidationError({'template': _("Select valid template")}) | ||||
|  | ||||
|         stock_item = StockItem.objects.get(pk=self.kwargs['pk']) | ||||
|  | ||||
|         url = reverse('stock-item-test-report-download') | ||||
|  | ||||
|         url += '?stock_item={id}'.format(id=stock_item.pk) | ||||
|         url += '&template={id}'.format(id=template.pk) | ||||
|  | ||||
|         data = { | ||||
|             'form_valid': True, | ||||
|             'url': url, | ||||
|         } | ||||
|  | ||||
|         return self.renderJsonResponse(request, self.get_form(), data=data) | ||||
|  | ||||
|  | ||||
| class StockItemTestReportDownload(AjaxView): | ||||
|     """ | ||||
|     Download a TestReport against a StockItem. | ||||
|  | ||||
|     Requires the following arguments to be passed as URL params: | ||||
|  | ||||
|     stock_item - Valid PK of a StockItem object | ||||
|     template - Valid PK of a TestReport template object | ||||
|  | ||||
|     """ | ||||
|     role_required = 'stock.view' | ||||
|  | ||||
|     def get(self, request, *args, **kwargs): | ||||
|  | ||||
|         template = request.GET.get('template', None) | ||||
|         stock_item = request.GET.get('stock_item', None) | ||||
|  | ||||
|         try: | ||||
|             template = TestReport.objects.get(pk=template) | ||||
|         except (ValueError, TestReport.DoesNotExist): | ||||
|             raise ValidationError({'template': 'Invalid template ID'}) | ||||
|  | ||||
|         try: | ||||
|             stock_item = StockItem.objects.get(pk=stock_item) | ||||
|         except (ValueError, StockItem.DoesNotExist): | ||||
|             raise ValidationError({'stock_item': 'Invalid StockItem ID'}) | ||||
|  | ||||
|         template.stock_item = stock_item | ||||
|  | ||||
|         return template.render(request) | ||||
|  | ||||
|  | ||||
| class StockExportOptions(AjaxView): | ||||
|     """ Form for selecting StockExport options """ | ||||
|  | ||||
|   | ||||
| @@ -121,6 +121,7 @@ InvenTree | ||||
| <script type='text/javascript' src="{% url 'part.js' %}"></script> | ||||
| <script type='text/javascript' src="{% url 'modals.js' %}"></script> | ||||
| <script type='text/javascript' src="{% url 'label.js' %}"></script> | ||||
| <script type='text/javascript' src="{% url 'report.js' %}"></script> | ||||
| <script type='text/javascript' src="{% url 'stock.js' %}"></script> | ||||
| <script type='text/javascript' src="{% url 'build.js' %}"></script> | ||||
| <script type='text/javascript' src="{% url 'order.js' %}"></script> | ||||
|   | ||||
							
								
								
									
										124
									
								
								InvenTree/templates/js/report.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								InvenTree/templates/js/report.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,124 @@ | ||||
| {% load i18n %} | ||||
|  | ||||
|  | ||||
| function selectTestReport(reports, items, options={}) { | ||||
|     /** | ||||
|      * Present the user with the available test reports, | ||||
|      * and allow them to select which test report to print. | ||||
|      *  | ||||
|      * The intent is that the available report templates have been requested | ||||
|      * (via AJAX) from the server. | ||||
|      */ | ||||
|  | ||||
|     var modal = options.modal || '#modal-form'; | ||||
|  | ||||
|     var report_list = makeOptionsList( | ||||
|         reports, | ||||
|         function(item) { | ||||
|             var text = item.name; | ||||
|  | ||||
|             if (item.description) { | ||||
|                 text += ` - ${item.description}`; | ||||
|             } | ||||
|  | ||||
|             return text; | ||||
|         }, | ||||
|         function(item) { | ||||
|             return item.pk; | ||||
|         } | ||||
|     ); | ||||
|  | ||||
|     // Construct form | ||||
|     var html = ` | ||||
|  | ||||
|     <form method='post' action='' class='js-modal-form' enctype='multipart/form-data'> | ||||
|         <div class='form-group'> | ||||
|             <label class='control-label requiredField' for='id_report'> | ||||
|             {% trans "Select Label" %} | ||||
|             </label> | ||||
|             <div class='controls'> | ||||
|                 <select id='id_report' class='select form-control name='report'> | ||||
|                     ${report_list} | ||||
|                 </select> | ||||
|             </div> | ||||
|         </div> | ||||
|     </form>`; | ||||
|  | ||||
|     openModal({ | ||||
|         modal: modal, | ||||
|     }); | ||||
|      | ||||
|     modalEnable(modal, true); | ||||
|     modalSetTitle(modal, '{% trans "Select Test Report Template" %}'); | ||||
|     modalSetContent(modal, html); | ||||
|  | ||||
|     attachSelect(modal); | ||||
|  | ||||
|     modalSubmit(modal, function() { | ||||
|  | ||||
|         var label = $(modal).find('#id_report'); | ||||
|  | ||||
|         var pk = label.val(); | ||||
|  | ||||
|         closeModal(modal); | ||||
|  | ||||
|         if (options.success) { | ||||
|             options.success(pk); | ||||
|         } | ||||
|     }); | ||||
|  | ||||
| } | ||||
|  | ||||
|  | ||||
| function printTestReports(items, options={}) { | ||||
|     /** | ||||
|      * Print test reports for the provided stock item(s) | ||||
|      */ | ||||
|  | ||||
|     if (items.length == 0) { | ||||
|         showAlertDialog( | ||||
|             '{% trans "Select Stock Items" %}', | ||||
|             '{% trans "Stock item(s) must be selected before printing reports" %}' | ||||
|         ); | ||||
|  | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     // Request available labels from the server | ||||
|     inventreeGet( | ||||
|         '{% url "api-stockitem-testreport-list" %}', | ||||
|         { | ||||
|             enabled: true, | ||||
|             items: items, | ||||
|         }, | ||||
|         { | ||||
|             success: function(response) { | ||||
|                 if (response.length == 0) { | ||||
|                     showAlertDialog( | ||||
|                         '{% trans "No Reports Found" %}', | ||||
|                         '{% trans "No report templates found which match selected stock item(s)" %}', | ||||
|                     ); | ||||
|  | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 // Select report template to print | ||||
|                 selectTestReport( | ||||
|                     response, | ||||
|                     items, | ||||
|                     { | ||||
|                         success: function(pk) { | ||||
|                             var href = `/api/report/test/${pk}/print/?`; | ||||
|  | ||||
|                             items.forEach(function(item) { | ||||
|                                 href += `items[]=${item}&`; | ||||
|                             }); | ||||
|  | ||||
|                             window.location.href = href; | ||||
|                         } | ||||
|                     } | ||||
|                 ); | ||||
|             } | ||||
|         } | ||||
|     ); | ||||
| } | ||||
		Reference in New Issue
	
	Block a user