2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-19 13:35:40 +00:00

Add the ability to extract image URL information when drag-and-dropping image URL from a browser window

- Can't do anything with it yet...
- Code is almost there but leaving for now
This commit is contained in:
Oliver Walters
2019-05-07 00:04:35 +10:00
parent 3c7238f29c
commit fe4acd48a7
5 changed files with 113 additions and 47 deletions

View File

@ -106,37 +106,29 @@
var transfer = event.originalEvent.dataTransfer;
var formData = new FormData();
if (isFileTransfer(transfer)) {
var file = transfer.files[0];
inventreeFileUpload(
"{% url 'part-image-upload' part.id %}",
file,
{},
{
success: function(data, status, xhr) {
location.reload();
},
error: function(xhr, status, error) {
showAlertDialog('Error uploading image', renderErrorMessage(xhr));
}
}
);
formData.append('image_file', transfer.files[0]);
} else if (isOnlineTransfer(transfer)) {
getImageUrlFromTransfer(transfer);
/*
for (var i = 0; i < 12; i++) {
transfer.items[i].getAsString(function(text) {
console.log('item ' + i + ' - ' + text);
});
}
*/
formData.append('image_url', getImageUrlFromTransfer(transfer));
} else {
console.log('Unknown transfer');
return;
}
inventreeFormDataUpload(
"{% url 'part-image-upload' part.id %}",
formData,
{
success: function(data, status, xhr) {
location.reload();
},
error: function(xhr, status, error) {
showAlertDialog('Error uploading image', renderErrorMessage(xhr));
}
}
);
});
$("#show-qr-code").click(function() {

View File

@ -7,6 +7,9 @@ from __future__ import unicode_literals
from django.shortcuts import get_object_or_404
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from django.http import JsonResponse
from django.urls import reverse_lazy
from django.views.generic import DetailView, ListView
@ -30,7 +33,7 @@ from .forms import EditSupplierPartForm
from InvenTree.views import AjaxView, AjaxCreateView, AjaxUpdateView, AjaxDeleteView
from InvenTree.views import QRCodeView
from InvenTree.helpers import DownloadFile, str2bool, TestIfImage
from InvenTree.helpers import DownloadFile, str2bool, TestIfImage, TestIfImageURL
class PartIndex(ListView):
@ -270,7 +273,20 @@ class PartImage(AjaxUpdateView):
class UploadPartImage(AjaxView):
""" View for uploading a Part image """
""" View for uploading a Part image via AJAX request.
e.g. via a "drag and drop" event.
There are two ways to upload a file:
1. Attach an image file as request.FILES['image_file']
- Image is validated saved
- Part object is saved
2. Attach an iamge URL as request.POST['image_url']
NOT YET IMPLEMENTED
- Image URL is valiated
- Image is downloaded and validated
- Part object is saved
"""
model = Part
@ -285,18 +301,63 @@ class UploadPartImage(AjaxView):
response['error'] = 'Part not found'
return JsonResponse(error_dict, status=404)
uploaded_file = request.FILES['file']
# Direct image upload
if 'image_file' in request.FILES:
image = request.FILES['image_file']
if TestIfImage(uploaded_file):
part.image = uploaded_file
part.clean()
part.save()
if TestIfImage(image):
part.image = image
part.clean()
part.save()
response['success'] = 'File was uploaded successfully'
else:
response['error'] = 'Not a valid image file'
status = 400
response['success'] = 'File was uploaded successfully'
status = 200
else:
response['error'] = 'Not a valid image file'
status = 400
return JsonResponse(response, status=status)
elif 'image_url' in request.POST:
image_url = request.POST['image_url']
validator = URLValidator()
try:
validator(image_url)
except ValidationError:
response['error'] = 'Invalid image URL'
response['url'] = image_url
return JsonResponse(response, status=400)
# Test the the URL at least looks like an image
if not TestIfImageURL(image_url):
response['error'] = 'Invalid image URL'
return JsonResponse(response, status=400)
response['error'] = 'Cannot download cross-site images (yet)'
response['url'] = image_url
response['apology'] = 'deepest'
return JsonResponse(response, status=400)
# TODO - Attempt to download the image file here
"""
head = requests.head(url, stream=True, headers={'User-agent': 'Mozilla/5.0'})
if head.headers['Content-Length'] < SOME_MAX_LENGTH:
image = requests.get(url, stream=True, headers={'User-agent': 'Mozilla/5.0'})
file = io.BytesIO(image.raw.read())
- Save the file?
"""
# Default response
return JsonResponse(response, status=status)