universityInfo.py 1.6 KB
Newer Older
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
from django.db import models
from rex.models.module import BasicModule
from rex.models.university import University
from rex.models.tools import Currency
from django.core.validators import MinValueValidator
from rest_framework import serializers, viewsets, permissions


class UniversityInfo(BasicModule):

    university = models.OneToOneField(
        University, on_delete=models.CASCADE, related_name='university_info', primary_key=True, null=False)

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

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

    costs_currency = models.ForeignKey(Currency, on_delete=models.PROTECT)


class UniversityInfoSerializer(serializers.ModelSerializer):

    # TODO generalize below
    modified_by = serializers.SerializerMethodField()
    modified_date = serializers.SerializerMethodField()

    def get_modified_by(self, obj):
        return obj.modified_by()

    def get_modified_date(self, obj):
        return obj.modified_date()

    class Meta:
        model = UniversityInfo
        fields = ('university', 'cost_exchange', 'cost_double_degree',
                  'costs_currency', 'modified_by', 'modified_date')


class UniversityInfoViewSet(viewsets.ModelViewSet):  # TODO switch to dict
    permission_classes = (permissions.DjangoModelPermissions,)
    queryset = UniversityInfo.objects.all()  # pylint: disable=E1101
    serializer_class = UniversityInfoSerializer