<commit_before>from __future__ import division
from __future__ import unicode_literals

import hashlib
import re
from ast import literal_eval
from datetime import timedelta

from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
from django.utils.encoding import force_bytes, python_2_unicode_compatible
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _

import bleach
import mutagen

from .managers import EpisodeManager
from .utils import enclosure_file_path, enclosure_poster_path, episode_image_path, show_image_path, speaker_photo_path
from . import settings


@python_2_unicode_compatible
class Speaker(models.Model):
    name = models.CharField(_("Name"), max_length=255)
    email = models.EmailField(_("E-mail"), unique=True)
    photo = models.ImageField(_("Photo"), upload_to=speaker_photo_path, blank=True)
    bio = models.TextField(_("Bio"), blank=True)

    class Meta:
        ordering = ["name", "email"]
        verbose_name = _("speaker")
        verbose_name_plural = _("speakers")

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Category(models.Model):
    parent = models.ForeignKey("self", verbose_name=_("parent"), related_name=_("children"), null=True, blank=True, on_delete=models.CASCADE)
    title = models.CharField(_("title"), max_length=255)
    slug = models.SlugField(_("slug"), unique=True)
    full = models.CharField(_("full title"), max_length=255, editable=False)
    json = models.TextField(_("JSON"), editable=False)

    class Meta:
        ordering = ["full"]
        verbose_name = _("category")
        verbose_name_plural = _("categories")

    def __str__(self):
        return self.get_full(self)

    def save(self, *args, **kwargs):
        self.full = self.get_full(self)
        self.json = self.get_json(self)
        super(Category, self).save(*args, **kwargs)

    def get_full(self, obj):
        if obj.parent is None:
            return obj.title
        else:
            return "%s / %s" % (self.get_full(obj.parent), obj.title)

    def get_json(self, obj, children=False):
        if obj.parent is None:
            if not children:
                return '{"%s": []}' % obj.title
            else:
                return "%s" % obj.title
        else:
            return '{"%s": ["%s"]}' % (self.get_json(obj.parent, children=True), obj.title)


@python_2_unicode_compatible
class Show(models.Model):
    TYPE_CHOICES = (
        ("episodic", _("Episodic")),
        ("serial", _("Serial")),
    )
    type = models.CharField(_("type"), max_length=255, choices=TYPE_CHOICES, default="episodic", help_text=_("Episodic presents episodes by latest, serial presents episodes by episode number; both support seasons"))
    title = models.CharField(_("title"), max_length=255)
    slug = models.SlugField(_("slug"), unique=True)
    image = models.ImageField(_("image"), upload_to=show_image_path, blank=True, help_text=_("1400&times;1400&ndash;3000&times;3000px; 72DPI; JPG, PNG; RGB; if blank, default <a href=\"%s\">no artwork</a> is used") % staticfiles_storage.url(settings.PODCAST_NO_ARTWORK))
    description = models.TextField(_("description"), help_text=_("Accepts HTML"))
    subtitle = models.CharField(_("subtitle"), max_length=255, help_text=_("A single, descriptive sentence of the show"))
    summary = models.TextField(_("summary"), blank=True, max_length=4000, help_text=_("Max length of 4,000 characters; accepts HTML; if blank, uses show's description"))
    author_name = models.CharField(_("author name"), max_length=255, help_text=_("Appears as the \"artist\" of the podcast"))
    author_email = models.EmailField(_("author e-mail"))
    owner_name = models.CharField(_("owner name"), max_length=255, blank=True, help_text=_("Administrative contact of the podcast; if blank, uses author name"))
    owner_email = models.EmailField(_("owner e-mail"), blank=True, max_length=255, help_text=_("Administrative contact of the podcast; if blank, uses author email"))
    copyright = models.CharField(_("copyright"), max_length=255, blank=True, help_text=_("Organization name; &copy; and %s will be prepended automatically; if blank, uses show's title" % timezone.now().year))
    categories = models.ManyToManyField(Category, verbose_name=_("categories"), help_text=_("Please select parent category if selecting a subcategory, e.g. <strong>Arts</strong> if <strong>Arts / Design</strong>"))
    explicit = models.BooleanField(_("explicit?"), default=False, help_text=_("Indicates explicit language or adult content"))
    block = models.BooleanField(_("block?"), default=False, help_text=_("Prevents entire podcast from appearing on the iTunes Store"))
    complete = models.BooleanField(_("complete?"), default=False, help_text=_("Indicates entire podcast is complete and no future episodes will be created"))
    itunes = models.URLField(_("iTunes URL"), blank=True, help_text=_("Paste iTunes URL here after submission of show feed URL to <a href=\"https://podcastsconnect.apple.com/\">iTunes Connect</a>"))
    coming = models.BooleanField(_("coming"), default=False, help_text=_("Indicates whether users are coming from an old feed; if set, leave for four weeks; see <a href=\"https://help.apple.com/itc/podcasts_connect/#/itca489031e0\">documentation</a>"))
    going = models.URLField(_("going"), blank=True, help_text=_("Permanently redirect users to URL of a new feed; see <a href=\"https://help.apple.com/itc/podcasts_connect/#/itca489031e0\">documentation</a>"))
    hosts = models.ManyToManyField(Speaker, verbose_name=_("hosts"), blank=True)

    class Meta:
        ordering = ["title"]
        verbose_name = _("show")
        verbose_name_plural = _("shows")

    def __str__(self):
        return "%s" % self.title

    def get_absolute_url(self):
        if settings.PODCAST_SINGULAR:
            return reverse("podcast:show_detail")
        else:
            return reverse("podcast:show_detail", args=[self.slug])

    def get_absolute_feed_url(self):
        if settings.PODCAST_SINGULAR:
            return reverse("podcast:show_feed")
        else:
            return reverse("podcast:show_feed", args=[self.slug])

    def get_image_url(self):
        if self.image:
            return self.image.url
        else:
            return staticfiles_storage.url(settings.PODCAST_NO_ARTWORK)

    def get_subtitle(self):
        text = self.subtitle
        tags = []
        return bleach.clean(text, tags=tags, strip=True)

    def get_summary(self):
        text = self.summary if self.summary else self.description
        tags = settings.PODCAST_ALLOWED_TAGS
        return bleach.clean(text, tags=tags, strip=True)

    def get_owner_name(self):
        return self.owner_name if self.owner_name else self.author_name

    def get_owner_email(self):
        return self.owner_email if self.owner_email else self.author_email

    def get_categories_dict(self):
        old_dicts = [literal_eval(c.json) for c in self.categories.all()]
        new_dict = {}
        for old_dict in old_dicts:
            try:
                for key, value in old_dict.iteritems():
                    if key not in new_dict:
                        new_dict[key] = value
                    else:
                        for sub in value:
                            new_dict[key].append(sub)
            except AttributeError:  # Python 3
                for key, value in old_dict.items():
                    if key not in new_dict:
                        new_dict[key] = value
                    else:
                        for sub in value:
                            new_dict[key].append(sub)
        try:
            return sorted(new_dict.iteritems())
        except AttributeError:  # Python 3
            return sorted(new_dict.items())

    def get_explicit(self):
        return "yes" if self.explicit else "no"

    def get_block(self):
        return "yes" if self.block else "no"
<commit_msg>Condensed model method try/except<commit_after>from __future__ import division
from __future__ import unicode_literals

import hashlib
import re
from ast import literal_eval
from datetime import timedelta

from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.db import models
from django.utils import timezone
from django.utils.encoding import force_bytes, python_2_unicode_compatible
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _

import bleach
import mutagen

from .managers import EpisodeManager
from .utils import enclosure_file_path, enclosure_poster_path, episode_image_path, show_image_path, speaker_photo_path
from . import settings


@python_2_unicode_compatible
class Speaker(models.Model):
    name = models.CharField(_("Name"), max_length=255)
    email = models.EmailField(_("E-mail"), unique=True)
    photo = models.ImageField(_("Photo"), upload_to=speaker_photo_path, blank=True)
    bio = models.TextField(_("Bio"), blank=True)

    class Meta:
        ordering = ["name", "email"]
        verbose_name = _("speaker")
        verbose_name_plural = _("speakers")

    def __str__(self):
        return self.name


@python_2_unicode_compatible
class Category(models.Model):
    parent = models.ForeignKey("self", verbose_name=_("parent"), related_name=_("children"), null=True, blank=True, on_delete=models.CASCADE)
    title = models.CharField(_("title"), max_length=255)
    slug = models.SlugField(_("slug"), unique=True)
    full = models.CharField(_("full title"), max_length=255, editable=False)
    json = models.TextField(_("JSON"), editable=False)

    class Meta:
        ordering = ["full"]
        verbose_name = _("category")
        verbose_name_plural = _("categories")

    def __str__(self):
        return self.get_full(self)

    def save(self, *args, **kwargs):
        self.full = self.get_full(self)
        self.json = self.get_json(self)
        super(Category, self).save(*args, **kwargs)

    def get_full(self, obj):
        if obj.parent is None:
            return obj.title
        else:
            return "%s / %s" % (self.get_full(obj.parent), obj.title)

    def get_json(self, obj, children=False):
        if obj.parent is None:
            if not children:
                return '{"%s": []}' % obj.title
            else:
                return "%s" % obj.title
        else:
            return '{"%s": ["%s"]}' % (self.get_json(obj.parent, children=True), obj.title)


@python_2_unicode_compatible
class Show(models.Model):
    TYPE_CHOICES = (
        ("episodic", _("Episodic")),
        ("serial", _("Serial")),
    )
    type = models.CharField(_("type"), max_length=255, choices=TYPE_CHOICES, default="episodic", help_text=_("Episodic presents episodes by latest, serial presents episodes by episode number; both support seasons"))
    title = models.CharField(_("title"), max_length=255)
    slug = models.SlugField(_("slug"), unique=True)
    image = models.ImageField(_("image"), upload_to=show_image_path, blank=True, help_text=_("1400&times;1400&ndash;3000&times;3000px; 72DPI; JPG, PNG; RGB; if blank, default <a href=\"%s\">no artwork</a> is used") % staticfiles_storage.url(settings.PODCAST_NO_ARTWORK))
    description = models.TextField(_("description"), help_text=_("Accepts HTML"))
    subtitle = models.CharField(_("subtitle"), max_length=255, help_text=_("A single, descriptive sentence of the show"))
    summary = models.TextField(_("summary"), blank=True, max_length=4000, help_text=_("Max length of 4,000 characters; accepts HTML; if blank, uses show's description"))
    author_name = models.CharField(_("author name"), max_length=255, help_text=_("Appears as the \"artist\" of the podcast"))
    author_email = models.EmailField(_("author e-mail"))
    owner_name = models.CharField(_("owner name"), max_length=255, blank=True, help_text=_("Administrative contact of the podcast; if blank, uses author name"))
    owner_email = models.EmailField(_("owner e-mail"), blank=True, max_length=255, help_text=_("Administrative contact of the podcast; if blank, uses author email"))
    copyright = models.CharField(_("copyright"), max_length=255, blank=True, help_text=_("Organization name; &copy; and %s will be prepended automatically; if blank, uses show's title" % timezone.now().year))
    categories = models.ManyToManyField(Category, verbose_name=_("categories"), help_text=_("Please select parent category if selecting a subcategory, e.g. <strong>Arts</strong> if <strong>Arts / Design</strong>"))
    explicit = models.BooleanField(_("explicit?"), default=False, help_text=_("Indicates explicit language or adult content"))
    block = models.BooleanField(_("block?"), default=False, help_text=_("Prevents entire podcast from appearing on the iTunes Store"))
    complete = models.BooleanField(_("complete?"), default=False, help_text=_("Indicates entire podcast is complete and no future episodes will be created"))
    itunes = models.URLField(_("iTunes URL"), blank=True, help_text=_("Paste iTunes URL here after submission of show feed URL to <a href=\"https://podcastsconnect.apple.com/\">iTunes Connect</a>"))
    coming = models.BooleanField(_("coming"), default=False, help_text=_("Indicates whether users are coming from an old feed; if set, leave for four weeks; see <a href=\"https://help.apple.com/itc/podcasts_connect/#/itca489031e0\">documentation</a>"))
    going = models.URLField(_("going"), blank=True, help_text=_("Permanently redirect users to URL of a new feed; see <a href=\"https://help.apple.com/itc/podcasts_connect/#/itca489031e0\">documentation</a>"))
    hosts = models.ManyToManyField(Speaker, verbose_name=_("hosts"), blank=True)

    class Meta:
        ordering = ["title"]
        verbose_name = _("show")
        verbose_name_plural = _("shows")

    def __str__(self):
        return "%s" % self.title

    def get_absolute_url(self):
        if settings.PODCAST_SINGULAR:
            return reverse("podcast:show_detail")
        else:
            return reverse("podcast:show_detail", args=[self.slug])

    def get_absolute_feed_url(self):
        if settings.PODCAST_SINGULAR:
            return reverse("podcast:show_feed")
        else:
            return reverse("podcast:show_feed", args=[self.slug])

    def get_image_url(self):
        if self.image:
            return self.image.url
        else:
            return staticfiles_storage.url(settings.PODCAST_NO_ARTWORK)

    def get_subtitle(self):
        text = self.subtitle
        tags = []
        return bleach.clean(text, tags=tags, strip=True)

    def get_summary(self):
        text = self.summary if self.summary else self.description
        tags = settings.PODCAST_ALLOWED_TAGS
        return bleach.clean(text, tags=tags, strip=True)

    def get_owner_name(self):
        return self.owner_name if self.owner_name else self.author_name

    def get_owner_email(self):
        return self.owner_email if self.owner_email else self.author_email

    def get_categories_dict(self):
        old_dicts = [literal_eval(c.json) for c in self.categories.all()]
        new_dict = {}
        for old_dict in old_dicts:
            try:
                o = old_dict.iteritems()
            except AttributeError:  # Python 3
                o = old_dict.items()
            for key, value in o:
                if key not in new_dict:
                    new_dict[key] = value
                else:
                    for sub in value:
                        new_dict[key].append(sub)
        try:
            n = new_dict.iteritems()
        except AttributeError:  # Python 3
            n = new_dict.items()
        return sorted(n)

    def get_explicit(self):
        return "yes" if self.explicit else "no"

    def get_block(self):
        return "yes" if self.block else "no"

{'fraction_non_alphanumeric': 0.1197165589259075, 'fraction_numerical': 0.009448035803083043, 'mean_word_length': 3.460493484890491, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 6, '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': '3695267', 'n_tokens_mistral': 4887, 'n_tokens_neox': 4691, 'n_words': 1197}