Commit 9895a8ab authored by Florent Chehab's avatar Florent Chehab

Updated University model and related

parent 44a6c5fc
......@@ -21,11 +21,11 @@ class LoadUniversities(LoadGeneric):
dtype=object)
for index, row in data.iterrows():
univ_name, city_name, country_code, lat, lon = row
utc_id, univ_name, city_name, country_code, lat, lon, acronym, website, logo = row
lat = round(float(lat), 6)
lon = round(float(lon), 6)
country = Country.objects.filter(pk=country_code)[0]
country = Country.objects.get(pk=country_code)
city = City(
name=city_name,
country=country
......@@ -33,10 +33,15 @@ class LoadUniversities(LoadGeneric):
city.save()
self.add_info(city, self.admin)
univ = University(
name=univ_name
)
univ.save()
univ = University.objects.update_or_create(
utc_id=utc_id,
defaults={
"name": univ_name,
"acronym": acronym,
"website": website,
"logo": logo
}
)[0]
self.add_info(univ, self.admin)
main_campus = Campus(
......
# Generated by Django 2.0.3 on 2018-09-03 07:53
import backend.models.university.university
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20180902_1716'),
]
operations = [
migrations.AddField(
model_name='university',
name='utc_id',
field=models.IntegerField(default=1, unique=True),
preserve_default=False,
),
migrations.AlterField(
model_name='university',
name='logo',
field=models.URLField(blank=True, null=True, validators=[backend.models.university.university.validate_extension_django]),
),
migrations.AlterField(
model_name='university',
name='website',
field=models.URLField(blank=True, null=True),
),
]
from django.conf import settings
PHOTOS_TAG_GONFIG = {
"type": "array",
"required": True,
......@@ -6,12 +8,7 @@ PHOTOS_TAG_GONFIG = {
"type": "url",
"required": True,
"validators": {
"extension": [
"jpg",
"jpeg",
"png",
"svg"
]
"extension": settings.ALLOWED_PHOTOS_EXTENSION
}
},
"title": {
......
from .university import University # noqa: F401
from .university import University, UniversitySerializer, UniversityViewSet # noqa: F401
from .universityAPI import UniversitySerializer, UniversityViewSet # noqa: F401
from .universityScholarship import UniversityScholarship, UniversityScholarshipSerializer, UniversityScholarshipViewSet # noqa: F401
from .universityDri import UniversityDri, UniversityDriSerializer, UniversityDriViewSet # noqa: F401
from .universityInfo import UniversityInfo, UniversityInfoSerializer, UniversityInfoViewSet # noqa: F401
......
from django.db import models
from backend.models.my_model import MyModel
from backend.utils.friendly_path import friendly_path
from backend.utils import get_model_config
from django.core.exceptions import ValidationError
from rest_framework.validators import ValidationError as RFValidationError
from backend.models.tag.validators.url import validate_extension
from django.conf import settings
from backend.models.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
path_and_rename = friendly_path("uploads/universities/logos/", 'name')
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")
class University(MyModel):
......@@ -14,9 +25,20 @@ class University(MyModel):
name = models.CharField(max_length=200)
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 !
website = models.URLField(null=True)
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',)
# Rest API related stuff are in universityAPI.py to prevent circular depedencies
class UniversityViewSet(MyModelViewSet):
permission_classes = get_viewset_permissions("UniversityViewSet")
serializer_class = UniversitySerializer
queryset = University.objects.all() # pylint: disable=E1101
from backend.models.university import University
from backend.models.my_model import MyModelSerializer, MyModelViewSet
from backend.utils import get_viewset_permissions
class UniversitySerializer(MyModelSerializer):
class Meta:
model = University
fields = '__all__'
class UniversityViewSet(MyModelViewSet):
permission_classes = get_viewset_permissions("UniversityViewSet")
serializer_class = UniversitySerializer
queryset = University.objects.all() # pylint: disable=E1101
......@@ -216,7 +216,7 @@ class ModerationTestCase(WithUserTestCase):
Test to check that when moderation IS activated
no error regarding saving occures with foreing key
"""
c = University(name="Univ de test")
c = University(name="Univ de test", utc_id=100)
c.save()
data = {
......
......@@ -52,6 +52,7 @@ MIDDLEWARE = [
# If we want to activate the moderation functionnalities
MODERATION_ACTIVATED = True
TESTING = "pytest" in sys.modules
ALLOWED_PHOTOS_EXTENSION = ["jpg", "jpeg", "png", "svg"]
INTERNAL_IPS = ['127.0.0.1']
......
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