mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-11-03 23:05:44 +00:00 
			
		
		
		
	Upload test result - now with ability to attach a file!
This commit is contained in:
		@@ -297,7 +297,6 @@ class InvenTreeAPI {
 | 
			
		||||
    var response = await request.send();
 | 
			
		||||
 | 
			
		||||
    return response;
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Perform a POST request
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,9 @@ import 'package:flutter/cupertino.dart';
 | 
			
		||||
import 'package:http/http.dart' as http;
 | 
			
		||||
import 'model.dart';
 | 
			
		||||
 | 
			
		||||
import 'dart:async';
 | 
			
		||||
import 'dart:io';
 | 
			
		||||
 | 
			
		||||
import 'package:InvenTree/api.dart';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -129,12 +132,12 @@ class InvenTreeStockItem extends InvenTreeModel {
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<bool> uploadTestResult(BuildContext context, String testName, bool result, {String value, String notes}) async {
 | 
			
		||||
  Future<bool> uploadTestResult(BuildContext context, String testName, bool result, {String value, String notes, File attachment}) async {
 | 
			
		||||
 | 
			
		||||
    Map<String, dynamic> data = {
 | 
			
		||||
      "stock_item": pk,
 | 
			
		||||
    Map<String, String> data = {
 | 
			
		||||
      "stock_item": pk.toString(),
 | 
			
		||||
      "test": testName,
 | 
			
		||||
      "result": result,
 | 
			
		||||
      "result": result.toString(),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if (value != null && !value.isEmpty) {
 | 
			
		||||
@@ -145,15 +148,23 @@ class InvenTreeStockItem extends InvenTreeModel {
 | 
			
		||||
      data["notes"] = notes;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bool _result = false;
 | 
			
		||||
    /*
 | 
			
		||||
     * Upload is performed in different ways, depending if an attachment is provided.
 | 
			
		||||
     * TODO: Is there a nice way to refactor this one?
 | 
			
		||||
     */
 | 
			
		||||
    if (attachment == null) {
 | 
			
		||||
      var _result = await InvenTreeStockItemTestResult().create(context, data);
 | 
			
		||||
 | 
			
		||||
    await InvenTreeStockItemTestResult().create(context, data).then((InvenTreeModel model) {
 | 
			
		||||
      return (_result != null) && (_result is InvenTreeStockItemTestResult);
 | 
			
		||||
    } else {
 | 
			
		||||
      var url = InvenTreeStockItemTestResult().URL;
 | 
			
		||||
      http.StreamedResponse _uploadResponse = await InvenTreeAPI().uploadFile(url, attachment, fields: data);
 | 
			
		||||
 | 
			
		||||
      _result = model != null && model is InvenTreeStockItemTestResult;
 | 
			
		||||
      // Check that the HTTP status code is HTTP_201_CREATED
 | 
			
		||||
      return _uploadResponse.statusCode == 201;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    return _result;
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int get partId => jsondata['part'] ?? -1;
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:InvenTree/widget/refreshable_state.dart';
 | 
			
		||||
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
 | 
			
		||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
 | 
			
		||||
import 'package:image_picker/image_picker.dart';
 | 
			
		||||
 | 
			
		||||
class StockItemTestResultsWidget extends StatefulWidget {
 | 
			
		||||
 | 
			
		||||
@@ -41,7 +42,7 @@ class _StockItemTestResultDisplayState extends RefreshableState<StockItemTestRes
 | 
			
		||||
 | 
			
		||||
  _StockItemTestResultDisplayState(this.item);
 | 
			
		||||
 | 
			
		||||
  void uploadTestResult(String name, bool result, String value, String notes) async {
 | 
			
		||||
  void uploadTestResult(String name, bool result, String value, String notes, File attachment) async {
 | 
			
		||||
 | 
			
		||||
    item.uploadTestResult(
 | 
			
		||||
      context,
 | 
			
		||||
@@ -49,6 +50,7 @@ class _StockItemTestResultDisplayState extends RefreshableState<StockItemTestRes
 | 
			
		||||
      result,
 | 
			
		||||
      value: value,
 | 
			
		||||
      notes: notes,
 | 
			
		||||
      attachment: attachment
 | 
			
		||||
    ).then((bool success) {
 | 
			
		||||
      if (success) {
 | 
			
		||||
        // TODO - Show a SnackBar here!
 | 
			
		||||
@@ -65,6 +67,7 @@ class _StockItemTestResultDisplayState extends RefreshableState<StockItemTestRes
 | 
			
		||||
    bool _result;
 | 
			
		||||
    String _value;
 | 
			
		||||
    String _notes;
 | 
			
		||||
    File _attachment;
 | 
			
		||||
 | 
			
		||||
    showFormDialog(context, "Add Test Data",
 | 
			
		||||
      key: _addResultKey,
 | 
			
		||||
@@ -81,7 +84,7 @@ class _StockItemTestResultDisplayState extends RefreshableState<StockItemTestRes
 | 
			
		||||
            if (_addResultKey.currentState.validate()) {
 | 
			
		||||
              _addResultKey.currentState.save();
 | 
			
		||||
              Navigator.pop(context);
 | 
			
		||||
              uploadTestResult(_name, _result, _value, _notes);
 | 
			
		||||
              uploadTestResult(_name, _result, _value, _notes, _attachment);
 | 
			
		||||
            }
 | 
			
		||||
          },
 | 
			
		||||
        )
 | 
			
		||||
@@ -111,6 +114,11 @@ class _StockItemTestResultDisplayState extends RefreshableState<StockItemTestRes
 | 
			
		||||
            return null;
 | 
			
		||||
          },
 | 
			
		||||
        ),
 | 
			
		||||
        ImagePickerField(
 | 
			
		||||
          label: "Attach Image",
 | 
			
		||||
          required: attachmentRequired,
 | 
			
		||||
          onSaved: (attachment) => _attachment = attachment,
 | 
			
		||||
        ),
 | 
			
		||||
        StringField(
 | 
			
		||||
          allowEmpty: true,
 | 
			
		||||
          label: "Notes",
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user