<commit_before>from django.contrib.auth import authenticate, get_user_model
from django.core import validators
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers

from goals import user_feed
from goals.models import (
    UserAction,
    UserBehavior,
    UserCategory,
    UserGoal,
)
from goals.serializers.v1 import (
    GoalSerializer,
    ReadOnlyUserActionSerializer,
    ReadOnlyUserGoalSerializer,
    UserBehaviorSerializer,
    UserCategorySerializer,
)
from goals.serializers.simple import (
    SimpleGoalSerializer,
    SimpleUserBehaviorSerializer,
    SimpleUserCategorySerializer,
)
from utils import user_utils
from utils.decorators import cached_method
from utils.serializers import ObjectTypeModelSerializer

from .. import models


class PlaceSerializer(ObjectTypeModelSerializer):
    class Meta:
        model = models.Place
        fields = ('id', 'name', 'slug', 'primary', 'updated_on', 'created_on')


class PlaceField(serializers.RelatedField):

    def to_internal_value(self, data):
        return models.Place.objects.get(pk=data)

    def to_representation(self, value):
        return {
            'id': value.id,
            'name': value.name,
            'slug': value.slug,
            'primary': value.primary,
        }


class UserPlaceSerializer(ObjectTypeModelSerializer):
    place = PlaceField(queryset=models.Place.objects.all())

    class Meta:
        model = models.UserPlace
        fields = (
            'id', 'user', 'profile', 'place', 'latitude', 'longitude'
        )
        read_only_fields = ("id", "updated_on", "created_on", )


class UserAccountSerializer(ObjectTypeModelSerializer):
    full_name = serializers.ReadOnlyField(source='get_full_name')
    userprofile_id = serializers.ReadOnlyField(source='userprofile.id')
    needs_onboarding = serializers.ReadOnlyField(source='userprofile.needs_onboarding')
    username = serializers.CharField(required=False)
    timezone = serializers.ReadOnlyField(source='userprofile.timezone')
    password = serializers.CharField(write_only=True)
    token = serializers.ReadOnlyField(source='auth_token.key')

    class Meta:
        model = get_user_model()
        fields = (
            'id', 'username', 'email', 'is_staff', 'first_name', 'last_name',
            "timezone", "full_name", 'date_joined', 'userprofile_id', "password",
            'token', 'needs_onboarding',
        )
        read_only_fields = ("id", "date_joined", )

    def validate_username(self, value):
        User = get_user_model()
        if not self.partial and User.objects.filter(username=value).exists():
            raise serializers.ValidationError("This user account already exists.")
        return value

    def validate_email(self, value):
        """Validate several things, given a user's email:<commit_msg>including object_type on Place and UserPlace serializers<commit_after>from django.contrib.auth import authenticate, get_user_model
from django.core import validators
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers

from goals import user_feed
from goals.models import (
    UserAction,
    UserBehavior,
    UserCategory,
    UserGoal,
)
from goals.serializers.v1 import (
    GoalSerializer,
    ReadOnlyUserActionSerializer,
    ReadOnlyUserGoalSerializer,
    UserBehaviorSerializer,
    UserCategorySerializer,
)
from goals.serializers.simple import (
    SimpleGoalSerializer,
    SimpleUserBehaviorSerializer,
    SimpleUserCategorySerializer,
)
from utils import user_utils
from utils.decorators import cached_method
from utils.serializers import ObjectTypeModelSerializer

from .. import models


class PlaceSerializer(ObjectTypeModelSerializer):
    class Meta:
        model = models.Place
        fields = (
            'id', 'name', 'slug', 'primary', 'updated_on', 'created_on',
            'object_type',
        )


class PlaceField(serializers.RelatedField):

    def to_internal_value(self, data):
        return models.Place.objects.get(pk=data)

    def to_representation(self, value):
        return {
            'id': value.id,
            'name': value.name,
            'slug': value.slug,
            'primary': value.primary,
        }


class UserPlaceSerializer(ObjectTypeModelSerializer):
    place = PlaceField(queryset=models.Place.objects.all())

    class Meta:
        model = models.UserPlace
        fields = (
            'id', 'user', 'profile', 'place', 'latitude', 'longitude',
            'object_type',
        )
        read_only_fields = ("id", "updated_on", "created_on", )


class UserAccountSerializer(ObjectTypeModelSerializer):
    full_name = serializers.ReadOnlyField(source='get_full_name')
    userprofile_id = serializers.ReadOnlyField(source='userprofile.id')
    needs_onboarding = serializers.ReadOnlyField(source='userprofile.needs_onboarding')
    username = serializers.CharField(required=False)
    timezone = serializers.ReadOnlyField(source='userprofile.timezone')
    password = serializers.CharField(write_only=True)
    token = serializers.ReadOnlyField(source='auth_token.key')

    class Meta:
        model = get_user_model()
        fields = (
            'id', 'username', 'email', 'is_staff', 'first_name', 'last_name',
            "timezone", "full_name", 'date_joined', 'userprofile_id', "password",
            'token', 'needs_onboarding',
        )
        read_only_fields = ("id", "date_joined", )

    def validate_username(self, value):
        User = get_user_model()
        if not self.partial and User.objects.filter(username=value).exists():
            raise serializers.ValidationError("This user account already exists.")
        return value

    def validate_email(self, value):
        """Validate several things, given a user's email:
{'fraction_non_alphanumeric': 0.10022082554781722, 'fraction_numerical': 0.00033973161202649905, 'mean_word_length': 3.5188027628549503, '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': '7382094', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1624, 'n_words': 363}