mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Select existing image and upload successfully
This commit is contained in:
parent
534b60d4b8
commit
d4fe83170f
@ -98,20 +98,17 @@ class PartThumbs(generics.ListAPIView):
|
||||
# Get all Parts which have an associated image
|
||||
queryset = Part.objects.all().exclude(image='')
|
||||
|
||||
# Return the most popular parts first
|
||||
data = queryset.values(
|
||||
'image',
|
||||
).annotate(count=Count('image')).order_by('-count')
|
||||
|
||||
print("Parts with img:", queryset.count())
|
||||
|
||||
print(data)
|
||||
|
||||
return Response(data)
|
||||
|
||||
|
||||
class PartDetail(generics.RetrieveUpdateAPIView):
|
||||
""" API endpoint for detail view of a single Part object """
|
||||
|
||||
|
||||
queryset = Part.objects.all()
|
||||
serializer_class = part_serializers.PartSerializer
|
||||
|
||||
|
@ -12,7 +12,6 @@ from django.core.exceptions import ValidationError
|
||||
from django.urls import reverse
|
||||
from django.conf import settings
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
from django.db import models, transaction
|
||||
from django.db.models import Sum
|
||||
from django.db.models import prefetch_related_objects
|
||||
|
@ -218,14 +218,22 @@
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
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({
|
||||
pagination: true,
|
||||
pageSize: 25,
|
||||
url: "{% url 'api-part-thumbs' %}",
|
||||
showHeader: false,
|
||||
clickToSelect: true,
|
||||
singleSelect: true,
|
||||
columns: [
|
||||
{
|
||||
checkbox: true,
|
||||
},
|
||||
{
|
||||
field: '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 }}
|
||||
|
||||
Select from existing images.
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block form %}
|
||||
<form method='post' action='' class='js-modal-form' enctype='multipart/form-data'>
|
||||
{% csrf_token %}
|
||||
{% load crispy_forms_tags %}
|
||||
{% csrf_token %}
|
||||
{% 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>
|
||||
|
@ -14,6 +14,9 @@ from django.urls import reverse, reverse_lazy
|
||||
from django.views.generic import DetailView, ListView, FormView, UpdateView
|
||||
from django.forms.models import model_to_dict
|
||||
from django.forms import HiddenInput, CheckboxInput
|
||||
from django.conf import settings
|
||||
|
||||
import os
|
||||
|
||||
from fuzzywuzzy import fuzz
|
||||
from decimal import Decimal
|
||||
@ -626,10 +629,32 @@ class PartImageSelect(AjaxUpdateView):
|
||||
'image',
|
||||
]
|
||||
|
||||
def get_data(self):
|
||||
return {
|
||||
'success': _('Selected part image')
|
||||
}
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user