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("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") 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
    
@@ -1547,18 +1547,89 @@
  key=key
+,%0A                           title=username + %22 %22 + date.strftime(%22%25x%22)
 )%0A
-
         
@@ -2093,16 +2093,21 @@
 Model):%0A
+    %0A
     key 
@@ -2156,16 +2156,76 @@
 =True)%0A%0A
+    # Automatically generated metadata, user may not change%0A
     gene
@@ -2330,113 +2330,8 @@
 40)%0A
-    %0A    user_nickname = models.CharField(max_length=40)%0A    public = models.BooleanField(default=False)%0A
     
@@ -2431,24 +2431,355 @@
 ult=0)%0A    %0A
+    # User editable fields%0A    user_nickname = models.CharField(max_length=40)%0A    public = models.BooleanField(default=False, help_text=%22Share the song everyone? If unchecked, you still get a private link for sharing with friends.%22)%0A    title = models.CharField(max_length=100)%0A    description = models.TextField(blank=True)%0A    %0A
     song_fil
@@ -2899,26 +2899,18 @@
 rn self.
-user_nicknam
+titl
 e%0A    

Add model for storing songs