- def get_favored_results(self, error=5):
- """Calculate the set of most-favored results. A higher error
- will cause this method to be more reticent about adding new
- items."""
- results = self.result_urls.values_list('pk', 'url',)
-
- result_dict = {}
- for pk, url in results:
- result_dict[pk] = {'url': url, 'value': 0}
-
- clicks = Click.objects.filter(result__pk__in=result_dict.keys()).values_list('result__pk', 'datetime')
-
- now = datetime.datetime.now()
-
- def datetime_value(dt):
- days = (now - dt).days
- if days < 0:
- raise ValueError("Click dates must be in the past.")
- if days == 0:
- value = 1.0
- else:
- value = 1.0/days**2
- return value
-
- for pk, dt in clicks:
- value = datetime_value(dt)
- result_dict[pk]['value'] += value
-
- #TODO: is there a reasonable minimum value for consideration?
- subsets = {}
- for d in result_dict.values():
- subsets.setdefault(d['value'], []).append(d)
-
- # Now calculate the result set.
- results = []
+ def get_weighted_results(self, threshhold=None):
+ "Returns this search's results ordered by decreasing weight."
+ if not hasattr(self, '_weighted_results'):
+ result_qs = self.result_urls.all()
+
+ if threshhold is not None:
+ result_qs = result_qs.filter(counts__datetime__gte=threshhold)
+
+ results = [result for result in result_qs]
+
+ results.sort(cmp=lambda x,y: cmp(y.weight, x.weight))
+
+ self._weighted_results = results