from rest_framework import serializers
from punkweb_boards.conf.settings import SHOUTBOX_DISABLED_TAGS
from punkweb_boards.models import (
    BoardProfile,
    Category,
    Subcategory,
    Thread,
    Post,
    Conversation,
    Message,
    Report,
    Shout,
)


class BoardProfileSerializer(serializers.ModelSerializer):
    post_count = serializers.ReadOnlyField()
    can_shout = serializers.ReadOnlyField()
    rendered_username = serializers.ReadOnlyField()
    rendered_rank = serializers.ReadOnlyField()

    class Meta:
        model = BoardProfile
        fields = "__all__"


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        exclude = ("auth_req",)


class SubcategorySerializer(serializers.ModelSerializer):
    last_thread = serializers.ReadOnlyField(source="last_thread.id")
    last_thread_title = serializers.ReadOnlyField(source="last_thread.title")
    last_thread_created = serializers.ReadOnlyField(
        source="last_thread.created"
    )
    last_thread_user = serializers.ReadOnlyField(
        source="last_thread.user.profile.rendered_username"
    )
    parent_name = serializers.ReadOnlyField(source="parent.name")
    thread_count = serializers.ReadOnlyField()
    post_count = serializers.ReadOnlyField()
    can_post = serializers.SerializerMethodField()

    def get_can_post(self, obj):
        return obj.can_post(self.context.get("request").user)

    class Meta:
        model = Subcategory
        exclude = ("auth_req",)


class ThreadSerializer(serializers.ModelSerializer):
    last_post = serializers.ReadOnlyField(source="last_post.id")
    last_post_created = serializers.ReadOnlyField(source="last_post.created")
    last_post_username = serializers.ReadOnlyField(
        source="last_post.user.username"
    )
    last_post_rendered_username = serializers.ReadOnlyField(
        source="last_post.user.profile.rendered_username"
    )
    user_username = serializers.ReadOnlyField(source="user.username")
    user_rendered_username = serializers.ReadOnlyField(
        source="user.profile.rendered_username"
    )
    user_image = serializers.ReadOnlyField(source="user.profile.avatar")
    user_post_count = serializers.ReadOnlyField(
        source="user.profile.post_count"
    )
    user_join_date = serializers.ReadOnlyField(source="user.created")
    flagged = serializers.ReadOnlyField(source="reported")
    posts_count = serializers.ReadOnlyField()
    can_edit = serializers.SerializerMethodField()

    def get_can_edit(self, obj):
        return obj.can_edit(self.context.get("request").user)

    class Meta:
        model = Thread
        fields = "__all__"
        read_only_fields = (
            "pinned",
            "closed",
            "user",
            "upvoted_by",
            "downvoted_by",
        )


class PostSerializer(serializers.ModelSerializer):
    flagged = serializers.ReadOnlyField(source="reported")
    can_edit = serializers.SerializerMethodField()

    def get_can_edit(self, obj):
        return obj.can_edit(self.context.get("request").user)

    class Meta:
        model = Post
        fields = "__all__"
        read_only_fields = ("user", "upvoted_by", "downvoted_by")


class ConversationSerializer(serializers.ModelSerializer):
    last_message = serializers.ReadOnlyField(source="last_message.id")
    last_message_title = serializers.ReadOnlyField(source="last_message.title")
    last_message_created = serializers.ReadOnlyField(
        source="last_message.created"
    )
    last_message_user = serializers.ReadOnlyField(
        source="last_message.user.profile.rendered_username"
    )
    message_count = serializers.ReadOnlyField()

    class Meta:
        model = Conversation
        fields = "__all__"
        read_only_fields = ("unread_by",)


class MessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = "__all__"
        read_only_fields = ("user",)


class ShoutSerializer(serializers.ModelSerializer):
    username = serializers.ReadOnlyField(source="user.username")
    rendered_username = serializers.ReadOnlyField(
        source="user.profile.rendered_username"
    )

    class Meta:
        model = Shout
        fields = (
            "id",
            "user",
            "username",
            "rendered_username",
            "content",
            "_content_rendered",
            "created",
            "modified",
        )
        read_only_fields = ("user",)

    def create(self, validated_data):
        for key in SHOUTBOX_DISABLED_TAGS:
            key_tag = "[{}]".format(key).lower()
            if (
                key_tag[: len(key_tag) - 1]
                in validated_data.get("content").lower()
            ):
                raise serializers.ValidationError(
                    {
                        "notAllowed": "{} is not allowed in the shoutbox".format(
                            key_tag
                        )
                    }
                )

        return Shout.objects.create(**validated_data)

[((344, 371), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (369, 371), False, 'from rest_framework import serializers\n'), ((388, 415), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (413, 415), False, 'from rest_framework import serializers\n'), ((440, 467), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (465, 467), False, 'from rest_framework import serializers\n'), ((488, 515), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (513, 515), False, 'from rest_framework import serializers\n'), ((797, 847), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_thread.id"""'}), "(source='last_thread.id')\n", (822, 847), False, 'from rest_framework import serializers\n'), ((872, 925), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_thread.title"""'}), "(source='last_thread.title')\n", (897, 925), False, 'from rest_framework import serializers\n'), ((952, 1007), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_thread.created"""'}), "(source='last_thread.created')\n", (977, 1007), False, 'from rest_framework import serializers\n'), ((1045, 1123), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_thread.user.profile.rendered_username"""'}), "(source='last_thread.user.profile.rendered_username')\n", (1070, 1123), False, 'from rest_framework import serializers\n'), ((1156, 1203), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""parent.name"""'}), "(source='parent.name')\n", (1181, 1203), False, 'from rest_framework import serializers\n'), ((1223, 1250), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (1248, 1250), False, 'from rest_framework import serializers\n'), ((1268, 1295), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (1293, 1295), False, 'from rest_framework import serializers\n'), ((1311, 1346), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (1344, 1346), False, 'from rest_framework import serializers\n'), ((1591, 1639), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_post.id"""'}), "(source='last_post.id')\n", (1616, 1639), False, 'from rest_framework import serializers\n'), ((1664, 1717), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_post.created"""'}), "(source='last_post.created')\n", (1689, 1717), False, 'from rest_framework import serializers\n'), ((1743, 1802), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_post.user.username"""'}), "(source='last_post.user.username')\n", (1768, 1802), False, 'from rest_framework import serializers\n'), ((1851, 1927), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_post.user.profile.rendered_username"""'}), "(source='last_post.user.profile.rendered_username')\n", (1876, 1927), False, 'from rest_framework import serializers\n'), ((1962, 2011), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.username"""'}), "(source='user.username')\n", (1987, 2011), False, 'from rest_framework import serializers\n'), ((2041, 2107), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.profile.rendered_username"""'}), "(source='user.profile.rendered_username')\n", (2066, 2107), False, 'from rest_framework import serializers\n'), ((2139, 2194), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.profile.avatar"""'}), "(source='user.profile.avatar')\n", (2164, 2194), False, 'from rest_framework import serializers\n'), ((2217, 2276), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.profile.post_count"""'}), "(source='user.profile.post_count')\n", (2242, 2276), False, 'from rest_framework import serializers\n'), ((2312, 2360), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.created"""'}), "(source='user.created')\n", (2337, 2360), False, 'from rest_framework import serializers\n'), ((2375, 2419), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""reported"""'}), "(source='reported')\n", (2400, 2419), False, 'from rest_framework import serializers\n'), ((2438, 2465), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (2463, 2465), False, 'from rest_framework import serializers\n'), ((2481, 2516), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (2514, 2516), False, 'from rest_framework import serializers\n'), ((2904, 2948), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""reported"""'}), "(source='reported')\n", (2929, 2948), False, 'from rest_framework import serializers\n'), ((2964, 2999), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ([], {}), '()\n', (2997, 2999), False, 'from rest_framework import serializers\n'), ((3307, 3358), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_message.id"""'}), "(source='last_message.id')\n", (3332, 3358), False, 'from rest_framework import serializers\n'), ((3384, 3438), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_message.title"""'}), "(source='last_message.title')\n", (3409, 3438), False, 'from rest_framework import serializers\n'), ((3466, 3522), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_message.created"""'}), "(source='last_message.created')\n", (3491, 3522), False, 'from rest_framework import serializers\n'), ((3561, 3640), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""last_message.user.profile.rendered_username"""'}), "(source='last_message.user.profile.rendered_username')\n", (3586, 3640), False, 'from rest_framework import serializers\n'), ((3675, 3702), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {}), '()\n', (3700, 3702), False, 'from rest_framework import serializers\n'), ((4047, 4096), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.username"""'}), "(source='user.username')\n", (4072, 4096), False, 'from rest_framework import serializers\n'), ((4121, 4187), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ([], {'source': '"""user.profile.rendered_username"""'}), "(source='user.profile.rendered_username')\n", (4146, 4187), False, 'from rest_framework import serializers\n'), ((5042, 5080), 'punkweb_boards.models.Shout.objects.create', 'Shout.objects.create', ([], {}), '(**validated_data)\n', (5062, 5080), False, 'from punkweb_boards.models import BoardProfile, Category, Subcategory, Thread, Post, Conversation, Message, Report, Shout\n')]