Added "create fleet" feature
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sun, 7 Nov 2010 11:21:29 +0000 (11:21 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sun, 7 Nov 2010 11:21:29 +0000 (11:21 +0000)
bin/djais/views.py
html_templates/fleet_edit.html
html_templates/fleets.html

index f3408cab4001fdfe64ffd987847980ae774d9b74..3a76a759aa8b746ec48a4f1178eb9843facf419e 100644 (file)
@@ -460,20 +460,28 @@ class FleetEditForm(forms.Form):
 
 @http_authenticate(auth, 'ais')
 def fleet_edit(request, fleetname):
-    # TODO fleetname = None => add
-    fleet = get_object_or_404(Fleet, name=fleetname)
-    if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
-        return HttpResponseForbidden('<h1>Forbidden</h1>')
-
     initial = {}
-    initial['name'] = fleet.name
-    initial['description'] = fleet.description
+    if fleetname is not None:
+        fleet = get_object_or_404(Fleet, name=fleetname)
+        if not FleetUser.objects.filter(fleet=fleet.id, user=request.user.id).all():
+            return HttpResponseForbidden('<h1>Forbidden</h1>')
+
+        initial['name'] = fleet.name
+        initial['description'] = fleet.description
+    else:
+        fleet = Fleet()
+
     if request.method == 'POST':
         form = FleetEditForm(request.POST, initial=initial)
         if form.is_valid():
             fleet.name = form.cleaned_data['name']
             fleet.description = form.cleaned_data['description']
             fleet.save()
+            if not fleetname: # new one
+                fu = FleetUser()
+                fu.fleet = fleet
+                fu.user = request.user
+                fu.save()
             request.user.info('Fleet updated')
             return HttpResponseRedirect('/fleet/%s/' % fleet.name)
     else: # GET
index b7cd0a1f7d967752346255179ea40a5a95a8976a..914328e4bddc22b7bbb4bd4b542ee0c8fa3ba240 100644 (file)
@@ -2,11 +2,19 @@
 
 {% block breadcrumbs %}
 {{ block.super }}
+{% if fleet.id %}
 / <a href="/fleet/{{fleet.name}}/edit">edit</a>
+{% else %}
+<a href="/fleet/add">add</a>
+{% endif %}
 {% endblock %}
 
 {% block content %}
+{% if fleet.id %}
 <h3>Edition of fleet {{ fleet.name }}</h3>
+{% else %}
+<h3>Creation of a new fleet</h3>
+{% endif %}
 <form method=post>
 {% include "fragment_formerror.html" %}
 <table>
index 2f00ad4f126ec8cb76a22d84b905bc2ddaf7c30e..d872b2a659ac9915cf0c5e532ba2baba25f8cf64 100644 (file)
@@ -32,4 +32,7 @@ div.fleet span.fleetname {
 
 <br><br>
 {% endfor %}
+
+<a href=add class=button>Create a new fleet</a>
+
 {% endblock %}