Created
May 10, 2012 16:43
-
-
Save evz/2654394 to your computer and use it in GitHub Desktop.
Terrace under the hood, part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AudioFile(models.Model): | |
audio = models.FileField(storage=cloudfiles_storage, upload_to='audio') | |
title = models.CharField(max_length=255) | |
description = models.TextField(blank=True) | |
duration = models.CharField(max_length=25, editable=False) | |
size = models.CharField(max_length=25, editable=False) | |
format = models.CharField(max_length=5, editable=False) | |
uploaded = models.DateTimeField(auto_now_add=True) | |
recorded = models.DateField() | |
tags = models.ManyToManyField(Tags) | |
def __unicode__(self): | |
return self.title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from hsaudiotag import auto | |
... | |
def sizeof_fmt(num): | |
for x in ['bytes','KB','MB','GB','TB']: | |
if num < 1024.0: | |
return "%3.1f%s" % (num, x) | |
num /= 1024.0 | |
def get_hms(seconds): | |
hours = seconds / 3600 | |
seconds -= 3600*hours | |
minutes = seconds / 60 | |
seconds -= 60*minutes | |
if hours is 0: | |
return "%02d:%02d" % (minutes, seconds) | |
return "%02d:%02d:%02d" % (hours, minutes, seconds) | |
def audio_save(request): | |
form = AudioFileForm(request.POST,request.FILES) | |
res = [] | |
if form.is_valid(): | |
obj = form.save(commit=False) | |
obj.duration = get_hms(auto.File(request.FILES['audio']).duration) | |
obj.size = sizeof_fmt(auto.File(request.FILES['audio']).size) | |
obj.format = request.FILES['audio'].name.rsplit('.', 1)[1] | |
obj.save() | |
form.save_m2m() | |
res.append({'title':obj.title}) | |
else: | |
res.append({'error':form.errors}) | |
return HttpResponse(json.dumps(res)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment