university.py 1.49 KB
Newer Older
1
from django.db import models
2
from backend.models.abstract.my_model import MyModel
Florent Chehab's avatar
Florent Chehab committed
3
from backend.utils import get_model_config
4 5
from django.core.exceptions import ValidationError
from rest_framework.validators import ValidationError as RFValidationError
6
from backend.validators.tag.url import validate_extension
7
from django.conf import settings
8
from backend.models.abstract.my_model import MyModelSerializer, MyModelViewSet
9
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