<commit_before>import tempfile
import os
import random

from django.core.files.base import File
from django.db import models
from django.conf import settings


class SongManager(models.Manager):
    def make_song(self, username, date, access_token):
        """Creates a new song by fetching users data from Beddit and
        storing it to mp3 file.
        """
        output_file_name = tempfile.mktemp(suffix=".mp3")
        command_template = "/home/mikko/DreamsToMusic/dreamstomusic %s %s %s %s"
        command = command_template % (username,
                                      date.strftime("%Y-%m-%d"),
                                      access_token,
                                      output_file_name)
#        command = "source /home/mikko/.bash_profile && workon dreamstomusic && %s --username %s --date %s --token %s %s" % (settings.SLEEP_TO_MUSIC_BIN,
#                                                                username,
#                                                                date.strftime("%Y-%m-%d"),
#                                                                access_token,
#                                                                output_file_name)
        ret = os.system(command)
        
        print "RETURN CODE",ret
        
        key = unicode(''.join([random.choice("<KEY>") for i in range(12)]))
        
        song = self.create(user_nickname=username,
                           beddit_username=username,
                           key=key)
        
        with open(output_file_name) as outfile:
            song.song_file.save(key + ".mp3", File(outfile))
            
        song.save()
        
        return song
    
    def get_latest_public_songs(self):
        return self.filter(public=True).order_by("-generated")
    
    def get_number_of_public_songs(self):
        return self.filter(public=True).count()
    
    def get_my_songs(self, beddit_username):
        pass


class Song(models.Model):
    key = models.CharField(max_length=20, db_index=True)

    generated = models.DateTimeField(auto_now_add=True)
    beddit_username = models.CharField(max_length=40)
    
    user_nickname = models.CharField(max_length=40)
    public = models.BooleanField(default=False)
    times_listened = models.IntegerField(default=0)
    length_seconds = models.IntegerField(default=0)
    
    song_file = models.FileField(upload_to="songs")

    objects = SongManager()
    
    def __unicode__(self):
        return self.user_nickname
    <commit_msg>Add model for storing songs<commit_after>import tempfile
import os
import random

from django.core.files.base import File
from django.db import models
from django.conf import settings


class SongManager(models.Manager):
    def make_song(self, username, date, access_token):
        """Creates a new song by fetching users data from Beddit and
        storing it to mp3 file.
        """
        output_file_name = tempfile.mktemp(suffix=".mp3")
        command_template = "/home/mikko/DreamsToMusic/dreamstomusic %s %s %s %s"
        command = command_template % (username,
                                      date.strftime("%Y-%m-%d"),
                                      access_token,
                                      output_file_name)
#        command = "source /home/mikko/.bash_profile && workon dreamstomusic && %s --username %s --date %s --token %s %s" % (settings.SLEEP_TO_MUSIC_BIN,
#                                                                username,
#                                                                date.strftime("%Y-%m-%d"),
#                                                                access_token,
#                                                                output_file_name)
        ret = os.system(command)
        
        print "RETURN CODE",ret
        
        key = unicode(''.join([random.choice("<KEY>") for i in range(12)]))
        
        song = self.create(user_nickname=username,
                           beddit_username=username,
                           key=key,
                           title=username + " " + date.strftime("%x"))
        
        with open(output_file_name) as outfile:
            song.song_file.save(key + ".mp3", File(outfile))
            
        song.save()
        
        return song
    
    def get_latest_public_songs(self):
        return self.filter(public=True).order_by("-generated")
    
    def get_number_of_public_songs(self):
        return self.filter(public=True).count()
    
    def get_my_songs(self, beddit_username):
        pass


class Song(models.Model):
    
    key = models.CharField(max_length=20, db_index=True)

    # Automatically generated metadata, user may not change
    generated = models.DateTimeField(auto_now_add=True)
    beddit_username = models.CharField(max_length=40)
    times_listened = models.IntegerField(default=0)
    length_seconds = models.IntegerField(default=0)
    
    # User editable fields
    user_nickname = models.CharField(max_length=40)
    public = models.BooleanField(default=False, help_text="Share the song everyone? If unchecked, you still get a private link for sharing with friends.")
    title = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    
    song_file = models.FileField(upload_to="songs")

    objects = SongManager()
    
    def __unicode__(self):
        return self.title
    
{'fraction_non_alphanumeric': 0.09425625920471281, 'fraction_numerical': 0.005338733431516937, 'mean_word_length': 1.7342727730246603, 'pattern_counts': {'":': 0, '<': 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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4963910', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1446, 'n_words': 306}