Improved error message when looking for an invalid mmsi number
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 5 Nov 2010 23:13:40 +0000 (23:13 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 5 Nov 2010 23:13:40 +0000 (23:13 +0000)
Fixed Integrity error handling when try to add a second time a ship to a fleet
Nicer feedback to ship add, redirect to ships list.

bin/djais/models.py
bin/djais/views.py

index 9cb484d19f75613115369a5cbc5eaf9016f19612..b67ae2c39ca35f3afcaf5146db516df0575ad8dc 100644 (file)
@@ -74,8 +74,8 @@ class User(models.Model):
         messages.delete()
         return messages_dict
     
-    def info(self, message):
-        UserMessage(user_id = self.id, category_id='info', txt=message).save()
+    def info(self, message, category_id='info'):
+        UserMessage(user_id = self.id, category_id=category_id, txt=message).save()
 
 
 class Vessel(models.Model):
index 5a537b9ee80ac184e63908bd0414c08f02c00aeb..025a8e6defbf0e8e99cc3f2f9d492eb15c02bab9 100644 (file)
@@ -75,6 +75,13 @@ class VesselSearchForm(forms.Form):
                 return cleaned_data
         raise forms.ValidationError("You must enter at least one criteria")
 
+    def clean_mmsi(self):
+        mmsi = self.cleaned_data['mmsi']
+        try:
+            mmsi = strmmsi_to_mmsi(mmsi)
+        except AssertionError:
+            raise forms.ValidationError("Invalid MMSI")
+        return mmsi
 
 
 @http_authenticate(auth, 'ais')
@@ -85,7 +92,7 @@ def vessel_search(request):
             data = form.cleaned_data
             vessels = Vessel.objects
             if data['mmsi']:
-                vessels = vessels.filter(mmsi=strmmsi_to_mmsi(data['mmsi']))
+                vessels = vessels.filter(mmsi=data['mmsi'])
             if data['name']:
                 vessels = vessels.filter(name__contains=data['name'].upper())
             if data['imo']:
@@ -425,7 +432,7 @@ def vessel_csv(request, strmmsi):
 
 @http_authenticate(auth, 'ais')
 def fleets(request):
-    fleetusers = request.user.fleetuser_set.all()
+    fleetusers = request.user.fleetuser_set.order_by('fleet')
     return render_to_response('fleets.html', {'fleetusers':fleetusers}, RequestContext(request))
 
 
@@ -479,12 +486,16 @@ def fleet_vessel_add(request, fleetname):
     try:
         vessel = Vessel.objects.get(pk=mmsi)
     except Vessel.DoesNotExist:
-        return HttpResponse('No such vessel')
-    try:
-        FleetVessel(fleet=fleet, vessel=vessel).save()
-    except IntegrityError:
-        return HttpResponse('Integrity error: Is the ship already in that fleet?')
-    return HttpResponse('Done')
+        request.user.info('No such vessel', category_id='error')
+    else:
+        try:
+            fv = FleetVessel.objects.get(fleet=fleet, vessel=vessel)
+        except FleetVessel.DoesNotExist:
+            FleetVessel(fleet=fleet, vessel=vessel).save()
+            request.user.info(u'Vessel %s added to fleet %s.' % (vessel.name, fleetname))
+        else:
+            request.user.info('Vessel with MMSI %s is already in that fleet' % strmmsi, category_id='error')
+    return HttpResponseRedirect('/fleet/%s/list' % fleetname)
 
 
 class FleetAddVessel(forms.Form):