Uniformized vessel archive request forms
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sun, 7 Nov 2010 14:42:04 +0000 (14:42 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sun, 7 Nov 2010 14:42:04 +0000 (14:42 +0000)
bin/djais/urls.py
bin/djais/views.py
html_templates/vessel.html

index 2a0f43120164828f9d80798ea3d1b8950a8efbae..2e9a0072476fca448a9a871b7a122f1be1ba1be0 100644 (file)
@@ -19,9 +19,10 @@ urlpatterns = patterns('',
     (r'^vessel/$', 'ais.djais.views.vessel_search'),
     (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/$', 'ais.djais.views.vessel'),
     (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/manual_input$', 'ais.djais.views.vessel_manual_input'),
-    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/track$', 'ais.djais.views.vessel_track'),
-    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/animation$', 'ais.djais.views.vessel_animation'),
-    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/csv$', 'ais.djais.views.vessel_csv'),
+    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/history$', 'ais.djais.views.vessel_history'),
+    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/track$', 'ais.djais.views.vessel_history', {'format': u'track'}),
+    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/animation$', 'ais.djais.views.vessel_history', {'format': u'animation'}),
+    (r'^vessel/(?P<strmmsi>[0-9A-Z]{9})/csv$', 'ais.djais.views.vessel_history', {'format': u'csv'}),
     (r'^user/$', 'ais.djais.views.users'),
     (r'^user/(?P<login>[a-zA-Z0-9_]+)/$', 'ais.djais.views.user_detail'),
     (r'^user/(?P<login>[a-zA-Z0-9_]+)/edit$', 'ais.djais.views.user_edit'),
index ca7be404911ca73f510a90d2b203600e23f288c8..80aa9e4751516c8e7a5994e8e778e27ad68afa60 100644 (file)
@@ -361,50 +361,9 @@ def vessel_manual_input(request, strmmsi):
         form = VesselManualInputForm()
     return render_to_response('vessel_manual_input.html', {'form': form, 'nmea': nmea}, RequestContext(request))
 
-@http_authenticate(auth, 'ais')
-def vessel_track(request, strmmsi):
-    ndays = request.REQUEST.get('ndays', 90)
-    try:
-        ndays = int(ndays)
-    except ValueError:
-        ndays = 90
-    grain = request.REQUEST.get('grain', 3600)
-    try:
-        grain = int(grain)
-    except ValueError:
-        grain = 3600
-    date_end = datetime.utcnow()
-    date_start = date_end - timedelta(ndays)
-    nmea_iterator = NmeaFeeder(strmmsi, date_end, date_start, granularity=grain)
-    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
-
 
 @http_authenticate(auth, 'ais')
-def vessel_animation(request, strmmsi):
-    ndays = request.REQUEST.get('ndays', 90)
-    try:
-        ndays = int(ndays)
-    except ValueError:
-        ndays = 90
-    grain = request.REQUEST.get('grain', 3600)
-    try:
-        grain = int(grain)
-    except ValueError:
-        grain = 3600
-    date_end = datetime.utcnow()
-    date_start = date_end - timedelta(ndays)
-    nmea_iterator = NmeaFeeder(strmmsi, date_end, date_start, granularity=grain)
-    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
-
-
-@http_authenticate(auth, 'ais')
-def vessel_csv(request, strmmsi):
+def vessel_history(request, strmmsi, format=None):
     ndays = request.REQUEST.get('ndays', 90)
     try:
         ndays = int(ndays)
@@ -419,14 +378,32 @@ def vessel_csv(request, strmmsi):
     date_start = date_end - timedelta(ndays)
     nmea_iterator = NmeaFeeder(strmmsi, date_end, date_start, granularity=grain)
     
-    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
+    if format is None:
+        format = request.REQUEST.get('format', u'track')
+    if format == u'track':
+        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
+
+    elif format == u'animation':
+        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
+
+    elif format == u'csv':
+        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
+    else:
+        request.user.error(u'Invalid archive format')
+        return HttpResponseRedirect('/vessel/%s/' % strmmsi)
 
 
 @http_authenticate(auth, 'ais')
index 1e425eee6df5528ca43038d0e2ea70cd0da23740..9782002883d3c3225b973ab19f1d68f073472c35 100644 (file)
@@ -36,25 +36,17 @@ ETA: {{ nmea.get_eta_str }}<br>
 <br>
 Sources: position by {{ nmea.get_source_1_str }}, voyage by {{ nmea.get_source_5_str }}<br>
 
-<h2>Get historical track</h2>
-<form action='track'>
-Last <input name=ndays size=3 value=7> days <br>
-Definition <input name=grain size=4 value=3600> seconds<br>
-<input type=submit value=Get>
-</form>
-
-<h2>Get historical animation</h2>
-<form action='animation'>
-Last <input name=ndays size=3 value=7> days <br>
-Definition <input name=grain size=4 value=3600> seconds<br>
-<input type=submit value=Get>
-</form>
 
-<h2>Get historical data in csv format</h2>
-<form action='csv'>
+<h2>Get historical data</h2>
+<form action='history'>
+Format: <select name=format onchange="if (this.value=='csv') $('#csvhint').show(); else $('#csvhint').hide();">
+<option value=track>Track line (Google Earth)</option>
+<option value=animation>Animation (Google Earth)</option>
+<option value=csv>Coma separated values (SpreadSheet)</option>
+</select><br>
 Last <input name=ndays size=3 value=7> days <br>
 Definition <input name=grain size=4 value=3600> seconds<br>
-Make sure you select "Charset: UTF-8" and "Separated by: Comma" when you <a href="/oocalc_howto.png">choose import options</a>.<br>
+<span id=csvhint style="display:none;">Make sure you select "Charset: UTF-8" and "Separated by: Comma" when you <a href="/oocalc_howto.png">choose import options</a>.<br></span>
 <input type=submit value=Get>
 </form>
 {% endblock %}