from django.contrib import admin from lib.decorators import readonly from lib.mikrotik import sync_from_mikrotik as _sync_from_mikrotik from mikrotik.models import DNSStatic, IPAddress, IPDHCPLease @readonly def sync_dns_static_from_mikrotik(*args, **kwargs): _sync_from_mikrotik(DNSStatic) @readonly def sync_ipaddress_from_mikrotik(*args, **kwargs): _sync_from_mikrotik(IPAddress) @readonly def sync_ipdhcplease_from_mikrotik(*args, **kwargs): _sync_from_mikrotik(IPDHCPLease) def sync_to_mikrotik(_, request, queryset): for i in queryset: i.sync_to_router() class MikrotikModelMixinAdmin(admin.ModelAdmin): def change_view(self, request, object_id, form_url='', extra_context=None): self.get_object(request, object_id).sync_from_router() return super().change_view( request, object_id, form_url, extra_context=extra_context or {'show_save_and_continue': False} ) @admin.register(DNSStatic) class DNSStaticAdmin(admin.ModelAdmin): actions = [sync_dns_static_from_mikrotik, sync_to_mikrotik] list_display = ('name', 'regexp', 'address','disabled', 'comment', 'id') list_filter = ('disabled',('id', admin.EmptyFieldListFilter), ('name', admin.EmptyFieldListFilter), ('address', admin.EmptyFieldListFilter), ('regexp', admin.EmptyFieldListFilter),) search_fields = ('name', 'address','regexp') @admin.register(IPAddress) class IPAddressAdmin(admin.ModelAdmin): actions = [sync_ipaddress_from_mikrotik, sync_to_mikrotik] list_display = ('address','network', 'disabled', 'comment', 'id') list_filter = ('disabled',('id', admin.EmptyFieldListFilter), ('address', admin.EmptyFieldListFilter), ('network', admin.EmptyFieldListFilter),) search_fields = ('address', 'network', 'comment') @admin.register(IPDHCPLease) class IPDHCPLeaseAdmin(admin.ModelAdmin): actions = [sync_ipdhcplease_from_mikrotik, sync_to_mikrotik] list_display = ('address', 'mac_address', 'hostname', 'disabled', 'dynamic', 'comment', 'status', 'id') list_filter = ('disabled', 'dynamic', 'status', ('id', admin.EmptyFieldListFilter), ('address', admin.EmptyFieldListFilter), ('mac_address', admin.EmptyFieldListFilter), ('hostname', admin.EmptyFieldListFilter), ('comment', admin.EmptyFieldListFilter),) search_fields = ('address', 'mac_address', 'hostname')