# 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

        if queryset is None:
            queryset = self.rated_model._default_manager.all()

        ordering = descending and "-%s" % alias or alias

        if not is_gfk(related_field):
            query_name = related_field.related_query_name()

            if len(self.query.where.children):
                queryset = queryset.filter(**{"%s__pk__in" % query_name: self.values_list("pk")})

            return queryset.annotate(**{alias: aggregator("%s__score" % query_name)}).order_by(ordering)

        else:
            return generic_annotate(queryset, self, aggregator("score"), related_field, alias=alias).order_by(ordering)


class _RatingsDescriptor(models.Manager):
    def __init__(self, rated_model, rating_model, rating_field):
        self.rated_model = rated_model
        self.rating_model = rating_model
        self.rating_field = rating_field

    def __get__(self, instance, instance_type=None):
        if instance is None:
            return self

        return self.create_manager(instance, self.rating_model._default_manager.__class__)


# 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


        if queryset is None:
            queryset = self.rated_model._default_manager.all()

        ordering = descending and "-%s" % alias or alias

        if not is_gfk(related_field):
            query_name = related_field.related_query_name()

            if len(self.query.where.children):
                queryset = queryset.filter(**{"%s__pk__in" % query_name: self.values_list("pk")})

            return queryset.annotate(**{alias: aggregator("%s__score" % query_name)}).order_by(ordering)

        else:
            return generic_annotate(queryset, self, aggregator("score"), related_field, alias=alias).order_by(ordering)


class _RatingsDescriptor(models.Manager):
    def __init__(self, rated_model, rating_model, rating_field):
        self.rated_model = rated_model
        self.rating_model = rating_model
        self.rating_field = rating_field

    def __get__(self, instance, instance_type=None):
        if instance is None:
            return self

        return self.create_manager(instance, self.rating_model._default_manager.__class__)


    def __set__(self, instance, value):