# (c) cavaliba.com - home - export.py


from django.forms.models import model_to_dict

from .models import DashboardApp


def home_export_dict(keyname=None):

    datalist = []

    dict_attributs = [
        "keyname",
        "displayname",
        "is_enabled",
        "description",
        "icon",
        "page",
        "url",
        "order",
    ]

    if keyname:
        apps = DashboardApp.objects.filter(keyname=keyname).prefetch_related("permission")
    else:
        apps = DashboardApp.objects.all().prefetch_related("permission").order_by("order")

    for item in apps:
        m = model_to_dict(item, fields=dict_attributs)
        m["classname"] = "_home"
        m["permission"] = item.permission.keyname

        m2 = {}
        for k, v in m.items():
            if not isinstance(v, bool):
                if v:
                    m2[k] = v
            else:
                m2[k] = v

        datalist.append(m2)

    return datalist
