base.py 2.87 KB
Newer Older
1
from django.conf import settings
2
from django.db import models
3
from rest_framework import serializers, viewsets
4 5

from backend_app.custom.mySerializerWithJSON import MySerializerWithJSON
6
from backend_app.permissions.default import DEFAULT_VIEWSET_PERMISSIONS
7 8 9 10 11 12 13 14 15 16 17 18


class BaseModel(models.Model):
    """
    All models in the app inherits from this one.

    As of now, this model doesn't have any special fields.
    It is basically here to have a coherent naming convention. In fact some
    high level behaviors have been implemented in the corresponding Serializer and
    viewset.
    """

19 20 21 22 23
    # Look at the documentation about config files to know more about this
    # https://rex-dri.gitlab.utc.fr/rex-dri/documentation/#/Application/Backend/moderation_and_versioning?id=model-level
    # http://localhost:5000/#/Application/Backend/moderation_and_versioning?id=model-level
    moderation_level = settings.DEFAULT_MODEL_MODERATION_LEVEL

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    class Meta:
        abstract = True


class BaseModelSerializer(MySerializerWithJSON):
    """
    Serializer to go along the BaseModel model. This serializer make sure some
    relevant data is always returned.
    """

    obj_info = serializers.SerializerMethodField()

    # For easier handling on the client side, we force an id field
    # this is useful when a model has a dedicated primary key
    id = serializers.SerializerMethodField()

    def get_obj_info(self, obj) -> dict:
        """
        Serializer for the `obj_info` *dynamic* field.
        `obj` is required in the function definition, but it's not used.

        For all object return by the backend api, we add a custom `obj_info`
        field. The default value are chown below.

        This methods is overrided in EssentialModuleSerializer for
        a smarter behavior.
        """
        return {"user_can_edit": False, "user_can_moderate": False}

    def get_id(self, obj: BaseModel):
        """
        Serializer for the id field.
        """
        return obj.pk

59 60 61 62 63 64
    def get_user_from_request(self):
        """
        Function to retrieve the user from the request
        """
        return self.context["request"].user

65 66
    class Meta:
        model = BaseModel
67
        fields = ("obj_info", "id")
68 69 70 71 72 73 74 75


class BaseModelViewSet(viewsets.ModelViewSet):
    """
    Custom default viewset
    """

    serializer_class = BaseModelSerializer
76
    permission_classes = None
77

78 79 80
    # We store the api endpoint route directly in the viewset classes
    # so that we can easily access them
    end_point_route = None
81

82
    def get_permissions(self):
83
        """
84 85
        We override the permission getter to make sure we add the default
        app viewsets permissions
86
        """
87 88 89 90 91 92
        if self.permission_classes is None:
            return [DEFAULT_VIEWSET_PERMISSIONS()]
        else:
            return [
                (DEFAULT_VIEWSET_PERMISSIONS & p)() for p in self.permission_classes
            ]