changeset 300:4f221f34bdc7

browser_side (plugins radiocol, xep-0054): handle upload errors
author souliane <souliane@mailoo.org>
date Tue, 17 Dec 2013 19:37:47 +0100
parents e4f586fc6101
children 5943eaa6f422
files browser_side/menu.py browser_side/radiocol.py libervia.tac
diffstat 3 files changed, 58 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/menu.py	Tue Dec 24 02:00:30 2013 +0100
+++ b/browser_side/menu.py	Tue Dec 17 19:37:47 2013 +0100
@@ -124,8 +124,12 @@
     def onSubmitComplete(self, event):
         result = event.getResults()
         if result != "OK":
-            Window.alert('Something went wrong while submitting file')
-        self.close_cb()
+            Window.alert("Can't open image... did you actually submit an image?")
+            self.message.setHTML('Please select another image file')
+            self.upload_btn.setEnabled(True)
+        else:
+            Window.alert("Your new profile picture has been set!")
+            self.close_cb()
 
 
 class Menu(SimplePanel):
--- a/browser_side/radiocol.py	Tue Dec 24 02:00:30 2013 +0100
+++ b/browser_side/radiocol.py	Tue Dec 17 19:37:47 2013 +0100
@@ -81,6 +81,7 @@
         self.setEncoding(FormPanel.ENCODING_MULTIPART)
         self.setMethod(FormPanel.METHOD_POST)
         self.setAction("upload_radiocol")
+        self.timer_on = False
         self._parent = parent
         vPanel = VerticalPanel()
 
@@ -105,6 +106,8 @@
         self.addFormHandler(self)
 
     def updateStatus(self):
+        if self.timer_on:
+            return
         # TODO: the status should be different if a song is being played or not
         queue = self._parent.getQueueSize()
         queue_data = self._parent.queue_data
@@ -138,7 +141,13 @@
     def setTemporaryStatus(self, text, style):
         self.status.setText(text)
         self.status.setStyleName('radiocol_upload_status_%s' % style)
-        Timer(5000, self.updateStatus)
+        self.timer_on = True
+
+        def cb():
+            self.timer_on = False
+            self.updateStatus()
+
+        Timer(5000, cb)
 
     def onSubmitComplete(self, event):
         result = event.getResults()
@@ -147,6 +156,10 @@
             self.setTemporaryStatus('[Your song has been submitted to the radio]', "ok")
         elif result == "KO":
             self.setTemporaryStatus('[Something went wrong during your song upload]', "ko")
+            self._parent.radiocolSongRejected("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable")
+            # TODO: would be great to re-use the original Exception class and message
+            # but it is lost in the middle of the traceback and encapsulated within
+            # a DBusException instance --> extract the data from the traceback?
         else:
             Window.alert('Submit error: %s' % result)
             self.status.setText('')
--- a/libervia.tac	Tue Dec 24 02:00:30 2013 +0100
+++ b/libervia.tac	Tue Dec 17 19:37:47 2013 +0100
@@ -846,7 +846,12 @@
         raise NotImplementedError
 
     def _fileWritten(self, request, filepath):
-        """Called once the file is actually written on disk"""
+        """Called once the file is actually written on disk
+        @param request: HTTP request object
+        @param filepath: full filepath on the server
+        @return: a tuple with the name of the async bridge method
+        to be called followed by its arguments.
+        """
         raise NotImplementedError
 
     def render(self, request):
@@ -866,10 +871,21 @@
 
         with open(filepath,'w') as f:
             f.write(request.args[self.NAME][0])
-        self._fileWritten(request, filepath)
-        #end = time.time()
-        #print "time spent in render: %fs" % (end - start)
-        return "OK"
+
+        def finish(d):
+            #end = time.time()
+            #print "time spent in render: %fs" % (end - start)
+            error = isinstance(d, Exception) or isinstance (d, Failure)
+            request.write('KO' if error else 'OK')
+            # TODO: would be great to re-use the original Exception class and message
+            # but it is lost in the middle of the backtrace and encapsulated within
+            # a DBusException instance --> extract the data from the backtrace?
+            request.finish()
+
+        d = JSONRPCMethodManager(self.sat_host).asyncBridgeCall(*self._fileWritten(request, filepath))
+        d.addCallbacks(lambda d: finish(d), lambda failure: finish(failure))
+        return server.NOT_DONE_YET
+
 
 class UploadManagerRadioCol(UploadManager):
     NAME = 'song'
@@ -878,9 +894,15 @@
         return "%s.ogg" % str(uuid.uuid4()) #XXX: chromium doesn't seem to play song without the .ogg extension, even with audio/ogg mime-type
 
     def _fileWritten(self, request, filepath):
-        """Called once the file is actually written on disk"""
+        """Called once the file is actually written on disk
+        @param request: HTTP request object
+        @param filepath: full filepath on the server
+        @return: a tuple with the name of the async bridge method
+        to be called followed by its arguments.
+        """
         profile = ISATSession(request.getSession()).profile
-        self.sat_host.bridge.radiocolSongAdded(request.args['referee'][0], filepath, profile)
+        return ("radiocolSongAdded", request.args['referee'][0], filepath, profile)
+
 
 class UploadManagerAvatar(UploadManager):
     NAME = 'avatar_path'
@@ -889,10 +911,16 @@
         return str(uuid.uuid4())
 
     def _fileWritten(self, request, filepath):
-        """Called once the file is actually written on disk"""
+        """Called once the file is actually written on disk
+        @param request: HTTP request object
+        @param filepath: full filepath on the server
+        @return: a tuple with the name of the async bridge method
+        to be called followed by its arguments.
+        """
         profile = ISATSession(request.getSession()).profile
-        print u"fichier écrit:", filepath
-        self.sat_host.bridge.setAvatar(filepath, profile)
+        debug("fichier écrit:", filepath)  # /!\ unicode output raises UnicodeDecodeError
+        return ("setAvatar", filepath, profile)
+
 
 class Libervia(service.Service):