mirror of
https://github.com/inventree/inventree-app.git
synced 2025-06-11 17:55:28 +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:
39
lib/api.dart
39
lib/api.dart
@ -187,7 +187,8 @@ class InvenTreeAPI {
|
||||
}
|
||||
|
||||
// Minimum required API version for server
|
||||
static const _minApiVersion = 20;
|
||||
// 2023-03-04
|
||||
static const _minApiVersion = 100;
|
||||
|
||||
bool _strictHttps = false;
|
||||
|
||||
@ -282,30 +283,6 @@ class InvenTreeAPI {
|
||||
String get serverVersion => (serverInfo["version"] ?? "") as String;
|
||||
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
|
||||
bool get supportsConsolidatedSearch => isConnected() && apiVersion >= 102;
|
||||
|
||||
@ -346,7 +323,11 @@ class InvenTreeAPI {
|
||||
bool get supportsCompanyActiveStatus => isConnected() && apiVersion >= 189;
|
||||
|
||||
// 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)
|
||||
List<InvenTreePlugin> _plugins = [];
|
||||
@ -1517,7 +1498,6 @@ class InvenTreeAPI {
|
||||
Map<String, InvenTreeUserSetting> _userSettings = {};
|
||||
|
||||
Future<String> getGlobalSetting(String key) async {
|
||||
if (!supportsSettings) return "";
|
||||
|
||||
InvenTreeGlobalSetting? setting = _globalSettings[key];
|
||||
|
||||
@ -1543,7 +1523,6 @@ class InvenTreeAPI {
|
||||
}
|
||||
|
||||
Future<String> getUserSetting(String key) async {
|
||||
if (!supportsSettings) return "";
|
||||
|
||||
InvenTreeUserSetting? setting = _userSettings[key];
|
||||
|
||||
@ -1687,10 +1666,6 @@ class InvenTreeAPI {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!supportsNotifications) {
|
||||
return;
|
||||
}
|
||||
|
||||
InvenTreeNotification().count(filters: {"read": "false"}).then((int n) {
|
||||
notification_counter = n;
|
||||
});
|
||||
|
@ -341,12 +341,7 @@ class UniqueBarcodeHandler extends BarcodeHandler {
|
||||
} else {
|
||||
String barcode;
|
||||
|
||||
if (InvenTreeAPI().supportModernBarcodes) {
|
||||
barcode = (data["barcode_data"] ?? "") as String;
|
||||
} else {
|
||||
// Legacy barcode API
|
||||
barcode = (data["hash"] ?? data["barcode_hash"] ?? "") as String;
|
||||
}
|
||||
barcode = (data["barcode_data"] ?? "") as String;
|
||||
|
||||
if (barcode.isEmpty) {
|
||||
barcodeFailureTone();
|
||||
|
@ -106,7 +106,10 @@ class InvenTreeCompanyAttachment extends InvenTreeAttachment {
|
||||
String get REFERENCE_FIELD => "company";
|
||||
|
||||
@override
|
||||
String get URL => "company/attachment/";
|
||||
String get REF_MODEL_TYPE => "company";
|
||||
|
||||
@override
|
||||
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "company/attachment/";
|
||||
|
||||
@override
|
||||
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeCompanyAttachment.fromJson(json);
|
||||
|
@ -938,9 +938,17 @@ class InvenTreeAttachment extends InvenTreeModel {
|
||||
|
||||
InvenTreeAttachment.fromJson(Map<String, dynamic> json) : super.fromJson(json);
|
||||
|
||||
@override
|
||||
String get URL => "attachment/";
|
||||
|
||||
// Override this reference field for any subclasses
|
||||
// Note: This is used for the *legacy* attachment API
|
||||
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");
|
||||
|
||||
// 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
|
||||
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(
|
||||
URL,
|
||||
url,
|
||||
attachment,
|
||||
method: "POST",
|
||||
name: "attachment",
|
||||
|
@ -36,10 +36,6 @@ class InvenTreePartCategory extends InvenTreeModel {
|
||||
"structural": {},
|
||||
};
|
||||
|
||||
if (!api.supportsStructuralCategories) {
|
||||
fields.remove("structural");
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
@ -475,7 +471,10 @@ class InvenTreePartAttachment extends InvenTreeAttachment {
|
||||
String get REFERENCE_FIELD => "part";
|
||||
|
||||
@override
|
||||
String get URL => "part/attachment/";
|
||||
String get REF_MODEL_TYPE => "part";
|
||||
|
||||
@override
|
||||
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "part/attachment/";
|
||||
|
||||
@override
|
||||
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreePartAttachment.fromJson(json);
|
||||
|
@ -237,7 +237,10 @@ class InvenTreePurchaseOrderAttachment extends InvenTreeAttachment {
|
||||
String get REFERENCE_FIELD => "order";
|
||||
|
||||
@override
|
||||
String get URL => "order/po/attachment/";
|
||||
String get REF_MODEL_TYPE => "purchaseorder";
|
||||
|
||||
@override
|
||||
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "order/po/attachment/";
|
||||
|
||||
@override
|
||||
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreePurchaseOrderAttachment.fromJson(json);
|
||||
|
@ -280,6 +280,9 @@ class InvenTreeSalesOrderAttachment extends InvenTreeAttachment {
|
||||
String get REFERENCE_FIELD => "order";
|
||||
|
||||
@override
|
||||
String get URL => "order/so/attachment/";
|
||||
String get REF_MODEL_TYPE => "salesorder";
|
||||
|
||||
@override
|
||||
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "order/so/attachment/";
|
||||
|
||||
}
|
||||
|
@ -635,11 +635,13 @@ class InvenTreeStockItemAttachment extends InvenTreeAttachment {
|
||||
String get REFERENCE_FIELD => "stock_item";
|
||||
|
||||
@override
|
||||
String get URL => "stock/attachment/";
|
||||
String get REF_MODEL_TYPE => "stockitem";
|
||||
|
||||
@override
|
||||
String get URL => InvenTreeAPI().supportsModernAttachments ? "attachment/" : "stock/attachment/";
|
||||
|
||||
@override
|
||||
InvenTreeModel createFromJson(Map<String, dynamic> json) => InvenTreeStockItemAttachment.fromJson(json);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -669,10 +671,6 @@ class InvenTreeStockLocation extends InvenTreeModel {
|
||||
"structural": {},
|
||||
};
|
||||
|
||||
if (!api.supportsStructuralCategories) {
|
||||
fields.remove("structural");
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
@ -121,15 +121,13 @@ class InvenTreeAboutWidget extends StatelessWidget {
|
||||
);
|
||||
|
||||
// Display extra tile if the server supports plugins
|
||||
if (InvenTreeAPI().pluginsEnabled) {
|
||||
tiles.add(
|
||||
ListTile(
|
||||
title: Text(L10().pluginSupport),
|
||||
subtitle: Text(L10().pluginSupportDetail),
|
||||
leading: FaIcon(FontAwesomeIcons.plug),
|
||||
)
|
||||
);
|
||||
}
|
||||
tiles.add(
|
||||
ListTile(
|
||||
title: Text(L10().pluginSupport),
|
||||
subtitle: Text(L10().pluginSupportDetail),
|
||||
leading: FaIcon(FontAwesomeIcons.plug),
|
||||
)
|
||||
);
|
||||
|
||||
} else {
|
||||
tiles.add(
|
||||
|
@ -6,6 +6,7 @@ import "package:font_awesome_flutter/font_awesome_flutter.dart";
|
||||
import "package:one_context/one_context.dart";
|
||||
import "package:url_launcher/url_launcher.dart";
|
||||
|
||||
import "package:inventree/api.dart";
|
||||
import "package:inventree/l10.dart";
|
||||
import "package:inventree/app_colors.dart";
|
||||
|
||||
@ -25,10 +26,10 @@ import "package:inventree/widget/refreshable_state.dart";
|
||||
*/
|
||||
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 int referenceId;
|
||||
final InvenTreeAttachment attachmentClass;
|
||||
final int modelId;
|
||||
final bool hasUploadPermission;
|
||||
|
||||
@override
|
||||
@ -74,7 +75,9 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
|
||||
if (file == null) return;
|
||||
|
||||
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();
|
||||
|
||||
if (result) {
|
||||
@ -131,14 +134,24 @@ class _AttachmentWidgetState extends RefreshableState<AttachmentWidget> {
|
||||
@override
|
||||
Future<void> request(BuildContext context) async {
|
||||
|
||||
await widget.attachment.list(
|
||||
filters: {
|
||||
widget.attachment.REFERENCE_FIELD: widget.referenceId.toString()
|
||||
}
|
||||
Map<String, String> filters = {};
|
||||
|
||||
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) {
|
||||
attachments.clear();
|
||||
|
||||
print("Found ${results.length} results:");
|
||||
|
||||
for (var result in results) {
|
||||
print(result.toString());
|
||||
if (result is InvenTreeAttachment) {
|
||||
attachments.add(result);
|
||||
}
|
||||
|
@ -188,19 +188,14 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
|
||||
}
|
||||
});
|
||||
|
||||
if (api.supportCompanyAttachments) {
|
||||
InvenTreeCompanyAttachment().count(
|
||||
filters: {
|
||||
"company": widget.company.pk.toString()
|
||||
}
|
||||
).then((value) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachmentCount = value;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
InvenTreeCompanyAttachment().countAttachments(widget.company.pk)
|
||||
.then((value) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachmentCount = value;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future <void> editCompany(BuildContext context) async {
|
||||
@ -397,25 +392,24 @@ class _CompanyDetailState extends RefreshableState<CompanyDetailWidget> {
|
||||
));
|
||||
}
|
||||
|
||||
if (api.supportCompanyAttachments) {
|
||||
tiles.add(ListTile(
|
||||
title: Text(L10().attachments),
|
||||
leading: FaIcon(FontAwesomeIcons.fileLines, color: COLOR_ACTION),
|
||||
trailing: attachmentCount > 0 ? Text(attachmentCount.toString()) : null,
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => AttachmentWidget(
|
||||
InvenTreeCompanyAttachment(),
|
||||
widget.company.pk,
|
||||
InvenTreeCompany().canEdit
|
||||
)
|
||||
|
||||
tiles.add(ListTile(
|
||||
title: Text(L10().attachments),
|
||||
leading: FaIcon(FontAwesomeIcons.fileLines, color: COLOR_ACTION),
|
||||
trailing: attachmentCount > 0 ? Text(attachmentCount.toString()) : null,
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => AttachmentWidget(
|
||||
InvenTreeCompanyAttachment(),
|
||||
widget.company.pk,
|
||||
InvenTreeCompany().canEdit
|
||||
)
|
||||
);
|
||||
}
|
||||
));
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
));
|
||||
|
||||
return tiles;
|
||||
}
|
||||
|
@ -174,18 +174,16 @@ class InvenTreeDrawer extends StatelessWidget {
|
||||
tiles.add(Divider());
|
||||
}
|
||||
|
||||
if (InvenTreeAPI().supportsNotifications) {
|
||||
int notification_count = InvenTreeAPI().notification_counter;
|
||||
int notification_count = InvenTreeAPI().notification_counter;
|
||||
|
||||
tiles.add(
|
||||
ListTile(
|
||||
leading: FaIcon(FontAwesomeIcons.bell, color: COLOR_ACTION),
|
||||
trailing: notification_count > 0 ? Text(notification_count.toString()) : null,
|
||||
title: Text(L10().notifications),
|
||||
onTap: _notifications,
|
||||
)
|
||||
);
|
||||
}
|
||||
tiles.add(
|
||||
ListTile(
|
||||
leading: FaIcon(FontAwesomeIcons.bell, color: COLOR_ACTION),
|
||||
trailing: notification_count > 0 ? Text(notification_count.toString()) : null,
|
||||
title: Text(L10().notifications),
|
||||
onTap: _notifications,
|
||||
)
|
||||
);
|
||||
|
||||
tiles.add(
|
||||
ListTile(
|
||||
|
@ -228,9 +228,7 @@ class _PurchaseOrderDetailState extends RefreshableState<PurchaseOrderDetailWidg
|
||||
}
|
||||
}
|
||||
|
||||
InvenTreePurchaseOrderAttachment().count(filters: {
|
||||
"order": widget.order.pk.toString()
|
||||
}).then((int value) {
|
||||
InvenTreePurchaseOrderAttachment().countAttachments(widget.order.pk).then((int value) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachmentCount = value;
|
||||
|
@ -175,9 +175,7 @@ class _SalesOrderDetailState extends RefreshableState<SalesOrderDetailWidget> {
|
||||
|
||||
supportsProjectCodes = api.supportsProjectCodes && await api.getGlobalBooleanSetting("PROJECT_CODES_ENABLED");
|
||||
|
||||
InvenTreeSalesOrderAttachment().count(filters: {
|
||||
"order": widget.order.pk.toString()
|
||||
}).then((int value) {
|
||||
InvenTreeSalesOrderAttachment().countAttachments(widget.order.pk).then((int value) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachmentCount = value;
|
||||
|
@ -90,15 +90,13 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
||||
List<SpeedDialChild> actions = [];
|
||||
|
||||
if (InvenTreePart().canEdit) {
|
||||
if (api.supportModernBarcodes) {
|
||||
actions.add(
|
||||
customBarcodeAction(
|
||||
context, this,
|
||||
widget.part.customBarcode, "part",
|
||||
widget.part.pk
|
||||
)
|
||||
);
|
||||
}
|
||||
actions.add(
|
||||
customBarcodeAction(
|
||||
context, this,
|
||||
widget.part.customBarcode, "part",
|
||||
widget.part.pk
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return actions;
|
||||
@ -184,18 +182,10 @@ class _PartDisplayState extends RefreshableState<PartDetailWidget> {
|
||||
});
|
||||
|
||||
// Request the number of parameters for this part
|
||||
if (api.supportsPartParameters) {
|
||||
showParameters = await InvenTreeSettingsManager().getValue(INV_PART_SHOW_PARAMETERS, true) as bool;
|
||||
} else {
|
||||
showParameters = false;
|
||||
}
|
||||
showParameters = await InvenTreeSettingsManager().getValue(INV_PART_SHOW_PARAMETERS, true) as bool;
|
||||
|
||||
// Request the number of attachments
|
||||
InvenTreePartAttachment().count(
|
||||
filters: {
|
||||
"part": part.pk.toString(),
|
||||
}
|
||||
).then((int value) {
|
||||
InvenTreePartAttachment().countAttachments(part.pk).then((int value) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachmentCount = value;
|
||||
|
@ -140,15 +140,13 @@ class _LocationDisplayState extends RefreshableState<LocationDisplayWidget> {
|
||||
}
|
||||
|
||||
// Assign or un-assign barcodes
|
||||
if (api.supportModernBarcodes) {
|
||||
actions.add(
|
||||
customBarcodeAction(
|
||||
context, this,
|
||||
location!.customBarcode, "stocklocation",
|
||||
location!.pk
|
||||
)
|
||||
);
|
||||
}
|
||||
actions.add(
|
||||
customBarcodeAction(
|
||||
context, this,
|
||||
location!.customBarcode, "stocklocation",
|
||||
location!.pk
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return actions;
|
||||
|
@ -183,15 +183,13 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
|
||||
)
|
||||
);
|
||||
|
||||
if (api.supportModernBarcodes) {
|
||||
actions.add(
|
||||
customBarcodeAction(
|
||||
context, this,
|
||||
widget.item.customBarcode,
|
||||
"stockitem", widget.item.pk
|
||||
)
|
||||
);
|
||||
}
|
||||
actions.add(
|
||||
customBarcodeAction(
|
||||
context, this,
|
||||
widget.item.customBarcode,
|
||||
"stockitem", widget.item.pk
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return actions;
|
||||
@ -246,12 +244,7 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
|
||||
}
|
||||
|
||||
// Request the number of attachments
|
||||
InvenTreeStockItemAttachment().count(
|
||||
filters: {
|
||||
"stock_item": widget.item.pk.toString()
|
||||
}
|
||||
).then((int value) {
|
||||
|
||||
InvenTreeStockItemAttachment().countAttachments(widget.item.pk).then((int value) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
attachmentCount = value;
|
||||
|
@ -110,10 +110,6 @@ void main() {
|
||||
|
||||
// Check supported functions
|
||||
assert(api.apiVersion >= 50);
|
||||
assert(api.supportsSettings);
|
||||
assert(api.supportsNotifications);
|
||||
assert(api.supportsPoReceive);
|
||||
|
||||
assert(api.serverInstance.isNotEmpty);
|
||||
assert(api.serverVersion.isNotEmpty);
|
||||
|
||||
|
Reference in New Issue
Block a user