from django.http import HttpResponse, HttpResponseRedirect
from django.utils import simplejson as json

from speeches.forms import SpeechForm, SpeechAudioForm
from speeches.models import Speech
from speeches.tasks import transcribe_speech
from django.views.generic import CreateView, UpdateView, DetailView, ListView
from django.views.generic.edit import BaseFormView

import celery
import logging

logger = logging.getLogger(__name__)

# Base as don't want template mixin. I find CBVs confusing :-/
class SpeechAudioCreate(BaseFormView):
    form_class = SpeechAudioForm

    def render_to_response(self, context, **kwargs):
        form = context.pop('form', None) # Don't care about the form
        data = json.dumps(context)
        kwargs['content_type'] = 'application/json'
        return HttpResponse(data, **kwargs)

    def form_valid(self, form):
        # The cleaned_data contains the TemporaryUploadedFile (File/UploadedFile subclass).
        # The form.instance contains the FieldFile which the magic save() we want.
        audio = form.cleaned_data['audio']
        form.instance.audio.save(audio.name, audio, save=False)
        return self.render_to_response({ 'status': 'done', 'filename': form.instance.audio.name })

    def form_invalid(self, form):
        return self.render_to_response({ 'error': form.errors['audio'] })

class SpeechCreate(CreateView):
    model = Speech
    form_class = SpeechForm

    def form_valid(self, form):
        # Do things with audio here...

        # First save the form - we can't let the super-class do it because
        # we need to add some stuff to the object afterwards
        self.object = form.save()

        # Now set off a Celery task to transcribe the audio for this speech
        speech = self.object
        print("Speech retrieved: {0}".format(speech.id))
        # If someone is adding a new audio file and there's already a task
        # We need to clear it
        if speech.celery_task_id:   
            print("Removing existing task {0} for speech {1}".format(speech.id, speech.celery_task_id))
            celery.task.control.revoke(speech.celery_task_id)
        # Now we can start a new one
        result = transcribe_speech.delay(speech.id)
        print("New task result is: {0}".format(result))

        # Finally, we can remember the new task in the model
        speech.celery_task_id = result.task_id
        speech.save()
        
        return HttpResponseRedirect(self.get_success_url())

class SpeechUpdate(UpdateView):
    model = Speech
    form_class = SpeechForm

class SpeechView(DetailView):
    model = Speech

class SpeechList(ListView):
    model = Speech

@@ -1738,24 +1738,48 @@
 this speech%0A
+        # if we need to%0A
         spee
@@ -1807,57 +1807,32 @@
     
-print(%22Speech retrieved: %7B0%7D%22.format(speech.id))%0A
+if not speech.text:%0A    
     
@@ -1910,16 +1910,20 @@
         
+    
 # We nee
@@ -1944,16 +1944,20 @@
         
+  
+  
 if speec
@@ -1985,108 +1985,8 @@
     
-        print(%22Removing existing task %7B0%7D for speech %7B1%7D%22.format(speech.id, speech.celery_task_id))%0A
     
@@ -2047,24 +2047,28 @@
 id)%0A        
+    
 # Now we can
@@ -2092,16 +2092,20 @@
         
+   
+ 
 result =
@@ -2148,61 +2148,8 @@
     
-    print(%22New task result is: %7B0%7D%22.format(result))%0A%0A
     
@@ -2209,24 +2209,28 @@
 del%0A        
+    
 speech.celer
@@ -2256,16 +2256,20 @@
 task_id%0A
+    
         

Make form submission check speech text before transcribing