
from django.utils.translation import gettext_lazy as _
from django.contrib import admin
from django_celery_beat.admin import PeriodicTaskAdmin, PeriodicTask

from .models import (Device, DeviceType)

class DeviceAdmin(admin.ModelAdmin):
    list_display = ('label', 'uuid', 'image_tag', 'device_type__code', 'index', 'task', 'lifetime', 'task_d', 'active')
    list_filter = ('device_type', 'active')
    search_fields = ('label',)
    readonly_fields = ('uuid',)

    def task_e(self, instance):
        if instance.task:
            return instance.task.enabled
    task_e.short_description = _('Activé')

    def task_d(self, instance):
        if instance.task:
            return instance.task.description
    task_d.short_description = _('Description')

    def has_add_permission(self, request, obj=None):
        return False


class DeviceTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'code',)
    search_fields = ('name', 'code')


class CustomPeriodicTaskAdmin(PeriodicTaskAdmin):
    list_display = ('name', 'task', 'enabled', 'description', 'scheduler', 'interval',
                    'start_time', 'last_run_at', 'expires', 'one_off', 'total_run_count')
    search_fields = ('name', 'task')


admin.site.unregister(PeriodicTask)
admin.site.register(PeriodicTask, CustomPeriodicTaskAdmin)
admin.site.register(DeviceType, DeviceTypeAdmin)
admin.site.register(Device, DeviceAdmin)
