<gh_stars>1-10
from typing import Optional

from common.utils import ModelManager
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import serializers
from rest_framework.reverse import reverse

from scalade.entities import ActionEntity


class BaseSerializer(serializers.Serializer):
    def create(self, validated_data):
        raise NotImplementedError

    def update(self, instance, validated_data):
        raise NotImplementedError


class ListItemsWithURLSerializer(serializers.ModelSerializer):
    url_basename: Optional[str] = None

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super().__init__(*args, **kwargs)

    def get_url(self, obj):
        return reverse(
            self.url_basename, args=[obj.uuid], request=self.request
        )


class EmailValidatorMixin:
    def validate_email(self, email):
        try:
            ModelManager.handle("accounts.account", "get", email=email)
            raise serializers.ValidationError("'%s' already exist." % email)
        except ObjectDoesNotExist:
            return email


class ActionMixinSerializer(serializers.ModelSerializer):
    kind = serializers.SerializerMethodField()
    level = serializers.SerializerMethodField()

    def get_kind(self, obj):
        try:
            return ActionEntity.get_kind_verbose_value(obj.kind)
        except ValueError as err:
            raise serializers.ValidationError(err.__repr__())

    def get_level(self, obj):
        try:
            return ActionEntity.get_level_verbose_value(obj.level)
        except ValueError as err:
            raise serializers.ValidationError(err.__repr__())

from typing import Optional

from common.utils import ModelManager
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import serializers
from rest_framework.reverse import reverse

from scalade.entities import ActionEntity


class BaseSerializer(serializers.Serializer):
    def create(self, validated_data):
        raise NotImplementedError

    def update(self, instance, validated_data):
        raise NotImplementedError


class ListItemsWithURLSerializer(serializers.ModelSerializer):
    url_basename: Optional[str] = None

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super().__init__(*args, **kwargs)

    def get_url(self, obj):
        return reverse(
            self.url_basename, args=[obj.uuid], request=self.request
        )


class EmailValidatorMixin:
    def validate_email(self, email):
        try:
            ModelManager.handle("accounts.account", "get", email=email)
            raise serializers.ValidationError("'%s' already exist." % email)
        except ObjectDoesNotExist:
            return email


class ActionMixinSerializer(serializers.ModelSerializer):
    kind = serializers.SerializerMethodField()
    level = serializers.SerializerMethodField()

    def get_kind(self, obj):
        try:
            return ActionEntity.get_kind_verbose_value(obj.kind)
        except ValueError as err:
            raise serializers.ValidationError(err.__repr__())

    def get_level(self, obj):
        try:
            return ActionEntity.get_level_verbose_value(obj.level)
        except ValueError as err:
            raise serializers.ValidationError(err.__repr__())