userData.py 2.35 KB
Newer Older
1 2 3 4
from django.db import models
from rest_framework import serializers

from backend_app.fields import JSONField
5 6 7 8 9
from backend_app.models.abstract.base import (
    BaseModel,
    BaseModelSerializer,
    BaseModelViewSet,
)
10
from backend_app.utils import get_user_level
11
from base_app.models import User
12 13


14
class UserData(BaseModel):
15 16
    moderation_level = 0

17
    owner = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
Florent Chehab's avatar
Florent Chehab committed
18 19
    config = JSONField(default=dict)
    other_data = JSONField(default=dict)
20 21


22
class UserDataSerializer(BaseModelSerializer):
Florent Chehab's avatar
Florent Chehab committed
23
    owner = serializers.CharField(read_only=True)
24
    owner_level = serializers.SerializerMethodField()
25
    owner_can_post_to = serializers.SerializerMethodField()
26 27 28

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

30 31
    _list_user_post_function = None

32
    def get_owner_can_post_to(self, obj):
33 34 35 36 37 38 39
        """
        Serializer for the field `get_owner_level`.
        The function is imported at runtime to prevent annoying
        cyclic imports.
        """

        if self._list_user_post_function is None:
40
            from backend_app.permissions.request import (
41 42 43 44 45 46
                list_user_permission_for_request_type,
            )

            self._list_user_post_function = list_user_permission_for_request_type

        return self._list_user_post_function(obj.owner, "POST")
47

48 49 50 51 52 53 54 55 56
    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
57

58
        # we try to recover the correct instance
59
        query = UserData.objects.filter(owner=user)
60 61 62
        if len(query) == 1:
            self.instance = query[0]

63 64
    class Meta:
        model = UserData
65
        fields = "__all__"
66 67


68
class UserDataViewSet(BaseModelViewSet):
69
    serializer_class = UserDataSerializer
70
    end_point_route = "userData"
71

Florent Chehab's avatar
Florent Chehab committed
72
    def get_queryset(self):
Florent Chehab's avatar
Florent Chehab committed
73
        def get_for_querier():
74 75 76 77
            return UserData.objects.filter(
                owner=self.request.user
            )  # pylint: disable=E1101

Florent Chehab's avatar
Florent Chehab committed
78 79 80 81 82
        queryset = get_for_querier()
        if len(queryset) == 0:
            UserData.objects.update_or_create(owner=self.request.user)
            queryset = get_for_querier()
        return queryset