scholarship.py 1.04 KB
Newer Older
1 2
from django.db import models
from rex.models.module import BasicModule
3
from rex.models.tools 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 26
    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)]
    )

    # Need custom validation
    def clean(self, *args, **kwargs):
        if self.amount_max < self.amount_min:
27
            raise serializers.ValidationError(
28 29 30 31 32 33 34 35 36
                "Amount_max should be greater or equal than amount_min")
        super(Scholarship, self).clean(*args, **kwargs)

    def save(self, *args, **kwargs):
        self.full_clean()
        super(Scholarship, self).save(*args, **kwargs)

    class Meta:
        abstract = True