Add an all_products property to FlagBase
It's common to need to query the fill list of products covered by a Flag - whether directly, or by being in an included category. Add an all_products property which does this.
This commit is contained in:
		
							parent
							
								
									0e1038de97
								
							
						
					
					
						commit
						6a9652dfd2
					
				
					 1 changed files with 18 additions and 1 deletions
				
			
		|  | @ -5,6 +5,7 @@ from . import inventory | |||
| from django.contrib.auth.models import Group | ||||
| from django.core.exceptions import ValidationError | ||||
| from django.db import models | ||||
| from django.db.models import F, Q | ||||
| from django.utils.encoding import python_2_unicode_compatible | ||||
| from django.utils.translation import ugettext_lazy as _ | ||||
| from model_utils.managers import InheritanceManager | ||||
|  | @ -383,10 +384,15 @@ class FlagBase(models.Model): | |||
|             ``ENABLE_IF_TRUE`` condition. | ||||
| 
 | ||||
|         products ([inventory.Product, ...]): | ||||
|             The Products affected by this flag. | ||||
|             The Products affected directly by this flag. | ||||
| 
 | ||||
|         categories ([inventory.Category, ...]): | ||||
|             The Categories whose Products are affected by this flag. | ||||
| 
 | ||||
|         all_products ([inventory.Product, ...]): | ||||
|             All products affected by this flag, either by being listed directly | ||||
|             or by having their category listed. | ||||
| 
 | ||||
|     ''' | ||||
| 
 | ||||
|     objects = InheritanceManager() | ||||
|  | @ -439,6 +445,17 @@ class FlagBase(models.Model): | |||
|         related_name="flagbase_set", | ||||
|     ) | ||||
| 
 | ||||
|     @property | ||||
|     def all_products(self): | ||||
|         all_products = inventory.Product.objects.all() | ||||
|         all_products = all_products.filter( | ||||
|             ( | ||||
|                 Q(id__in=self.products.all()) | | ||||
|                 Q(category_id__in=self.categories.all()) | ||||
|             ) | ||||
|         ) | ||||
|         return all_products | ||||
| 
 | ||||
| 
 | ||||
| class TimeOrStockLimitFlag(TimeOrStockLimitCondition, FlagBase): | ||||
|     ''' Product groupings that can be used to enable a product during a | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 James Polley
						James Polley