<commit_before># -*- coding: utf-8 -*-
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _

from django.contrib.contenttypes.models import ContentType

from django_nyt import settings


_notification_type_cache = {}


class NotificationType(models.Model):

    """
    Notification types are added on-the-fly by the
    applications adding new notifications
    """
    key = models.CharField(
        max_length=128,
        primary_key=True,
        verbose_name=_('unique key'),
        unique=True
    )
    label = models.CharField(
        max_length=128,
        verbose_name=_('optional label'),  # TODO: This isn't translatable, why do we have it!?
        blank=True,
        null=True
    )
    content_type = models.ForeignKey(ContentType, blank=True, null=True)

    def __unicode__(self):
        return self.key

    class Meta:
        db_table = settings.DB_TABLE_PREFIX + '_notificationtype'
        verbose_name = _('type')
        verbose_name_plural = _('types')
    
    @classmethod
    def get_by_key(cls, key, content_type=None):
        if key in _notification_type_cache:
            return _notification_type_cache[key]
        try:
            nt = cls.objects.get(key=key)
        except cls.DoesNotExist:
            nt = cls.objects.create(key=key, content_type=content_type)
        _notification_type_cache[key] = nt
        return nt


class Settings(models.Model):

    """
    Reusable settings object for a subscription
    """

    user = models.ForeignKey(
        settings.USER_MODEL,
        on_delete=models.CASCADE,  # If a user is deleted, remove all settings
        verbose_name=_(u"user"),
    )
    interval = models.SmallIntegerField(
        choices=settings.INTERVALS,
        verbose_name=_('interval'),
        default=settings.INTERVALS_DEFAULT,
    )

    def __unicode__(self):
        obj_name = _("Settings for %s") % self.user.username
        return str(obj_name)

    class Meta:
        db_table = settings.DB_TABLE_PREFIX + '_settings'
        verbose_name = _('settings')
        verbose_name_plural = _('settings')


class Subscription(models.Model):

    settings = models.ForeignKey(
        Settings,
        verbose_name=_(u"settings"),
        on_delete=models.CASCADE,  # If settings are deleted, remove all subscriptions
    )
    notification_type = models.ForeignKey(
        NotificationType,
        verbose_name=_(u"notification type"),
    )
    object_id = models.CharField(
        max_length=64,
        null=True,
        blank=True,
        help_text=_('Leave this blank to subscribe to any kind of object'),
        verbose_name=_(u"object ID"),
    )
    send_emails = models.BooleanField(
        default=True,
        verbose_name=_(u"send emails"),
    )
    latest = models.ForeignKey(
        'Notification',
        null=True,
        blank=True,
        related_name='latest_for',
        verbose_name=_(u"latest notification"),
    )

    def __unicode__(self):
        obj_name = _("Subscription for: %s") % str(self.settings.user.username)
        return str(obj_name)

    class Meta:
        db_table = settings.DB_TABLE_PREFIX + '_subscription'
        verbose_name = _('subscription')
        verbose_name_plural = _('subscriptions')


class Notification(models.Model):
    
    # Either set the subscription
    subscription = models.ForeignKey(
        Subscription,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        verbose_name=_(u"subscription"),
    )
    # Or the user to receive the notification
    user = models.ForeignKey(
        'auth.User',
        null=True,
        blank=True,
        on_delete=models.CASCADE,  # If a user is deleted, remove all notifications
        verbose_name=_(u"user"),
    )
    message = models.TextField()
    url = models.CharField(<commit_msg>Update models.py<commit_after># -*- coding: utf-8 -*-
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _

from django.contrib.contenttypes.models import ContentType

from django_nyt import settings


_notification_type_cache = {}


class NotificationType(models.Model):

    """
    Notification types are added on-the-fly by the
    applications adding new notifications
    """
    key = models.CharField(
        max_length=128,
        primary_key=True,
        verbose_name=_('unique key'),
        unique=True
    )
    label = models.CharField(
        max_length=128,
        verbose_name=_('optional label'),  # TODO: This isn't translatable, why do we have it!?
        blank=True,
        null=True
    )
    content_type = models.ForeignKey(ContentType, blank=True, null=True)

    def __unicode__(self):
        return self.key

    class Meta:
        db_table = settings.DB_TABLE_PREFIX + '_notificationtype'
        verbose_name = _('type')
        verbose_name_plural = _('types')
    
    @classmethod
    def get_by_key(cls, key, content_type=None):
        if key in _notification_type_cache:
            return _notification_type_cache[key]
        try:
            nt = cls.objects.get(key=key)
        except cls.DoesNotExist:
            nt = cls.objects.create(key=key, content_type=content_type)
        _notification_type_cache[key] = nt
        return nt


class Settings(models.Model):

    """
    Reusable settings object for a subscription
    """

    user = models.ForeignKey(
        settings.USER_MODEL,
        on_delete=models.CASCADE,  # If a user is deleted, remove all settings
        verbose_name=_(u"user"),
    )
    interval = models.SmallIntegerField(
        choices=settings.INTERVALS,
        verbose_name=_('interval'),
        default=settings.INTERVALS_DEFAULT,
    )

    def __unicode__(self):
        obj_name = _("Settings for %s") % self.user.username
        return str(obj_name)

    class Meta:
        db_table = settings.DB_TABLE_PREFIX + '_settings'
        verbose_name = _('settings')
        verbose_name_plural = _('settings')


class Subscription(models.Model):

    settings = models.ForeignKey(
        Settings,
        verbose_name=_(u"settings"),
        on_delete=models.CASCADE,  # If settings are deleted, remove all subscriptions
    )
    notification_type = models.ForeignKey(
        NotificationType,
        verbose_name=_(u"notification type"),
    )
    object_id = models.CharField(
        max_length=64,
        null=True,
        blank=True,
        help_text=_('Leave this blank to subscribe to any kind of object'),
        verbose_name=_(u"object ID"),
    )
    send_emails = models.BooleanField(
        default=True,
        verbose_name=_(u"send emails"),
    )
    latest = models.ForeignKey(
        'Notification',
        null=True,
        blank=True,
        related_name='latest_for',
        verbose_name=_(u"latest notification"),
    )

    def __unicode__(self):
        obj_name = _("Subscription for: %s") % str(self.settings.user.username)
        return str(obj_name)

    class Meta:
        db_table = settings.DB_TABLE_PREFIX + '_subscription'
        verbose_name = _('subscription')
        verbose_name_plural = _('subscriptions')


class Notification(models.Model):
    
    # Either set the subscription
    subscription = models.ForeignKey(
        Subscription,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        verbose_name=_(u"subscription"),
    )
    # Or the user to receive the notification
    user = models.ForeignKey(
        settings.USER_MODEL,
        null=True,
        blank=True,
        on_delete=models.CASCADE,  # If a user is deleted, remove all notifications
        verbose_name=_(u"user"),
    )
    message = models.TextField()
    url = models.CharField(
{'fraction_non_alphanumeric': 0.09651715717774065, 'fraction_numerical': 0.0023133273358180184, 'mean_word_length': 2.584523261169968, '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': '5407350', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2250, 'n_words': 502}