Added flag selector for vessel search
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 6 Aug 2010 19:49:46 +0000 (19:49 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 6 Aug 2010 19:49:46 +0000 (19:49 +0000)
bin/djais/views.py

index 268cc45ab5db12bb9a9cc170958365b8b530c88d..664b7fd90568dca30cc63e0f46b74ca1ba1c8f1c 100644 (file)
@@ -7,6 +7,7 @@ from time import time as get_timestamp
 import logging
 import crack
 import struct
 import logging
 import crack
 import struct
+import operator
 import rrdtool
 from django.http import *
 from django.template import loader, RequestContext
 import rrdtool
 from django.http import *
 from django.template import loader, RequestContext
@@ -19,7 +20,7 @@ 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.djais.basicauth import http_authenticate
 from ais.djais.models import *
 from ais.show_targets_ships import *
-from ais.common import 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.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
 from ais.inputs.stats import STATS_DIR
 from ais.ntools import datetime_to_timestamp, clean_ais_charset
 from ais.inputs.common import is_id4_active
 from ais.inputs.stats import STATS_DIR
@@ -43,11 +44,25 @@ def index(request):
 
 
 class VesselSearchForm(forms.Form):
 
 
 class VesselSearchForm(forms.Form):
+    def country_choices():
+        choices = {}
+        for mid, countryname in COUNTRIES_MID.iteritems():
+            if choices.has_key(countryname):
+                choices[countryname] += u','+unicode(mid)
+            else:
+                choices[countryname] = unicode(mid)
+        choices = [ (countryname, mids) for mids, countryname in choices.iteritems() ]
+        choices = sorted(choices, key=operator.itemgetter(1))
+        return [(u'', u'Any')] + choices
+
     mmsi = forms.CharField(max_length=9, required=False)
     name = forms.CharField(max_length=20, required=False)
     imo = forms.IntegerField(required=False)
     callsign = forms.CharField(max_length=7, required=False)
     destination = forms.CharField(max_length=20, required=False)
     mmsi = forms.CharField(max_length=9, required=False)
     name = forms.CharField(max_length=20, required=False)
     imo = forms.IntegerField(required=False)
     callsign = forms.CharField(max_length=7, required=False)
     destination = forms.CharField(max_length=20, required=False)
+    flag = forms.ChoiceField(choices=country_choices(), required=False)
+
+
     def clean(self):
         cleaned_data = self.cleaned_data
         for value in cleaned_data.values():
     def clean(self):
         cleaned_data = self.cleaned_data
         for value in cleaned_data.values():
@@ -74,6 +89,8 @@ def vessel_search(request):
                 vessels = vessels.filter(callsign__contains=data['callsign'].upper())
             if data['destination']:
                 vessels = vessels.filter(destination__contains=data['destination'].upper())
                 vessels = vessels.filter(callsign__contains=data['callsign'].upper())
             if data['destination']:
                 vessels = vessels.filter(destination__contains=data['destination'].upper())
+            if data['flag']:
+                vessels = vessels.extra(where=['mmsi/1000000 IN (%s) ' % data['flag']])
             return render_to_response('vessels.html', {'vessels': vessels}, RequestContext(request))
     else: # GET
         form = VesselSearchForm()
             return render_to_response('vessels.html', {'vessels': vessels}, RequestContext(request))
     else: # GET
         form = VesselSearchForm()