Oliver Walters 2019-05-06 21:49:01 +10:00
parent eec0fc34d2
commit c88149b9aa
3 changed files with 71 additions and 1 deletions

View File

@ -110,7 +110,47 @@
console.log('dropped'); 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() { $("#show-qr-code").click(function() {

View File

@ -47,6 +47,10 @@ part_detail_urls = [
url(r'^qr_code/?', views.PartQRCode.as_view(), name='part-qr'), 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'), url(r'^thumbnail/?', views.PartImage.as_view(), name='part-image'),
# Any other URLs go to the part detail page # Any other URLs go to the part detail page

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from django.forms.models import model_to_dict 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): class PartEdit(AjaxUpdateView):
""" View for editing Part object """ """ View for editing Part object """