userData.py 2.35 KB
Newer Older
1
from django.contrib.auth.models import User
2
from django.db import models
3

Florent Chehab's avatar
Florent Chehab committed
4
from backend_app.fields import JSONField
5
6
7
8
9
from backend_app.models.abstract.my_model import (
    MyModel,
    MyModelSerializer,
    MyModelViewSet,
)
10
from backend_app.models.university import University
11
12
13
from backend_app.permissions.__list_user_post_permission import (
    list_user_post_permission,
)
14
15
from backend_app.utils import get_model_config, get_user_level, get_viewset_permissions
from rest_framework import serializers
16
17


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

21
    owner = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
Florent Chehab's avatar
Florent Chehab committed
22
    contact_info = JSONField(default=dict)
23
    contact_info_is_public = models.BooleanField(default=False)
Florent Chehab's avatar
Florent Chehab committed
24
    config = JSONField(default=dict)
25
    black_list = models.ManyToManyField(University, related_name="+", blank=True)
Florent Chehab's avatar
Florent Chehab committed
26
    other_data = JSONField(default=dict)
27
28


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

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

37
38
39
    def get_owner_can_post_to(self, obj):
        return list_user_post_permission(obj.owner)

40
41
42
43
44
45
46
47
48
    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
49

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

55
56
    class Meta:
        model = UserData
57
        fields = "__all__"
58
59


Florent Chehab's avatar
Florent Chehab committed
60
class UserDataViewSet(MyModelViewSet):
61
    permission_classes = get_viewset_permissions("UserDataViewSet")
62
    serializer_class = UserDataSerializer
63
    LIST_SHOULD_BE_DETAIL = True
64

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

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