Changed fleet identifier from name to a proper serial id
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 6 Nov 2010 14:01:03 +0000 (14:01 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 6 Nov 2010 14:01:03 +0000 (14:01 +0000)
bin/common.py
bin/djais/models.py
bin/djais/urls.py
bin/djais/views.py
html_templates/fleet.html
structure.sql

index 5bad99bfb0f4fdfeefe4f682f808e76e04e51231..f97420b8e5291002c2854fa794228287e99e5bef 100755 (executable)
@@ -1718,7 +1718,7 @@ def load_fleet_to_uset(fleetname):
     Returns an array of strmmsi.
     """
     result = []
-    sqlexec(u"SELECT mmsi FROM fleet_vessel WHERE fleet=%(fleetname)s", {'fleetname': fleetname})
+    sqlexec(u"SELECT mmsi FROM fleet_vessel JOIN fleet ON (fleet_vessel.fleet_id = fleet.id) WHERE fleet.name=%(fleetname)s", {'fleetname': fleetname})
     cursor = get_common_cursor()
     while True:
         row = cursor.fetchone()
index e14c072aac985ec488f2ef1ad4608ac648a70a15..6e70836410d27732c6c465a907d901d8a68553b3 100644 (file)
@@ -78,16 +78,18 @@ class User(models.Model):
         UserMessage(user_id = self.id, category_id=category_id, txt=message).save()
 
     def check_sandbox_access(self, source_user=None):
+        SANDBOX_FLEET = 1
         try:
-            FleetUser.objects.get(fleet = 'sandbox', user = self)
+            FleetUser.objects.get(fleet = SANDBOX_FLEET, user = self)
         except FleetUser.DoesNotExist:
             fu = FleetUser()
             fu.user_id = self.id
-            fu.fleet_id = 'sandbox'
+            fu.fleet_id = SANDBOX_FLEET
             fu.save()
             if source_user:
                 source_user.info("%s was granted access to 'sandbox' fleet" % self.login)
 
+
 class Vessel(models.Model):
     mmsi = models.IntegerField(primary_key=True)
     name = models.CharField(max_length=20)
@@ -110,8 +112,10 @@ class Vessel(models.Model):
         strmmsi = mmsi_to_strmmsi(self.mmsi)
         return Nmea.new_from_lastinfo(strmmsi)
 
+
 class Fleet(models.Model):
-    name = models.CharField(max_length=50, primary_key=True)
+    id = models.AutoField(primary_key=True)
+    name = models.CharField(max_length=50)
     vessel = models.ManyToManyField(Vessel, through='FleetVessel')
     description = models.TextField()
     class Meta:
@@ -119,9 +123,9 @@ class Fleet(models.Model):
     def __unicode__(self):
         return self.name
     def vessel_count(self):
-        return FleetVessel.objects.filter(fleet=self.name).count()
+        return FleetVessel.objects.filter(fleet=self.id).count()
     def user_count(self):
-        return FleetUser.objects.filter(fleet=self.name).count()
+        return FleetUser.objects.filter(fleet=self.id).count()
     def job_count(self):
         if os.path.exists('/var/lib/ais/cron/fleets/%s.cron' % self.name):
             return 1
@@ -130,14 +134,14 @@ class Fleet(models.Model):
 
 class FleetUser(models.Model):
     id = models.AutoField(primary_key=True)
-    fleet = models.ForeignKey(Fleet, db_column='fleet', to_field='name')
+    fleet = models.ForeignKey(Fleet) #, db_column='fleet_id', to_field='id')
     user = models.ForeignKey(User)
     class Meta:
         db_table = u'fleet_user'
 
 class FleetVessel(models.Model):
     id = models.AutoField(primary_key=True)
-    fleet = models.ForeignKey(Fleet, db_column='fleet', to_field='name')
+    fleet = models.ForeignKey(Fleet, db_column='fleet_id', to_field='id')
     vessel = models.ForeignKey(Vessel, db_column='mmsi', to_field='mmsi')
     class Meta:
         db_table = u'fleet_vessel'
index e9d15fcdd49e3c3c52121c946797ee6007de3333..2a0f43120164828f9d80798ea3d1b8950a8efbae 100644 (file)
@@ -9,6 +9,7 @@ urlpatterns = patterns('',
     (r'^$', 'ais.djais.views.index'),
     (r'^fleet/$', 'ais.djais.views.fleets'),
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/$', 'ais.djais.views.fleet'),
+    (r'^fleet/add$', 'ais.djais.views.fleet_edit', { 'fleetname': None}),
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/edit$', 'ais.djais.views.fleet_edit'),
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/list$', 'ais.djais.views.fleet_vessels'),
     (r'^fleet/(?P<fleetname>[ a-zA-Z0-9_]+)/add$', 'ais.djais.views.fleet_vessel_add'),
index d94ba5ed59aa2e82d0b92d97ad513e223b71a2fb..5870d73dfcecd249c5a17bae59265d39265b78be 100644 (file)
@@ -438,23 +438,26 @@ def fleets(request):
 
 @http_authenticate(auth, 'ais')
 def fleet(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    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>')
     return render_to_response('fleet.html', {'fleet':fleet}, RequestContext(request))
 
 
 class FleetEditForm(forms.Form):
+    #name = forms.CharField()
     description = forms.CharField(widget=forms.Textarea)
 
 @http_authenticate(auth, 'ais')
 def fleet_edit(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    # 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['description'] = fleet.description
+    initial['id'] = fleet.name
     if request.method == 'POST':
         form = FleetEditForm(request.POST, initial=initial)
         if form.is_valid():
@@ -469,8 +472,8 @@ def fleet_edit(request, fleetname):
 
 @http_authenticate(auth, 'ais')
 def fleet_vessels(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    fleet = get_object_or_404(Fleet, name=fleetname)
+    if not FleetUser.objects.filter(fleet=fleet, user=request.user.id).all():
         return HttpResponseForbidden('<h1>Forbidden</h1>')
     vessels = fleet.vessel.all()
     return render_to_response('fleet_vessels.html', {'fleet':fleet, 'vessels': vessels}, RequestContext(request))
@@ -478,8 +481,8 @@ def fleet_vessels(request, fleetname):
 
 @http_authenticate(auth, 'ais')
 def fleet_vessel_add(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    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>')
     strmmsi = request.REQUEST['mmsi']
     mmsi = strmmsi_to_mmsi(strmmsi)
@@ -513,8 +516,8 @@ class FleetAddVessel(forms.Form):
 
 @http_authenticate(auth, 'ais')
 def fleet_vessel_add2(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    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>')
     if request.method == 'POST' or request.META['QUERY_STRING']:
         form = FleetAddVessel(request.REQUEST)
@@ -553,8 +556,8 @@ def fleet_vessel_add2(request, fleetname):
 
 @http_authenticate(auth, 'ais')
 def fleet_users(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    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>')
 
     message = u''
@@ -587,13 +590,14 @@ def fleet_users(request, fleetname):
 
     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))
 
 
 @http_authenticate(auth, 'ais')
 def fleet_lastpos(request, fleetname):
-    fleet = get_object_or_404(Fleet, pk=fleetname)
-    if not FleetUser.objects.filter(fleet=fleetname, user=request.user.id).all():
+    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>')
     fleet_uset = load_fleet_to_uset(fleetname)
     # = set([mmsi_to_strmmsi(vessel.mmsi) for vessel in fleet.vessel.all()])
index e392aa5a2cabad71798eaccc54fbb5edabbb0147..af8e64330f05c073b1d6b7ab228c8678f742c5a4 100644 (file)
@@ -11,6 +11,6 @@
 <a href=lastpos class=button>Last positions in Google Earth</a><br>
 <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 descrription</a><br>
+<a href=edit class=button>Change description</a><br>
 
 {% endblock %}
index 594ef537777ca8acac28dcf9c458cd7d62dbab78..c517e6882486bc769e2759755ebd50ee6c413d2a 100644 (file)
@@ -21,18 +21,37 @@ SET default_with_oids = false;
 CREATE TABLE fleet (
     name character varying(50) NOT NULL,
     description text DEFAULT ''::text NOT NULL,
-    created timestamp without time zone DEFAULT now() NOT NULL
+    created timestamp without time zone DEFAULT now() NOT NULL,
+    id integer NOT NULL
 );
 
 
+--
+-- Name: fleet_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE fleet_id_seq
+    INCREMENT BY 1
+    NO MAXVALUE
+    NO MINVALUE
+    CACHE 1;
+
+
+--
+-- Name: fleet_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE fleet_id_seq OWNED BY fleet.id;
+
+
 --
 -- Name: fleet_user; Type: TABLE; Schema: public; Owner: -; Tablespace: 
 --
 
 CREATE TABLE fleet_user (
     id integer NOT NULL,
-    fleet character varying(50) NOT NULL,
-    user_id integer NOT NULL
+    user_id integer NOT NULL,
+    fleet_id integer NOT NULL
 );
 
 
@@ -60,8 +79,8 @@ ALTER SEQUENCE fleet_user_id_seq OWNED BY fleet_user.id;
 
 CREATE TABLE fleet_vessel (
     id integer NOT NULL,
-    fleet character varying(50) NOT NULL,
-    mmsi integer NOT NULL
+    mmsi integer NOT NULL,
+    fleet_id integer NOT NULL
 );
 
 
@@ -283,6 +302,13 @@ CREATE TABLE vessel (
 );
 
 
+--
+-- Name: id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE fleet ALTER COLUMN id SET DEFAULT nextval('fleet_id_seq'::regclass);
+
+
 --
 -- Name: id; Type: DEFAULT; Schema: public; Owner: -
 --
@@ -330,7 +356,7 @@ ALTER TABLE user_message ALTER COLUMN id SET DEFAULT nextval('user_message_id_se
 --
 
 ALTER TABLE ONLY fleet
-    ADD CONSTRAINT fleet_pkey PRIMARY KEY (name);
+    ADD CONSTRAINT fleet_pkey PRIMARY KEY (id);
 
 
 --
@@ -346,7 +372,7 @@ ALTER TABLE ONLY fleet_user
 --
 
 ALTER TABLE ONLY fleet_user
-    ADD CONSTRAINT fleet_user_pkey2 UNIQUE (fleet, user_id);
+    ADD CONSTRAINT fleet_user_pkey2 UNIQUE (user_id, fleet_id);
 
 
 --
@@ -362,7 +388,7 @@ ALTER TABLE ONLY fleet_vessel
 --
 
 ALTER TABLE ONLY fleet_vessel
-    ADD CONSTRAINT fleet_vessel_pkey2 UNIQUE (fleet, mmsi);
+    ADD CONSTRAINT fleet_vessel_pkey2 UNIQUE (mmsi, fleet_id);
 
 
 --
@@ -453,11 +479,11 @@ CREATE INDEX user_message_user_idx ON user_message USING btree (user_id);
 
 
 --
--- Name: fleet_user_fleet_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
+-- Name: fleet_user_fleet_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
 
 ALTER TABLE ONLY fleet_user
-    ADD CONSTRAINT fleet_user_fleet_fkey FOREIGN KEY (fleet) REFERENCES fleet(name) ON UPDATE CASCADE ON DELETE CASCADE;
+    ADD CONSTRAINT fleet_user_fleet_id_fkey FOREIGN KEY (fleet_id) REFERENCES fleet(id) ON UPDATE CASCADE ON DELETE CASCADE;
 
 
 --
@@ -469,11 +495,11 @@ ALTER TABLE ONLY fleet_user
 
 
 --
--- Name: fleet_vessel_fleet_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
+-- Name: fleet_vessel_fleet_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
 
 ALTER TABLE ONLY fleet_vessel
-    ADD CONSTRAINT fleet_vessel_fleet_fkey FOREIGN KEY (fleet) REFERENCES fleet(name) ON UPDATE CASCADE ON DELETE CASCADE;
+    ADD CONSTRAINT fleet_vessel_fleet_id_fkey FOREIGN KEY (fleet_id) REFERENCES fleet(id) ON UPDATE CASCADE ON DELETE CASCADE;
 
 
 --