<commit_before>import uuid
from django.db import transaction
from django.db.utils import IntegrityError
from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from lib.serializers import (
    LegacyBaseAPISerializer, LegacyLocationMixin, RelatedLabelField, UpdatableListSerializer
)
from clients.serializers import PersonAuthorizationSerializer

from . import models


class PersonEmailSerializer(serializers.ModelSerializer):
    """Basic PersonEmail serializer used to show and edit PersonEmail
    """

    def __init__(self, *args, **kwargs):
        serializers.ModelSerializer.__init__(self, *args, **kwargs)
        queryset = models.PersonEmail.objects.all()
        try:
            pk = self.context['view'].kwargs['pk']
        except KeyError:
            pk = None
        if isinstance(pk, uuid.UUID):
            queryset = queryset.exclude(person__id=pk)
        elif uuid is not None:
            queryset = queryset.exclude(person__nb_id=pk)

        self.fields['address'] = serializers.EmailField(
            max_length=254,
            validators=[UniqueValidator(queryset=queryset)])

    class Meta:
        model = models.PersonEmail<commit_msg>does not assume UUID is intanciated<commit_after>import uuid
from django.db import transaction
from django.db.utils import IntegrityError
from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from lib.serializers import (
    LegacyBaseAPISerializer, LegacyLocationMixin, RelatedLabelField, UpdatableListSerializer
)
from clients.serializers import PersonAuthorizationSerializer

from . import models


class PersonEmailSerializer(serializers.ModelSerializer):
    """Basic PersonEmail serializer used to show and edit PersonEmail
    """

    def __init__(self, *args, **kwargs):
        serializers.ModelSerializer.__init__(self, *args, **kwargs)
        queryset = models.PersonEmail.objects.all()
        try:
            pk = self.context['view'].kwargs['pk']
        except KeyError:
            pk = None
        if pk is not None:
            try:
                queryset = queryset.exclude(person__id=pk)
            except ValueError:
                queryset = queryset.exclude(person__nb_id=pk)

        self.fields['address'] = serializers.EmailField(
            max_length=254,
            validators=[UniqueValidator(queryset=queryset)])

    class Meta:
        model = models.PersonEmail
{'fraction_non_alphanumeric': 0.07858306188925081, 'fraction_numerical': 0.0024429967426710096, 'mean_word_length': 3.2730434782608695, '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': '3276578', 'n_tokens_mistral': 650, 'n_tokens_neox': 631, 'n_words': 162}