university.py 1.48 KB
Newer Older
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
from backend.models.my_model import MyModel
Florent Chehab's avatar
Florent Chehab committed
3
from backend.utils import get_model_config
4
5
6
7
8
9
from django.core.exceptions import ValidationError
from rest_framework.validators import ValidationError as RFValidationError
from backend.models.tag.validators.url import validate_extension
from django.conf import settings
from backend.models.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
10

11
12
13
14
15
16
17

def validate_extension_django(url):
    try:
        validate_extension(settings.ALLOWED_PHOTOS_EXTENSION, url)
    except RFValidationError:
        raise ValidationError(
            "Extension in URL doesn't correspond to an image")
Florent Chehab's avatar
Florent Chehab committed
18

19

20
class University(MyModel):
21
22
23
    """
    Model storing information about universities
    """
Florent Chehab's avatar
Florent Chehab committed
24
    model_config = get_model_config("University")
25

26
    name = models.CharField(max_length=200)
Florent Chehab's avatar
Florent Chehab committed
27
    acronym = models.CharField(max_length=20, null=True, blank=True)
28
29
30
31
32
33
34
35
36
37
38
    logo = models.URLField(null=True, blank=True, validators=[
                           validate_extension_django])
    website = models.URLField(null=True, blank=True)
    utc_id = models.IntegerField(unique=True)


class UniversitySerializer(MyModelSerializer):

    class Meta:
        model = University
        exclude = ('utc_id',)
Florent Chehab's avatar
Florent Chehab committed
39
40


41
42
43
44
class UniversityViewSet(MyModelViewSet):
    permission_classes = get_viewset_permissions("UniversityViewSet")
    serializer_class = UniversitySerializer
    queryset = University.objects.all()  # pylint: disable=E1101