Browse Source

switch : premier et dernier numéro de port

Élie Bouttier 7 years ago
parent
commit
7af7def929

+ 3 - 3
services/admin.py

@@ -678,13 +678,13 @@ class AntennaAdmin(admin.ModelAdmin):
 
 
 class SwitchAdmin(admin.ModelAdmin):
-    list_display = ('name', 'size',)
-    fields = ('name', 'size', 'notes',)
+    list_display = ('name',)
+    fields = ('name', 'first_port', 'last_port', 'notes',)
     inlines = (SwitchPortInline,)
 
     def get_readonly_fields(self, request, obj=None):
         if obj:
-            return ('size',)
+            return ('first_port', 'last_port',)
         else:
             return ()
 

+ 5 - 2
services/management/commands/fetchport.py

@@ -27,6 +27,8 @@ class Command(BaseCommand):
                 if not g:
                     continue
                 port = int(g.group('id'))
+                if port < sw.first_port or port > sw.last_port:
+                    continue
                 g = status_regex.match(info)
                 if not g:
                     continue
@@ -36,8 +38,9 @@ class Command(BaseCommand):
                 else:
                     assert(status == 'down')
                     down.append(port)
-            unknown = [] # TODO
+            unknown = set(range(sw.first_port, sw.last_port + 1)) - set(up) - set(down)
             upped = sw.ports.filter(port__in=up).exclude(up=True).update(up=True)
             downed = sw.ports.filter(port__in=down).exclude(up=False).update(up=False)
             unknowned = sw.ports.filter(port__in=unknown).exclude(up__isnull=True).update(up=None)
-            print("Switch %s: UP: %d (%+d), DOWN: %d (%+d), UNKNOWN: %d (%+d)" % (sw.name, len(up), upped, len(down), downed, len(unknown), unknowned))
+            #print("Switch %s: UP: %d (%+d), DOWN: %d (%+d), UNKNOWN: %d (%+d)" \
+            #        % (sw.name, len(up), upped, len(down), downed, len(unknown), unknowned))

+ 48 - 0
services/migrations/0047_auto_20180210_1829.py

@@ -0,0 +1,48 @@
+# Generated by Django 2.0.2 on 2018-02-10 17:29
+
+import django.core.validators
+from django.db import migrations, models
+
+
+def forward(apps, schema_editor):
+    db_alias = schema_editor.connection.alias
+    Switch = apps.get_model("services", "Switch")
+    for sw in Switch.objects.using(db_alias).all():
+        sw.first_port = sw.ports.order_by('port').first().port
+        sw.last_port = sw.ports.order_by('port').last().port
+        sw.save()
+
+
+def backward(apps, schema_editor):
+    db_alias = schema_editor.connection.alias
+    Switch = apps.get_model("services", "Switch")
+    for sw in Switch.objects.using(db_alias).all():
+        sw.size = sw.last_port - sw.first_port + 1
+        sw.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('services', '0046_auto_20180210_1824'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='switch',
+            name='first_port',
+            field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(0)], verbose_name='Premier port'),
+            preserve_default=False,
+        ),
+        migrations.AddField(
+            model_name='switch',
+            name='last_port',
+            field=models.IntegerField(default=64, validators=[django.core.validators.MaxValueValidator(64)], verbose_name='Dernier port'),
+            preserve_default=False,
+        ),
+        migrations.RunPython(forward, backward),
+        migrations.RemoveField(
+            model_name='switch',
+            name='size',
+        ),
+    ]

+ 2 - 2
services/models.py

@@ -318,8 +318,8 @@ class AntennaAllocation(Allocation):
 
 class Switch(models.Model):
     name = models.CharField(max_length=64, verbose_name='Nom', unique=True)
-    size = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(64)],
-                               verbose_name='Nombre de ports')
+    first_port = models.IntegerField(validators=[MinValueValidator(0)], verbose_name='Premier port')
+    last_port = models.IntegerField(validators=[MaxValueValidator(64)], verbose_name='Dernier port')
     notes = models.TextField(blank=True, default='')
 
     class Meta:

+ 1 - 1
services/signals.py

@@ -36,5 +36,5 @@ def delete_service_contribution(sender, instance, **kwargs):
 @receiver(post_save, sender=Switch, dispatch_uid='create_switch_ports')
 def create_switch_ports(sender, instance, created, **kwargs):
     if created:
-        ports = [ Port(switch=instance, port=i) for i in range(1, instance.size + 1) ]
+        ports = [ Port(switch=instance, port=i) for i in range(instance.first_port, instance.last_port + 1) ]
         Port.objects.bulk_create(ports)