UserCustomSerializers.py 2.29 KB
Newer Older
asmundh's avatar
asmundh committed
1
from rest_framework import serializers
2
3
from django.contrib.auth import get_user_model
from users.models import Offer, AthleteFile, RememberMe
asmundh's avatar
asmundh committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


class UserGetSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = get_user_model()
        fields = [
            "url",
            "id",
            "email",
            "username",
            "athletes",
            "phone_number",
            "country",
            "city",
            "street_address",
            "coach",
            "workouts",
            "coach_files",
            "athlete_files",
23
            "profile_visible",
asmundh's avatar
asmundh committed
24
25
26
        ]


Christopher Løkken's avatar
Christopher Løkken committed
27
class UserPutAthleteSerializer(serializers.ModelSerializer):
asmundh's avatar
asmundh committed
28
29
    class Meta:
        model = get_user_model()
Christopher Løkken's avatar
Christopher Løkken committed
30
        fields = ["athletes"]
asmundh's avatar
asmundh committed
31
32
33
34

    def update(self, instance, validated_data):
        athletes_data = validated_data["athletes"]
        instance.athletes.set(athletes_data)
Christopher Løkken's avatar
Christopher Løkken committed
35
36
        return instance

37

Christopher Løkken's avatar
Christopher Løkken committed
38
39
40
41
42
43
44
class UserPutVisibilitySerializer(serializers.ModelSerializer):
    class Meta:
        model = get_user_model()
        fields = ["profile_visible"]

    def update(self, instance, validated_data):
        visibility_data = validated_data["profile_visible"]
Christopher Løkken's avatar
{    
Christopher Løkken committed
45
46
        setattr(instance, "profile_visible", visibility_data)
        instance.save()
asmundh's avatar
asmundh committed
47
48
        return instance

49

asmundh's avatar
asmundh committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class AthleteFileSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.ReadOnlyField(source="owner.username")

    class Meta:
        model = AthleteFile
        fields = ["url", "id", "owner", "file", "athlete"]

    def create(self, validated_data):
        return AthleteFile.objects.create(**validated_data)


class OfferSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.ReadOnlyField(source="owner.username")

    class Meta:
        model = Offer
        fields = [
            "url",
            "id",
            "owner",
            "recipient",
            "status",
            "timestamp",
        ]
74
75
76
77
78
79
80
81
82
83
84
85
86

class RememberMeSerializer(serializers.HyperlinkedModelSerializer):
    """Serializer for an RememberMe. Hyperlinks are used for relationships by default.

    Serialized fields: remember_me

    Attributes:
        remember_me:    Value of cookie used for remember me functionality
    """

    class Meta:
        model = RememberMe
        fields = ["remember_me"]