<commit_before>import os
import rfc822
from datetime import datetime
import urlparse

from sorl.thumbnail import get_thumbnail
from django.contrib.sites.models import Site
from django.db import models
from django.db.models import Count

from core.utils import url
from spa.models.Genre import Genre
from spa.models.MixPlay import MixPlay
from spa.models.MixDownload import MixDownload
from dss import settings, localsettings
from spa.models.UserProfile import UserProfile
from spa.models._BaseModel import _BaseModel
from core.utils.file import generate_save_file_name


def mix_file_name(instance, filename):
    return generate_save_file_name(instance.uid, 'mixes', filename)


def mix_image_name(instance, filename):
    ret = generate_save_file_name(instance.uid, 'mix-images', filename)
    return ret


class Mix(_BaseModel):
    class Meta:
        app_label = 'spa'

    title = models.CharField(max_length=50)
    description = models.TextField()
    upload_date = models.DateTimeField(default=datetime.now())
    mix_image = models.ImageField(blank=True, upload_to=mix_image_name)
    local_file = models.FileField(blank=True, upload_to=mix_file_name)
    download_url = models.CharField(max_length=255)
    stream_url = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_featured = models.BooleanField(default=True)
    user = models.ForeignKey(UserProfile, editable=False)
    waveform_generated = models.BooleanField(default=False)
    uid = models.CharField(max_length=38, blank=True, unique=True)
    download_allowed = models.BooleanField(default=False)
    duration = models.IntegerField(null=True, blank=True)

    genres = models.ManyToManyField(Genre)

    def __unicode__(self):
        return self.title

    def save(self, force_insert=False, force_update=False, using=None):
        #turn away now - horrid hack to strip media root url
        #from image - will sort when I've figured backbone out better
        if self.mix_image.name.startswith(settings.MEDIA_URL):
            self.mix_image.name = self.mix_image.name[len(settings.MEDIA_URL):len(self.mix_image.name)]

        #Check for the unlikely event that the waveform has been generated
        if os.path.isfile(self.get_waveform_path()):
            self.waveform_generated = True

        self.clean_image('mix_image', Mix)
        super(Mix, self).save(force_insert, force_update, using)

    def get_absolute_path(self, prefix=""):
        fileName, extension = os.path.splitext(self.local_file.name)
        if extension == "" or extension == ".":
            extension = ".mp3"
        return '%s/mixes/%s%s%s' % (settings.MEDIA_ROOT, prefix, self.uid, extension)

    def get_absolute_url(self):
        return '/mix/%i' % self.id

    def get_full_url(self):
        return 'http://%s%s' % (Site.objects.get_current().domain, self.get_absolute_url())

    def get_download_url(self):
        return 'http://%s/audio/download/%s' % (Site.objects.get_current().domain, self.pk)

    def get_waveform_path(self):
        return os.path.join(settings.MEDIA_ROOT, "waveforms/", "%s.%s" % (self.uid, "png"))

    def get_waveform_url(self):
        if self.waveform_generated:
            waveform_root = localsettings.WAVEFORM_URL if hasattr(localsettings,
                                                                  'WAVEFORM_URL') else "%swaveforms" % settings.MEDIA_URL
            ret = "%s/%s.%s" % (waveform_root, self.uid, "png")
            return url.urlclean(ret)

        else:
            return urlparse.urljoin(settings.STATIC_URL, "img/waveform-processing.gif")

    def _get_social_image(self):
        if self.user:
            return self.user.get_medium_profile_image()
        return None

    def get_image_url(self):
        try:
            ret = get_thumbnail(self.mix_image, '160x160', crop='center')
            return "%s/%s" % (settings.MEDIA_URL, ret.name)
        except Exception:
            social_image = self._get_social_image()
            if social_image:
                return social_image

        return super(Mix, self).get_image_url(self.mix_image, settings.STATIC_URL + 'img/default-track.png')
<commit_msg>Display placeholder waveform for missing image<commit_after>import os
import rfc822
from datetime import datetime
import urlparse

from sorl.thumbnail import get_thumbnail
from django.contrib.sites.models import Site
from django.db import models
from django.db.models import Count

from core.utils import url
from spa.models.Genre import Genre
from spa.models.MixPlay import MixPlay
from spa.models.MixDownload import MixDownload
from dss import settings, localsettings
from spa.models.UserProfile import UserProfile
from spa.models._BaseModel import _BaseModel
from core.utils.file import generate_save_file_name


def mix_file_name(instance, filename):
    return generate_save_file_name(instance.uid, 'mixes', filename)


def mix_image_name(instance, filename):
    ret = generate_save_file_name(instance.uid, 'mix-images', filename)
    return ret


class Mix(_BaseModel):
    class Meta:
        app_label = 'spa'

    title = models.CharField(max_length=50)
    description = models.TextField()
    upload_date = models.DateTimeField(default=datetime.now())
    mix_image = models.ImageField(blank=True, upload_to=mix_image_name)
    local_file = models.FileField(blank=True, upload_to=mix_file_name)
    download_url = models.CharField(max_length=255)
    stream_url = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_featured = models.BooleanField(default=True)
    user = models.ForeignKey(UserProfile, editable=False)
    waveform_generated = models.BooleanField(default=False)
    uid = models.CharField(max_length=38, blank=True, unique=True)
    download_allowed = models.BooleanField(default=False)
    duration = models.IntegerField(null=True, blank=True)

    genres = models.ManyToManyField(Genre)

    def __unicode__(self):
        return self.title

    def save(self, force_insert=False, force_update=False, using=None):
        #turn away now - horrid hack to strip media root url
        #from image - will sort when I've figured backbone out better
        if self.mix_image.name.startswith(settings.MEDIA_URL):
            self.mix_image.name = self.mix_image.name[len(settings.MEDIA_URL):len(self.mix_image.name)]

        #Check for the unlikely event that the waveform has been generated
        if os.path.isfile(self.get_waveform_path()):
            self.waveform_generated = True

        self.clean_image('mix_image', Mix)
        super(Mix, self).save(force_insert, force_update, using)

    def get_absolute_path(self, prefix=""):
        fileName, extension = os.path.splitext(self.local_file.name)
        if extension == "" or extension == ".":
            extension = ".mp3"
        return '%s/mixes/%s%s%s' % (settings.MEDIA_ROOT, prefix, self.uid, extension)

    def get_absolute_url(self):
        return '/mix/%i' % self.id

    def get_full_url(self):
        return 'http://%s%s' % (Site.objects.get_current().domain, self.get_absolute_url())

    def get_download_url(self):
        return 'http://%s/audio/download/%s' % (Site.objects.get_current().domain, self.pk)

    def get_waveform_path(self):
        return os.path.join(settings.MEDIA_ROOT, "waveforms/", "%s.%s" % (self.uid, "png"))

    def get_waveform_url(self):

        if self.waveform_generated and os.file.exists(self.get_waveform_path()):

            waveform_root = localsettings.WAVEFORM_URL if hasattr(localsettings,
                                                                  'WAVEFORM_URL') else "%swaveforms" % settings.MEDIA_URL
            ret = "%s/%s.%s" % (waveform_root, self.uid, "png")
            return url.urlclean(ret)

        else:
            return urlparse.urljoin(settings.STATIC_URL, "img/waveform-processing.gif")

    def _get_social_image(self):
        if self.user:
            return self.user.get_medium_profile_image()
        return None

    def get_image_url(self):
        try:
            ret = get_thumbnail(self.mix_image, '160x160', crop='center')
            return "%s/%s" % (settings.MEDIA_URL, ret.name)
        except Exception:
            social_image = self._get_social_image()
            if social_image:
                return social_image

        return super(Mix, self).get_image_url(self.mix_image, settings.STATIC_URL + 'img/default-track.png')

{'fraction_non_alphanumeric': 0.10376242309512541, 'fraction_numerical': 0.00473260766682442, 'mean_word_length': 3.293042153377349, 'pattern_counts': {'":': 2, '<': 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': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1630945', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2523, 'n_words': 545}