university.py 1.35 KB
Newer Older
1 2
from django.conf import settings
from django.core.exceptions import ValidationError
3
from django.db import models
4 5
from rest_framework.validators import ValidationError as RFValidationError

6 7 8 9 10
from backend_app.models.abstract.essentialModule import (
    EssentialModule,
    EssentialModuleSerializer,
    EssentialModuleViewSet,
)
11
from backend_app.validators.tags import validate_extension
12

13 14 15 16 17

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

20

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

26
    name = models.CharField(max_length=200)
27 28 29 30 31
    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)
32 33 34
    utc_id = models.IntegerField(unique=True)


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


41
class UniversityViewSet(EssentialModuleViewSet):
42 43
    serializer_class = UniversitySerializer
    queryset = University.objects.all()  # pylint: disable=E1101
44
    end_point_route = "universities"