university.py 1.31 KB
Newer Older
1
from django.db import models
2
3
4
5
6
from backend_app.models.abstract.essentialModule import (
    EssentialModule,
    EssentialModuleSerializer,
    EssentialModuleViewSet,
)
7
8
from django.core.exceptions import ValidationError
from rest_framework.validators import ValidationError as RFValidationError
9
from backend_app.validators.tags import validate_extension
10
from django.conf import settings
11

12
13
14
15
16

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

19

20
class University(EssentialModule):
21
22
23
    """
    Model storing information about universities
    """
24

25
    name = models.CharField(max_length=200)
26
27
28
29
30
    acronym = models.CharField(max_length=20, default="", blank=True)
    logo = models.URLField(
        default="", blank=True, validators=[validate_extension_django]
    )
    website = models.URLField(default="", blank=True, max_length=300)
31
32
33
    utc_id = models.IntegerField(unique=True)


34
class UniversitySerializer(EssentialModuleSerializer):
35
36
    class Meta:
        model = University
37
        exclude = ("utc_id",)
Florent Chehab's avatar
Florent Chehab committed
38
39


40
class UniversityViewSet(EssentialModuleViewSet):
41
42
    serializer_class = UniversitySerializer
    queryset = University.objects.all()  # pylint: disable=E1101