Switch score calculation to pure Python
Rip out the score expression madness, and calculate the score in Python, since I hear that Python is better at math than SQL is.
This commit is contained in:
		
							parent
							
								
									084c78e2cd
								
							
						
					
					
						commit
						866217bf35
					
				
					 1 changed files with 8 additions and 17 deletions
				
			
		
							
								
								
									
										25
									
								
								vendor/symposion/reviews/models.py
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										25
									
								
								vendor/symposion/reviews/models.py
									
										
									
									
										vendored
									
									
								
							|  | @ -19,20 +19,6 @@ from symposion.proposals.models import ProposalBase | |||
| from symposion.schedule.models import Presentation | ||||
| 
 | ||||
| 
 | ||||
| def score_expression(): | ||||
|     score = ( | ||||
|         (Decimal(2) * F("plus_two") + F("plus_one")) - | ||||
|         (F("minus_one") + Decimal(2) * F("minus_two")) | ||||
|     ) / ( | ||||
|         F("vote_count") * Decimal(1) | ||||
|     ) | ||||
| 
 | ||||
|     return Case( | ||||
|         When(vote_count=0, then=Value("0")),  # no divide by zero | ||||
|         default=score, | ||||
|     ) | ||||
| 
 | ||||
| 
 | ||||
| class Votes(object): | ||||
|     ABSTAIN = "0" | ||||
|     PLUS_TWO = "+2" | ||||
|  | @ -196,7 +182,7 @@ class Review(models.Model): | |||
|             else: | ||||
|                 # self is not the latest review so we just need to decrement | ||||
|                 # the comment count | ||||
|                 self.proposal.result.comment_count = models.F("comment_count") - 1 | ||||
|                 self.proposal.result.comment_count = F("comment_count") - 1 | ||||
|                 self.proposal.result.save() | ||||
|         # in all cases we need to delete the review; let's do it! | ||||
|         super(Review, self).delete() | ||||
|  | @ -274,6 +260,12 @@ class ProposalResult(models.Model): | |||
|             result, created = cls._default_manager.get_or_create(proposal=proposal) | ||||
|             result.update_vote() | ||||
| 
 | ||||
|     def calculate_score(self): | ||||
|         if self.vote_count == 0: | ||||
|             return 0 | ||||
|         else: | ||||
|             return ((2 * self.plus_two + self.plus_one) - (2 * self.minus_two + self.minus_one)) / self.vote_count | ||||
| 
 | ||||
|     def update_vote(self, *a, **k): | ||||
|         proposal = self.proposal | ||||
|         self.comment_count = Review.objects.filter(proposal=proposal).count() | ||||
|  | @ -296,9 +288,8 @@ class ProposalResult(models.Model): | |||
|         self.minus_one = vote_count[VOTES.MINUS_ONE] | ||||
|         self.minus_two = vote_count[VOTES.MINUS_TWO] | ||||
|         self.vote_count = sum(i[1] for i in vote_count.items()) - self.abstain | ||||
|         self.score = self.calculate_score() | ||||
|         self.save() | ||||
|         model = self.__class__ | ||||
|         model._default_manager.filter(pk=self.pk).update(score=score_expression()) | ||||
| 
 | ||||
|     class Meta: | ||||
|         verbose_name = _("proposal_result") | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Steve Kowalik
						Steve Kowalik