scholarship.py 1.13 KB
Newer Older
1
from django.db import models
2
from backend.models.module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
Florent Chehab's avatar
Florent Chehab committed
3
from backend.models.location import Currency
4
from rest_framework import serializers
5
6
7
8
9
from django.core.validators import MinValueValidator


class Scholarship(BasicModule):

Florent Chehab's avatar
Florent Chehab committed
10
    type = models.CharField(max_length=200)
11
    currency = models.ForeignKey(Currency, on_delete=models.PROTECT)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    amount_min = models.DecimalField(
        max_digits=20,
        decimal_places=2,
        validators=[MinValueValidator(0)]
    )

    amount_max = models.DecimalField(
        max_digits=20,
        decimal_places=2,
        validators=[MinValueValidator(0)]
    )

    class Meta:
        abstract = True
26
27
28
29


class ScholarshipSerializer(BasicModuleSerializer):

30
31
32
33
34
35
36
    def validate(self, attrs):
        if attrs['amount_max'] < attrs['amount_min']:
            raise serializers.ValidationError(
                "Amount_max should be greater or equal than amount_min")

        return attrs

37
38
39
40
41
42
43
    class Meta:
        model = Scholarship
        fields = '__all__'


class ScholarshipViewSet(BasicModuleViewSet):
    serializer_class = ScholarshipSerializer