mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Add instance-specific filters to API OPTIONS data
This commit is contained in:
		| @@ -32,6 +32,9 @@ class InvenTreeMetadata(SimpleMetadata): | |||||||
|  |  | ||||||
|     def determine_metadata(self, request, view): |     def determine_metadata(self, request, view): | ||||||
|          |          | ||||||
|  |         self.request = request | ||||||
|  |         self.view = view | ||||||
|  |  | ||||||
|         metadata = super().determine_metadata(request, view) |         metadata = super().determine_metadata(request, view) | ||||||
|  |  | ||||||
|         user = request.user |         user = request.user | ||||||
| @@ -136,6 +139,42 @@ class InvenTreeMetadata(SimpleMetadata): | |||||||
|         except AttributeError: |         except AttributeError: | ||||||
|             pass |             pass | ||||||
|  |  | ||||||
|  |         # Try to extract 'instance' information | ||||||
|  |         instance = None | ||||||
|  |  | ||||||
|  |         # Extract extra information if an instance is available | ||||||
|  |         if hasattr(serializer, 'instance'): | ||||||
|  |             instance = serializer.instance | ||||||
|  |          | ||||||
|  |         if instance is None: | ||||||
|  |             try: | ||||||
|  |                 instance = self.view.get_object() | ||||||
|  |             except: | ||||||
|  |                 pass | ||||||
|  |  | ||||||
|  |         if instance is not None: | ||||||
|  |             """ | ||||||
|  |             If there is an instance associated with this API View, | ||||||
|  |             introspect that instance to find any specific API info. | ||||||
|  |             """ | ||||||
|  |  | ||||||
|  |             if hasattr(instance, 'api_instance_filters'): | ||||||
|  |  | ||||||
|  |                 instance_filters = instance.api_instance_filters() | ||||||
|  |  | ||||||
|  |                 for field_name, field_filters in instance_filters.items(): | ||||||
|  |  | ||||||
|  |                     if field_name not in serializer_info.keys(): | ||||||
|  |                         # The field might be missing, but is added later on | ||||||
|  |                         # This function seems to get called multiple times? | ||||||
|  |                         continue | ||||||
|  |  | ||||||
|  |                     if 'instance_filters' not in serializer_info[field_name].keys(): | ||||||
|  |                         serializer_info[field_name]['instance_filters'] = {} | ||||||
|  |  | ||||||
|  |                     for key, value in field_filters.items(): | ||||||
|  |                         serializer_info[field_name]['instance_filters'][key] = value | ||||||
|  |  | ||||||
|         return serializer_info |         return serializer_info | ||||||
|  |  | ||||||
|     def get_field_info(self, field): |     def get_field_info(self, field): | ||||||
|   | |||||||
| @@ -93,6 +93,17 @@ class InvenTreeTree(MPTTModel): | |||||||
|         parent: The item immediately above this one. An item with a null parent is a top-level item |         parent: The item immediately above this one. An item with a null parent is a top-level item | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|  |     def api_instance_filters(self): | ||||||
|  |         """ | ||||||
|  |         Instance filters for InvenTreeTree models | ||||||
|  |         """ | ||||||
|  |  | ||||||
|  |         return { | ||||||
|  |             'parent': { | ||||||
|  |                 'exclude_tree': self.pk, | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|     def save(self, *args, **kwargs): |     def save(self, *args, **kwargs): | ||||||
|  |  | ||||||
|         try: |         try: | ||||||
|   | |||||||
| @@ -359,6 +359,18 @@ class Part(MPTTModel): | |||||||
|  |  | ||||||
|         return reverse('api-part-list') |         return reverse('api-part-list') | ||||||
|  |  | ||||||
|  |     def api_instance_filters(self): | ||||||
|  |         """ | ||||||
|  |         Return API query filters for limiting field results against this instance | ||||||
|  |         """ | ||||||
|  |  | ||||||
|  |         return { | ||||||
|  |             'variant_of': { | ||||||
|  |                 'exclude_tree': self.pk, | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|     def get_context_data(self, request, **kwargs): |     def get_context_data(self, request, **kwargs): | ||||||
|         """ |         """ | ||||||
|         Return some useful context data about this part for template rendering |         Return some useful context data about this part for template rendering | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user