Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

from django.core.validators import MinValueValidator 

from django.db import models 

 

from backend_app.models.abstract.module import Module, ModuleSerializer, ModuleViewSet 

 

from backend_app.models.currency import Currency 

from rest_framework import serializers 

 

SCHOLARSHIP_FREQUENCIES = ( 

("w", "week"), 

("m", "month"), 

("s", "semester"), 

("y", "year"), 

("o", "one_shot"), 

) 

 

 

class Scholarship(Module): 

""" 

Abstract model for scholarships 

""" 

 

short_description = models.CharField(max_length=200) 

currency = models.ForeignKey(Currency, null=True, on_delete=models.PROTECT) 

other_advantages = models.CharField(default="", blank=True, max_length=5000) 

 

frequency = models.CharField( 

max_length=1, 

choices=SCHOLARSHIP_FREQUENCIES, 

default="m", 

null=True, 

blank=True, 

) 

 

amount_min = models.DecimalField( 

null=True, max_digits=20, decimal_places=2, validators=[MinValueValidator(0)] 

) 

 

amount_max = models.DecimalField( 

null=True, max_digits=20, decimal_places=2, validators=[MinValueValidator(0)] 

) 

 

class Meta: 

abstract = True 

 

 

class ScholarshipSerializer(ModuleSerializer): 

""" 

Serializer for the scholarship class 

""" 

 

def validate(self, attrs): 

""" 

Custom attribute validation 

""" 

 

attrs = super().validate(attrs) 

 

if attrs["amount_min"] is not None: 

if attrs["currency"] is None: 

raise serializers.ValidationError( 

"A currency must be specified when there is a value" 

) 

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" 

) 

 

return attrs 

 

class Meta: 

model = Scholarship 

fields = ModuleSerializer.Meta.fields + ( 

"short_description", 

"currency", 

"other_advantages", 

"frequency", 

"amount_min", 

"amount_max", 

) 

 

 

class ScholarshipViewSet(ModuleViewSet): 

serializer_class = ScholarshipSerializer