Commit 5cc00cd85275a37d3555d4599e7d246763fdd60f

Authored by Carlos Coêlho
1 parent 9475b27b

Using def function instead of lambda

Signed-off-by: Carlos Oliveira <carlospecter@gmail.com>
Signed-off-by: Lucas Kanashiro <kanashiro.duarte@gmail.com>
Showing 1 changed file with 12 additions and 9 deletions   Show diff stats
colab/super_archives/models.py
@@ -134,6 +134,10 @@ class Thread(models.Model, HitCounterModelMixin): @@ -134,6 +134,10 @@ class Thread(models.Model, HitCounterModelMixin):
134 objects = NotSpamManager() 134 objects = NotSpamManager()
135 tags = TaggableManager() 135 tags = TaggableManager()
136 136
  137 + # Save this pseudo now to avoid calling the
  138 + # function N times in the loops below
  139 + now = timezone.now()
  140 +
137 class Meta: 141 class Meta:
138 verbose_name = _(u"Thread") 142 verbose_name = _(u"Thread")
139 verbose_name_plural = _(u"Threads") 143 verbose_name_plural = _(u"Threads")
@@ -182,6 +186,12 @@ class Thread(models.Model, HitCounterModelMixin): @@ -182,6 +186,12 @@ class Thread(models.Model, HitCounterModelMixin):
182 self.subject_token, 186 self.subject_token,
183 self.message_set.count()) 187 self.message_set.count())
184 188
  189 + def _days_ago(self, date):
  190 + return (self.now - date).days
  191 +
  192 + def _get_score(self, weight, created):
  193 + return max(weight - (self.days_ago(created) // 3), 5)
  194 +
185 def update_score(self): 195 def update_score(self):
186 """Update the relevance score for this thread. 196 """Update the relevance score for this thread.
187 197
@@ -205,22 +215,15 @@ class Thread(models.Model, HitCounterModelMixin): @@ -205,22 +215,15 @@ class Thread(models.Model, HitCounterModelMixin):
205 if not self.subject_token: 215 if not self.subject_token:
206 return 216 return
207 217
208 - # Save this pseudo now to avoid calling the  
209 - # function N times in the loops below  
210 - now = timezone.now()  
211 - days_ago = lambda date: (now - date).days  
212 - get_score = lambda weight, created: max(weight - (days_ago(created)  
213 - // 3), 5)  
214 -  
215 vote_score = 0 218 vote_score = 0
216 replies_score = 0 219 replies_score = 0
217 for msg in self.message_set.all(): 220 for msg in self.message_set.all():
218 # Calculate replies_score 221 # Calculate replies_score
219 - replies_score += get_score(300, msg.received_time) 222 + replies_score += self._get_score(300, msg.received_time)
220 223
221 # Calculate vote_score 224 # Calculate vote_score
222 for vote in msg.vote_set.all(): 225 for vote in msg.vote_set.all():
223 - vote_score += get_score(100, vote.created) 226 + vote_score += self._get_score(100, vote.created)
224 227
225 # Calculate page_view_score 228 # Calculate page_view_score
226 page_view_score = self.hits * 10 229 page_view_score = self.hits * 10