# Path: ratings/models.py
# 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
#     def lookup_kwargs(cls, instance):
#         return {"content_object": instance}
# 
#     @classmethod
#     def base_kwargs(cls, model_class):
#         return {}
# 
# 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)


from django.db import models
from ..models import RatedItemBase, Ratings



class Food(models.Model):
    name = models.CharField(max_length=50)

    ratings = Ratings()

    def __str__(self):
        return self.name


class BeverageRating(RatedItemBase):
    content_object = models.ForeignKey("Beverage", on_delete=models.CASCADE)


class Beverage(models.Model):
    name = models.CharField(max_length=50)


# Path: ratings/models.py
# 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
#     def lookup_kwargs(cls, instance):
#         return {"content_object": instance}
# 
#     @classmethod
#     def base_kwargs(cls, model_class):
#         return {}
# 
# 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)


# Path: ratings/tests/models.py
from django.db import models
from ..models import RatedItemBase, Ratings




class Food(models.Model):
    name = models.CharField(max_length=50)

    ratings = Ratings()

    def __str__(self):
        return self.name


class BeverageRating(RatedItemBase):
    content_object = models.ForeignKey("Beverage", on_delete=models.CASCADE)


class Beverage(models.Model):
    name = models.CharField(max_length=50)


    ratings = Ratings(BeverageRating)