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

Campus fusion finished with API

parent 423731b0
from django.contrib import admin from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin 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 from rex.models.location import Country, City
...@@ -9,4 +9,4 @@ admin.site.register(Country) ...@@ -9,4 +9,4 @@ admin.site.register(Country)
admin.site.register(City) admin.site.register(City)
admin.site.register(University, CompareVersionAdmin) 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 import django.core.validators
from django.db import migrations, models from django.db import migrations, models
...@@ -14,6 +14,17 @@ class Migration(migrations.Migration): ...@@ -14,6 +14,17 @@ class Migration(migrations.Migration):
] ]
operations = [ 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( migrations.CreateModel(
name='City', name='City',
fields=[ fields=[
...@@ -37,20 +48,6 @@ class Migration(migrations.Migration): ...@@ -37,20 +48,6 @@ class Migration(migrations.Migration):
('intermediate_region_un_code', models.CharField(blank=True, max_length=3, null=True)), ('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( migrations.CreateModel(
name='University', name='University',
fields=[ fields=[
...@@ -58,12 +55,29 @@ class Migration(migrations.Migration): ...@@ -58,12 +55,29 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=200)), ('name', models.CharField(max_length=200)),
('acronym', models.CharField(blank=True, max_length=20, null=True)), ('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'))), ('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( migrations.AddField(
model_name='city', model_name='city',
name='country', name='country',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rex.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 .university import University # noqa: F401
from .campus import MainCampus, MainCampusViewSet # noqa: F401 from .campus import Campus, CampusViewSet, CampusSerializer # noqa: F401
from .universityAPI import UniversitySerializer, UniversityViewSet # noqa: F401
from django.db import models from django.db import models
from rex.models.module import Module from rex.models.module import Module
from rex.models.location import City from rex.models.location import City
from rex.models.university import University
from django.core.validators import MinValueValidator, MaxValueValidator from django.core.validators import MinValueValidator, MaxValueValidator
from rest_framework import serializers, viewsets, permissions from rest_framework import serializers, viewsets, permissions
class Campus(Module): 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) name = models.CharField(max_length=200, null=True)
comment = models.TextField(null=True, blank=True) comment = models.TextField(null=True, blank=True)
city = models.ForeignKey(City, on_delete=models.PROTECT, null=False) 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( lat = models.DecimalField(
max_digits=10, max_digits=10,
...@@ -28,16 +36,10 @@ class Campus(Module): ...@@ -28,16 +36,10 @@ class Campus(Module):
return {'lat': self.lat, 'lon': self.lon} return {'lat': self.lat, 'lon': self.lon}
class Meta: class Meta:
abstract = True unique_together = ('is_main_campus', 'university')
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é."
class MainCampusSerializer(serializers.ModelSerializer): class CampusSerializer(serializers.ModelSerializer):
modified_by = serializers.SerializerMethodField() modified_by = serializers.SerializerMethodField()
modified_date = serializers.SerializerMethodField() modified_date = serializers.SerializerMethodField()
...@@ -48,11 +50,13 @@ class MainCampusSerializer(serializers.ModelSerializer): ...@@ -48,11 +50,13 @@ class MainCampusSerializer(serializers.ModelSerializer):
return obj.modified_date() return obj.modified_date()
class Meta: class Meta:
model = MainCampus model = Campus
fields = ('lat', 'lon', 'name', 'city', 'modified_by', 'modified_date', 'id') 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,) permission_classes = (permissions.DjangoModelPermissions,)
queryset = MainCampus.objects.all() # pylint: disable=E1101 queryset = Campus.objects.all() # pylint: disable=E1101
serializer_class = MainCampusSerializer serializer_class = CampusSerializer
from django.db import models 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.utils.friendly_path import friendly_path
from rex.models.university.campus import MainCampus
path_and_rename = friendly_path("uploads/universities/logos/", 'name') path_and_rename = friendly_path("uploads/universities/logos/", 'name')
class University(models.Model): class University(Module):
""" """
Model storing information about universities Model storing information about universities
""" """
...@@ -15,21 +13,7 @@ class University(models.Model): ...@@ -15,21 +13,7 @@ class University(models.Model):
acronym = models.CharField(max_length=20, null=True, blank=True) acronym = models.CharField(max_length=20, null=True, blank=True)
logo = models.ImageField(upload_to=path_and_rename, max_length=250, logo = models.ImageField(upload_to=path_and_rename, max_length=250,
default='path/to/my/default/image.jpg') # TODO CHANGE ! default='path/to/my/default/image.jpg') # TODO CHANGE !
website = models.URLField(null=True)
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')
class UniversityViewSet(viewsets.ModelViewSet): # TODO switch to dict # Rest API related stuff are in universityAPI.py to prevent circular depedencies
permission_classes = (permissions.DjangoModelPermissions,)
queryset = University.objects.all() # pylint: disable=E1101
serializer_class = UniversitySerializer
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 ...@@ -2,7 +2,7 @@ from django.conf.urls import url, include
from rex import views from rex import views
from rest_framework import routers from rest_framework import routers
from rex.models.location import CountryViewSet, CityViewSet 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 from rest_framework.documentation import include_docs_urls
...@@ -15,7 +15,7 @@ urlpatterns = [ ...@@ -15,7 +15,7 @@ urlpatterns = [
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'country', CountryViewSet) router.register(r'country', CountryViewSet)
router.register(r'city', CityViewSet) router.register(r'city', CityViewSet)
router.register(r'main-campus', MainCampusViewSet) router.register(r'campus', CampusViewSet)
router.register(r'university', UniversityViewSet) router.register(r'university', UniversityViewSet)
urlpatterns += [url(r'^api/', include(router.urls))] urlpatterns += [url(r'^api/', include(router.urls))]
...@@ -61,15 +61,17 @@ def insert_city(city_name, country): ...@@ -61,15 +61,17 @@ def insert_city(city_name, country):
return inserted_city_cc[city_name + country] return inserted_city_cc[city_name + country]
def insert_main_campus(name, city_id, lat, lon): def insert_university(name):
address = api_address + "/main-campus/" address = api_address + "/university/"
data = {'name': name, 'city': city_id, 'lat': lat, 'lon': lon} data = {'name': name}
return make_post(address, data) return make_post(address, data)
def insert_university(name, main_campus_id): def insert_main_campus(name, city_id, univ_id, lat, lon):
address = api_address + "/university/" address = api_address + "/campus/"
data = {'name': name, 'main_campus': main_campus_id} data = {'name': "Campus " + name, 'city': city_id, 'lat': lat,
'lon': lon, 'is_main_campus': True,
'university': univ_id}
return make_post(address, data) return make_post(address, data)
...@@ -78,5 +80,5 @@ for index, row in data.iterrows(): ...@@ -78,5 +80,5 @@ for index, row in data.iterrows():
lat = round(float(lat), 6) lat = round(float(lat), 6)
lon = round(float(lon), 6) lon = round(float(lon), 6)
city_id = insert_city(city, country) city_id = insert_city(city, country)
m_c_id = insert_main_campus(univ, city_id, lat, lon) univ_id = insert_university(univ)
insert_university(univ, m_c_id) m_c_id = insert_main_campus(univ, city_id, univ_id, lat, lon)
Supports Markdown
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