mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-19 21:45:39 +00:00
Enable DnD for Company image
- Can actually use the existing form to do partial upload! - Generecise the drag-and-drop upload function - Remove some (now unnecessary) Python View code
This commit is contained in:
@ -13,17 +13,14 @@
|
||||
<div class="col-sm-6">
|
||||
<div class="media">
|
||||
<div class="media-left">
|
||||
<form id='upload-photo' method='post' action="{% url 'part-image-upload' part.id %}">
|
||||
<div class='dropzone' id='part-thumb'>
|
||||
<div class='dropzone' id='part-thumb'>
|
||||
<img class="part-thumb"
|
||||
{% if part.image %}
|
||||
src="{{ part.image.url }}"
|
||||
{% else %}
|
||||
src="{% static 'img/blank_image.png' %}"
|
||||
{% endif %}/>
|
||||
</div>
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="media-body">
|
||||
<h4>
|
||||
@ -102,34 +99,16 @@
|
||||
{% block js_ready %}
|
||||
{{ block.super }}
|
||||
|
||||
$('#part-thumb').on('drop', function(event) {
|
||||
|
||||
var transfer = event.originalEvent.dataTransfer;
|
||||
|
||||
var formData = new FormData();
|
||||
|
||||
if (isFileTransfer(transfer)) {
|
||||
formData.append('image_file', transfer.files[0]);
|
||||
} else if (isOnlineTransfer(transfer)) {
|
||||
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));
|
||||
}
|
||||
enableDragAndDrop(
|
||||
'#part-thumb',
|
||||
"{% url 'part-image' part.id %}",
|
||||
{
|
||||
label: 'image',
|
||||
success: function(data, status, xhr) {
|
||||
location.reload();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
$("#show-qr-code").click(function() {
|
||||
launchModalForm(
|
||||
|
@ -47,9 +47,6 @@ 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'),
|
||||
|
||||
|
@ -272,96 +272,6 @@ class PartImage(AjaxUpdateView):
|
||||
}
|
||||
|
||||
|
||||
class UploadPartImage(AjaxView):
|
||||
""" 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
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
response = {}
|
||||
status = 200
|
||||
|
||||
try:
|
||||
part = Part.objects.get(pk=kwargs.get('pk'))
|
||||
except Part.DoesNotExist:
|
||||
response['error'] = 'Part not found'
|
||||
return JsonResponse(response, status=404)
|
||||
|
||||
# Direct image upload
|
||||
if 'image_file' in request.FILES:
|
||||
image = request.FILES['image_file']
|
||||
|
||||
if TestIfImage(image):
|
||||
part.image = image
|
||||
part.clean()
|
||||
part.save()
|
||||
|
||||
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 = str(request.POST['image_url']).split('?')[0]
|
||||
|
||||
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'
|
||||
response['url'] = 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)
|
||||
|
||||
|
||||
class PartEdit(AjaxUpdateView):
|
||||
""" View for editing Part object """
|
||||
|
||||
|
Reference in New Issue
Block a user