|
@@ -25,7 +25,9 @@ from datetime import timedelta
|
|
|
from djadhere.utils import get_active_filter
|
|
|
from adhesions.models import Adhesion
|
|
|
from banking.models import PaymentUpdate
|
|
|
-from .models import Service, ServiceType, IPPrefix, IPResource, Route, Tunnel, ServiceAllocation, Antenna, AntennaAllocation, Allocation
|
|
|
+from .models import Service, ServiceType, IPPrefix, IPResource, Route, Tunnel, \
|
|
|
+ ServiceAllocation, Antenna, AntennaAllocation, Allocation, \
|
|
|
+ Switch, Port
|
|
|
from .utils import notify_allocation
|
|
|
from .forms import AntennaForm
|
|
|
|
|
@@ -245,6 +247,33 @@ class InactiveAntennaAllocationInline(AntennaAllocationMixin, InactiveAllocation
|
|
|
pass
|
|
|
|
|
|
|
|
|
+class PortInline(admin.TabularInline):
|
|
|
+ model = Port
|
|
|
+
|
|
|
+ def has_add_permission(self, request):
|
|
|
+ return False
|
|
|
+
|
|
|
+ def has_delete_permission(self, request, obj=None):
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+class SwitchPortInline(PortInline):
|
|
|
+ fields = ('port', 'reserved', 'service', 'notes',)
|
|
|
+ readonly_fields = ('port',)
|
|
|
+ raw_id_fields = ('service',)
|
|
|
+
|
|
|
+ def get_queryset(self, request):
|
|
|
+ qs = super().get_queryset(request)
|
|
|
+ qs = qs.prefetch_related('switch', 'service', 'service__service_type')
|
|
|
+ return qs
|
|
|
+
|
|
|
+
|
|
|
+class ServicePortInline(PortInline):
|
|
|
+ fields = ('switch', 'port', 'notes',)
|
|
|
+ readonly_fields = ('switch', 'port',)
|
|
|
+ max_num = 0
|
|
|
+
|
|
|
+
|
|
|
### Actions
|
|
|
|
|
|
def ends_resource(resource, request, queryset):
|
|
@@ -263,7 +292,6 @@ class ServiceAdmin(admin.ModelAdmin):
|
|
|
ActiveServiceFilter,
|
|
|
('service_type', admin.RelatedOnlyFieldListFilter),
|
|
|
)
|
|
|
- inlines = (ActiveServiceAllocationInline, InactiveServiceAllocationInline,)
|
|
|
search_fields = ('=id', 'service_type__name', 'label', 'notes',)
|
|
|
fields = ('adhesion', 'service_type', 'label', 'notes', 'get_contribution_link', 'is_active',)
|
|
|
readonly_fields = ('get_contribution_link', 'is_active',)
|
|
@@ -279,6 +307,13 @@ class ServiceAdmin(admin.ModelAdmin):
|
|
|
return format_html(u'<a href="{}">{}</a>', obj.contribution.get_absolute_url(), obj.contribution)
|
|
|
get_contribution_link.short_description = 'Contribution financière'
|
|
|
|
|
|
+ def get_inline_instances(self, request, obj=None):
|
|
|
+ inlines = []
|
|
|
+ if obj and obj.ports.exists():
|
|
|
+ inlines += [ServicePortInline]
|
|
|
+ inlines += [ActiveServiceAllocationInline, InactiveServiceAllocationInline]
|
|
|
+ return [inline(self.model, self.admin_site) for inline in inlines]
|
|
|
+
|
|
|
def get_actions(self, request):
|
|
|
actions = super().get_actions(request)
|
|
|
if 'delete_selected' in actions:
|
|
@@ -615,6 +650,27 @@ class AntennaAdmin(admin.ModelAdmin):
|
|
|
return HttpResponse(geojson, content_type='application/json')
|
|
|
|
|
|
|
|
|
+class SwitchAdmin(admin.ModelAdmin):
|
|
|
+ list_display = ('name', 'size',)
|
|
|
+ fields = ('name', 'size', 'notes',)
|
|
|
+ inlines = (SwitchPortInline,)
|
|
|
+
|
|
|
+ def get_readonly_fields(self, request, obj=None):
|
|
|
+ if obj:
|
|
|
+ return ('size',)
|
|
|
+ else:
|
|
|
+ return ()
|
|
|
+
|
|
|
+ def get_actions(self, request):
|
|
|
+ actions = super().get_actions(request)
|
|
|
+ if 'delete_selected' in actions:
|
|
|
+ del actions['delete_selected']
|
|
|
+ return actions
|
|
|
+
|
|
|
+ def has_delete_permission(self, request, obj=None):
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
admin.site.register(ServiceType, ServiceTypeAdmin)
|
|
|
admin.site.register(Service, ServiceAdmin)
|
|
|
admin.site.register(IPPrefix, IPPrefixAdmin)
|
|
@@ -622,3 +678,4 @@ admin.site.register(IPResource, IPResourceAdmin)
|
|
|
admin.site.register(Route, RouteAdmin)
|
|
|
admin.site.register(Tunnel, TunnelAdmin)
|
|
|
admin.site.register(Antenna, AntennaAdmin)
|
|
|
+admin.site.register(Switch, SwitchAdmin)
|