mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
Select existing image and upload successfully
This commit is contained in:
parent
534b60d4b8
commit
d4fe83170f
@ -98,14 +98,11 @@ class PartThumbs(generics.ListAPIView):
|
|||||||
# Get all Parts which have an associated image
|
# Get all Parts which have an associated image
|
||||||
queryset = Part.objects.all().exclude(image='')
|
queryset = Part.objects.all().exclude(image='')
|
||||||
|
|
||||||
|
# Return the most popular parts first
|
||||||
data = queryset.values(
|
data = queryset.values(
|
||||||
'image',
|
'image',
|
||||||
).annotate(count=Count('image')).order_by('-count')
|
).annotate(count=Count('image')).order_by('-count')
|
||||||
|
|
||||||
print("Parts with img:", queryset.count())
|
|
||||||
|
|
||||||
print(data)
|
|
||||||
|
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@ from django.core.exceptions import ValidationError
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from django.core.files.base import ContentFile
|
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
from django.db.models import prefetch_related_objects
|
from django.db.models import prefetch_related_objects
|
||||||
|
@ -218,14 +218,22 @@
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function onSelectImage(response) {
|
function onSelectImage(response) {
|
||||||
|
// Callback when the image-selection modal form is displayed
|
||||||
|
// Populate the form with image data (requested via AJAX)
|
||||||
|
|
||||||
$("#modal-form").find("#image-select-table").bootstrapTable({
|
$("#modal-form").find("#image-select-table").bootstrapTable({
|
||||||
pagination: true,
|
pagination: true,
|
||||||
pageSize: 25,
|
pageSize: 25,
|
||||||
url: "{% url 'api-part-thumbs' %}",
|
url: "{% url 'api-part-thumbs' %}",
|
||||||
showHeader: false,
|
showHeader: false,
|
||||||
|
clickToSelect: true,
|
||||||
|
singleSelect: true,
|
||||||
columns: [
|
columns: [
|
||||||
|
{
|
||||||
|
checkbox: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
field: 'image',
|
field: 'image',
|
||||||
title: 'Image',
|
title: 'Image',
|
||||||
@ -234,6 +242,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
onCheck: function(row, element) {
|
||||||
|
|
||||||
|
// Update the selected image in the form
|
||||||
|
var ipt = $("#modal-form").find("#image-input");
|
||||||
|
ipt.val(row.image);
|
||||||
|
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,14 +4,15 @@
|
|||||||
|
|
||||||
{{ block.super }}
|
{{ block.super }}
|
||||||
|
|
||||||
Select from existing images.
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block form %}
|
{% block form %}
|
||||||
<form method='post' action='' class='js-modal-form' enctype='multipart/form-data'>
|
<form method='post' action='' class='js-modal-form' enctype='multipart/form-data'>
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{% load crispy_forms_tags %}
|
{% load crispy_forms_tags %}
|
||||||
|
|
||||||
|
<input id='image-input' name='image' type='hidden' value="{{ part.image }}">
|
||||||
|
|
||||||
<table id='image-select-table' class='table table-striped table-condensed table-img-grid'>
|
<table id='image-select-table' class='table table-striped table-condensed table-img-grid'>
|
||||||
</table>
|
</table>
|
||||||
|
@ -14,6 +14,9 @@ from django.urls import reverse, reverse_lazy
|
|||||||
from django.views.generic import DetailView, ListView, FormView, UpdateView
|
from django.views.generic import DetailView, ListView, FormView, UpdateView
|
||||||
from django.forms.models import model_to_dict
|
from django.forms.models import model_to_dict
|
||||||
from django.forms import HiddenInput, CheckboxInput
|
from django.forms import HiddenInput, CheckboxInput
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
from fuzzywuzzy import fuzz
|
from fuzzywuzzy import fuzz
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
@ -626,10 +629,32 @@ class PartImageSelect(AjaxUpdateView):
|
|||||||
'image',
|
'image',
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_data(self):
|
def post(self, request, *args, **kwargs):
|
||||||
return {
|
|
||||||
'success': _('Selected part image')
|
part = self.get_object()
|
||||||
}
|
form = self.get_form()
|
||||||
|
|
||||||
|
img = request.POST.get('image', '')
|
||||||
|
|
||||||
|
img = os.path.basename(img)
|
||||||
|
|
||||||
|
data = {}
|
||||||
|
|
||||||
|
if img:
|
||||||
|
img_path = os.path.join(settings.MEDIA_ROOT, 'part_images', img)
|
||||||
|
|
||||||
|
# Ensure that the image already exists
|
||||||
|
if os.path.exists(img_path):
|
||||||
|
|
||||||
|
part.image = os.path.join('part_images', img)
|
||||||
|
part.save()
|
||||||
|
|
||||||
|
data['success'] = _('Updated part image')
|
||||||
|
|
||||||
|
if 'success' not in data:
|
||||||
|
data['error'] = _('Part image not found')
|
||||||
|
|
||||||
|
return self.renderJsonResponse(request, form, data)
|
||||||
|
|
||||||
|
|
||||||
class PartEdit(AjaxUpdateView):
|
class PartEdit(AjaxUpdateView):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user