from smartfarming.models.user import Role, UserPurview, HomeThemeModel
from rest_framework import serializers


class RoleSerializers(serializers.ModelSerializer):

    role_perm_name = serializers.SerializerMethodField()

    def get_role_perm_name(self, obj):
        per_ids = obj.role_perm
        per_ids = per_ids.split(",")
        user_per = UserPurview.objects.filter(id__in=[int(i) for i in per_ids]).values("purview_name")
        user_per_lst = [i.get("purview_name") for i in user_per]
        return ",".join(user_per_lst)

    class Meta:
        model = Role
        fields = ["id", "role_name","role_perm", "role_perm_name", "role_message", "role_status", "mark"]


class UserPurviewSerializers(serializers.ModelSerializer):

    class Meta:
        model = UserPurview
        fields = [
            "id",
            "parent_perm_id",
            "purview_name",
            "addtime"
        ]


class HomeThemeModelSerializers(serializers.ModelSerializer):

    class Meta:
        model = HomeThemeModel
        fields = "__all__"