<commit_before>from decimal import Decimal

from django.db import models
from django.conf import settings
from django.db.models import Avg
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible

from django.contrib.auth.models import User
from django.contrib.contenttypes.generic import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from .categories import RATING_CATEGORY_CHOICES
from .managers import OverallRatingManager

USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', User)


class OverallRating(models.Model):

    object_id = models.IntegerField(db_index=True)
    content_type = models.ForeignKey(ContentType)
    content_object = GenericForeignKey()
    rating = models.DecimalField(decimal_places=1, max_digits=6, null=True)
    category = models.IntegerField(null=True, choices=RATING_CATEGORY_CHOICES)

    objects = OverallRatingManager()

    class Meta:
        unique_together = [
            ("object_id", "content_type", "category"),
        ]

    def update(self):
        r = Rating.objects.filter(overall_rating=self).aggregate(r=Avg("rating"))["r"] or 0
        self.rating = Decimal(str(r))
        self.save()


@python_2_unicode_compatible<commit_msg>Handle different location for GenericForeignKey<commit_after>from decimal import Decimal

from django.db import models
from django.conf import settings
from django.db.models import Avg
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible

from django.contrib.auth.models import User
try:
    from django.contrib.contenttypes.generic import GenericForeignKey
except:
    from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from .categories import RATING_CATEGORY_CHOICES
from .managers import OverallRatingManager

USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', User)


class OverallRating(models.Model):

    object_id = models.IntegerField(db_index=True)
    content_type = models.ForeignKey(ContentType)
    content_object = GenericForeignKey()
    rating = models.DecimalField(decimal_places=1, max_digits=6, null=True)
    category = models.IntegerField(null=True, choices=RATING_CATEGORY_CHOICES)

    objects = OverallRatingManager()

    class Meta:
        unique_together = [
            ("object_id", "content_type", "category"),
        ]

    def update(self):
        r = Rating.objects.filter(overall_rating=self).aggregate(r=Avg("rating"))["r"] or 0
        self.rating = Decimal(str(r))
        self.save()


@python_2_unicode_compatible
{'fraction_non_alphanumeric': 0.08498475609756098, 'fraction_numerical': 0.0038109756097560975, 'mean_word_length': 4.78193832599119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2096566', 'n_tokens_mistral': 781, 'n_tokens_neox': 742, 'n_words': 171}