You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829
  1. from django.db.models import F
  2. from oscar.core.loading import get_model
  3. ProductRecord = get_model('analytics', 'ProductRecord')
  4. Product = get_model('catalogue', 'Product')
  5. class Calculator(object):
  6. # Map of field name to weight
  7. weights = {
  8. 'num_views': 1,
  9. 'num_basket_additions': 3,
  10. 'num_purchases': 5
  11. }
  12. def __init__(self, logger):
  13. self.logger = logger
  14. def run(self):
  15. self.calculate_scores()
  16. def calculate_scores(self):
  17. self.logger.info("Calculating product scores")
  18. total_weight = float(sum(self.weights.values()))
  19. weighted_fields = [
  20. self.weights[name] * F(name) for name in self.weights.keys()]
  21. ProductRecord.objects.update(
  22. score=sum(weighted_fields)/total_weight)