Commit 5b2410d0 authored by Florent Chehab's avatar Florent Chehab

Campus fusion finished with API

parent 423731b0
from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin
from rex.models.university import University, MainCampus
from rex.models.university import University, Campus
from rex.models.location import Country, City
......@@ -9,4 +9,4 @@ admin.site.register(Country)
admin.site.register(City)
admin.site.register(University, CompareVersionAdmin)
admin.site.register(MainCampus, CompareVersionAdmin)
admin.site.register(Campus, CompareVersionAdmin)
# Generated by Django 2.0.3 on 2018-08-18 08:44
# Generated by Django 2.0.3 on 2018-08-19 16:39
import django.core.validators
from django.db import migrations, models
......@@ -14,6 +14,17 @@ class Migration(migrations.Migration):
]
operations = [
migrations.CreateModel(
name='Campus',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('is_main_campus', models.BooleanField()),
('name', models.CharField(max_length=200, null=True)),
('comment', models.TextField(blank=True, null=True)),
('lat', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-85.05112878), django.core.validators.MaxValueValidator(85.05112878)])),
('lon', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-180), django.core.validators.MaxValueValidator(180)])),
],
),
migrations.CreateModel(
name='City',
fields=[
......@@ -37,20 +48,6 @@ class Migration(migrations.Migration):
('intermediate_region_un_code', models.CharField(blank=True, max_length=3, null=True)),
],
),
migrations.CreateModel(
name='MainCampus',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200, null=True)),
('comment', models.TextField(blank=True, null=True)),
('lat', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-85.05112878), django.core.validators.MaxValueValidator(85.05112878)])),
('lon', models.DecimalField(decimal_places=6, max_digits=10, validators=[django.core.validators.MinValueValidator(-180), django.core.validators.MaxValueValidator(180)])),
('city', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.City')),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='University',
fields=[
......@@ -58,12 +55,29 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=200)),
('acronym', models.CharField(blank=True, max_length=20, null=True)),
('logo', models.ImageField(default='path/to/my/default/image.jpg', max_length=250, upload_to=rex.utils.friendly_path.friendly_path('uploads/universities/logos/', 'name'))),
('main_campus', models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, related_name='main_campus_of', to='rex.MainCampus')),
('website', models.URLField(null=True)),
],
options={
'abstract': False,
},
),
migrations.AddField(
model_name='city',
name='country',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.Country'),
),
migrations.AddField(
model_name='campus',
name='city',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.City'),
),
migrations.AddField(
model_name='campus',
name='university',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='univCampus', to='rex.University'),
),
migrations.AlterUniqueTogether(
name='campus',
unique_together={('is_main_campus', 'university')},
),
]
from .university import University, UniversityViewSet # noqa: F401
from .campus import MainCampus, MainCampusViewSet # noqa: F401
from .university import University # noqa: F401
from .campus import Campus, CampusViewSet, CampusSerializer # noqa: F401
from .universityAPI import UniversitySerializer, UniversityViewSet # noqa: F401
from django.db import models
from rex.models.module import Module
from rex.models.location import City
from rex.models.university import University
from django.core.validators import MinValueValidator, MaxValueValidator
from rest_framework import serializers, viewsets, permissions
class Campus(Module):
module_icon = "You forget to override the icon in the sub-class : I know"
module_name = "Campus principal"
module_description = "Campus de l'université."
is_main_campus = models.BooleanField(null=False)
name = models.CharField(max_length=200, null=True)
comment = models.TextField(null=True, blank=True)
city = models.ForeignKey(City, on_delete=models.PROTECT, null=False)
university = models.ForeignKey(
University, on_delete=models.PROTECT, null=False, related_name="univCampus")
lat = models.DecimalField(
max_digits=10,
......@@ -28,16 +36,10 @@ class Campus(Module):
return {'lat': self.lat, 'lon': self.lon}
class Meta:
abstract = True
class MainCampus(Campus):
module_icon = "You forget to override the icon in the sub-class : I know"
module_name = "Campus principal"
module_description = "Campus principal de l'université."
unique_together = ('is_main_campus', 'university')
class MainCampusSerializer(serializers.ModelSerializer):
class CampusSerializer(serializers.ModelSerializer):
modified_by = serializers.SerializerMethodField()
modified_date = serializers.SerializerMethodField()
......@@ -48,11 +50,13 @@ class MainCampusSerializer(serializers.ModelSerializer):
return obj.modified_date()
class Meta:
model = MainCampus
fields = ('lat', 'lon', 'name', 'city', 'modified_by', 'modified_date', 'id')
model = Campus
fields = ('is_main_campus', 'university', 'lat', 'lon',
'name', 'city', 'modified_by', 'modified_date',
'id')
class MainCampusViewSet(viewsets.ModelViewSet): # TODO switch to dict
class CampusViewSet(viewsets.ModelViewSet): # TODO switch to dict
permission_classes = (permissions.DjangoModelPermissions,)
queryset = MainCampus.objects.all() # pylint: disable=E1101
serializer_class = MainCampusSerializer
queryset = Campus.objects.all() # pylint: disable=E1101
serializer_class = CampusSerializer
from django.db import models
from rest_framework import serializers, viewsets, permissions
from rex.models.module import Module
from rex.utils.friendly_path import friendly_path
from rex.models.university.campus import MainCampus
path_and_rename = friendly_path("uploads/universities/logos/", 'name')
class University(models.Model):
class University(Module):
"""
Model storing information about universities
"""
......@@ -15,21 +13,7 @@ class University(models.Model):
acronym = models.CharField(max_length=20, null=True, blank=True)
logo = models.ImageField(upload_to=path_and_rename, max_length=250,
default='path/to/my/default/image.jpg') # TODO CHANGE !
main_campus = models.OneToOneField(
MainCampus, on_delete=models.PROTECT, related_name='main_campus_of')
def location(self):
return self.main_campus.location() # pylint: disable=E1101
class UniversitySerializer(serializers.ModelSerializer):
class Meta:
model = University
fields = ('name', 'acronym', 'logo', 'main_campus', 'id')
website = models.URLField(null=True)
class UniversityViewSet(viewsets.ModelViewSet): # TODO switch to dict
permission_classes = (permissions.DjangoModelPermissions,)
queryset = University.objects.all() # pylint: disable=E1101
serializer_class = UniversitySerializer
# Rest API related stuff are in universityAPI.py to prevent circular depedencies
from rex.models.university import University
from rest_framework import serializers, viewsets, permissions
from rex.models.university.campus import CampusSerializer
class UniversitySerializer(serializers.ModelSerializer):
univCampus = CampusSerializer(many=True, read_only=True)
class Meta:
model = University
fields = ('name', 'acronym', 'logo', 'id', 'univCampus')
class UniversityViewSet(viewsets.ModelViewSet): # TODO switch to dict
permission_classes = (permissions.DjangoModelPermissions,)
queryset = University.objects.all() # pylint: disable=E1101
serializer_class = UniversitySerializer
......@@ -2,7 +2,7 @@ from django.conf.urls import url, include
from rex import views
from rest_framework import routers
from rex.models.location import CountryViewSet, CityViewSet
from rex.models.university import MainCampusViewSet, UniversityViewSet
from rex.models.university import CampusViewSet, UniversityViewSet
from rest_framework.documentation import include_docs_urls
......@@ -15,7 +15,7 @@ urlpatterns = [
router = routers.DefaultRouter()
router.register(r'country', CountryViewSet)
router.register(r'city', CityViewSet)
router.register(r'main-campus', MainCampusViewSet)
router.register(r'campus', CampusViewSet)
router.register(r'university', UniversityViewSet)
urlpatterns += [url(r'^api/', include(router.urls))]
......@@ -61,15 +61,17 @@ def insert_city(city_name, country):
return inserted_city_cc[city_name + country]
def insert_main_campus(name, city_id, lat, lon):
address = api_address + "/main-campus/"
data = {'name': name, 'city': city_id, 'lat': lat, 'lon': lon}
def insert_university(name):
address = api_address + "/university/"
data = {'name': name}
return make_post(address, data)
def insert_university(name, main_campus_id):
address = api_address + "/university/"
data = {'name': name, 'main_campus': main_campus_id}
def insert_main_campus(name, city_id, univ_id, lat, lon):
address = api_address + "/campus/"
data = {'name': "Campus " + name, 'city': city_id, 'lat': lat,
'lon': lon, 'is_main_campus': True,
'university': univ_id}
return make_post(address, data)
......@@ -78,5 +80,5 @@ for index, row in data.iterrows():
lat = round(float(lat), 6)
lon = round(float(lon), 6)
city_id = insert_city(city, country)
m_c_id = insert_main_campus(univ, city_id, lat, lon)
insert_university(univ, m_c_id)
univ_id = insert_university(univ)
m_c_id = insert_main_campus(univ, city_id, univ_id, lat, lon)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment