# Copyright (c) 2015 iXsystems, Inc.
# All rights reserved.
# This file is a part of TrueNAS
# and may not be copied and/or distributed
# without the express permission of iXsystems.

import logging

from freenasUI.api.utils import (
    APIAuthentication, APIAuthorization,
    DojoModelResource, DojoPaginator,
)
from freenasUI.middleware.notifier import notifier
from .models import Failover

log = logging.getLogger('failover.resources')


class FailoverResource(DojoModelResource):

    class Meta:
        queryset = Failover.objects.all()
        resource_name = 'failover/failover'
        always_return_data = True
        paginator_class = DojoPaginator
        authentication = APIAuthentication
        authorization = APIAuthorization()
        include_resource_uri = False
        allowed_methods = ['get', 'post']

    def dehydrate(self, bundle):
        bundle = super(FailoverResource, self).dehydrate(bundle)
        bundle.data['status'] = notifier().failover_status()
        return bundle
