86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
from django.contrib.auth import get_user_model
|
|
from django.utils.translation import gettext_lazy as _
|
|
from settings.puckoviews import PuckoView
|
|
from settings.puckoviews import PuckoSlugView
|
|
from rest_framework import serializers
|
|
|
|
from mobile_phone.models import Phone_Swedish
|
|
from .models import User_Profile
|
|
from .models import Friend_Request
|
|
|
|
class Friend_Request_Serializer(serializers.ModelSerializer):
|
|
asked_by = serializers.CharField(source="from_user.nickname")
|
|
to = serializers.CharField(source="to_user.nickname")
|
|
nickname = serializers.CharField(write_only=True)
|
|
|
|
class Meta:
|
|
model = Friend_Request
|
|
fields = ("id", "nickname", "asked_by", "to")
|
|
read_only_fields = ["id", "asked_by", "to"]
|
|
extra_kwargs = {
|
|
"url": {"lookup_field": "pk"}
|
|
}
|
|
|
|
class User_Searchalizer(serializers.ModelSerializer):
|
|
search = serializers.CharField(max_length=50, required=True, write_only=True)
|
|
|
|
class Meta:
|
|
model = User_Profile
|
|
fields = ("search", "nickname", "birthday", "first_name", "last_name", "slug")
|
|
read_only_fields = ("nickname", "birthday", "first_name", "last_name", "slug")
|
|
extra_kwargs = {
|
|
"url": {"lookup_field": "slug"}
|
|
}
|
|
|
|
def validate(self, attrs):
|
|
if not isinstance(attrs["search"], str):
|
|
raise serializers.ValidationError({"search": "needs to be a string."})
|
|
return attrs
|
|
|
|
|
|
class User_Profile_Serializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = User_Profile
|
|
read_only_fields = ["slug"]
|
|
exclude = ("id", "user", "friends", "created_at", "updated_at")
|
|
extra_kwargs = {
|
|
"url": {"lookup_field": "pk"}
|
|
}
|
|
|
|
class User_Profile_Small_Serializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = User_Profile
|
|
read_only_fields = ["slug"]
|
|
exclude = ("id", "user", "friends", "created_at", "updated_at", "first_name", "last_name", "birthday")
|
|
extra_kwargs = {
|
|
"url": {"lookup_field": "pk"}
|
|
}
|
|
|
|
class My_Profile_Serializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = User_Profile
|
|
read_only_fields = ["slug", "created_at", "updated_at"]
|
|
exclude = ("id", "user", "friends")
|
|
extra_kwargs = {
|
|
"url": {"lookup_field": "slug"}
|
|
}
|
|
|
|
def update_profile(self, instance, validated_data):
|
|
instance.nickname = validated_data.get("nickname", instance.nickname)
|
|
instance.first_name = validated_data.get("first_name", instance.first_name)
|
|
instance.last_name = validated_data.get("last_name", instance.last_name)
|
|
instance.birthday = validated_data.get("birthday", instance.birthday)
|
|
return instance
|
|
|
|
def create(self, validated_data):
|
|
profile = User_Profile.objects.create(**validated_data)
|
|
profile.save()
|
|
return profile
|
|
|
|
def update(self, instance, validated_data):
|
|
instance = self.update_profile(instance=instance, validated_data=validated_data)
|
|
instance.save()
|
|
return instance |