userData.py 2.21 KB
Newer Older
1
from base_app.models import User
2
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
3
from backend_app.fields import JSONField
4 5 6 7 8 9 10 11
from backend_app.models.abstract.my_model import (
    MyModel,
    MyModelSerializer,
    MyModelViewSet,
)
from backend_app.permissions.__list_user_post_permission import (
    list_user_post_permission,
)
12 13
from backend_app.utils import get_model_config, get_user_level, get_viewset_permissions
from rest_framework import serializers
14 15


Florent Chehab's avatar
Florent Chehab committed
16
class UserData(MyModel):
17
    model_config = get_model_config("UserData")
18

19
    owner = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
Florent Chehab's avatar
Florent Chehab committed
20
    contact_info = JSONField(default=dict)
21
    contact_info_is_public = models.BooleanField(default=False)
Florent Chehab's avatar
Florent Chehab committed
22 23
    config = JSONField(default=dict)
    other_data = JSONField(default=dict)
24 25


Florent Chehab's avatar
Florent Chehab committed
26 27
class UserDataSerializer(MyModelSerializer):
    owner = serializers.CharField(read_only=True)
28
    owner_level = serializers.SerializerMethodField()
29
    owner_can_post_to = serializers.SerializerMethodField()
30 31 32

    def get_owner_level(self, obj):
        return get_user_level(obj.owner)
Florent Chehab's avatar
Florent Chehab committed
33

34 35 36
    def get_owner_can_post_to(self, obj):
        return list_user_post_permission(obj.owner)

37 38 39 40 41 42 43 44 45
    def do_before_save(self):
        """
        For safety: enforce (for sure) that we update the model corresponding to the user/owner.
        """
        super().do_before_save()

        user = self.get_user_from_request()

        self.override_validated_data({"owner": user})
Florent Chehab's avatar
Florent Chehab committed
46

47
        # we try to recover the correct instance
48
        query = UserData.objects.filter(owner=user)
49 50 51
        if len(query) == 1:
            self.instance = query[0]

52 53
    class Meta:
        model = UserData
54
        fields = "__all__"
55 56


Florent Chehab's avatar
Florent Chehab committed
57
class UserDataViewSet(MyModelViewSet):
58
    permission_classes = get_viewset_permissions("UserDataViewSet")
59
    serializer_class = UserDataSerializer
60
    LIST_SHOULD_BE_DETAIL = True
61

Florent Chehab's avatar
Florent Chehab committed
62
    def get_queryset(self):
Florent Chehab's avatar
Florent Chehab committed
63
        def get_for_querier():
64 65 66 67
            return UserData.objects.filter(
                owner=self.request.user
            )  # pylint: disable=E1101

Florent Chehab's avatar
Florent Chehab committed
68 69 70 71 72
        queryset = get_for_querier()
        if len(queryset) == 0:
            UserData.objects.update_or_create(owner=self.request.user)
            queryset = get_for_querier()
        return queryset