Flee history
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 27 Dec 2010 20:51:52 +0000 (20:51 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Mon, 27 Dec 2010 20:51:52 +0000 (20:51 +0000)
bin/djais/urls.py
bin/djais/views.py

index 9ed59d5ac6f3be9571cc84a682a54d41e9b7a1d4..4838785965364d302a5fdae4f01de9e3fa5565f4 100644 (file)
@@ -17,6 +17,8 @@ urlpatterns = patterns('',
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/add_vessel$', 'ais.djais.views.fleet_vessel_add2'),
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/users$', 'ais.djais.views.fleet_users'),
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/lastpos$', 'ais.djais.views.fleet_lastpos'),
+    (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/history$', 'ais.djais.views.fleet_history'),
+
     (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})/lastpos$', 'ais.djais.views.vessel_lastpos'),
index 3a8ab79e962719b589c6f669e4be2a1c64790e4f..54713f91539ee4abcef02530aadee6615176ef35 100644 (file)
@@ -209,6 +209,50 @@ class HistoryForm(forms.Form):
             if start_date >= end_date:
                 self._errors["start_date"] = self.error_class(['Start date must be before end date.'])
         return cleaned_data
+    
+    def get_cmdext(self):
+        '''
+        Must not be called unless is_valid
+        Returns (command, extension)
+        '''
+        data = self.cleaned_data
+        if data['period_type'] == 'duration':
+            date_start = datetime.utcnow() - timedelta(0, data['duration'])
+            date_end = None # Now
+        elif data['period_type'] == 'date_date':
+            date_start = data['start_date']
+            date_end = data['end_date']
+        else:
+            assert data['period_type'] == 'start_duration', ('Invalid period type %s' % data['period_type'])
+            date_start = data['start_date']
+            date_end = date_start + timedelta(0, data['duration'])
+
+        format = data['format']
+
+        if format == u'track':
+            command = u'show_targets_ships'
+            command += u' --format=track'
+            extension = u'kmz'
+
+        elif format == u'animation':
+            command = u'show_targets_ships'
+            command += u' --format=animation'
+            extension = u'kmz'
+        
+        elif format == u'csv':
+            command = u'common'
+            extension = u'csv'
+        else:
+            raise Http404(u'Invalid archive format')
+        
+        command += u' --start=\'' + date_start.strftime('%Y%m%d') + u'\''
+        if date_end:
+            command += u' --end=\'' + date_end.strftime('%Y%m%d') + u'\''
+        
+        grain = data['grain']
+        command += u' --granularity=' + unicode(grain)
+
+        return command, extension
 
 @http_authenticate(auth, 'ais')
 def vessel(request, strmmsi):
@@ -284,7 +328,7 @@ class VesselManualInputForm(forms.Form):
 
     def clean_latitude(self):
         data = self.cleaned_data['latitude']
-        data = data.replace(u"''", u'"') # commong mistake
+        data = data.replace(u"''", u'"') # common mistake
         data = data.replace(u' ', u'') # remove spaces
         sides = u'SN'
         if not data:
@@ -338,7 +382,7 @@ class VesselManualInputForm(forms.Form):
 
     def clean_longitude(self):
         data = self.cleaned_data['longitude']
-        data = data.replace(u"''", u'"') # commong mistake
+        data = data.replace(u"''", u'"') # common mistake
         data = data.replace(u' ', u'') # remove spaces
         sides = u'WE'
         if not data:
@@ -481,42 +525,7 @@ def vessel_history(request, strmmsi):
     if request.method == 'POST':
         form = HistoryForm(request.POST, initial=initial)
         if form.is_valid():
-            data = form.cleaned_data
-            if data['period_type'] == 'duration':
-                date_start = datetime.utcnow() - timedelta(0, data['duration'])
-                date_end = None # Now
-            elif data['period_type'] == 'date_date':
-                date_start = data['start_date']
-                date_end = data['end_date']
-            else:
-                assert data['period_type'] == 'start_duration', ('Invalid period type %s' % data['period_type'])
-                date_start = data['start_date']
-                date_end = date_start + timedelta(0, data['duration'])
-
-            format = data['format']
-
-            if format == u'track':
-                command = u'show_targets_ships'
-                command += u' --format=track'
-                extension = u'kmz'
-
-            elif format == u'animation':
-                command = u'show_targets_ships'
-                command += u' --format=animation'
-                extension = u'kmz'
-            
-            elif format == u'csv':
-                command = u'common'
-                extension = u'csv'
-            else:
-                raise Http404(u'Invalid archive format')
-            
-            command += u' --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\''
-            if date_end:
-                command += u' --end=\'' + date_end.strftime('%Y%m%d %H%M%S') + u'\''
-            
-            grain = data['grain']
-            command += u' --granularity=' + unicode(grain)
+            command, extension = form.get_cmdext()
 
             command += u' ' + strmmsi
 
@@ -731,6 +740,31 @@ def fleet_lastpos(request, fleetname):
     response['Content-Disposition'] = 'attachment; filename=%s.kmz' % fleetname
     return response
 
+@http_authenticate(auth, 'ais')
+def fleet_history(request, fleetname):
+    fleet = get_object_or_404(Fleet, name=fleetname)
+    if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
+        return HttpResponseForbidden('<h1>Forbidden</h1>')
+    initial = {}
+    if request.method == 'POST':
+        form = HistoryForm(request.POST, initial=initial)
+        if form.is_valid():
+            command, extension = form.get_cmdext()
+
+            command += u' @' + fleetname
+
+            job = Job()
+            job.friendly_filename = u'%s.%s' % (fleetname, 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)
+    else: # GET
+        form = HistoryForm(initial=initial)
+    return render_to_response('fleet_history.html', {'fleet': fleet, 'form':form}, RequestContext(request))
+
 
 @http_authenticate(auth, 'ais')
 def jobs_index(request):