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)

[((17, 17, 17, 44), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((18, 16, 18, 43), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((19, 24, 19, 51), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((20, 20, 20, 47), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((34, 18, 34, 68), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((35, 24, 35, 77), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((36, 26, 38, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((39, 23, 41, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((42, 18, 42, 65), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((43, 19, 43, 46), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((44, 17, 44, 44), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((45, 15, 45, 50), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((56, 16, 56, 64), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((57, 24, 57, 77), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((58, 25, 60, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((61, 34, 63, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((64, 20, 64, 69), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((65, 29, 67, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((68, 17, 68, 72), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((69, 22, 71, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((72, 21, 72, 69), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((73, 14, 73, 58), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((74, 18, 74, 45), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((75, 15, 75, 50), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((93, 14, 93, 58), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((94, 15, 94, 50), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((106, 19, 106, 70), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((107, 25, 107, 79), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((108, 27, 110, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((111, 24, 113, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((114, 20, 114, 47), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((130, 15, 130, 64), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((131, 24, 133, 5), 'rest_framework.serializers.ReadOnlyField', 'serializers.ReadOnlyField', (), '', False, 'from rest_framework import serializers\n'), ((164, 15, 164, 53), 'punkweb_boards.models.Shout.objects.create', 'Shout.objects.create', ({}, {}), '(**validated_data)', False, 'from punkweb_boards.models import BoardProfile, Category, Subcategory, Thread, Post, Conversation, Message, Report, Shout\n')]