fundgoal: Add provider convenience methods to FundraisingGoal.
This commit is contained in:
		
							parent
							
								
									2461bc19b1
								
							
						
					
					
						commit
						4fd27245d6
					
				
					 1 changed files with 14 additions and 0 deletions
				
			
		|  | @ -1,3 +1,5 @@ | |||
| import random | ||||
| 
 | ||||
| from django.db import models | ||||
| 
 | ||||
| class FundraisingGoal(models.Model): | ||||
|  | @ -18,6 +20,18 @@ class FundraisingGoal(models.Model): | |||
|     class Meta: | ||||
|         ordering = ('fundraiser_code_name',) | ||||
| 
 | ||||
|     def providers(self): | ||||
|         return GoalProvider.objects.filter(fundraising_goal=self) | ||||
| 
 | ||||
|     def random_providers(self, k=None): | ||||
|         providers = self.providers() | ||||
|         if not providers.exists(): | ||||
|             return None | ||||
|         elif k is None: | ||||
|             return random.choice(providers) | ||||
|         else: | ||||
|             return random.sample(providers, k) | ||||
| 
 | ||||
| 
 | ||||
| class GoalProvider(models.Model): | ||||
|     fundraising_goal = models.ForeignKey( | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Brett Smith
						Brett Smith