scholarship.py 2.24 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
from backend_app.models.abstract.module import Module, ModuleSerializer, ModuleViewSet

Florent Chehab's avatar
Florent Chehab committed
6
from backend_app.models.currency import Currency
7
from rest_framework import serializers
8

9
SCHOLARSHIP_FREQUENCIES = (
10 11 12 13 14
    ("w", "week"),
    ("m", "month"),
    ("s", "semester"),
    ("y", "year"),
    ("o", "one_shot"),
15 16
)

17

18
class Scholarship(Module):
Florent Chehab's avatar
Florent Chehab committed
19 20 21
    """
    Abstract model for scholarships
    """
22

Florent Chehab's avatar
Florent Chehab committed
23
    short_description = models.CharField(max_length=200)
Florent Chehab's avatar
Florent Chehab committed
24
    currency = models.ForeignKey(Currency, null=True, on_delete=models.PROTECT)
25
    other_advantages = models.CharField(default="", blank=True, max_length=5000)
Florent Chehab's avatar
Florent Chehab committed
26

27 28 29
    frequency = models.CharField(
        max_length=1,
        choices=SCHOLARSHIP_FREQUENCIES,
30
        default="m",
31 32
        null=True,
        blank=True,
33 34
    )

35
    amount_min = models.DecimalField(
36
        null=True, max_digits=20, decimal_places=2, validators=[MinValueValidator(0)]
37 38 39
    )

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

    class Meta:
        abstract = True
45 46


47
class ScholarshipSerializer(ModuleSerializer):
Florent Chehab's avatar
Florent Chehab committed
48 49 50 51
    """
    Serializer for the scholarship class
    """

52
    def validate(self, attrs):
Florent Chehab's avatar
Florent Chehab committed
53 54 55
        """
        Custom attribute validation
        """
56 57 58

        attrs = super().validate(attrs)

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

        return attrs

74 75
    class Meta:
        model = Scholarship
76 77 78 79 80 81 82 83
        fields = ModuleSerializer.Meta.fields + (
            "short_description",
            "currency",
            "other_advantages",
            "frequency",
            "amount_min",
            "amount_max",
        )
84 85


86
class ScholarshipViewSet(ModuleViewSet):
87
    serializer_class = ScholarshipSerializer