<commit_before>from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.crypto import get_random_string
from rest_framework import serializers
from tinylinks.models import Tinylink

User = get_user_model()


class UserSerializer(serializers.ModelSerializer):
    tinylinks = serializers.PrimaryKeyRelatedField(
        many=True, queryset=User.objects.all()
    )

    class Meta:
        model = User
        fields = ("id", "username", "tinylinks")


class TinylinkSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tinylink
        fields = ("id", "user", "long_url", "short_url")

    def create(self, validated_data):
        user = self.context["request"].user
        brothers = Tinylink.objects.filter(
            long_url=validated_data["long_url"], user=user
        )
        if brothers:
            return brothers[0]

        slug = None
        while not slug or Tinylink.objects.filter(short_url=slug):
            slug = get_random_string(getattr(settings, "TINYLINK_LENGTH", 6))

        validated_data["user"] = user
        validated_data["short_url"] = slug
        instance = super().create(validated_data)
        return instance


# class TinylinkSerializer(serializers.Serializer):
#     pk = serializers.Field()  # Note: `Field` is an untyped read-only field.<commit_msg>fix: get_short_url w/o prefix take 3<commit_after>from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.crypto import get_random_string
from rest_framework import serializers
from tinylinks.models import Tinylink

User = get_user_model()


class UserSerializer(serializers.ModelSerializer):
    tinylinks = serializers.PrimaryKeyRelatedField(
        many=True, queryset=User.objects.all()
    )

    class Meta:
        model = User
        fields = ("id", "username", "tinylinks")


class TinylinkSerializer(serializers.ModelSerializer):

    short_url = serializers.CharField(read_only=True)

    class Meta:
        model = Tinylink
        fields = ("id", "user", "long_url")

    def create(self, validated_data):
        user = self.context["request"].user
        brothers = Tinylink.objects.filter(
            long_url=validated_data["long_url"], user=user
        )
        if brothers:
            return brothers[0]

        slug = None
        while not slug or Tinylink.objects.filter(short_url=slug):
            slug = get_random_string(getattr(settings, "TINYLINK_LENGTH", 6))

        validated_data["user"] = user
        validated_data["short_url"] = slug
        instance = super().create(validated_data)
        return instance


# class TinylinkSerializer(serializers.Serializer):
#     pk = serializers.Field()  # Note: `Field` is an untyped read-only field.
{'fraction_non_alphanumeric': 0.0975609756097561, 'fraction_numerical': 0.001793400286944046, 'mean_word_length': 3.2257575757575756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7286955', 'n_tokens_mistral': 818, 'n_tokens_neox': 798, 'n_words': 185}