# 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

# this goes on your model
class Ratings:
    def __init__(self, rating_model=None):
        self.rating_model = rating_model or RatedItem

    def contribute_to_class(self, cls, name):
        # set up the ForeignRelatedObjectsDescriptor right hyah
        setattr(cls, name, _RatingsDescriptor(cls, self.rating_model, name))
        setattr(cls, "_ratings_field", name)


class RatingsQuerySet(QuerySet):
    def __init__(self, model=None, query=None, using=None, hints=None, rated_model=None):
        self.rated_model = rated_model
        super(RatingsQuerySet, self).__init__(model, query, using, hints)

    def _clone(self, *args, **kwargs):
        instance = super(RatingsQuerySet, self)._clone(*args, **kwargs)
        instance.rated_model = self.rated_model
        return instance

    def order_by_rating(self, aggregator=models.Sum, descending=True, queryset=None, alias="score"):
        related_field = self.model._meta.get_field("content_object")

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

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


# 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


# this goes on your model
class Ratings:
    def __init__(self, rating_model=None):
        self.rating_model = rating_model or RatedItem

    def contribute_to_class(self, cls, name):
        # set up the ForeignRelatedObjectsDescriptor right hyah
        setattr(cls, name, _RatingsDescriptor(cls, self.rating_model, name))
        setattr(cls, "_ratings_field", name)


class RatingsQuerySet(QuerySet):
    def __init__(self, model=None, query=None, using=None, hints=None, rated_model=None):
        self.rated_model = rated_model
        super(RatingsQuerySet, self).__init__(model, query, using, hints)

    def _clone(self, *args, **kwargs):
        instance = super(RatingsQuerySet, self)._clone(*args, **kwargs)
        instance.rated_model = self.rated_model
        return instance

    def order_by_rating(self, aggregator=models.Sum, descending=True, queryset=None, alias="score"):
        related_field = self.model._meta.get_field("content_object")

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

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


        if not is_gfk(related_field):