scholarship.py 2.15 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.core.validators import MinValueValidator
2
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
3

4 5 6 7 8
from backend_app.models.abstract.basic_module import (
    BasicModule,
    BasicModuleSerializer,
    BasicModuleViewSet,
)
Florent Chehab's avatar
Florent Chehab committed
9
from backend_app.models.currency import Currency
10
from rest_framework import serializers
11

12
SCHOLARSHIP_FREQUENCIES = (
13 14 15 16 17
    ("w", "week"),
    ("m", "month"),
    ("s", "semester"),
    ("y", "year"),
    ("o", "one_shot"),
18 19
)

20 21

class Scholarship(BasicModule):
Florent Chehab's avatar
Florent Chehab committed
22 23 24
    """
    Abstract model for scholarships
    """
25

Florent Chehab's avatar
Florent Chehab committed
26
    # TODO change this, don't use python primitive
Florent Chehab's avatar
Florent Chehab committed
27
    type = models.CharField(max_length=200)
Florent Chehab's avatar
Florent Chehab committed
28
    currency = models.ForeignKey(Currency, null=True, on_delete=models.PROTECT)
29
    other_advantages = models.CharField(default="", blank=True, max_length=5000)
Florent Chehab's avatar
Florent Chehab committed
30

31 32 33
    frequency = models.CharField(
        max_length=1,
        choices=SCHOLARSHIP_FREQUENCIES,
34
        default="m",
35 36
        null=True,
        blank=True,
37 38
    )

39
    amount_min = models.DecimalField(
40
        null=True, max_digits=20, decimal_places=2, validators=[MinValueValidator(0)]
41 42 43
    )

    amount_max = models.DecimalField(
44
        null=True, max_digits=20, decimal_places=2, validators=[MinValueValidator(0)]
45 46 47 48
    )

    class Meta:
        abstract = True
49 50 51


class ScholarshipSerializer(BasicModuleSerializer):
Florent Chehab's avatar
Florent Chehab committed
52 53 54 55
    """
    Serializer for the scholarship class
    """

56
    FORCE_FULL_DISPLAY = True
57

58
    def validate(self, attrs):
Florent Chehab's avatar
Florent Chehab committed
59 60 61
        """
        Custom attribute validation
        """
62 63 64

        attrs = super().validate(attrs)

65 66
        if attrs["amount_min"] is not None:
            if attrs["currency"] is None:
Florent Chehab's avatar
Florent Chehab committed
67
                raise serializers.ValidationError(
68 69 70 71
                    "A currency must be specified when there is a value"
                )
            if attrs["amount_max"] is not None:
                if attrs["amount_max"] < attrs["amount_min"]:
Florent Chehab's avatar
Florent Chehab committed
72
                    raise serializers.ValidationError(
73 74
                        "amount_max should be greater or equal than amount_min"
                    )
75 76 77

        return attrs

78 79
    class Meta:
        model = Scholarship
80
        fields = "__all__"
81 82 83 84


class ScholarshipViewSet(BasicModuleViewSet):
    serializer_class = ScholarshipSerializer