<commit_before>from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _, ugettext
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.contrib.redirects.models import Redirect
from django.core.validators import validate_slug
from django.core.exceptions import ValidationError

from app_data import AppDataField

from ella.core.box import Box
from ella.core.cache import CachedGenericForeignKey, \
    CachedForeignKey, ContentTypeForeignKey, CategoryForeignKey
from ella.core.conf import core_settings
from ella.core.managers import ListingManager, RelatedManager, \
    PublishableManager
from ella.core.models.main import Author, Source
from ella.core.signals import content_published, content_unpublished
from ella.photos.models import Photo
from ella.utils.timezone import now


def PublishableBox(publishable, box_type, nodelist, model=None):
    "add some content type info of self.target"
    if not model:
        model = publishable.content_type.model_class()
    box_class = model.box_class

    if box_class == PublishableBox:
        box_class = Box
    return box_class(publishable, box_type, nodelist, model=model)


class Publishable(models.Model):
    """
    Base class for all objects that can be published in Ella.
    """
    box_class = staticmethod(PublishableBox)

    content_type = ContentTypeForeignKey(editable=False)
    target = CachedGenericForeignKey('content_type', 'id')

    category = CategoryForeignKey(verbose_name=_('Category'))

    # Titles
    title = models.CharField(_('Title'), max_length=255)
    slug = models.SlugField(_('Slug'), max_length=255, validators=[validate_slug])

    # Authors and Sources
    authors = models.ManyToManyField(Author, verbose_name=_('Authors'))
    source = models.ForeignKey(Source, blank=True, null=True,
        verbose_name=_('Source'), on_delete=models.SET_NULL)

    # Main Photo
    photo = CachedForeignKey(Photo, blank=True, null=True, on_delete=models.SET_NULL,
        verbose_name=_('Photo'))

    # Description
    description = models.TextField(_('Description'), blank=True)

    # Publish data
    published = models.BooleanField(_('Published'))
    publish_from = models.DateTimeField(_('Publish from'),
        default=core_settings.PUBLISH_FROM_WHEN_EMPTY, db_index=True)
    publish_to = models.DateTimeField(_("End of visibility"), null=True, blank=True)
    static = models.BooleanField(_('static'), default=False)

    # Last updated
    last_updated = models.DateTimeField(_('Last updated'), blank=True)

    # generic JSON field to store app cpecific data
    app_data = AppDataField(default='{}', editable=False)

    # has the content_published signal been sent for this instance?
    announced = models.BooleanField(help_text='Publish signal sent', default=False, editable=False)

    objects = PublishableManager()

    class Meta:
        app_label = 'core'
        verbose_name = _('Publishable object')<commit_msg>changed ForeignKey to CachedForeignKey for source<commit_after>from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _, ugettext
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.contrib.redirects.models import Redirect
from django.core.validators import validate_slug
from django.core.exceptions import ValidationError

from app_data import AppDataField

from ella.core.box import Box
from ella.core.cache import CachedGenericForeignKey, \
    CachedForeignKey, ContentTypeForeignKey, CategoryForeignKey
from ella.core.conf import core_settings
from ella.core.managers import ListingManager, RelatedManager, \
    PublishableManager
from ella.core.models.main import Author, Source
from ella.core.signals import content_published, content_unpublished
from ella.photos.models import Photo
from ella.utils.timezone import now


def PublishableBox(publishable, box_type, nodelist, model=None):
    "add some content type info of self.target"
    if not model:
        model = publishable.content_type.model_class()
    box_class = model.box_class

    if box_class == PublishableBox:
        box_class = Box
    return box_class(publishable, box_type, nodelist, model=model)


class Publishable(models.Model):
    """
    Base class for all objects that can be published in Ella.
    """
    box_class = staticmethod(PublishableBox)

    content_type = ContentTypeForeignKey(editable=False)
    target = CachedGenericForeignKey('content_type', 'id')

    category = CategoryForeignKey(verbose_name=_('Category'))

    # Titles
    title = models.CharField(_('Title'), max_length=255)
    slug = models.SlugField(_('Slug'), max_length=255, validators=[validate_slug])

    # Authors and Sources
    authors = models.ManyToManyField(Author, verbose_name=_('Authors'))
    source = CachedForeignKey(Source, blank=True, null=True,
        verbose_name=_('Source'), on_delete=models.SET_NULL)

    # Main Photo
    photo = CachedForeignKey(Photo, blank=True, null=True, on_delete=models.SET_NULL,
        verbose_name=_('Photo'))

    # Description
    description = models.TextField(_('Description'), blank=True)

    # Publish data
    published = models.BooleanField(_('Published'))
    publish_from = models.DateTimeField(_('Publish from'),
        default=core_settings.PUBLISH_FROM_WHEN_EMPTY, db_index=True)
    publish_to = models.DateTimeField(_("End of visibility"), null=True, blank=True)
    static = models.BooleanField(_('static'), default=False)

    # Last updated
    last_updated = models.DateTimeField(_('Last updated'), blank=True)

    # generic JSON field to store app cpecific data
    app_data = AppDataField(default='{}', editable=False)

    # has the content_published signal been sent for this instance?
    announced = models.BooleanField(help_text='Publish signal sent', default=False, editable=False)

    objects = PublishableManager()

    class Meta:
        app_label = 'core'
        verbose_name = _('Publishable object')
{'fraction_non_alphanumeric': 0.09129511677282377, 'fraction_numerical': 0.0019598236158745713, 'mean_word_length': 4.718020541549953, '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': '903814', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1711, 'n_words': 442}