<commit_before>from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db import models
from django.db.models.signals import post_save, pre_save
from django.utils.translation import get_language, ugettext_lazy as _
from cms.models.pluginmodel import CMSPlugin
from cms.models import Page
from cms.utils import i18n
from filer.fields.image import FilerImageField
from filer.models import Image 
from storybase.managers import FeaturedManager
from storybase.utils import unique_slugify
from storybase.fields import ShortTextField
from storybase.models import (PermissionMixin, PublishedModel,
        TimestampedModel, TranslatedModel, TranslationModel, 
        set_date_on_published)
from storybase_user.utils import format_user_name
from cmsplugin_storybase.managers import TeaserManager

class List(CMSPlugin):
    num_items = models.IntegerField(default=3)


class NewsItemPermission(PermissionMixin):
    """Permissions for the NewsItem model"""
    def user_can_view(self, user):
        from storybase_user.utils import is_admin

        if self.status == 'published':
            return True

        if self.author == user:
            return True

        if user.is_superuser or is_admin(user):
            return True

        return False

    def anonymoususer_can_view(self, user):
        if self.status == 'published':
            return True

        return False


class NewsItemTranslation(TranslationModel):
    news_item = models.ForeignKey('NewsItem')
    title = ShortTextField(blank=True) 
    body = models.TextField(blank=True)
    image = FilerImageField(null=True)

    class Meta:
        """Model metadata options"""
        unique_together = (('news_item', 'language'))

    def __unicode__(self):
        return self.title


class NewsItem(NewsItemPermission, PublishedModel, TimestampedModel, 
        TranslatedModel):
    author = models.ForeignKey(User, related_name="news_items", blank=True,
                               null=True)
    slug = models.SlugField(blank=True)
    on_homepage = models.BooleanField(_("Featured on homepage"),
                                      default=False)

    objects = FeaturedManager()
        
    translated_fields = ['title', 'body', 'image',] 
    translation_set = 'newsitemtranslation_set'
    translation_class = NewsItemTranslation

    def get_absolute_url(self):
        kwargs = {'slug': self.slug}
        if self.published:
            kwargs['year'] = self.published.year
            kwargs['month'] = self.published.month
        else:
            kwargs['year'] = self.last_updated.year
            kwargs['month'] = self.last_updated.month

        # IMPORTANT: The AppHook needs to be connected in all languages
        # for the call to reverse() below to work correctly.
        # See http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks
        # and http://stackoverflow.com/questions/11216565/django-cms-urls-used-by-apphooks-dont-work-with-reverse-or-url
        try:
            return reverse('newsitem_detail', kwargs=kwargs)
        except NoReverseMatch:
            return None 

    def normalize_for_view(self, img_width):
        """Return attributes as a dictionary for use in a view context
        
        This allows using the same template across different models with
        differently-named attributes that hold similar information.

        """
        return {
            "type": _("News"),
            "title": self.title,
            "author": format_user_name(self.author),
            "date": self.created, 
            "image_html":' <img src="%s" />' % (self.image.url),
            "excerpt": self.body,
        }


def set_slug(sender, instance, **kwargs):
    try:
        if not instance.news_item.slug:
            unique_slugify(instance.news_item, instance.title)
        instance.news_item.save()
    except NewsItem.DoesNotExist:
        # Instance doesn't have a related News Item.
        # Encountered this when loading fixture
        pass


pre_save.connect(set_date_on_published, sender=NewsItem)<commit_msg>Return an empty string rather than None<commit_after>from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db import models
from django.db.models.signals import post_save, pre_save
from django.utils.translation import get_language, ugettext_lazy as _
from cms.models.pluginmodel import CMSPlugin
from cms.models import Page
from cms.utils import i18n
from filer.fields.image import FilerImageField
from filer.models import Image 
from storybase.managers import FeaturedManager
from storybase.utils import unique_slugify
from storybase.fields import ShortTextField
from storybase.models import (PermissionMixin, PublishedModel,
        TimestampedModel, TranslatedModel, TranslationModel, 
        set_date_on_published)
from storybase_user.utils import format_user_name
from cmsplugin_storybase.managers import TeaserManager

class List(CMSPlugin):
    num_items = models.IntegerField(default=3)


class NewsItemPermission(PermissionMixin):
    """Permissions for the NewsItem model"""
    def user_can_view(self, user):
        from storybase_user.utils import is_admin

        if self.status == 'published':
            return True

        if self.author == user:
            return True

        if user.is_superuser or is_admin(user):
            return True

        return False

    def anonymoususer_can_view(self, user):
        if self.status == 'published':
            return True

        return False


class NewsItemTranslation(TranslationModel):
    news_item = models.ForeignKey('NewsItem')
    title = ShortTextField(blank=True) 
    body = models.TextField(blank=True)
    image = FilerImageField(null=True)

    class Meta:
        """Model metadata options"""
        unique_together = (('news_item', 'language'))

    def __unicode__(self):
        return self.title


class NewsItem(NewsItemPermission, PublishedModel, TimestampedModel, 
        TranslatedModel):
    author = models.ForeignKey(User, related_name="news_items", blank=True,
                               null=True)
    slug = models.SlugField(blank=True)
    on_homepage = models.BooleanField(_("Featured on homepage"),
                                      default=False)

    objects = FeaturedManager()
        
    translated_fields = ['title', 'body', 'image',] 
    translation_set = 'newsitemtranslation_set'
    translation_class = NewsItemTranslation

    def get_absolute_url(self):
        kwargs = {'slug': self.slug}
        if self.published:
            kwargs['year'] = self.published.year
            kwargs['month'] = self.published.month
        else:
            kwargs['year'] = self.last_updated.year
            kwargs['month'] = self.last_updated.month

        # IMPORTANT: The AppHook needs to be connected in all languages
        # for the call to reverse() below to work correctly.
        # See http://docs.django-cms.org/en/2.1.3/extending_cms/app_integration.html#app-hooks
        # and http://stackoverflow.com/questions/11216565/django-cms-urls-used-by-apphooks-dont-work-with-reverse-or-url
        try:
            return reverse('newsitem_detail', kwargs=kwargs)
        except NoReverseMatch:
            return ""

    def normalize_for_view(self, img_width):
        """Return attributes as a dictionary for use in a view context
        
        This allows using the same template across different models with
        differently-named attributes that hold similar information.

        """
        return {
            "type": _("News"),
            "title": self.title,
            "author": format_user_name(self.author),
            "date": self.created, 
            "image_html":' <img src="%s" />' % (self.image.url),
            "excerpt": self.body,
        }


def set_slug(sender, instance, **kwargs):
    try:
        if not instance.news_item.slug:
            unique_slugify(instance.news_item, instance.title)
        instance.news_item.save()
    except NewsItem.DoesNotExist:
        # Instance doesn't have a related News Item.
        # Encountered this when loading fixture
        pass


pre_save.connect(set_date_on_published, sender=NewsItem)
{'fraction_non_alphanumeric': 0.08650314018248607, 'fraction_numerical': 0.0033179286645337127, 'mean_word_length': 2.8398544131028207, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4803782', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2273, 'n_words': 596}