<commit_before>from django.db import models

from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.utils.translation import ugettext_lazy as _
from cases.models import Case
from notifications import notify


class Record(models.Model):
    STATIC_RELATION = ['letter', 'event', 'alarm']
    case = models.ForeignKey(Case)
    created_on = models.DateTimeField(auto_now_add=True)
    letter = models.OneToOneField('letters.Letter', null=True, blank=True)
    event = models.OneToOneField('events.Event', null=True, blank=True)
    alarm = models.OneToOneField('events.Alarm', null=True, blank=True)

    content_type = models.ForeignKey(
        ContentType,
        null=True,
        blank=True,
    )
    object_id = models.PositiveIntegerField(
        null=True,
    )
    related_object = generic.GenericForeignKey('content_type', 'object_id')

    @property  # We use OneToOneField as possible
    def content_object(self):
        for field in self.STATIC_RELATION:
            if getattr(self, field + "_id"):
                return getattr(self, field)
        return self.related_object

    @content_object.setter
    def content_object(self, obj):
        for field in self.STATIC_RELATION:
            if self._meta.get_field_by_name(field)[0].rel.to == obj._meta.model:
                setattr(self, field, obj)
        self.related_object = obj

    def get_users_with_perms(self, *args, **kwargs):
        return Case(pk=self.case_id).get_users_with_perms()

    def case_get_absolute_url(self):
        return Case(pk=self.case_id).get_absolute_url()

    class Meta:
        ordering = ['created_on', 'id']
        verbose_name = _('Record')
        verbose_name_plural = _('Records')


class AbstractRecord(models.Model):
    record_general = GenericRelation('Record', related_query_name='record')
    case = models.ForeignKey(Case)

    def show_modifier(self):
        return (self.modified_by_id and self.modified_by_id != self.created_by_id)

    def get_users_with_perms(self, *args, **kwargs):
        return self.case.get_users_with_perms(*args, **kwargs)

    def get_template_list(self):
        return "%s/_%s_list.html" % (self._meta.app_label, self._meta.model_name)

    def send_notification(self, actor, verb):
        for user in self.case.get_users_with_perms().exclude(pk=actor.pk):
            user.notify(actor=actor,
                verb=verb,
                target=self,
                from_email=self.case.get_email())

    def save(self, *args, **kwargs):
        created = True if self.pk is None else False<commit_msg>Update template name to use unicode text<commit_after>from django.db import models

from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.utils.translation import ugettext_lazy as _
from cases.models import Case
from notifications import notify


class Record(models.Model):
    STATIC_RELATION = ['letter', 'event', 'alarm']
    case = models.ForeignKey(Case)
    created_on = models.DateTimeField(auto_now_add=True)
    letter = models.OneToOneField('letters.Letter', null=True, blank=True)
    event = models.OneToOneField('events.Event', null=True, blank=True)
    alarm = models.OneToOneField('events.Alarm', null=True, blank=True)

    content_type = models.ForeignKey(
        ContentType,
        null=True,
        blank=True,
    )
    object_id = models.PositiveIntegerField(
        null=True,
    )
    related_object = generic.GenericForeignKey('content_type', 'object_id')

    @property  # We use OneToOneField as possible
    def content_object(self):
        for field in self.STATIC_RELATION:
            if getattr(self, field + "_id"):
                return getattr(self, field)
        return self.related_object

    @content_object.setter
    def content_object(self, obj):
        for field in self.STATIC_RELATION:
            if self._meta.get_field_by_name(field)[0].rel.to == obj._meta.model:
                setattr(self, field, obj)
        self.related_object = obj

    def get_users_with_perms(self, *args, **kwargs):
        return Case(pk=self.case_id).get_users_with_perms()

    def case_get_absolute_url(self):
        return Case(pk=self.case_id).get_absolute_url()

    class Meta:
        ordering = ['created_on', 'id']
        verbose_name = _('Record')
        verbose_name_plural = _('Records')


class AbstractRecord(models.Model):
    record_general = GenericRelation('Record', related_query_name='record')
    case = models.ForeignKey(Case)

    def show_modifier(self):
        return (self.modified_by_id and self.modified_by_id != self.created_by_id)

    def get_users_with_perms(self, *args, **kwargs):
        return self.case.get_users_with_perms(*args, **kwargs)

    def get_template_list(self):
        return u"%s/_%s_list.html" % (self._meta.app_label, self._meta.model_name)

    def send_notification(self, actor, verb):
        for user in self.case.get_users_with_perms().exclude(pk=actor.pk):
            user.notify(actor=actor,
                verb=verb,
                target=self,
                from_email=self.case.get_email())

    def save(self, *args, **kwargs):
        created = True if self.pk is None else False
{'fraction_non_alphanumeric': 0.10491318803103065, 'fraction_numerical': 0.0003694126339120798, 'mean_word_length': 3.280632411067194, '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': '3190001', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1618, 'n_words': 343}