scholarship.py 1.21 KB
Newer Older
1
from django.db import models
2
3
from backend.models.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
from backend.models.currency 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):

Florent Chehab's avatar
Florent Chehab committed
30
    def my_validate(self, attrs):
31
        attrs = super(ScholarshipSerializer, self).my_validate(attrs)
32
33
34
35
36
37
        if attrs['amount_max'] < attrs['amount_min']:
            raise serializers.ValidationError(
                "Amount_max should be greater or equal than amount_min")

        return attrs

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


class ScholarshipViewSet(BasicModuleViewSet):
    serializer_class = ScholarshipSerializer