Added a proper job_detail page that display information, and reloads itself.
[ais.git] / bin / djais / views.py
index 33363ffe6f0510454e4b7f04907d832de0f7a5f2..d9dce9d012e0ab33b62cfcc3b1c558d07ea0c286 100644 (file)
@@ -1,6 +1,8 @@
 # -*- coding: utf-8 -*-
 
+#TODO
 #from __future__ import division
+#Normalize 403 errors
 
 import os
 from datetime import *
@@ -416,8 +418,10 @@ def vessel_history(request, strmmsi, format=None):
             job.command = command
             job.extension = u'kmz'
             job.save()
-            request.user.info('Request queued as job %s' % job.id)
-            jobrunner.wakeup_daemon()
+            #request.user.info('Request queued as job %s' % job.id)
+            if not jobrunner.wakeup_daemon():
+                return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR)
+            return HttpResponseRedirect('/job/%s/' % job.id)
         else:
             value = kml_to_kmz(format_boat_track(nmea_iterator))
             response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
@@ -432,8 +436,10 @@ def vessel_history(request, strmmsi, format=None):
             job.command = command
             job.extension = u'kmz'
             job.save()
-            request.user.info('Request queued as job %s' % job.id)
-            jobrunner.wakeup_daemon()
+            #request.user.info('Request queued as job %s' % job.id)
+            if not jobrunner.wakeup_daemon():
+                return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR)
+            return HttpResponseRedirect('/job/%s/' % job.id)
         else:
             value = kml_to_kmz(format_boat_intime(nmea_iterator))
             response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
@@ -448,8 +454,10 @@ def vessel_history(request, strmmsi, format=None):
             job.command = command
             job.extension = u'csv'
             job.save()
-            request.user.info('Request queued as job %s' % job.id)
-            jobrunner.wakeup_daemon()
+            #request.user.info('Request queued as job %s' % job.id)
+            if not jobrunner.wakeup_daemon():
+                return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR)
+            return HttpResponseRedirect('/job/%s/' % job.id)
         else:
             value = StringIO()
             output = csv.writer(value)
@@ -671,6 +679,21 @@ def jobs_index(request):
         jobs = request.user.job_set.filter(archive_time__isnull=True)
     return render_to_response('jobs.html', {'jobs': jobs, 'archive': show_archive }, RequestContext(request))
 
+@http_authenticate(auth, 'ais')
+def job_detail(request, jobid):
+    job = get_object_or_404(Job, id=jobid)
+    if job.user != request.user:
+        return HttpResponseForbidden('403 Forbidden')
+    response = render_to_response('job.html', {'job': job}, RequestContext(request))
+    if not job.finish_time:
+        response['Refresh'] = 5
+    elif not job.archive_time:
+        # finished but not archived:
+        response['Refresh'] = '0;url=https://ais.nirgal.com/job/%s/download' % job.id
+        job.archive_time = datetime.utcnow()
+        job.save()
+    return response
+
 @http_authenticate(auth, 'ais')
 def job_get(request, jobid):
     job = get_object_or_404(Job, id=jobid)