university.py 1.23 KB
Newer Older
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
from rest_framework import serializers, viewsets, permissions
3

4
from rex.utils.friendly_path import friendly_path
Florent Chehab's avatar
Florent Chehab committed
5
from rex.models.university.campus import MainCampus
6

Florent Chehab's avatar
Florent Chehab committed
7
8
path_and_rename = friendly_path("uploads/universities/logos/", 'name')

9
10

class University(models.Model):
11
12
13
    """
    Model storing information about universities
    """
14
    name = models.CharField(max_length=200)
Florent Chehab's avatar
Florent Chehab committed
15
    acronym = models.CharField(max_length=20, null=True, blank=True)
Florent Chehab's avatar
linting    
Florent Chehab committed
16
17
    logo = models.ImageField(upload_to=path_and_rename, max_length=250,
                             default='path/to/my/default/image.jpg')  # TODO CHANGE !
18

Florent Chehab's avatar
Florent Chehab committed
19
20
    main_campus = models.OneToOneField(
        MainCampus, on_delete=models.PROTECT, related_name='main_campus_of')
21

22
23
    def location(self):
        return self.main_campus.location()  # pylint: disable=E1101
Florent Chehab's avatar
Florent Chehab committed
24
25
26
27
28
29
30
31
32
33
34
35


class UniversitySerializer(serializers.ModelSerializer):
    class Meta:
        model = University
        fields = ('name', 'acronym', 'logo', 'main_campus', 'id')


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