# 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




class RatedItemBase(models.Model):
    score = models.FloatField(default=0, db_index=True)
    user = models.ForeignKey(User, related_name="%(class)ss", on_delete=models.CASCADE)
    hashed = models.CharField(max_length=40, editable=False, db_index=True)

    class Meta:
        abstract = True

    def __str__(self):
        return "%s rated %s by %s" % (self.content_object, self.score, self.user)

    def save(self, *args, **kwargs):
        self.hashed = self.generate_hash()
        super(RatedItemBase, self).save(*args, **kwargs)

    def generate_hash(self):
        content_field = self._meta.get_field("content_object")
        related_object = getattr(self, content_field.name)
        uniq = "%s.%s" % (related_object._meta, related_object.pk)
        return hashlib.sha1(uniq.encode("ascii")).hexdigest()


# 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





class RatedItemBase(models.Model):
    score = models.FloatField(default=0, db_index=True)
    user = models.ForeignKey(User, related_name="%(class)ss", on_delete=models.CASCADE)
    hashed = models.CharField(max_length=40, editable=False, db_index=True)

    class Meta:
        abstract = True

    def __str__(self):
        return "%s rated %s by %s" % (self.content_object, self.score, self.user)

    def save(self, *args, **kwargs):
        self.hashed = self.generate_hash()
        super(RatedItemBase, self).save(*args, **kwargs)

    def generate_hash(self):
        content_field = self._meta.get_field("content_object")
        related_object = getattr(self, content_field.name)
        uniq = "%s.%s" % (related_object._meta, related_object.pk)
        return hashlib.sha1(uniq.encode("ascii")).hexdigest()


    @classmethod