diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index c1c64dddea..8abf7717f3 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -110,7 +110,47 @@ console.log('dropped'); - //$(this).removeClass('dragover'); + if (files.length > 0) { + var file = files[0]; + + var formData = new FormData(); + + var token = getCookie('csrftoken'); + + formData.append('file', file); + + $.ajax({ + beforeSend: function(xhr, settings) { + xhr.setRequestHeader('X-CSRFToken', token); + }, + url: "{% url 'part-image-upload' part.id %}", + type: 'POST', + data: formData, + processData: false, + contentType: false, + success: function(data, status, xhr) { + //location.reload(); + }, + error: function(xhr, status, error) { + console.log('Error uploading thumbnail: ' + status); + console.log(error); + } + }); + + /* + inventreeUpdate( + "{% url 'part-image-upload' part.id %}", + formData, + { + method: 'POST', + dataType: 'json', + } + ); + */ + + console.log('submitted'); + } + }); $("#show-qr-code").click(function() { diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index 4cda10a0f0..7663318f96 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -47,6 +47,10 @@ part_detail_urls = [ url(r'^qr_code/?', views.PartQRCode.as_view(), name='part-qr'), + # Drag-and-drop thumbnail upload + url(r'^thumbnail-upload/?', views.UploadPartImage.as_view(), name='part-image-upload'), + + # Normal thumbnail with form url(r'^thumbnail/?', views.PartImage.as_view(), name='part-image'), # Any other URLs go to the part detail page diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index c97808888e..6fbd18318e 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -7,6 +7,7 @@ from __future__ import unicode_literals from django.shortcuts import get_object_or_404 +from django.http import JsonResponse from django.urls import reverse_lazy from django.views.generic import DetailView, ListView from django.forms.models import model_to_dict @@ -268,6 +269,31 @@ class PartImage(AjaxUpdateView): } +class UploadPartImage(AjaxView): + """ View for uploading a Part image """ + + model = Part + + def post(self, request, *args, **kwargs): + try: + part = Part.objects.get(pk=kwargs.get('pk')) + except Part.DoesNotExist: + error_dict = { + 'error': 'Part not found', + } + return JsonResponse(error_dict, status=404) + + print("Files:", request.FILES) + uploaded_file = request.FILES['file'] + + response_dict = { + 'success': 'File was uploaded successfully', + } + + return JsonResponse(response_dict, status=200) + + + class PartEdit(AjaxUpdateView): """ View for editing Part object """