from django.core.management.base import BaseCommand, CommandParser from services.models import IPResource, IPPrefix, Service, ServiceType, ServiceAllocation, Switch from djadhere.utils import get_active_filter class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument('--switch', action='store_true') parser.add_argument('--ip', action='store_true') def handle(self, *args, **options): if options['switch']: self.export_switch() if options['ip']: self.export_ip() def export_switch(self): for switch in Switch.objects.all(): print() print("=== Switch %s ===" % switch.name) print(switch.notes) for port in switch.ports.all(): line = "Port %d" % port.port if port.service: line += ' - (%s %s) %s' % (port.service.adhesion, port.service.adhesion.adherent, port.service) if port.notes: line += ' - %s' % port.notes print(line) def export_ip(self): public_ips = IPResource.objects.filter(category=IPResource.CATEGORY_PUBLIC).values_list('pk', flat=True) for prefix in IPPrefix.objects.filter(ipresource__in=public_ips).distinct(): print("\n=== %s ===\n" % prefix) active_ips = prefix.ipresource_set.filter(in_use=True) if not active_ips.exists(): print("empty") continue elif active_ips.count() < 20: ip_set = active_ips else: ip_set = prefix.ipresource_set for ip in ip_set.all(): line = str(ip) if ip.reserved: line += ' - réservé' else: try: allocation = ip.allocations.get(active=True) except ServiceAllocation.DoesNotExist: line += ' - disponible' allocation = ip.allocations.order_by('-end').first() if allocation: service = allocation.service end = allocation.end.strftime('%Y%m%d') adhesion = service.adhesion adherent = adhesion.adherent line += ' depuis %s (dernière utilisation : %s %s %s)' % (end, adhesion, adherent, service) else: service = allocation.service start = allocation.start.strftime('%Y%m%d') adhesion = service.adhesion adherent = adhesion.adherent line += ' - %s %s %s (depuis %s)' % (adhesion, adherent, service, start) print(line)