2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-17 04:35:26 +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

@ -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);

View File

@ -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",

View File

@ -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);

View File

@ -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);

View File

@ -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/";
}

View File

@ -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;
}