# Path: ratings/utils.py
# def is_gfk(content_field):
#     return isinstance(content_field, GenericForeignKey)
# 
# def recommended_items(ratings_queryset, user):
#     from ratings.models import SimilarItem
# 
#     scores = {}
#     total_sim = {}
# 
#     for item in ratings_queryset.filter(user=user):
#         similar_items = SimilarItem.objects.get_for_item(item.content_object)
#         for similar_item in similar_items:
# 
#             actual = similar_item.similar_object
#             lookup_kwargs = ratings_queryset.model.lookup_kwargs(actual)
#             lookup_kwargs["user"] = user
# 
#             if ratings_queryset.filter(**lookup_kwargs):
#                 continue
# 
#             scores.setdefault(actual, 0)
#             scores[actual] += similar_item.score * item.score
# 
#             total_sim.setdefault(actual, 0)
#             total_sim[actual] += similar_item.score
# 
#     rankings = [(score / total_sim[item], item) for item, score in scores.items()]
# 
#     rankings.sort()
#     rankings.reverse()
#     return rankings


import hashlib
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.query import QuerySet
from generic_aggregation import generic_annotate
from .utils import is_gfk, recommended_items
        from ratings.utils import calculate_similar_items
                return self.perform_aggregation(models.Sum)

            def average_score(self):
                # the average of all the scores, useful for 1-5
                return self.perform_aggregation(models.Avg)

            def standard_deviation(self):
                # the standard deviation of all the scores, useful for 1-5
                return self.perform_aggregation(models.StdDev)

            def variance(self):
                # the variance of all the scores, useful for 1-5
                return self.perform_aggregation(models.Variance)

            def similar_items(self):
                return SimilarItem.objects.get_for_item(instance)

        manager = RelatedManager()
        manager.core_filters = rel_model.lookup_kwargs(instance)
        manager.model = rel_model

        return manager

    def update_similar_items(self):

        calculate_similar_items(self.all())

    def similar_items(self, item):
        return SimilarItem.objects.get_for_item(item)


# Path: ratings/utils.py
# def is_gfk(content_field):
#     return isinstance(content_field, GenericForeignKey)
# 
# def recommended_items(ratings_queryset, user):
#     from ratings.models import SimilarItem
# 
#     scores = {}
#     total_sim = {}
# 
#     for item in ratings_queryset.filter(user=user):
#         similar_items = SimilarItem.objects.get_for_item(item.content_object)
#         for similar_item in similar_items:
# 
#             actual = similar_item.similar_object
#             lookup_kwargs = ratings_queryset.model.lookup_kwargs(actual)
#             lookup_kwargs["user"] = user
# 
#             if ratings_queryset.filter(**lookup_kwargs):
#                 continue
# 
#             scores.setdefault(actual, 0)
#             scores[actual] += similar_item.score * item.score
# 
#             total_sim.setdefault(actual, 0)
#             total_sim[actual] += similar_item.score
# 
#     rankings = [(score / total_sim[item], item) for item, score in scores.items()]
# 
#     rankings.sort()
#     rankings.reverse()
#     return rankings


# Path: ratings/models.py
import hashlib
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models.query import QuerySet
from generic_aggregation import generic_annotate
from .utils import is_gfk, recommended_items
        from ratings.utils import calculate_similar_items

                return self.perform_aggregation(models.Sum)

            def average_score(self):
                # the average of all the scores, useful for 1-5
                return self.perform_aggregation(models.Avg)

            def standard_deviation(self):
                # the standard deviation of all the scores, useful for 1-5
                return self.perform_aggregation(models.StdDev)

            def variance(self):
                # the variance of all the scores, useful for 1-5
                return self.perform_aggregation(models.Variance)

            def similar_items(self):
                return SimilarItem.objects.get_for_item(instance)

        manager = RelatedManager()
        manager.core_filters = rel_model.lookup_kwargs(instance)
        manager.model = rel_model

        return manager

    def update_similar_items(self):

        calculate_similar_items(self.all())

    def similar_items(self, item):
        return SimilarItem.objects.get_for_item(item)


    def recommended_items(self, user):