Commit 32e2b6bb authored by Florent Chehab's avatar Florent Chehab
Browse files

Backend updated with aucreation of userdata and univ modules

Also removed some stuff
parent 5350a0b5
Pipeline #27164 passed with stages
in 2 minutes and 44 seconds
...@@ -5,4 +5,6 @@ class BackendConfig(AppConfig): ...@@ -5,4 +5,6 @@ class BackendConfig(AppConfig):
name = 'backend' name = 'backend'
def ready(self): def ready(self):
import backend.signals # noqa: F401 import backend.signals.__create_user_modules_post_create # noqa: F401
import backend.signals.__squash_revision_by_user # noqa: F401
import backend.signals.__create_univ_modules_post_save # noqa: F401
...@@ -49,4 +49,4 @@ class LoadCountries(LoadGeneric): ...@@ -49,4 +49,4 @@ class LoadCountries(LoadGeneric):
intermediate_region_un_code=r["Intermediate Region Code"], intermediate_region_un_code=r["Intermediate Region Code"],
) )
country.save() country.save()
self.add_info(country, self.admin) self.add_info_and_save(country, self.admin)
...@@ -26,4 +26,4 @@ class LoadCurrencies(LoadGeneric): ...@@ -26,4 +26,4 @@ class LoadCurrencies(LoadGeneric):
one_EUR_in_this_currency=Decimal(r[2]) one_EUR_in_this_currency=Decimal(r[2])
) )
currency.save() currency.save()
self.add_info(currency, self.admin) self.add_info_and_save(currency, self.admin)
...@@ -5,7 +5,7 @@ import reversion ...@@ -5,7 +5,7 @@ import reversion
class LoadGeneric(object): class LoadGeneric(object):
@classmethod @classmethod
def add_info(cls, obj, admin): def add_info_and_save(cls, obj, admin):
with reversion.create_revision(): with reversion.create_revision():
obj.moderated_by = admin obj.moderated_by = admin
obj.updated_by = admin obj.updated_by = admin
......
...@@ -20,4 +20,4 @@ class LoadTags(LoadGeneric): ...@@ -20,4 +20,4 @@ class LoadTags(LoadGeneric):
for tag in tags: for tag in tags:
t = Tag(name=tag['name'], config=tag['config']) t = Tag(name=tag['name'], config=tag['config'])
t.save() t.save()
self.add_info(t, self.admin) self.add_info_and_save(t, self.admin)
...@@ -33,7 +33,7 @@ class LoadUniversities(LoadGeneric): ...@@ -33,7 +33,7 @@ class LoadUniversities(LoadGeneric):
country=country country=country
) )
city.save() city.save()
self.add_info(city, self.admin) self.add_info_and_save(city, self.admin)
univ = University.objects.update_or_create( univ = University.objects.update_or_create(
utc_id=utc_id, utc_id=utc_id,
...@@ -44,7 +44,7 @@ class LoadUniversities(LoadGeneric): ...@@ -44,7 +44,7 @@ class LoadUniversities(LoadGeneric):
"logo": logo "logo": logo
} }
)[0] )[0]
self.add_info(univ, self.admin) self.add_info_and_save(univ, self.admin)
main_campus = Campus( main_campus = Campus(
is_main_campus=True, is_main_campus=True,
...@@ -55,4 +55,4 @@ class LoadUniversities(LoadGeneric): ...@@ -55,4 +55,4 @@ class LoadUniversities(LoadGeneric):
lon=lon lon=lon
) )
main_campus.save() main_campus.save()
self.add_info(main_campus, self.admin) self.add_info_and_save(main_campus, self.admin)
...@@ -26,24 +26,18 @@ class LoadUniversityEx(LoadGeneric): ...@@ -26,24 +26,18 @@ class LoadUniversityEx(LoadGeneric):
comment="Attention les cours de master sont 100% en anglais") comment="Attention les cours de master sont 100% en anglais")
univ_dri_1.save() univ_dri_1.save()
univ_dri_1.universities.add(EPFL) univ_dri_1.universities.add(EPFL)
univ_dri_1.save() self.add_info_and_save(univ_dri_1, self.admin)
self.add_info(univ_dri_1, self.admin)
univ_info = UniversityInfo( univ_info = UniversityInfo.objects.get(university=EPFL)
university=EPFL, univ_info.cost_exchange = 0
cost_exchange=0, univ_info.costs_currency = CHF
costs_currency=CHF self.add_info_and_save(univ_info, self.admin)
)
univ_info.save()
self.add_info(univ_info, self.admin)
univ_semesters_dates = UniversitySemestersDates( usd = UniversitySemestersDates.objects.get(
university=EPFL, university=EPFL)
autumn_begin=datetime.strptime("17/09/2018", '%d/%m/%Y'), usd.autumn_begin = datetime.strptime("17/09/2018", '%d/%m/%Y')
autumn_end=datetime.strptime("29/01/2019", '%d/%m/%Y') usd.autumn_end = datetime.strptime("29/01/2019", '%d/%m/%Y')
) self.add_info_and_save(usd, self.admin)
univ_semesters_dates.save()
self.add_info(univ_semesters_dates, self.admin)
univ_scholarship_1 = UniversityScholarship( univ_scholarship_1 = UniversityScholarship(
type="Swiss European...", type="Swiss European...",
...@@ -55,8 +49,7 @@ class LoadUniversityEx(LoadGeneric): ...@@ -55,8 +49,7 @@ class LoadUniversityEx(LoadGeneric):
) )
univ_scholarship_1.save() univ_scholarship_1.save()
univ_scholarship_1.universities.add(EPFL) univ_scholarship_1.universities.add(EPFL)
univ_scholarship_1.save() self.add_info_and_save(univ_scholarship_1, self.admin)
self.add_info(univ_scholarship_1, self.admin)
univ_tag_1 = UniversityTaggedItem( univ_tag_1 = UniversityTaggedItem(
university=EPFL, university=EPFL,
...@@ -65,5 +58,4 @@ class LoadUniversityEx(LoadGeneric): ...@@ -65,5 +58,4 @@ class LoadUniversityEx(LoadGeneric):
comment="Mon commentaire.", comment="Mon commentaire.",
importance_level='++' importance_level='++'
) )
univ_tag_1.save() self.add_info_and_save(univ_tag_1, self.admin)
self.add_info(univ_tag_1, self.admin)
# Generated by Django 2.0.3 on 2018-09-11 09:14
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0006_auto_20180910_2144'),
]
operations = [
migrations.RemoveField(
model_name='userdata',
name='id',
),
migrations.AlterField(
model_name='userdata',
name='owner',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 2.0.3 on 2018-09-11 09:47
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0007_auto_20180911_1114'),
]
operations = [
migrations.AlterField(
model_name='universityinfo',
name='costs_currency',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='backend.Currency'),
),
]
from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet from backend.models.abstract.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.signals import new_revision_saved from backend.signals.__squash_revision_by_user import new_revision_saved
from rest_framework import serializers, mixins, viewsets from rest_framework import serializers, mixins, viewsets
import reversion import reversion
from reversion.models import Version from reversion.models import Version
......
...@@ -27,7 +27,7 @@ class UniversityInfo(BasicModule): ...@@ -27,7 +27,7 @@ class UniversityInfo(BasicModule):
) )
costs_currency = models.ForeignKey( costs_currency = models.ForeignKey(
Currency, on_delete=models.PROTECT, default='EUR') Currency, on_delete=models.PROTECT, null=True)
@classmethod @classmethod
def get_serializer(cls): def get_serializer(cls):
......
...@@ -29,7 +29,8 @@ class UniversitySemestersDates(BasicModule): ...@@ -29,7 +29,8 @@ class UniversitySemestersDates(BasicModule):
class UniversitySemestersDatesSerializer(BasicModuleSerializer): class UniversitySemestersDatesSerializer(BasicModuleSerializer):
def my_validate(self, attrs): def my_validate(self, attrs):
attrs = super(UniversitySemestersDatesSerializer, self).my_validate(attrs) attrs = super(UniversitySemestersDatesSerializer,
self).my_validate(attrs)
s_b, s_e = attrs['spring_begin'], attrs['spring_end'] s_b, s_e = attrs['spring_begin'], attrs['spring_end']
a_b, a_e = attrs['autumn_begin'], attrs['autumn_end'] a_b, a_e = attrs['autumn_begin'], attrs['autumn_end']
...@@ -45,9 +46,6 @@ class UniversitySemestersDatesSerializer(BasicModuleSerializer): ...@@ -45,9 +46,6 @@ class UniversitySemestersDatesSerializer(BasicModuleSerializer):
test(s_b, s_e, 'spring') test(s_b, s_e, 'spring')
test(a_b, a_e, 'autumn') test(a_b, a_e, 'autumn')
if s_b is None and a_b is None:
semester_error('autaumn and spring')
return attrs return attrs
class Meta: class Meta:
......
...@@ -10,7 +10,7 @@ from backend.utils import get_viewset_permissions, get_model_config ...@@ -10,7 +10,7 @@ from backend.utils import get_viewset_permissions, get_model_config
class UserData(MyModel): class UserData(MyModel):
model_config = get_model_config("UserData") model_config = get_model_config("UserData")
owner = models.OneToOneField(User, on_delete=models.CASCADE) owner = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
contact_info = JSONField(default=dict) contact_info = JSONField(default=dict)
contact_info_is_public = models.BooleanField(default=False) contact_info_is_public = models.BooleanField(default=False)
config = JSONField(default=dict) config = JSONField(default=dict)
......
from django.db.models.signals import post_save
from backend.models.university import University
from backend.models.university import UniversityInfo
from backend.models.university import UniversitySemestersDates
def create_univ_modules(sender, instance, created, **kwargs):
if created:
UniversityInfo.objects.create(university=instance)
UniversitySemestersDates.objects.create(university=instance)
post_save.connect(create_univ_modules, sender=University)
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from backend.models.user import UserData
def create_user_modules(sender, instance, created, **kwargs):
if created:
UserData.objects.create(owner=instance)
post_save.connect(create_user_modules, sender=User)
...@@ -4,7 +4,7 @@ import django.dispatch ...@@ -4,7 +4,7 @@ import django.dispatch
new_revision_saved = django.dispatch.Signal(providing_args=["obj"]) new_revision_saved = django.dispatch.Signal(providing_args=["obj"])
def squashRevisionByUser(sender, obj, **kwargs): def squash_revision_by_user(sender, obj, **kwargs):
""" """
It should also work with moderation as obj will be a versionned object It should also work with moderation as obj will be a versionned object
""" """
...@@ -25,6 +25,6 @@ def squashRevisionByUser(sender, obj, **kwargs): ...@@ -25,6 +25,6 @@ def squashRevisionByUser(sender, obj, **kwargs):
new_revision_saved.connect( new_revision_saved.connect(
squashRevisionByUser, squash_revision_by_user,
dispatch_uid="receiver_concat_revisions" dispatch_uid="receiver_concat_revisions"
) )
...@@ -21,8 +21,8 @@ class SemesterDatesTestCase(TestCase): ...@@ -21,8 +21,8 @@ class SemesterDatesTestCase(TestCase):
self.ser.my_validate(attrs) self.ser.my_validate(attrs)
self.ser = UniversitySemestersDatesSerializer() self.ser = UniversitySemestersDatesSerializer()
_test_attrs_error(build([None] * 4))
_test_attrs_error(build([None, 3, None, None])) _test_attrs_error(build([None, 3, None, None]))
_test_attrs_error(build([2, 3, 3, 2])) _test_attrs_error(build([2, 3, 3, 2]))
self.ser.my_validate(build([None] * 4))
self.ser.my_validate(build([2, 3, None, None])) self.ser.my_validate(build([2, 3, None, None]))
self.ser.my_validate(build([2, 3, 2, 3])) self.ser.my_validate(build([2, 3, 2, 3]))
from django.test import TestCase
from django.contrib.auth.models import User
from backend.models.user import UserData
from backend.models.university import University
from backend.models.university import UniversityInfo
from backend.models.university import UniversitySemestersDates
class AutomaticCreationPostCreateTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(
username="toto"
)
cls.univ = University.objects.create(
name="Univ de test",
utc_id=1000
)
def test_user_data_automatically_created(self):
self.assertTrue(
UserData.objects.filter(owner=self.user).exists()
)
def test_univ_data_automatically_created(self):
self.assertTrue(
UniversityInfo.objects.filter(university=self.univ).exists()
)
self.assertTrue(
UniversitySemestersDates.objects.filter(
university=self.univ).exists()
)
from backend.models.abstract.my_model import ForTestingVersioning from backend.models.abstract.my_model import ForTestingVersioning
from reversion.models import Version from reversion.models import Version
from backend.signals import squashRevisionByUser from backend.signals.__squash_revision_by_user import squash_revision_by_user
import reversion import reversion
from .withUserTestCase import WithUserTestCase from .withUserTestCase import WithUserTestCase
import json import json
...@@ -33,7 +33,7 @@ class SquashVersionsTestCase(WithUserTestCase): ...@@ -33,7 +33,7 @@ class SquashVersionsTestCase(WithUserTestCase):
self.obj.save() self.obj.save()
reversion.set_user(self.authenticated_user_2) reversion.set_user(self.authenticated_user_2)
squashRevisionByUser(None, self.obj) squash_revision_by_user(None, self.obj)
versions = self.get_versions(self.obj) versions = self.get_versions(self.obj)
self.assertEqual(len(versions), 2) self.assertEqual(len(versions), 2)
...@@ -53,7 +53,7 @@ class SquashVersionsTestCase(WithUserTestCase): ...@@ -53,7 +53,7 @@ class SquashVersionsTestCase(WithUserTestCase):
self.obj.save() self.obj.save()
reversion.set_user(self.authenticated_user) reversion.set_user(self.authenticated_user)
squashRevisionByUser(None, self.obj) squash_revision_by_user(None, self.obj)
versions = self.get_versions(self.obj) versions = self.get_versions(self.obj)
self.assertEqual(len(versions), 1) self.assertEqual(len(versions), 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