Delete fleet when deleting last user
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sun, 7 Nov 2010 13:38:34 +0000 (13:38 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sun, 7 Nov 2010 13:38:34 +0000 (13:38 +0000)
Split User.info function: use User.error for error category messages.
Remove use of message var in fleet_users view in favor of new User.info function
Added tip for deleting a fleet.

bin/djais/models.py
bin/djais/views.py
html_templates/fleet.html
html_templates/fleet_users.html

index 6e70836410d27732c6c465a907d901d8a68553b3..1f5409939c882af2fabea85a36b06f5c38b79fa4 100644 (file)
@@ -74,8 +74,11 @@ class User(models.Model):
         messages.delete()
         return messages_dict
     
-    def info(self, message, category_id='info'):
-        UserMessage(user_id = self.id, category_id=category_id, txt=message).save()
+    def info(self, message):
+        UserMessage(user_id = self.id, category_id=u'info', txt=message).save()
+
+    def error(self, message):
+        UserMessage(user_id = self.id, category_id=u'error', txt=message).save()
 
     def check_sandbox_access(self, source_user=None):
         SANDBOX_FLEET = 1
index 3a76a759aa8b746ec48a4f1178eb9843facf419e..983bd8656066b91764c71304da0dbb1da08f4367 100644 (file)
@@ -508,7 +508,7 @@ def fleet_vessel_add(request, fleetname):
     try:
         vessel = Vessel.objects.get(pk=mmsi)
     except Vessel.DoesNotExist:
-        request.user.info('No such vessel', category_id='error')
+        request.user.error('No such vessel')
     else:
         try:
             fv = FleetVessel.objects.get(fleet=fleet, vessel=vessel)
@@ -516,7 +516,7 @@ def fleet_vessel_add(request, fleetname):
             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')
+            request.user.error('Vessel with MMSI %s is already in that fleet' % strmmsi)
     return HttpResponseRedirect('/fleet/%s/list' % fleetname)
 
 
@@ -579,19 +579,18 @@ def fleet_users(request, fleetname):
     if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
         return HttpResponseForbidden('<h1>Forbidden</h1>')
 
-    message = u''
     if request.method == 'POST' or request.META['QUERY_STRING']:
         action = request.REQUEST['action']
         userlogin = request.REQUEST['user']
         try:
             user = User.objects.get(login=userlogin)
         except User.DoesNotExist:
-            message = u'User %s does not exist.' % userlogin
+            request.user.error(u'User %s does not exist.' % userlogin)
         else:
             if action == u'add':
                 try:
                     fu = FleetUser.objects.get(fleet=fleet, user=user)
-                    message = u'User %s already has access.' % user.login
+                    request.user.error(u'User %s already has access.' % user.login)
                 except FleetUser.DoesNotExist:
                     FleetUser(fleet=fleet, user=user).save()
                     #TODO log
@@ -602,15 +601,18 @@ def fleet_users(request, fleetname):
                     fu.delete()
                     #TODO log
                     request.user.info(u'Revoked access to user %s.' % user.login)
+                    if FleetUser.objects.filter(fleet=fleet).count() == 0:
+                        fleet.delete()
+                        request.user.info(u"Deleted fleet %s (no more users)." % fleet.name)
+                        return HttpResponseRedirect('/fleet/')
                 except FleetUser.DoesNotExist:
-                    message = u'User %s didn\'t have access.' % user.login
+                    request.user.error(u'User %s didn\'t have access.' % user.login)
             else:
-                message = u'Unknown action %s' % action
+                request.user.error(u'Unknown action %s' % action)
 
     fleetusers = fleet.fleetuser_set.all()
     otherusers = User.objects.exclude(id__in=[fu.user.id for fu in fleetusers]).order_by('name')
-    #FIXME: don't use message anymore!
-    return render_to_response('fleet_users.html', {'fleet':fleet, 'fleetusers': fleetusers, 'otherusers': otherusers, 'message': message }, RequestContext(request))
+    return render_to_response('fleet_users.html', {'fleet':fleet, 'fleetusers': fleetusers, 'otherusers': otherusers}, RequestContext(request))
 
 
 @http_authenticate(auth, 'ais')
index 995a20130f06cc9e76878276a7a54c724ed5b459..99319a7a9780af18cd2692791b3d1b6555cefe91 100644 (file)
@@ -12,5 +12,7 @@
 <a href=list class=button>Vessel list</a> ({{ fleet.vessel_count }} vessel{{ fleet.vessel_count|pluralize }})<br>
 <a href=users class=button>User list</a> ({{ fleet.user_count }} user{{ fleet.user_count|pluralize }})<br>
 <a href=edit class=button>Change details</a><br>
+<br>
+Tip: If you want to delete a fleet, just click "User list" above, and revoke your own access.<br>
 
 {% endblock %}
index 0a36663caac642a0471d0605dfbb7d7b83454f40..a994edfab589cda751767d0b2a66cde0589fca58 100644 (file)
@@ -6,8 +6,6 @@
 {% endblock %}
 
 {% block content %}
-{{ message }}
-
 <h3>Users of {{ fleet.name }} fleet</h3>
 <ul>
 {% for fleetuser in fleetusers %}