Merge pull request #828 from SchrodingersGat/api-stuff

Api stuff
This commit is contained in:
Oliver 2020-05-24 20:12:28 +10:00 committed by GitHub
commit 8537dc2a85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 89 deletions

View File

@ -1033,6 +1033,9 @@ class Part(MPTTModel):
# Return the tests which are required by this part
return self.getTestTemplates(required=True)
def requiredTestCount(self):
return self.getRequiredTests().count()
@property
def attachment_count(self):
""" Count the number of attachments for this part.

View File

@ -105,9 +105,11 @@ class PartBriefSerializer(InvenTreeModelSerializer):
'thumbnail',
'active',
'assembly',
'is_template',
'purchaseable',
'salable',
'stock',
'trackable',
'virtual',
]

View File

@ -80,21 +80,10 @@ class StockDetail(generics.RetrieveUpdateDestroyAPIView):
def get_serializer(self, *args, **kwargs):
try:
kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', False))
except AttributeError:
pass
try:
kwargs['location_detail'] = str2bool(self.request.query_params.get('location_detail', False))
except AttributeError:
pass
try:
kwargs['supplier_part_detail'] = str2bool(self.request.query_params.get('supplier_detail', False))
except AttributeError:
pass
kwargs['part_detail'] = True
kwargs['location_detail'] = True
kwargs['supplier_part_detail'] = True
kwargs['test_detail'] = True
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
@ -698,17 +687,14 @@ class StockItemTestResultList(generics.ListCreateAPIView):
'value',
]
ordering = 'date'
def get_serializer(self, *args, **kwargs):
try:
kwargs['user_detail'] = str2bool(self.request.query_params.get('user_detail', False))
except:
pass
try:
kwargs['attachment_detail'] = str2bool(self.request.query_params.get('attachment_detail', False))
except:
pass
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
@ -724,23 +710,6 @@ class StockItemTestResultList(generics.ListCreateAPIView):
# Capture the user information
test_result = serializer.save()
test_result.user = self.request.user
# Check if a file has been attached to the request
attachment_file = self.request.FILES.get('attachment', None)
if attachment_file:
# Create a new attachment associated with the stock item
attachment = StockItemAttachment(
attachment=attachment_file,
stock_item=test_result.stock_item,
user=test_result.user
)
attachment.save()
# Link the attachment back to the test result
test_result.attachment = attachment
test_result.save()

View File

@ -0,0 +1,19 @@
# Generated by Django 3.0.5 on 2020-05-23 01:21
from django.db import migrations, models
import stock.models
class Migration(migrations.Migration):
dependencies = [
('stock', '0041_stockitemtestresult_notes'),
]
operations = [
migrations.AlterField(
model_name='stockitemtestresult',
name='attachment',
field=models.FileField(blank=True, help_text='Test result attachment', null=True, upload_to=stock.models.rename_stock_item_test_result_attachment, verbose_name='Attachment'),
),
]

View File

@ -1007,6 +1007,10 @@ class StockItem(MPTTModel):
'failed': failed,
}
@property
def required_test_count(self):
return self.part.getRequiredTests().count()
def hasRequiredTests(self):
return self.part.getRequiredTests().count() > 0
@ -1090,6 +1094,11 @@ class StockItemTracking(models.Model):
# file = models.FileField()
def rename_stock_item_test_result_attachment(instance, filename):
return os.path.join('stock_files', str(instance.stock_item.pk), os.path.basename(filename))
class StockItemTestResult(models.Model):
"""
A StockItemTestResult records results of custom tests against individual StockItem objects.
@ -1119,13 +1128,11 @@ class StockItemTestResult(models.Model):
super().clean()
"""
# If this test result corresponds to a template, check the requirements of the template
key = helpers.generateTestKey(self.test)
key = self.key
templates = self.stock_item.part.getTestTemplates()
TODO: Re-introduce this at a later stage, it is buggy when uplaoding an attachment via the API
for template in templates:
if key == template.key:
@ -1142,17 +1149,10 @@ class StockItemTestResult(models.Model):
})
break
"""
# If an attachment is linked to this result, the attachment must also point to the item
try:
if self.attachment:
if not self.attachment.stock_item == self.stock_item:
raise ValidationError({
'attachment': _("Test result attachment must be linked to the same StockItem"),
})
except (StockItem.DoesNotExist, StockItemAttachment.DoesNotExist):
pass
@property
def key(self):
return helpers.generateTestKey(self.test)
stock_item = models.ForeignKey(
StockItem,
@ -1178,10 +1178,9 @@ class StockItemTestResult(models.Model):
help_text=_('Test output value')
)
attachment = models.ForeignKey(
StockItemAttachment,
on_delete=models.SET_NULL,
blank=True, null=True,
attachment = models.FileField(
null=True, blank=True,
upload_to=rename_stock_item_test_result_attachment,
verbose_name=_('Attachment'),
help_text=_('Test result attachment'),
)

View File

@ -108,11 +108,14 @@ class StockItemSerializer(InvenTreeModelSerializer):
quantity = serializers.FloatField()
allocated = serializers.FloatField()
required_tests = serializers.IntegerField(source='required_test_count', read_only=True)
def __init__(self, *args, **kwargs):
part_detail = kwargs.pop('part_detail', False)
location_detail = kwargs.pop('location_detail', False)
supplier_part_detail = kwargs.pop('supplier_part_detail', False)
test_detail = kwargs.pop('test_detail', False)
super(StockItemSerializer, self).__init__(*args, **kwargs)
@ -125,6 +128,9 @@ class StockItemSerializer(InvenTreeModelSerializer):
if supplier_part_detail is not True:
self.fields.pop('supplier_part_detail')
if test_detail is not True:
self.fields.pop('required_tests')
class Meta:
model = StockItem
fields = [
@ -141,6 +147,7 @@ class StockItemSerializer(InvenTreeModelSerializer):
'part_detail',
'pk',
'quantity',
'required_tests',
'sales_order',
'serial',
'supplier_part',
@ -222,31 +229,28 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer):
""" Serializer for the StockItemTestResult model """
user_detail = UserSerializerBrief(source='user', read_only=True)
attachment_detail = StockItemAttachmentSerializer(source='attachment', read_only=True)
key = serializers.CharField(read_only=True)
def __init__(self, *args, **kwargs):
user_detail = kwargs.pop('user_detail', False)
attachment_detail = kwargs.pop('attachment_detail', False)
super().__init__(*args, **kwargs)
if user_detail is not True:
self.fields.pop('user_detail')
if attachment_detail is not True:
self.fields.pop('attachment_detail')
class Meta:
model = StockItemTestResult
fields = [
'pk',
'stock_item',
'key',
'test',
'result',
'value',
'attachment',
'attachment_detail',
'notes',
'user',
'user_detail',

View File

@ -13,10 +13,13 @@
<div id='button-toolbar'>
<div class='button-toolbar container-fluid' style="float: right;">
<div class='btn-group' role='group'>
<button type='button' class='btn btn-success' id='add-test-result'>{% trans "Add Test Data" %}</button>
{% if user.is_staff %}
<button type='button' class='btn btn-danger' id='delete-test-results'>{% trans "Delete Test Data" %}</button>
{% endif %}
<button type='button' class='btn btn-success' id='add-test-result'>{% trans "Add Test Data" %}</button>
{% if item.part.has_test_report_templates %}
<button type='button' class='btn btn-default' id='test-report'>{% trans "Test Report" %} <span class='fas fa-tasks'></span></button>
{% endif %}
</div>
<div class='filter-list' id='filter-list-stocktests'>
<!-- Empty div -->
@ -43,6 +46,17 @@ function reloadTable() {
//$("#test-result-table").bootstrapTable("refresh");
}
{% if item.part.has_test_report_templates %}
$("#test-report").click(function() {
launchModalForm(
"{% url 'stock-item-test-report-select' item.id %}",
{
follow: true,
}
);
});
{% endif %}
{% if user.is_staff %}
$("#delete-test-results").click(function() {
launchModalForm(

View File

@ -292,17 +292,6 @@ class StockItemTestResultCreate(AjaxCreateView):
form = super().get_form()
form.fields['stock_item'].widget = HiddenInput()
# Extract the StockItem object
item_id = form['stock_item'].value()
# Limit the options for the file attachments
try:
stock_item = StockItem.objects.get(pk=item_id)
form.fields['attachment'].queryset = stock_item.attachments.all()
except (ValueError, StockItem.DoesNotExist):
# Hide the attachments field
form.fields['attachment'].widget = HiddenInput()
return form
@ -321,8 +310,6 @@ class StockItemTestResultEdit(AjaxUpdateView):
form.fields['stock_item'].widget = HiddenInput()
form.fields['attachment'].queryset = self.object.stock_item.attachments.all()
return form

View File

@ -32,17 +32,6 @@ function noResultBadge() {
return `<span class='label label-blue float-right'>{% trans "NO RESULT" %}</span>`;
}
function testKey(test_name) {
// Convert test name to a unique key without any illegal chars
test_name = test_name.trim().toLowerCase();
test_name = test_name.replace(' ', '');
test_name = test_name.replace(/[^0-9a-z]/gi, '');
return test_name;
}
function loadStockTestResultsTable(table, options) {
/*
* Load StockItemTestResult table
@ -56,8 +45,8 @@ function loadStockTestResultsTable(table, options) {
html += `<span class='badge'>${row.user_detail.username}</span>`;
}
if (row.attachment_detail) {
html += `<a href='${row.attachment_detail.attachment}'><span class='fas fa-file-alt label-right'></span></a>`;
if (row.attachment) {
html += `<a href='${row.attachment}'><span class='fas fa-file-alt label-right'></span></a>`;
}
return html;
@ -177,14 +166,14 @@ function loadStockTestResultsTable(table, options) {
var match = false;
var override = false;
var key = testKey(item.test);
var key = item.key;
// Try to associate this result with a test row
tableData.forEach(function(row, index) {
// The result matches the test template row
if (key == testKey(row.test_name)) {
if (key == row.key) {
// Force the names to be the same!
item.test_name = row.test_name;