From d4fe83170f12e9d17f14d3844e2f24e807fd0586 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 10 Feb 2020 23:48:45 +1100 Subject: [PATCH] Select existing image and upload successfully --- InvenTree/part/api.py | 7 ++-- InvenTree/part/models.py | 1 - InvenTree/part/templates/part/part_base.html | 15 +++++++++ .../part/templates/part/select_image.html | 7 ++-- InvenTree/part/views.py | 33 ++++++++++++++++--- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 424f03fd0b..b55e027152 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -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 diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 7d33a782de..4109946dce 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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 diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index 13d82c573b..cecf4244ea 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -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); + + } }); } diff --git a/InvenTree/part/templates/part/select_image.html b/InvenTree/part/templates/part/select_image.html index 851688bf05..5e7667824d 100644 --- a/InvenTree/part/templates/part/select_image.html +++ b/InvenTree/part/templates/part/select_image.html @@ -4,14 +4,15 @@ {{ block.super }} -Select from existing images. {% endblock %} {% block form %}
-{% csrf_token %} -{% load crispy_forms_tags %} + {% csrf_token %} + {% load crispy_forms_tags %} + +
diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index dbc57ece59..24717d55bb 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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):