university.py 1.44 KB
Newer Older
1
from django.db import models
2
3
from django.core.validators import MinValueValidator, MaxValueValidator

4
from rex.utils.friendly_path import friendly_path
5
6
7
from rex.models.country import Country

import reverse_geocoder as rg
8
9

path_and_rename = friendly_path("uploads/universities/logos/",'name')
10
11

class University(models.Model):
12
13
14
    """
    Model storing information about universities
    """
15
    name = models.CharField(max_length=200)
16
    acronym = models.CharField(max_length=20)
17
18
19
20
21
22
23
24
25
26
27
    localization_lat = models.DecimalField(
        max_digits=10, 
        decimal_places=6,
        validators=[MinValueValidator(-85.05112878), MaxValueValidator(85.05112878)]
        )
    localization_lon = models.DecimalField(
        max_digits=10, 
        decimal_places=6,
        validators=[MinValueValidator(-180), MaxValueValidator(180)]
        )
    country = models.ForeignKey(Country, on_delete='CASCADE')
28
    logo = models.ImageField(upload_to=path_and_rename, max_length=250)
29
30

    def localization(self):
31
32
33
34
35
36
37
38
39
40
41
        return {'lat':self.localization_lat, 'lon':self.localization_lon}

    def save(self, *args, **kwargs):
        """
        Override traditional save function to make sure that 
        the Country is coherent with given lat / lon
        """
        coord = (self.localization_lat, self.localization_lon)
        res = rg.search(coord, verbose=False)
        self.country = Country.objects.get(pk=res[0]['cc'])
        super(University, self).save(*args, **kwargs)