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


Florent Chehab's avatar
Florent Chehab committed
11
class UserData(MyModel):
Florent Chehab's avatar
Florent Chehab committed
12
    model_config = get_model_config("UserData")
13

14
15
    owner = models.OneToOneField(
        User, on_delete=models.CASCADE, primary_key=True)
Florent Chehab's avatar
Florent Chehab committed
16
    contact_info = JSONField(default=dict)
17
    contact_info_is_public = models.BooleanField(default=False)
Florent Chehab's avatar
Florent Chehab committed
18
    config = JSONField(default=dict)
Florent Chehab's avatar
Florent Chehab committed
19
20
    black_list = models.ManyToManyField(
        University, related_name='+', blank=True)
Florent Chehab's avatar
Florent Chehab committed
21
    other_data = JSONField(default=dict)
22
23


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

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

32
33
34
    def get_owner_can_post_to(self, obj):
        return list_user_post_permission(obj.owner)

Florent Chehab's avatar
Florent Chehab committed
35
    def my_pre_save(self):
36
        self.override_validated_data({'owner': self.user})
Florent Chehab's avatar
Florent Chehab committed
37

38
        # we try to recover the correct instance
39
        query = UserData.objects.filter(owner=self.user)
40
41
42
        if len(query) == 1:
            self.instance = query[0]

43
44
45
46
47
    class Meta:
        model = UserData
        fields = '__all__'


Florent Chehab's avatar
Florent Chehab committed
48
class UserDataViewSet(MyModelViewSet):
49
    permission_classes = get_viewset_permissions("UserDataViewSet")
50
    serializer_class = UserDataSerializer
51
    LIST_SHOULD_BE_DETAIL = True
52

Florent Chehab's avatar
Florent Chehab committed
53
    def get_queryset(self):
Florent Chehab's avatar
Florent Chehab committed
54
55
56
57
58
59
60
        def get_for_querier():
            return UserData.objects.filter(owner=self.request.user)  # pylint: disable=E1101
        queryset = get_for_querier()
        if len(queryset) == 0:
            UserData.objects.update_or_create(owner=self.request.user)
            queryset = get_for_querier()
        return queryset