Cleanup: removed reference to unqueued archive requests
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Thu, 11 Nov 2010 16:00:32 +0000 (16:00 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Thu, 11 Nov 2010 16:00:32 +0000 (16:00 +0000)
bin/djais/views.py

index 2a0b0905504d80c9f1d70ccdc0c58e9b8d479ddd..629e4580631cbc54d5c9b98b98a0f39b7dd0197f 100644 (file)
@@ -20,13 +20,12 @@ from django.http import *
 from django.template import loader, RequestContext
 from django import forms
 from django.shortcuts import render_to_response, get_object_or_404
-from django.db import IntegrityError
 
 from decoratedstr import remove_decoration
 
 from ais.djais.basicauth import http_authenticate
 from ais.djais.models import *
-from ais.show_targets_ships import *
+from ais.show_targets_ships import format_fleet, kml_to_kmz
 from ais.common import COUNTRIES_MID, Nmea, NmeaFeeder, strmmsi_to_mmsi, SHIP_TYPES, STATUS_CODES, AIS_STATUS_NOT_AVAILABLE, AIS_ROT_NOT_AVAILABLE, AIS_LATLON_SCALE, AIS_LON_NOT_AVAILABLE, AIS_LAT_NOT_AVAILABLE, AIS_COG_SCALE, AIS_COG_NOT_AVAILABLE, AIS_NO_HEADING, AIS_SOG_SCALE, AIS_SOG_NOT_AVAILABLE, AIS_SOG_MAX_SPEED, add_nmea1, add_nmea5_partial, load_fleet_to_uset
 from ais.ntools import datetime_to_timestamp, clean_ais_charset
 from ais.inputs.common import is_id4_active
@@ -406,72 +405,30 @@ def vessel_history(request, strmmsi, format=None):
     date_start = date_end - timedelta(0,period*period_type)
     nmea_iterator = NmeaFeeder(strmmsi, date_end, date_start, granularity=grain*grain_type)
     
-    #queue = request.REQUEST.get('queue', None)
-    queue = True
-
-    if format is None:
-        format = request.REQUEST.get('format', u'track')
+    format = request.REQUEST.get('format', u'track')
 
     if format == u'track':
-        if queue:
-            command = u'show_targets_ships --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' --format=track '+ strmmsi
-            job = Job()
-            job.user = request.user
-            job.command = command
-            job.friendly_filename = u'%s.kmz' % strmmsi
-            job.save()
-            #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/download' % job.id)
-        else:
-            value = kml_to_kmz(format_boat_track(nmea_iterator))
-            response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
-            response['Content-Disposition'] = 'attachment; filename=%s.kmz' % strmmsi
-            return response
+        command = u'show_targets_ships --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' --format=track '+ strmmsi
+        extension = u'kmz'
 
     elif format == u'animation':
-        if queue:
-            command = u'show_targets_ships --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' --format=animation '+ strmmsi
-            job = Job()
-            job.user = request.user
-            job.command = command
-            job.friendly_filename = u'%s.kmz' % strmmsi
-            job.save()
-            #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/download' % job.id)
-        else:
-            value = kml_to_kmz(format_boat_intime(nmea_iterator))
-            response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
-            response['Content-Disposition'] = 'attachment; filename=%s.kmz' % strmmsi
-            return response
-
+        command = u'show_targets_ships --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' --format=animation '+ strmmsi
+        extension = u'kmz'
+    
     elif format == u'csv':
-        if queue:
-            command = u'common --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' ' + strmmsi
-            job = Job()
-            job.user = request.user
-            job.command = command
-            job.friendly_filename = u'%s.csv' % strmmsi
-            job.save()
-            #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/download' % job.id)
-        else:
-            value = StringIO()
-            output = csv.writer(value)
-            output.writerow(Nmea.csv_headers)
-            for nmea in nmea_iterator:
-                output.writerow(nmea.get_dump_row())
-            response = HttpResponse(value.getvalue(), mimetype='text/csv; charset="UTF-8"')
-            response['Content-Disposition'] = 'attachment; filename=%s.csv' % strmmsi
-            return response
+        command = u'common --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' ' + strmmsi
+        extension = u'csv'
     else:
-        request.user.error(u'Invalid archive format')
-    return HttpResponseRedirect('/vessel/%s/' % strmmsi)
+        raise Http404(u'Invalid archive format')
+
+    job = Job()
+    job.friendly_filename = u'%s.%s' % (strmmsi, extension)
+    job.user = request.user
+    job.command = command
+    job.save()
+    if not jobrunner.wakeup_daemon():
+        return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR)
+    return HttpResponseRedirect('/job/%s/download' % job.id)
 
 
 @http_authenticate(auth, 'ais')
@@ -665,7 +622,6 @@ def fleet_lastpos(request, fleetname):
     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
         return HttpResponseForbidden('<h1>Forbidden</h1>')
     fleet_uset = load_fleet_to_uset(fleet.id)
-    # = set([mmsi_to_strmmsi(vessel.mmsi) for vessel in fleet.vessel.all()])
     value = kml_to_kmz(format_fleet(fleet_uset, document_name=fleetname+' fleet').encode('utf-8'))
     response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
     response['Content-Disposition'] = 'attachment; filename=%s.kmz' % fleetname