userData.py 2.61 KB
Newer Older
1 2
from django.db import models
from rest_framework import serializers
3
from rest_framework.response import Response
4 5

from backend_app.fields import JSONField
6 7 8 9 10
from backend_app.models.abstract.base import (
    BaseModel,
    BaseModelSerializer,
    BaseModelViewSet,
)
11 12
from backend_app.utils import get_user_level, get_default_theme_settings
from backend_app.validation.validators import ThemeValidator
13
from base_app.models import User
14 15


16
class UserData(BaseModel):
17 18
    moderation_level = 0

19
    owner = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
20
    theme = JSONField(default=get_default_theme_settings, validators=[ThemeValidator()])
21 22


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

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

31 32
    _list_user_post_function = None

33
    def get_owner_can_post_to(self, obj):
34 35 36 37 38 39 40
        """
        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:
41
            from backend_app.permissions.request import (
42 43 44 45 46 47
                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")
48

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

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

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


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

73 74 75 76
    def list(self, request, *args, **kwargs):
        # Prevent the querying of all objects.
        return Response(list())

Florent Chehab's avatar
Florent Chehab committed
77
    def get_queryset(self):
Florent Chehab's avatar
Florent Chehab committed
78
        def get_for_querier():
79 80 81 82
            return UserData.objects.filter(
                owner=self.request.user
            )  # pylint: disable=E1101

Florent Chehab's avatar
Florent Chehab committed
83 84 85 86 87
        queryset = get_for_querier()
        if len(queryset) == 0:
            UserData.objects.update_or_create(owner=self.request.user)
            queryset = get_for_querier()
        return queryset