2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-13 02:35:27 +00:00

Modern attachments (#505)

* Minimum API version is now 100

* Remove old API features

- Anything below API v100 no longer supported

* Reefactor attachment widget to support modern attachment API

* Filter and display attachments correctly

* Refactor
This commit is contained in:
Oliver
2024-06-11 23:16:01 +10:00
committed by GitHub
parent c3eb1a5fca
commit e837394495
18 changed files with 151 additions and 167 deletions

View File

@ -187,7 +187,8 @@ class InvenTreeAPI {
} }
// Minimum required API version for server // Minimum required API version for server
static const _minApiVersion = 20; // 2023-03-04
static const _minApiVersion = 100;
bool _strictHttps = false; bool _strictHttps = false;
@ -282,30 +283,6 @@ class InvenTreeAPI {
String get serverVersion => (serverInfo["version"] ?? "") as String; String get serverVersion => (serverInfo["version"] ?? "") as String;
int get apiVersion => (serverInfo["apiVersion"] ?? 1) as int; int get apiVersion => (serverInfo["apiVersion"] ?? 1) as int;
// Plugins enabled at API v34 and above
bool get pluginsEnabled => apiVersion >= 34 && (serverInfo["plugins_enabled"] ?? false) as bool;
// API endpoint for receiving purchase order line items was introduced in v12
bool get supportsPoReceive => apiVersion >= 12;
// Notification support requires API v25 or newer
bool get supportsNotifications => isConnected() && apiVersion >= 25;
// Return True if the API supports 'settings' (requires API v46)
bool get supportsSettings => isConnected() && apiVersion >= 46;
// Part parameter support requires API v56 or newer
bool get supportsPartParameters => isConnected() && apiVersion >= 56;
// Supports 'modern' barcode API (v80 or newer)
bool get supportModernBarcodes => isConnected() && apiVersion >= 80;
// Structural categories requires API v83 or newer
bool get supportsStructuralCategories => isConnected() && apiVersion >= 83;
// Company attachments require API v95 or newer
bool get supportCompanyAttachments => isConnected() && apiVersion >= 95;
// Consolidated search request API v102 or newer // Consolidated search request API v102 or newer
bool get supportsConsolidatedSearch => isConnected() && apiVersion >= 102; bool get supportsConsolidatedSearch => isConnected() && apiVersion >= 102;
@ -346,7 +323,11 @@ class InvenTreeAPI {
bool get supportsCompanyActiveStatus => isConnected() && apiVersion >= 189; bool get supportsCompanyActiveStatus => isConnected() && apiVersion >= 189;
// Does the server support the "modern" (consolidated) label printing API? // Does the server support the "modern" (consolidated) label printing API?
bool get supportsModernLabelPrinting => isConnected() && apiVersion >= 198; bool get supportsModernLabelPrinting => isConnected() && apiVersion >= 201;
// Does the server support the "modern" (consolidated) attachment API?
// Ref: https://github.com/inventree/InvenTree/pull/7420
bool get supportsModernAttachments => isConnected() && apiVersion >= 207;
// Cached list of plugins (refreshed when we connect to the server) // Cached list of plugins (refreshed when we connect to the server)
List<InvenTreePlugin> _plugins = []; List<InvenTreePlugin> _plugins = [];
@ -1517,7 +1498,6 @@ class InvenTreeAPI {
Map<String, InvenTreeUserSetting> _userSettings = {}; Map<String, InvenTreeUserSetting> _userSettings = {};
Future<String> getGlobalSetting(String key) async { Future<String> getGlobalSetting(String key) async {
if (!supportsSettings) return "";
InvenTreeGlobalSetting? setting = _globalSettings[key]; InvenTreeGlobalSetting? setting = _globalSettings[key];
@ -1543,7 +1523,6 @@ class InvenTreeAPI {
} }
Future<String> getUserSetting(String key) async { Future<String> getUserSetting(String key) async {
if (!supportsSettings) return "";
InvenTreeUserSetting? setting = _userSettings[key]; InvenTreeUserSetting? setting = _userSettings[key];
@ -1687,10 +1666,6 @@ class InvenTreeAPI {
return; return;
} }
if (!supportsNotifications) {
return;
}
InvenTreeNotification().count(filters: {"read": "false"}).then((int n) { InvenTreeNotification().count(filters: {"read": "false"}).then((int n) {
notification_counter = n; notification_counter = n;
}); });

View File

@ -341,12 +341,7 @@ class UniqueBarcodeHandler extends BarcodeHandler {
} else { } else {
String barcode; String barcode;
if (InvenTreeAPI().supportModernBarcodes) { barcode = (data["barcode_data"] ?? "") as String;
barcode = (data["barcode_data"] ?? "") as String;
} else {
// Legacy barcode API
barcode = (data["hash"] ?? data["barcode_hash"] ?? "") as String;
}
if (barcode.isEmpty) { if (barcode.isEmpty) {
barcodeFailureTone(); barcodeFailureTone();

View File

@ -106,7 +106,10 @@ class InvenTreeCompanyAttachment extends InvenTreeAttachment {
String get REFERENCE_FIELD => "company"; String get REFERENCE_FIELD => "company";
@override @override
String get URL => "company/attachment/"; String get REF_MODEL_TYPE => "company";
@override
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "company/attachment/";
@override @override
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeCompanyAttachment.fromJson(json); InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeCompanyAttachment.fromJson(json);

View File

@ -938,9 +938,17 @@ class InvenTreeAttachment extends InvenTreeModel {
InvenTreeAttachment.fromJson(Map<String, dynamic> json) : super.fromJson(json); InvenTreeAttachment.fromJson(Map<String, dynamic> json) : super.fromJson(json);
@override
String get URL => "attachment/";
// Override this reference field for any subclasses // Override this reference field for any subclasses
// Note: This is used for the *legacy* attachment API
String get REFERENCE_FIELD => ""; String get REFERENCE_FIELD => "";
// Override this reference field for any subclasses
// Note: This is used for the *modern* attachment API
String get REF_MODEL_TYPE => "";
String get attachment => getString("attachment"); String get attachment => getString("attachment");
// Return the filename of the attachment // Return the filename of the attachment
@ -989,15 +997,39 @@ class InvenTreeAttachment extends InvenTreeModel {
} }
} }
Future<bool> uploadAttachment(File attachment, int parentId, {String comment = "", Map<String, String> fields = const {}}) async { // Return a count of how many attachments exist against the specified model ID
Future<int> countAttachments(int modelId) {
Map<String, String> filters = {};
if (InvenTreeAPI().supportsModernAttachments) {
filters["model_type"] = REF_MODEL_TYPE;
filters["model_id"] = modelId.toString();
} else {
filters[REFERENCE_FIELD] = modelId.toString();
}
return count(filters: filters);
}
Future<bool> uploadAttachment(File attachment, String modelType, int modelId, {String comment = "", Map<String, String> fields = const {}}) async {
// Ensure that the correct reference field is set // Ensure that the correct reference field is set
Map<String, String> data = Map<String, String>.from(fields); Map<String, String> data = Map<String, String>.from(fields);
data[REFERENCE_FIELD] = parentId.toString(); String url = URL;
if (InvenTreeAPI().supportsModernAttachments) {
// All attachments are stored in a consolidated table
url = "attachment/";
data["model_id"] = modelId.toString();
data["model_type"] = modelType;
} else {
data[REFERENCE_FIELD] = modelId.toString();
}
final APIResponse response = await InvenTreeAPI().uploadFile( final APIResponse response = await InvenTreeAPI().uploadFile(
URL, url,
attachment, attachment,
method: "POST", method: "POST",
name: "attachment", name: "attachment",

View File

@ -36,10 +36,6 @@ class InvenTreePartCategory extends InvenTreeModel {
"structural": {}, "structural": {},
}; };
if (!api.supportsStructuralCategories) {
fields.remove("structural");
}
return fields; return fields;
} }
@ -475,7 +471,10 @@ class InvenTreePartAttachment extends InvenTreeAttachment {
String get REFERENCE_FIELD => "part"; String get REFERENCE_FIELD => "part";
@override @override
String get URL => "part/attachment/"; String get REF_MODEL_TYPE => "part";
@override
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "part/attachment/";
@override @override
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreePartAttachment.fromJson(json); InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreePartAttachment.fromJson(json);

View File

@ -237,7 +237,10 @@ class InvenTreePurchaseOrderAttachment extends InvenTreeAttachment {
String get REFERENCE_FIELD => "order"; String get REFERENCE_FIELD => "order";
@override @override
String get URL => "order/po/attachment/"; String get REF_MODEL_TYPE => "purchaseorder";
@override
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "order/po/attachment/";
@override @override
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreePurchaseOrderAttachment.fromJson(json); InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreePurchaseOrderAttachment.fromJson(json);

View File

@ -280,6 +280,9 @@ class InvenTreeSalesOrderAttachment extends InvenTreeAttachment {
String get REFERENCE_FIELD => "order"; String get REFERENCE_FIELD => "order";
@override @override
String get URL => "order/so/attachment/"; String get REF_MODEL_TYPE => "salesorder";
@override
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "order/so/attachment/";
} }

View File

@ -635,11 +635,13 @@ class InvenTreeStockItemAttachment extends InvenTreeAttachment {
String get REFERENCE_FIELD => "stock_item"; String get REFERENCE_FIELD => "stock_item";
@override @override
String get URL => "stock/attachment/"; String get REF_MODEL_TYPE => "stockitem";
@override
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "stock/attachment/";
@override @override
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeStockItemAttachment.fromJson(json); InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeStockItemAttachment.fromJson(json);
} }
@ -669,10 +671,6 @@ class InvenTreeStockLocation extends InvenTreeModel {
"structural": {}, "structural": {},
}; };
if (!api.supportsStructuralCategories) {
fields.remove("structural");
}
return fields; return fields;
} }

View File

@ -121,15 +121,13 @@ class InvenTreeAboutWidget extends StatelessWidget {
); );
// Display extra tile if the server supports plugins // Display extra tile if the server supports plugins
if (InvenTreeAPI().pluginsEnabled) { tiles.add(
tiles.add( ListTile(
ListTile( title: Text(L10().pluginSupport),
title: Text(L10().pluginSupport), subtitle: Text(L10().pluginSupportDetail),
subtitle: Text(L10().pluginSupportDetail), leading: FaIcon(FontAwesomeIcons.plug),
leading: FaIcon(FontAwesomeIcons.plug), )
) );
);
}
} else { } else {
tiles.add( tiles.add(

View File

@ -6,6 +6,7 @@ import "package:font_awesome_flutter/font_awesome_flutter.dart";
import "package:one_context/one_context.dart"; import "package:one_context/one_context.dart";
import "package:url_launcher/url_launcher.dart"; import "package:url_launcher/url_launcher.dart";
import "package:inventree/api.dart";
import "package:inventree/l10.dart"; import "package:inventree/l10.dart";
import "package:inventree/app_colors.dart"; import "package:inventree/app_colors.dart";
@ -25,10 +26,10 @@ import "package:inventree/widget/refreshable_state.dart";
*/ */
class AttachmentWidget extends StatefulWidget { class AttachmentWidget extends StatefulWidget {
const AttachmentWidget(this.attachment, this.referenceId, this.hasUploadPermission) : super(); const AttachmentWidget(this.attachmentClass, this.modelId, this.hasUploadPermission) : super();
final InvenTreeAttachment attachment; final InvenTreeAttachment attachmentClass;
final int referenceId; final int modelId;
final bool hasUploadPermission; final bool hasUploadPermission;
@override @override
@ -74,7 +75,9 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
if (file == null) return; if (file == null) return;
showLoadingOverlay(context); showLoadingOverlay(context);
final bool result = await widget.attachment.uploadAttachment(file, widget.referenceId);
final bool result = await widget.attachmentClass.uploadAttachment(file, widget.attachmentClass.MODEL_TYPE, widget.modelId);
hideLoadingOverlay(); hideLoadingOverlay();
if (result) { if (result) {
@ -131,14 +134,24 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
@override @override
Future<void> request(BuildContext context) async { Future<void> request(BuildContext context) async {
await widget.attachment.list( Map<String, String> filters = {};
filters: {
widget.attachment.REFERENCE_FIELD: widget.referenceId.toString() if (InvenTreeAPI().supportsModernAttachments) {
} filters["model_type"] = widget.attachmentClass.MODEL_TYPE;
filters["model_id"] = widget.modelId.toString();
} else {
filters[widget.attachmentClass.REFERENCE_FIELD] = widget.modelId.toString();
}
await widget.attachmentClass.list(
filters: filters
).then((var results) { ).then((var results) {
attachments.clear(); attachments.clear();
print("Found ${results.length} results:");
for (var result in results) { for (var result in results) {
print(result.toString());
if (result is InvenTreeAttachment) { if (result is InvenTreeAttachment) {
attachments.add(result); attachments.add(result);
} }

View File

@ -188,19 +188,14 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
} }
}); });
if (api.supportCompanyAttachments) { InvenTreeCompanyAttachment().countAttachments(widget.company.pk)
InvenTreeCompanyAttachment().count( .then((value) {
filters: { if (mounted) {
"company": widget.company.pk.toString() setState(() {
} attachmentCount = value;
).then((value) { });
if (mounted) { }
setState(() { });
attachmentCount = value;
});
}
});
}
} }
Future <void> editCompany(BuildContext context) async { Future <void> editCompany(BuildContext context) async {
@ -397,25 +392,24 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
)); ));
} }
if (api.supportCompanyAttachments) {
tiles.add(ListTile( tiles.add(ListTile(
title: Text(L10().attachments), title: Text(L10().attachments),
leading: FaIcon(FontAwesomeIcons.fileLines, color: COLOR_ACTION), leading: FaIcon(FontAwesomeIcons.fileLines, color: COLOR_ACTION),
trailing: attachmentCount > 0 ? Text(attachmentCount.toString()) : null, trailing: attachmentCount > 0 ? Text(attachmentCount.toString()) : null,
onTap: () { onTap: () {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => AttachmentWidget( builder: (context) => AttachmentWidget(
InvenTreeCompanyAttachment(), InvenTreeCompanyAttachment(),
widget.company.pk, widget.company.pk,
InvenTreeCompany().canEdit InvenTreeCompany().canEdit
)
) )
); )
} );
)); }
} ));
return tiles; return tiles;
} }

View File

@ -174,18 +174,16 @@ class InvenTreeDrawer extends StatelessWidget {
tiles.add(Divider()); tiles.add(Divider());
} }
if (InvenTreeAPI().supportsNotifications) { int notification_count = InvenTreeAPI().notification_counter;
int notification_count = InvenTreeAPI().notification_counter;
tiles.add( tiles.add(
ListTile( ListTile(
leading: FaIcon(FontAwesomeIcons.bell, color: COLOR_ACTION), leading: FaIcon(FontAwesomeIcons.bell, color: COLOR_ACTION),
trailing: notification_count > 0 ? Text(notification_count.toString()) : null, trailing: notification_count > 0 ? Text(notification_count.toString()) : null,
title: Text(L10().notifications), title: Text(L10().notifications),
onTap: _notifications, onTap: _notifications,
) )
); );
}
tiles.add( tiles.add(
ListTile( ListTile(

View File

@ -228,9 +228,7 @@ class _PurchaseOrderDetailState extends RefreshableState<PurchaseOrderDetailWidg
} }
} }
InvenTreePurchaseOrderAttachment().count(filters: { InvenTreePurchaseOrderAttachment().countAttachments(widget.order.pk).then((int value) {
"order": widget.order.pk.toString()
}).then((int value) {
if (mounted) { if (mounted) {
setState(() { setState(() {
attachmentCount = value; attachmentCount = value;

View File

@ -175,9 +175,7 @@ class _SalesOrderDetailState extends RefreshableState<SalesOrderDetailWidget> {
supportsProjectCodes = api.supportsProjectCodes && await api.getGlobalBooleanSetting("PROJECT_CODES_ENABLED"); supportsProjectCodes = api.supportsProjectCodes && await api.getGlobalBooleanSetting("PROJECT_CODES_ENABLED");
InvenTreeSalesOrderAttachment().count(filters: { InvenTreeSalesOrderAttachment().countAttachments(widget.order.pk).then((int value) {
"order": widget.order.pk.toString()
}).then((int value) {
if (mounted) { if (mounted) {
setState(() { setState(() {
attachmentCount = value; attachmentCount = value;

View File

@ -90,15 +90,13 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
List<SpeedDialChild> actions = []; List<SpeedDialChild> actions = [];
if (InvenTreePart().canEdit) { if (InvenTreePart().canEdit) {
if (api.supportModernBarcodes) { actions.add(
actions.add( customBarcodeAction(
customBarcodeAction( context, this,
context, this, widget.part.customBarcode, "part",
widget.part.customBarcode, "part", widget.part.pk
widget.part.pk )
) );
);
}
} }
return actions; return actions;
@ -184,18 +182,10 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
}); });
// Request the number of parameters for this part // Request the number of parameters for this part
if (api.supportsPartParameters) { showParameters = await InvenTreeSettingsManager().getValue(INV_PART_SHOW_PARAMETERS, true) as bool;
showParameters = await InvenTreeSettingsManager().getValue(INV_PART_SHOW_PARAMETERS, true) as bool;
} else {
showParameters = false;
}
// Request the number of attachments // Request the number of attachments
InvenTreePartAttachment().count( InvenTreePartAttachment().countAttachments(part.pk).then((int value) {
filters: {
"part": part.pk.toString(),
}
).then((int value) {
if (mounted) { if (mounted) {
setState(() { setState(() {
attachmentCount = value; attachmentCount = value;

View File

@ -140,15 +140,13 @@ class _LocationDisplayState extends RefreshableState<LocationDisplayWidget> {
} }
// Assign or un-assign barcodes // Assign or un-assign barcodes
if (api.supportModernBarcodes) { actions.add(
actions.add( customBarcodeAction(
customBarcodeAction( context, this,
context, this, location!.customBarcode, "stocklocation",
location!.customBarcode, "stocklocation", location!.pk
location!.pk )
) );
);
}
} }
return actions; return actions;

View File

@ -183,15 +183,13 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
) )
); );
if (api.supportModernBarcodes) { actions.add(
actions.add( customBarcodeAction(
customBarcodeAction( context, this,
context, this, widget.item.customBarcode,
widget.item.customBarcode, "stockitem", widget.item.pk
"stockitem", widget.item.pk )
) );
);
}
} }
return actions; return actions;
@ -246,12 +244,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
} }
// Request the number of attachments // Request the number of attachments
InvenTreeStockItemAttachment().count( InvenTreeStockItemAttachment().countAttachments(widget.item.pk).then((int value) {
filters: {
"stock_item": widget.item.pk.toString()
}
).then((int value) {
if (mounted) { if (mounted) {
setState(() { setState(() {
attachmentCount = value; attachmentCount = value;

View File

@ -110,10 +110,6 @@ void main() {
// Check supported functions // Check supported functions
assert(api.apiVersion >= 50); assert(api.apiVersion >= 50);
assert(api.supportsSettings);
assert(api.supportsNotifications);
assert(api.supportsPoReceive);
assert(api.serverInstance.isNotEmpty); assert(api.serverInstance.isNotEmpty);
assert(api.serverVersion.isNotEmpty); assert(api.serverVersion.isNotEmpty);