From: Jean-Michel Nirgal Vourgère Date: Fri, 6 Aug 2010 19:49:46 +0000 (+0000) Subject: Added flag selector for vessel search X-Git-Url: https://git.nirgal.com/?p=ais.git;a=commitdiff_plain;h=6769f6b0923de8f1f04c951374cbc73f655cfd69 Added flag selector for vessel search --- diff --git a/bin/djais/views.py b/bin/djais/views.py index 268cc45..664b7fd 100644 --- a/bin/djais/views.py +++ b/bin/djais/views.py @@ -7,6 +7,7 @@ from time import time as get_timestamp import logging import crack import struct +import operator 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.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 @@ -43,11 +44,25 @@ def index(request): 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) + flag = forms.ChoiceField(choices=country_choices(), required=False) + + 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()) + 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()