Cleanup: removed reference to unqueued archive requests
[ais.git] / bin / djais / views.py
1 # -*- coding: utf-8 -*-
2
3 #TODO
4 #from __future__ import division
5 #Normalize 403 errors
6
7 import os
8 import os.path
9 from datetime import *
10 import re
11 from time import time as get_timestamp, sleep
12 import logging
13 import crack
14 import struct
15 import operator
16 import rrdtool
17 import csv
18 from StringIO import StringIO
19 from django.http import *
20 from django.template import loader, RequestContext
21 from django import forms
22 from django.shortcuts import render_to_response, get_object_or_404
23
24 from decoratedstr import remove_decoration
25
26 from ais.djais.basicauth import http_authenticate
27 from ais.djais.models import *
28 from ais.show_targets_ships import format_fleet, kml_to_kmz
29 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
30 from ais.ntools import datetime_to_timestamp, clean_ais_charset
31 from ais.inputs.common import is_id4_active
32 from ais.inputs.stats import STATS_DIR
33 from ais.inputs.config import peers_get_config
34 from ais import jobrunner
35
36 def auth(username, raw_password):
37     try:
38         user = User.objects.get(login=username)
39     except User.DoesNotExist:
40         return None
41     if not user.check_password(raw_password):
42         return None
43     user.update_access_datetime()
44     return user
45
46
47 @http_authenticate(auth, 'ais')
48 def index(request):
49     news = News.objects.order_by('-created')[:5]
50     return render_to_response('index.html', {'news':news}, RequestContext(request))
51
52
53 class VesselSearchForm(forms.Form):
54     def country_choices():
55         choices = {}
56         for mid, countryname in COUNTRIES_MID.iteritems():
57             if choices.has_key(countryname):
58                 choices[countryname] += u','+unicode(mid)
59             else:
60                 choices[countryname] = unicode(mid)
61         choices = [ (countryname, mids) for mids, countryname in choices.iteritems() ]
62         choices = sorted(choices, key=operator.itemgetter(1))
63         return [(u'', u'Any')] + choices
64
65     mmsi = forms.CharField(max_length=9, required=False)
66     name = forms.CharField(max_length=20, required=False)
67     imo = forms.IntegerField(required=False)
68     callsign = forms.CharField(max_length=7, required=False)
69     flag = forms.ChoiceField(choices=country_choices(), required=False)
70     destination = forms.CharField(max_length=20, required=False)
71
72
73     def clean(self):
74         cleaned_data = self.cleaned_data
75         for value in cleaned_data.values():
76             if value:
77                 return cleaned_data
78         raise forms.ValidationError("You must enter at least one criteria")
79
80     def clean_mmsi(self):
81         mmsi = self.cleaned_data['mmsi']
82         if mmsi:
83             try:
84                 mmsi = strmmsi_to_mmsi(mmsi)
85             except AssertionError:
86                 raise forms.ValidationError("Invalid MMSI")
87         return mmsi
88
89
90 @http_authenticate(auth, 'ais')
91 def vessel_search(request):
92     if request.method == 'POST' or request.META['QUERY_STRING']:
93         form = VesselSearchForm(request.REQUEST)
94         if form.is_valid():
95             data = form.cleaned_data
96             vessels = Vessel.objects
97             if data['mmsi']:
98                 vessels = vessels.filter(mmsi=data['mmsi'])
99             if data['name']:
100                 vessels = vessels.filter(name__contains=data['name'].upper())
101             if data['imo']:
102                 vessels = vessels.filter(imo=data['imo'])
103             if data['callsign']:
104                 vessels = vessels.filter(callsign__contains=data['callsign'].upper())
105             if data['destination']:
106                 vessels = vessels.filter(destination__contains=data['destination'].upper())
107             if data['flag']:
108                 vessels = vessels.extra(where=['mmsi/1000000 IN (%s) ' % data['flag']])
109             return render_to_response('vessels.html', {'vessels': vessels}, RequestContext(request))
110     else: # GET
111         form = VesselSearchForm()
112
113     return render_to_response('vessel_index.html', {'form': form}, RequestContext(request))
114
115 @http_authenticate(auth, 'ais')
116 def vessel(request, strmmsi):
117     mmsi = strmmsi_to_mmsi(strmmsi)
118     vessel = get_object_or_404(Vessel, pk=mmsi)
119     nmea = Nmea.new_from_lastinfo(strmmsi)
120     #if not nmea.timestamp_1 and not nmea.timestamp_5:
121     #    raise Http404
122     return render_to_response('vessel.html', {'nmea': nmea}, RequestContext(request))
123
124
125 class VesselManualInputForm(forms.Form):
126     timestamp = forms.DateTimeField(label=u'When', help_text=u'When was the observation made in GMT. Use YYYY-MM-DD HH:MM:SS format')
127     imo = forms.IntegerField(required=False, min_value=1000000, max_value=9999999)
128     name = forms.CharField(max_length=20, required=False)
129     callsign = forms.CharField(max_length=7, required=False)
130     type = forms.TypedChoiceField(required=False, choices = [ kv for kv in SHIP_TYPES.iteritems() if 'reserved' not in kv[1].lower()], coerce=int, empty_value=0, initial=0)
131     status = forms.TypedChoiceField(required=False, choices = [ kv for kv in STATUS_CODES.iteritems() if 'reserved' not in kv[1].lower()], coerce=int, empty_value=AIS_STATUS_NOT_AVAILABLE, initial=AIS_STATUS_NOT_AVAILABLE)
132     sog = forms.FloatField(label='Speed', help_text='Over ground, in knots', required=False, min_value=0, max_value=AIS_SOG_MAX_SPEED/AIS_SOG_SCALE)
133     latitude = forms.CharField(required=False)
134     longitude = forms.CharField(required=False)
135     cog = forms.FloatField(label='Course', help_text='Over ground', required=False, min_value=0.0, max_value=359.9)
136     heading = forms.IntegerField(required=False, min_value=0, max_value=359)
137
138     @staticmethod
139     def _clean_ais_charset(ustr):
140         ustr = remove_decoration(ustr) # benign cleaning, but can increase size (œ->oe)
141         ustr = ustr.upper() # benign cleaning
142         str = clean_ais_charset(ustr.encode('ascii', 'replace'))
143         if unicode(str) != ustr:
144             raise forms.ValidationError('Invalid character: AIS alphabet is @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^- !"#$%&\'()*+,-./0123456789:;<=>?')
145         return str
146
147     def clean_timestamp(self):
148         data = self.cleaned_data['timestamp']
149         if data is None:
150             return None
151         if data < datetime.utcnow() - timedelta(365):
152             raise forms.ValidationError('Date is too much is the past.')
153         if data > datetime.utcnow():
154             raise forms.ValidationError('Date is be in the future. This form is only for observed results.')
155         return datetime_to_timestamp(data)
156
157     def clean_imo(self):
158         data = self.cleaned_data['imo']
159         if data is None:
160             return 0
161         return data
162
163     def clean_name(self):
164         name = self.cleaned_data['name']
165         if name is None:
166             return ''
167         name = VesselManualInputForm._clean_ais_charset(name)
168         if len(name)>20:
169             raise forms.ValidationError('Ensure this value has at most 20 characters (it has %s).' % len(name))
170         return name
171
172     def clean_callsign(self):
173         callsign = self.cleaned_data['callsign']
174         if callsign is None:
175             return ''
176         callsign = VesselManualInputForm._clean_ais_charset(callsign)
177         if len(callsign)>7:
178             raise forms.ValidationError('Ensure this value has at most 7 characters (it has %s).' % len(callsign))
179         return callsign
180
181     def clean_sog(self):
182         sog = self.cleaned_data['sog']
183         if sog is None:
184             return AIS_SOG_NOT_AVAILABLE
185         return int(sog*AIS_SOG_SCALE)
186
187     def clean_latitude(self):
188         data = self.cleaned_data['latitude']
189         data = data.replace(u"''", u'"') # commong mistake
190         data = data.replace(u' ', u'') # remove spaces
191         sides = u'SN'
192         if not data:
193             return AIS_LAT_NOT_AVAILABLE
194         tmp, side = data[:-1], data[-1]
195         if side == sides[0]:
196             side = -1
197         elif side == sides[1]:
198             side = 1
199         else:
200             raise forms.ValidationError(u'Last character must be either %s or %s.' % (sides[0], sides[1]))
201         spl = tmp.split(u'°')
202         if len(spl) == 1:
203             raise forms.ValidationError(u'You need to use the ° character.')
204         d, tmp = spl
205         try:
206             d = float(d)
207         except ValueError:
208             raise forms.ValidationError(u'Degrees must be an number. It\'s %s.' % d)
209         spl = tmp.split(u"'", 1)
210         if len(spl) == 1:
211             # no ' sign: ok only if there is nothing but the side after °
212             # we don't accept seconds if there is no minutes:
213             # It might be an entry mistake
214             tmp = spl[0]
215             if len(tmp) == 0:
216                 m = s = 0
217             else:
218                 raise forms.ValidationError(u'You must use the \' character between ° and %s.' % data[-1])
219         else:
220             m, tmp = spl
221             try:
222                 m = float(m)
223             except ValueError:
224                 raise forms.ValidationError(u'Minutes must be an number. It\'s %s.' % m)
225             if len(tmp) == 0:
226                 s = 0
227             else:
228                 if tmp[-1] != '"':
229                     raise forms.ValidationError(u'You must use the " character between seconds and %s.' % data[-1])
230                 s = tmp[:-1]
231                 try:
232                     s = float(s)
233                 except ValueError:
234                     raise forms.ValidationError(u'Seconds must be an number. It\'s %s.' % s)
235         data = side * ( d + m / 60 + s / 3600)
236
237         if data < -90 or data > 90:
238             raise forms.ValidationError(u'%s in not in -90..90 range' % data)
239         return int(data * AIS_LATLON_SCALE)
240
241     def clean_longitude(self):
242         data = self.cleaned_data['longitude']
243         data = data.replace(u"''", u'"') # commong mistake
244         data = data.replace(u' ', u'') # remove spaces
245         sides = u'WE'
246         if not data:
247             return AIS_LON_NOT_AVAILABLE
248         tmp, side = data[:-1], data[-1]
249         if side == sides[0]:
250             side = -1
251         elif side == sides[1]:
252             side = 1
253         else:
254             raise forms.ValidationError(u'Last character must be either %s or %s.' % (sides[0], sides[1]))
255         spl = tmp.split(u'°')
256         if len(spl) == 1:
257             raise forms.ValidationError(u'You need to use the ° character.')
258         d, tmp = spl
259         try:
260             d = float(d)
261         except ValueError:
262             raise forms.ValidationError(u'Degrees must be an number. It\'s %s.' % d)
263         spl = tmp.split(u"'", 1)
264         if len(spl) == 1:
265             # no ' sign: ok only if there is nothing but the side after °
266             # we don't accept seconds if there is no minutes:
267             # It might be an entry mistake
268             tmp = spl[0]
269             if len(tmp) == 0:
270                 m = s = 0
271             else:
272                 raise forms.ValidationError(u'You must use the \' character between ° and %s.' % data[-1])
273         else:
274             m, tmp = spl
275             try:
276                 m = float(m)
277             except ValueError:
278                 raise forms.ValidationError(u'Minutes must be an number. It\'s %s.' % m)
279             if len(tmp) == 0:
280                 s = 0
281             else:
282                 if tmp[-1] != '"':
283                     raise forms.ValidationError(u'You must use the " character between seconds and %s.' % data[-1])
284                 s = tmp[:-1]
285                 try:
286                     s = float(s)
287                 except ValueError:
288                     raise forms.ValidationError(u'Seconds must be an number. It\'s %s.' % s)
289         data = side * ( d + m / 60 + s / 3600)
290
291         if data < -180 or data > 180:
292             raise forms.ValidationError(u'%s in not in -180..180 range' % data)
293         return int(data * AIS_LATLON_SCALE)
294
295     def clean_cog(self):
296         data = self.cleaned_data['cog']
297         if data is None:
298             return AIS_COG_NOT_AVAILABLE
299         return int(data * AIS_COG_SCALE)
300     
301     def clean_heading(self):
302         #raise forms.ValidationError(u'clean_heading called')
303         data = self.cleaned_data['heading']
304         if data is None:
305             return AIS_NO_HEADING
306         return data
307
308     def clean(self):
309         cleaned_data = self.cleaned_data
310         if (cleaned_data.get('latitude', AIS_LAT_NOT_AVAILABLE) == AIS_LAT_NOT_AVAILABLE ) ^ ( cleaned_data.get('longitude', AIS_LON_NOT_AVAILABLE) == AIS_LON_NOT_AVAILABLE):
311             raise forms.ValidationError('It makes no sense to enter just a latitude or a longitude. Enter both or none.')
312         if cleaned_data.get('latitude', AIS_LAT_NOT_AVAILABLE) == AIS_LAT_NOT_AVAILABLE:
313             if cleaned_data.get('status', AIS_STATUS_NOT_AVAILABLE) != AIS_STATUS_NOT_AVAILABLE:
314                 raise forms.ValidationError('It makes no sense to enter a status without coordinates. Please enter latitute and longitude too.')
315             if cleaned_data.get('sog', AIS_SOG_NOT_AVAILABLE) != AIS_SOG_NOT_AVAILABLE:
316                 raise forms.ValidationError('It makes no sense to enter a speed without coordinates. Please enter latitute and longitude too.')
317             if cleaned_data.get('cog', AIS_COG_NOT_AVAILABLE) != AIS_COG_NOT_AVAILABLE:
318                 raise forms.ValidationError('It makes no sense to enter a course without coordinates. Please enter latitute and longitude too.')
319             if cleaned_data.get('heading', AIS_NO_HEADING) != AIS_NO_HEADING:
320                 raise forms.ValidationError('It makes no sense to enter a heading without coordinates. Please enter latitute and longitude too.')
321
322         if cleaned_data.get('timestamp', None) \
323         and cleaned_data.get('imo', 0) == 0 \
324         and cleaned_data.get('name', '') == '' \
325         and cleaned_data.get('callsign', '') == '' \
326         and cleaned_data.get('type', 0) == 0 \
327         and cleaned_data.get('status', AIS_STATUS_NOT_AVAILABLE) == AIS_STATUS_NOT_AVAILABLE \
328         and cleaned_data.get('sog', AIS_SOG_NOT_AVAILABLE) == AIS_SOG_NOT_AVAILABLE \
329         and cleaned_data.get('latitude', AIS_LAT_NOT_AVAILABLE) == AIS_LAT_NOT_AVAILABLE \
330         and cleaned_data.get('longitude', AIS_LON_NOT_AVAILABLE) == AIS_LON_NOT_AVAILABLE \
331         and cleaned_data.get('cog', AIS_COG_NOT_AVAILABLE) == AIS_COG_NOT_AVAILABLE \
332         and cleaned_data.get('heading', AIS_NO_HEADING) == AIS_NO_HEADING:
333             raise forms.ValidationError("You must enter some data, beside when.")
334         return cleaned_data
335
336 @http_authenticate(auth, 'ais')
337 def vessel_manual_input(request, strmmsi):
338     strmmsi = strmmsi.encode('utf-8')
339     nmea = Nmea.new_from_lastinfo(strmmsi)
340     if request.method == 'POST' or request.META['QUERY_STRING']:
341         form = VesselManualInputForm(request.REQUEST)
342         if form.is_valid():
343             data = form.cleaned_data
344             source = 'U' +  struct.pack('<I', request.user.id)[0:3]
345             result = u''
346             if data['imo'] != 0 \
347             or data['name'] != '' \
348             or data['callsign'] != '' \
349             or data['type'] != 0:
350                 toto = (strmmsi, data['timestamp'], data['imo'], data['name'], data['callsign'], data['type'], 0,0,0,0, 0,0,24,60, 0, '', source)
351                 result += 'UPDATING NMEA 5: '+repr(toto)+'<br>'
352                 add_nmea5_partial(*toto)
353             if data['status'] != AIS_STATUS_NOT_AVAILABLE \
354             or data['sog'] != AIS_SOG_NOT_AVAILABLE \
355             or data['latitude'] != AIS_LAT_NOT_AVAILABLE \
356             or data['longitude'] != AIS_LON_NOT_AVAILABLE \
357             or data['cog'] != AIS_COG_NOT_AVAILABLE \
358             or data['heading'] != AIS_NO_HEADING:
359                 
360                 toto = (strmmsi, data['timestamp'], data['status'], AIS_ROT_NOT_AVAILABLE, data['sog'], data['latitude'], data['longitude'], data['cog'], data['heading'], source)
361                 result += 'UPDATING NMEA 1: '+repr(toto)+'<br>'
362                 add_nmea1(*toto)
363             return HttpResponse('Not fully implemented: '+repr(data) + '<br>' + result)
364     else: # GET
365         form = VesselManualInputForm()
366     return render_to_response('vessel_manual_input.html', {'form': form, 'nmea': nmea}, RequestContext(request))
367
368
369 @http_authenticate(auth, 'ais')
370 def vessel_history(request, strmmsi, format=None):
371     """
372     That view is called from Google Earth, so that it must support GET method.  
373     """
374     ndays = request.REQUEST.get('ndays', None)
375     if ndays is not None:
376         try:
377             ndays = int(ndays)
378         except ValueError:
379             ndays = 90
380         period = ndays * 86400
381     else:
382         period = request.REQUEST.get('period', u'1')
383         try:
384             period = int(period)
385         except ValueError:
386             period = 1
387         period_type = request.REQUEST.get('period_type', u'86400')
388         try:
389             period_type = int(period_type)
390         except ValueError:
391             period_type = 86400
392
393     grain = request.REQUEST.get('grain', 1)
394     try:
395         grain = int(grain)
396     except ValueError:
397         grain = 1
398     grain_type = request.REQUEST.get('grain_type', 3600)
399     try:
400         grain_type = int(grain_type)
401     except ValueError:
402         grain_type = 3600
403
404     date_end = datetime.utcnow()
405     date_start = date_end - timedelta(0,period*period_type)
406     nmea_iterator = NmeaFeeder(strmmsi, date_end, date_start, granularity=grain*grain_type)
407     
408     format = request.REQUEST.get('format', u'track')
409
410     if format == u'track':
411         command = u'show_targets_ships --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' --format=track '+ strmmsi
412         extension = u'kmz'
413
414     elif format == u'animation':
415         command = u'show_targets_ships --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' --format=animation '+ strmmsi
416         extension = u'kmz'
417     
418     elif format == u'csv':
419         command = u'common --start=\'' + date_start.strftime('%Y%m%d %H%M%S') + u'\' --granularity=' + unicode(grain*grain_type) + ' ' + strmmsi
420         extension = u'csv'
421     else:
422         raise Http404(u'Invalid archive format')
423
424     job = Job()
425     job.friendly_filename = u'%s.%s' % (strmmsi, extension)
426     job.user = request.user
427     job.command = command
428     job.save()
429     if not jobrunner.wakeup_daemon():
430         return HttpResponseServerError(jobrunner.DAEMON_WAKEUP_ERROR)
431     return HttpResponseRedirect('/job/%s/download' % job.id)
432
433
434 @http_authenticate(auth, 'ais')
435 def fleets(request):
436     fleetusers = request.user.fleetuser_set.order_by('fleet')
437     return render_to_response('fleets.html', {'fleetusers':fleetusers}, RequestContext(request))
438
439
440 @http_authenticate(auth, 'ais')
441 def fleet(request, fleetname):
442     fleet = get_object_or_404(Fleet, name=fleetname)
443     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
444         return HttpResponseForbidden('<h1>Forbidden</h1>')
445     return render_to_response('fleet.html', {'fleet':fleet}, RequestContext(request))
446
447
448 class FleetEditForm(forms.Form):
449     name = forms.CharField(max_length=50)
450     description = forms.CharField(widget=forms.Textarea)
451
452     def clean_name(self):
453         name = self.cleaned_data['name']
454         if name == u'add':
455             raise forms.ValidationError("Sorry that name is reserved. Try another one.")
456         if name != self.initial.get('name', None):
457             if Fleet.objects.filter(name=name).count():
458                 raise forms.ValidationError("Sorry that fleet name is already in use. Try another one.")
459         return remove_decoration(name).replace(u' ', u'_')
460         
461
462 @http_authenticate(auth, 'ais')
463 def fleet_edit(request, fleetname):
464     initial = {}
465     if fleetname is not None:
466         fleet = get_object_or_404(Fleet, name=fleetname)
467         if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
468             return HttpResponseForbidden('<h1>Forbidden</h1>')
469
470         initial['name'] = fleet.name
471         initial['description'] = fleet.description
472     else:
473         fleet = Fleet()
474
475     if request.method == 'POST':
476         form = FleetEditForm(request.POST, initial=initial)
477         if form.is_valid():
478             fleet.name = form.cleaned_data['name']
479             fleet.description = form.cleaned_data['description']
480             fleet.save()
481             if not fleetname: # new one
482                 fu = FleetUser()
483                 fu.fleet = fleet
484                 fu.user = request.user
485                 fu.save()
486             request.user.info('Fleet updated')
487             return HttpResponseRedirect('/fleet/%s/' % fleet.name)
488     else: # GET
489         form = FleetEditForm(initial=initial)
490     return render_to_response('fleet_edit.html', {'fleet':fleet, 'form':form}, RequestContext(request))
491
492
493 @http_authenticate(auth, 'ais')
494 def fleet_vessels(request, fleetname):
495     fleet = get_object_or_404(Fleet, name=fleetname)
496     if not FleetUser.objects.filter(fleet=fleet, user=request.user.id).all():
497         return HttpResponseForbidden('<h1>Forbidden</h1>')
498     vessels = fleet.vessel.all()
499     return render_to_response('fleet_vessels.html', {'fleet':fleet, 'vessels': vessels}, RequestContext(request))
500
501
502 @http_authenticate(auth, 'ais')
503 def fleet_vessel_add(request, fleetname):
504     fleet = get_object_or_404(Fleet, name=fleetname)
505     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
506         return HttpResponseForbidden('<h1>Forbidden</h1>')
507     strmmsi = request.REQUEST['mmsi']
508     mmsi = strmmsi_to_mmsi(strmmsi)
509     try:
510         vessel = Vessel.objects.get(pk=mmsi)
511     except Vessel.DoesNotExist:
512         request.user.error('No such vessel')
513     else:
514         try:
515             fv = FleetVessel.objects.get(fleet=fleet, vessel=vessel)
516         except FleetVessel.DoesNotExist:
517             FleetVessel(fleet=fleet, vessel=vessel).save()
518             request.user.info(u'Vessel %s added to fleet %s.' % (vessel.name, fleetname))
519         else:
520             request.user.error('Vessel with MMSI %s is already in that fleet' % strmmsi)
521     return HttpResponseRedirect('/fleet/%s/list' % fleetname)
522
523
524 class FleetAddVessel(forms.Form):
525     mmsi = forms.CharField(help_text=u'Enter one MMSI per line', required=False, widget=forms.Textarea)
526     #name = forms.CharField(max_length=20, required=False)
527     #imo = forms.IntegerField(required=False)
528     #callsign = forms.CharField(max_length=7, required=False)
529     #destination = forms.CharField(max_length=20, required=False)
530     def clean(self):
531         cleaned_data = self.cleaned_data
532         for value in cleaned_data.values():
533             if value:
534                 return cleaned_data
535         raise forms.ValidationError("You must enter at least one criteria")
536
537 @http_authenticate(auth, 'ais')
538 def fleet_vessel_add2(request, fleetname):
539     fleet = get_object_or_404(Fleet, name=fleetname)
540     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
541         return HttpResponseForbidden('<h1>Forbidden</h1>')
542     if request.method == 'POST' or request.META['QUERY_STRING']:
543         form = FleetAddVessel(request.REQUEST)
544         if form.is_valid():
545             data = form.cleaned_data
546             result = []
547             a_strmmsi = request.REQUEST['mmsi']
548             if a_strmmsi:
549                 for strmmsi in a_strmmsi.split('\n'):
550                     strmmsi = strmmsi.strip('\r')
551                     if not strmmsi:
552                         continue
553                     try:
554                         sqlmmsi = strmmsi_to_mmsi(strmmsi)
555                     except AssertionError:
556                         result.append('Invalid mmsi %s' % strmmsi)
557                         continue
558                     try:
559                         vessel = Vessel.objects.get(pk=sqlmmsi)
560                     except Vessel.DoesNotExist:
561                         result.append('No vessel with MMSI '+strmmsi)
562                         continue
563                     try:
564                         fv = FleetVessel.objects.get(fleet=fleet, vessel=vessel)
565                         result.append('Vessel with MMSI %s is already in that fleet' % strmmsi)
566                     except FleetVessel.DoesNotExist:
567                         FleetVessel(fleet=fleet, vessel=vessel).save()
568                         result.append('Vessel with MMSI %s added' % strmmsi)
569
570             return HttpResponse('<br>'.join(result))
571     else: # GET
572         form = FleetAddVessel()
573
574     return render_to_response('fleet_vessel_add.html', {'form': form, 'fleet': fleet}, RequestContext(request))
575
576
577 @http_authenticate(auth, 'ais')
578 def fleet_users(request, fleetname):
579     fleet = get_object_or_404(Fleet, name=fleetname)
580     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
581         return HttpResponseForbidden('<h1>Forbidden</h1>')
582
583     if request.method == 'POST' or request.META['QUERY_STRING']:
584         action = request.REQUEST['action']
585         userlogin = request.REQUEST['user']
586         try:
587             user = User.objects.get(login=userlogin)
588         except User.DoesNotExist:
589             request.user.error(u'User %s does not exist.' % userlogin)
590         else:
591             if action == u'add':
592                 try:
593                     fu = FleetUser.objects.get(fleet=fleet, user=user)
594                     request.user.error(u'User %s already has access.' % user.login)
595                 except FleetUser.DoesNotExist:
596                     FleetUser(fleet=fleet, user=user).save()
597                     #TODO log
598                     request.user.info(u'Granted access to user %s.' % user.login)
599             elif action == u'revoke':
600                 try:
601                     fu = FleetUser.objects.get(fleet=fleet, user=user)
602                     fu.delete()
603                     #TODO log
604                     request.user.info(u'Revoked access to user %s.' % user.login)
605                     if FleetUser.objects.filter(fleet=fleet).count() == 0:
606                         fleet.delete()
607                         request.user.info(u"Deleted fleet %s (no more users)." % fleet.name)
608                         return HttpResponseRedirect('/fleet/')
609                 except FleetUser.DoesNotExist:
610                     request.user.error(u'User %s didn\'t have access.' % user.login)
611             else:
612                 request.user.error(u'Unknown action %s' % action)
613
614     fleetusers = fleet.fleetuser_set.all()
615     otherusers = User.objects.exclude(id__in=[fu.user.id for fu in fleetusers]).order_by('name')
616     return render_to_response('fleet_users.html', {'fleet':fleet, 'fleetusers': fleetusers, 'otherusers': otherusers}, RequestContext(request))
617
618
619 @http_authenticate(auth, 'ais')
620 def fleet_lastpos(request, fleetname):
621     fleet = get_object_or_404(Fleet, name=fleetname)
622     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
623         return HttpResponseForbidden('<h1>Forbidden</h1>')
624     fleet_uset = load_fleet_to_uset(fleet.id)
625     value = kml_to_kmz(format_fleet(fleet_uset, document_name=fleetname+' fleet').encode('utf-8'))
626     response = HttpResponse(value, mimetype="application/vnd.google-earth.kml")
627     response['Content-Disposition'] = 'attachment; filename=%s.kmz' % fleetname
628     return response
629
630
631 @http_authenticate(auth, 'ais')
632 def jobs_index(request):
633     show_archive = request.REQUEST.has_key('archive')
634     if show_archive:
635         jobs = request.user.job_set.all()
636     else:
637         jobs = request.user.job_set.filter(archive_time__isnull=True)
638     
639     response = render_to_response('jobs.html', {'jobs': jobs, 'queue_size': Job.queue_size(), 'archive': show_archive }, RequestContext(request))
640     response['Refresh'] = 15
641     return response
642
643 @http_authenticate(auth, 'ais')
644 def job_detail(request, jobid):
645     job = get_object_or_404(Job, id=jobid)
646     if job.user != request.user:
647         return HttpResponseForbidden('403 Forbidden')
648     response = render_to_response('job.html', {'job': job, 'queue_size': Job.queue_size()}, RequestContext(request))
649     if not job.finish_time:
650         response['Refresh'] = 5
651     elif not job.archive_time:
652         # finished but not archived:
653         response['Refresh'] = '0;url=https://ais.nirgal.com/job/%s/download' % job.id
654         job.archive_time = datetime.utcnow()
655         job.save()
656     return response
657
658 @http_authenticate(auth, 'ais')
659 def job_get(request, jobid):
660     job = get_object_or_404(Job, id=jobid)
661     if job.user != request.user:
662         return HttpResponseForbidden('403 Forbidden')
663     total_wait = 0
664     while not job.finish_time:
665         sleep(1)
666         total_wait += 1
667         if total_wait > 15:
668             request.user.info('Your query is slow. Please be patient.')
669             return HttpResponseRedirect('/job/%s/' % job.id)
670         job = get_object_or_404(Job, id=jobid)
671     
672     if not job.archive_time:
673         job.archive_time = datetime.utcnow()
674         job.save()
675
676     if job.result:
677         # Error occured
678          return HttpResponseRedirect('/job/%s/' % job.id)
679     extension = os.path.splitext(job.friendly_filename)[-1]
680     return HttpResponseRedirect('/job_result/%s%s/%s' % (job.id, extension, job.friendly_filename))
681
682 @http_authenticate(auth, 'ais')
683 def users(request):
684     users = User.objects.order_by('name')
685     for user in users:
686         user.admin_ok = user.is_admin_by(request.user.id)
687     if request.REQUEST.has_key('showtree'):
688         local_users = {}
689         for user in users:
690             user.children = []
691             local_users[user.id] = user
692         for user in users:
693             if user.father_id == None:
694                 root = user
695             else:
696                 local_users[user.father_id].children.append(user)
697         assert root
698         return render_to_response('users_tree.html', {'root': root, 'auser': request.user.id}, RequestContext(request))
699     else:
700         return render_to_response('users.html', {'users':users}, RequestContext(request))
701
702 phone_re = re.compile('^\\+.+')
703
704 class UserEditForm(forms.Form):
705     login = forms.RegexField(regex=r'^[a-zA-Z0-9_]+$', max_length=16,
706         error_message ='Login must only contain letters, numbers and underscores')
707     name = forms.CharField(max_length=50)
708     email = forms.EmailField()
709     phone = forms.RegexField(regex='\\+.+', max_length=20, required=False,
710         error_message ='Phones must start with a \'+\'')
711     def __init__(self, *args, **kargs):
712         forms.Form.__init__(self, *args, **kargs)
713         self.old_login = kargs['initial']['login']
714     def clean_login(self):
715         new_login = self.cleaned_data['login']
716         if new_login != self.old_login:
717             if User.objects.filter(login=new_login).count():
718                 raise forms.ValidationError("Sorry that login is already in use. Try another one.")
719             if new_login == u'add':
720                 raise forms.ValidationError("Sorry that login is reserved. Try another one.")
721         return new_login
722
723 @http_authenticate(auth, 'ais')
724 def user_detail(request, login):
725     user = get_object_or_404(User, login=login)
726     user.admin_ok = user.is_admin_by(request.user.id)
727     return render_to_response('user_detail.html', {'auser': user}, RequestContext(request))
728
729 @http_authenticate(auth, 'ais')
730 def user_edit(request, login):
731     initial = {}
732     if login:
733         user = get_object_or_404(User, login=login)
734         if not user.is_admin_by(request.user.id):
735             return HttpResponseForbidden('403 Forbidden')
736     else:
737         user = User()
738         user.father_id = request.user.id;
739     initial['login'] = user.login
740     initial['name'] = user.name
741     initial['email'] = user.email
742     initial['phone'] = user.phone
743     if request.method == 'POST':
744         form = UserEditForm(request.POST, initial=initial)
745         if form.is_valid():
746             user.login = form.cleaned_data['login']
747             user.name = form.cleaned_data['name']
748             user.email = form.cleaned_data['email']
749             user.phone = form.cleaned_data['phone']
750             user.save()
751             user.check_sandbox_access(request.user)
752             return HttpResponseRedirect('/user/')
753     else: # GET
754         form = UserEditForm(initial=initial)
755
756     return render_to_response('user_edit.html', {'form':form, 'auser': user}, RequestContext(request))
757
758
759 class ChangePasswordForm(forms.Form):
760     new_password = forms.CharField(max_length=16, widget=forms.PasswordInput())
761     new_password_check = forms.CharField(max_length=16, widget=forms.PasswordInput())
762
763     def clean_generic_password(self, field_name):
764         password = self.cleaned_data[field_name]
765         try:
766             crack.FascistCheck(password)
767         except ValueError, err:
768             raise forms.ValidationError(err.message)
769         return password
770
771     def clean_new_password(self):
772         return self.clean_generic_password('new_password')
773
774     def clean_new_password_check(self):
775         return self.clean_generic_password('new_password_check')
776
777     def clean(self):
778         cleaned_data = self.cleaned_data
779         pass1 = cleaned_data.get('new_password')
780         pass2 = cleaned_data.get('new_password_check')
781         if pass1 != pass2 and pass2:
782             self._errors['new_password_check'] = forms.util.ErrorList(['Passwords must match'])
783             del cleaned_data['new_password_check'] # FIXME
784         return cleaned_data
785
786
787 @http_authenticate(auth, 'ais')
788 def user_change_password(request, login):
789     user = get_object_or_404(User, login=login)
790     if not user.is_admin_by(request.user.id):
791         return HttpResponseForbidden('403 Forbidden')
792     if request.method == 'POST':
793         form = ChangePasswordForm(request.POST)
794         if form.is_valid():
795             user.set_password(form.cleaned_data['new_password'])
796             user.save()
797             return HttpResponseRedirect('/user/')
798     else: # GET
799         form = ChangePasswordForm()
800     return render_to_response('user_change_password.html', {'form':form, 'auser':user}, RequestContext(request))
801
802
803 @http_authenticate(auth, 'ais')
804 def user_delete(request, login):
805     user = get_object_or_404(User, login=login)
806     if not user.is_admin_by(request.user.id):
807         return HttpResponseForbidden('403 Forbidden')
808     if request.REQUEST.get('confirm', None):
809         user.delete()
810         return HttpResponseRedirect('/user/')
811     return render_to_response('user_delete.html', {'form':None, 'auser':user}, RequestContext(request))
812
813
814 def logout(request):
815     # TODO
816     return HttpResponse('Not implemented')
817     #response = render_to_response('logout.html', {}, RequestContext(request))
818     #return response
819
820 periods = ({
821     'name_tiny': '2h',
822     'name_long': '2 hours',
823     'seconds': 2*60*60
824     }, {
825     'name_tiny': '6h',
826     'name_long': '6 hours',
827     'seconds': 6*60*60,
828     'default': True,
829     }, {
830     'name_tiny': '2d',
831     'name_long': '2 days',
832     'seconds': 2*24*60*60
833     }, {
834     'name_tiny': '14d',
835     'name_long': '2 weeks',
836     'seconds': 14*24*60*60
837     }, {
838     'name_tiny': '90d',
839     'name_long': '3 monthes',
840     'seconds': 90*24*60*60
841     }, {
842     'name_tiny': '180d',
843     'name_long': '6 monthes',
844     'seconds': 180*24*60*60
845     })
846         
847 class StatsSelectorForm(forms.Form):
848     peers = forms.ChoiceField(choices=[('', 'All')] + [(id4,source['name']) for id4,source in peers_get_config().iteritems()])
849     types = forms.ChoiceField(choices=[('', 'All'), ('bytes', 'Bandwidth'), ('counts', "Packet'izer")], initial='counts')
850     periods = forms.ChoiceField(choices=[(period['name_tiny'], period['name_long']) for period in periods] + [('', 'All')], initial='2d')
851
852 @http_authenticate(auth, 'ais')
853 def sources_index(request):
854     active_minutes = 5
855     peers_config = peers_get_config()
856     peers_display = []
857     for id4, peer in peers_config.iteritems():
858         peer['id4'] = id4
859         peer['id2'] = id4[2:]
860         peer['active'] = is_id4_active(id4, 60*active_minutes)
861         peers_display.append(peer)
862         
863     peers_display = sorted(peers_display, key=lambda k: k['id4'])
864
865     form = StatsSelectorForm()
866
867     return render_to_response('sources_index.html', {'sources':peers_display, 'active_minutes': active_minutes, 'form': form}, RequestContext(request))
868
869
870 @http_authenticate(auth, 'ais')
871 def sources_stats(request):
872     filter_peers = request.REQUEST.get('peers', None)
873     if filter_peers:
874         filter_peers = filter_peers.split(u',')
875
876     peers_config = peers_get_config()
877     peers_display = []
878
879     for id4, peer in peers_config.iteritems():
880         if filter_peers:
881             if unicode(id4) not in filter_peers:
882                 continue
883         peer['id4'] = id4
884         peers_display.append(peer)
885
886     peers_display = sorted(peers_display, key=lambda k: k['id4'])
887
888     filter_types = request.REQUEST.get('types', None)
889     if filter_types:
890         filter_types = filter_types.split(u',')
891     else:
892         filter_types = [ 'bytes', 'counts' ]
893     logging.error('filter_types=%s', filter_types)
894
895     filter_periods = request.REQUEST.get('periods', None)
896     if filter_periods:
897         display_periods = []
898         for period_name in filter_periods.split(u','):
899             for period in periods:
900                 if period['name_tiny'] == period_name:
901                     display_periods.append(period)
902     else:
903         display_periods = periods
904     #logging.error('display_periods=%s', display_periods)
905
906
907     now = int(get_timestamp())
908     for config in peers_display:
909         id4 = config['id4']
910         for period in periods:
911             args = os.path.join(STATS_DIR, id4+'-'+period['name_tiny']+'-bytes.png'), \
912                 '--lazy', \
913                 '-l', '0', \
914                 '--title', config['name'] + ' - Bandwidth usage - ' + period['name_long'], \
915                 '--start', '%d' % (now - period['seconds']), \
916                 '--end', '%d' % now, \
917                 '--vertical-label', 'bps', \
918                 'DEF:bytes=%s:bytes:AVERAGE' % os.path.join(STATS_DIR, id4+'.rrd'), \
919                 'DEF:rawbytes=%s:rawbytes:AVERAGE' % os.path.join(STATS_DIR, id4+'.rrd'), \
920                 'CDEF:bits=bytes,8,*', \
921                 'CDEF:rawbits=rawbytes,8,*', \
922                 'LINE:bits#00FF00:IP payload', \
923                 'LINE:rawbits#FF0000:IP with headers'
924             rrdtool.graph(*args)
925             args = os.path.join(STATS_DIR, id4+'-'+period['name_tiny']+'-counts.png'), \
926                 '--lazy', \
927                 '-l', '0', \
928                 '--title', config['name'] + ' - Packet\'izer stats - ' + period['name_long'], \
929                 '--start', '%d' % (now-period['seconds']), \
930                 '--end', '%d' % now, \
931                 '--vertical-label', 'Hz', \
932                 'DEF:packets=%s:packets:AVERAGE' % os.path.join(STATS_DIR, id4+'.rrd'), \
933                 'DEF:lines=%s:lines:AVERAGE' % os.path.join(STATS_DIR, id4+'.rrd'), \
934                 'LINE:packets#FF0000:input packets', \
935                 'LINE:lines#00FF00:AIVDM lines'
936             rrdtool.graph(*args)
937     return render_to_response('sources.html', {'sources':peers_display, 'show_bytes': 'bytes' in filter_types,  'show_counts': 'counts' in filter_types, 'periods': display_periods}, RequestContext(request))
938
939
940 @http_authenticate(auth, 'ais')
941 def news(request, page):
942     NEWS_PER_PAGE = 10
943     if page:
944         page = int(page)
945     else:
946         page = 1
947     # TODO __future__
948     npages = int((News.objects.count() + NEWS_PER_PAGE - 1) / NEWS_PER_PAGE)
949     news = News.objects.order_by('-created')[(page - 1) * NEWS_PER_PAGE : page * NEWS_PER_PAGE]
950     return render_to_response('news.html', {'news':news, 'page': page, 'npages': npages}, RequestContext(request))
951
952 @http_authenticate(auth, 'ais')
953 def news_atom(request):
954     from django.db import connection
955
956     news = News.objects.order_by('-created')
957
958     cursor = connection.cursor()
959     cursor.execute("SELECT MAX(updated) FROM news")
960     row = cursor.fetchone()
961     last_update = row[0]
962
963     return render_to_response('news.atom', {'news':news, 'last_update':last_update}, RequestContext(request), mimetype='application/atom+xml')