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


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

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


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

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

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

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

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

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


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

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

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