scholarship.py 1.47 KB
Newer Older
1
from django.db import models
2
from backend.models.abstract.basic_module import BasicModule, BasicModuleSerializer, BasicModuleViewSet
3
from backend.models.currency import Currency
4
from rest_framework import serializers
5
6
from django.core.validators import MinValueValidator

7
8
9
10
11
12
13
14
SCHOLARSHIP_FREQUENCIES = (
    ('w', 'week'),
    ('m', 'month'),
    ('s', 'semester'),
    ('y', 'year'),
    ('o', 'one_shot')
)

15
16
17

class Scholarship(BasicModule):

Florent Chehab's avatar
Florent Chehab committed
18
    type = models.CharField(max_length=200)
19
    currency = models.ForeignKey(Currency, on_delete=models.PROTECT)
20
21
22
23
24
25
    frequency = models.CharField(
        max_length=1,
        choices=SCHOLARSHIP_FREQUENCIES,
        default='m',
    )

26
27
28
29
30
31
32
33
34
35
36
37
38
39
    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
40
41
42
43


class ScholarshipSerializer(BasicModuleSerializer):

Florent Chehab's avatar
Florent Chehab committed
44
    def my_validate(self, attrs):
45
        attrs = super(ScholarshipSerializer, self).my_validate(attrs)
46
47
48
49
50
51
        if attrs['amount_max'] < attrs['amount_min']:
            raise serializers.ValidationError(
                "Amount_max should be greater or equal than amount_min")

        return attrs

52
53
54
55
56
57
58
    class Meta:
        model = Scholarship
        fields = '__all__'


class ScholarshipViewSet(BasicModuleViewSet):
    serializer_class = ScholarshipSerializer