Skip to content
Snippets Groups Projects
Commit 642f6556 authored by florimondmanca's avatar florimondmanca
Browse files

add first name and last name to registration's emergency contact, update api and tests

parent af69342f
No related branches found
No related tags found
No related merge requests found
Showing
with 214 additions and 19 deletions
"""Register admin panels."""
from django.contrib import admin
from .models import Registration
from .models import Registration, EmergencyContact
# Register your models here.
......@@ -13,4 +13,14 @@ class RegistrationAdmin(admin.ModelAdmin):
list_display = ('id', 'first_name', 'last_name', 'submitted')
readonly_fields = ('submitted',)
list_filter = ('submitted',)
autocomplete_fields = ('address',)
autocomplete_fields = ('address', 'emergency_contact')
@admin.register(EmergencyContact)
class EmergencyContactAdmin(admin.ModelAdmin):
"""Admin panel for emergency contacts."""
list_display = ('last_name', 'first_name', 'contact', 'registration',)
# necessary to use emergency contact in Registration's admin autocomplete
search_fields = ('last_name', 'first_name',)
......@@ -9,6 +9,17 @@ from core.factory import AddressFactory
from . import models
class EmergencyContactFactory(factory.DjangoModelFactory):
"""Emergency contact object factory."""
class Meta: # noqa
model = models.EmergencyContact
first_name = factory.Faker('first_name', locale='fr')
last_name = factory.Faker('last_name', locale='fr')
contact = factory.Faker('phone_number', locale='fr')
class RegistrationFactory(factory.DjangoModelFactory):
"""Registration object factory."""
......@@ -28,3 +39,4 @@ class RegistrationFactory(factory.DjangoModelFactory):
phone = factory.Faker('phone_number')
date_of_birth = factory.Faker('past_date', start_date='-20y')
address = factory.SubFactory(AddressFactory)
emergency_contact = factory.SubFactory(EmergencyContactFactory)
# Generated by Django 2.0.3 on 2018-04-13 22:00
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('register', '0009_auto_20180408_1525'),
]
operations = [
migrations.CreateModel(
name='EmergencyContact',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(help_text='Prénom du contact (50 caractères max).', max_length=50, verbose_name='prénom')),
('last_name', models.CharField(help_text='Nom du contact (50 caractères max).', max_length=50, verbose_name='nom')),
('contact', models.CharField(help_text='Téléphone, adresse email…', max_length=100)),
],
options={
'verbose_name': "contact d'urgence",
'verbose_name_plural': "contacts d'urgence",
'ordering': ('last_name', 'first_name'),
},
),
migrations.AlterField(
model_name='registration',
name='emergency_contact',
field=models.ForeignKey(blank=True, help_text="Contact en cas d'urgence.", null=True, on_delete=django.db.models.deletion.CASCADE, to='register.EmergencyContact', verbose_name="contact d'urgence"),
),
]
# Generated by Django 2.0.3 on 2018-04-13 22:10
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('register', '0010_auto_20180414_0000'),
]
operations = [
migrations.AlterField(
model_name='emergencycontact',
name='first_name',
field=models.CharField(blank=True, default='', help_text='Prénom du contact (50 caractères max).', max_length=50, verbose_name='prénom'),
),
migrations.AlterField(
model_name='emergencycontact',
name='last_name',
field=models.CharField(blank=True, default='', help_text='Nom du contact (50 caractères max).', max_length=50, verbose_name='nom'),
),
]
# Generated by Django 2.0.3 on 2018-04-13 22:16
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('register', '0011_auto_20180414_0010'),
]
operations = [
migrations.AlterField(
model_name='registration',
name='emergency_contact',
field=models.OneToOneField(blank=True, help_text="Contact en cas d'urgence.", null=True, on_delete=django.db.models.deletion.CASCADE, to='register.EmergencyContact', verbose_name="contact d'urgence"),
),
]
......@@ -33,13 +33,11 @@ class Registration(models.Model):
'core.Address', on_delete=models.CASCADE, blank=True, null=True,
verbose_name='adresse',
help_text="Adresse du lycéen")
emergency_contact = models.CharField(
max_length=100, blank=True, null=True,
emergency_contact = models.OneToOneField(
'EmergencyContact',
on_delete=models.CASCADE, blank=True, null=True,
verbose_name="contact d'urgence",
help_text=(
"Contact en cas d'urgence (100 caractères max). "
"Exemple : adresse mail ou numéro de téléphone d'un parent."
))
help_text="Contact en cas d'urgence.")
submitted = models.DateTimeField(
auto_now_add=True, verbose_name='envoyé le',
help_text="Date d'envoi du dossier d'inscription")
......@@ -62,3 +60,28 @@ class Registration(models.Model):
def __str__(self):
return '{o.first_name} {o.last_name} ({o.submitted})'.format(o=self)
class EmergencyContact(models.Model):
"""Represents an emergency contact for a student."""
first_name = models.CharField(
'prénom', max_length=50,
help_text='Prénom du contact (50 caractères max).',
blank=True, default='')
last_name = models.CharField(
'nom', max_length=50,
help_text='Nom du contact (50 caractères max).',
blank=True, default='')
contact = models.CharField(
max_length=100,
help_text='Téléphone, adresse email…',
)
def __str__(self):
return '{o.first_name} {o.last_name}'.format(o=self)
class Meta: # noqa
verbose_name = "contact d'urgence"
verbose_name_plural = "contacts d'urgence"
ordering = ('last_name', 'first_name',)
"""Register serializers."""
from rest_framework import serializers
from .models import Registration
from core.serializers import AddressSerializer
from core.models import Address
from core.serializers import AddressSerializer
from .models import EmergencyContact, Registration
class EmergencyContactSerializer(serializers.ModelSerializer):
"""Serializer for emergency contacts."""
class Meta: # noqa
model = EmergencyContact
fields = ('first_name', 'last_name', 'contact')
class RegistrationSerializer(serializers.ModelSerializer):
......@@ -12,6 +22,9 @@ class RegistrationSerializer(serializers.ModelSerializer):
address = AddressSerializer(
required=False,
help_text="Adresse du lycéen")
emergency_contact = EmergencyContactSerializer(
required=False,
help_text="Contact en cas d'urgence")
class Meta: # noqa
model = Registration
......@@ -24,7 +37,8 @@ class RegistrationSerializer(serializers.ModelSerializer):
def create(self, validated_data):
"""Override for writable nested address field."""
address_data: dict = validated_data.pop('address', None)
address_data = validated_data.pop('address', None)
emergency_contact_data = validated_data.pop('emergency_contact', None)
registration = Registration.objects.create(**validated_data)
......@@ -33,4 +47,10 @@ class RegistrationSerializer(serializers.ModelSerializer):
registration.address = address
registration.save()
if emergency_contact_data:
emergency_contact = EmergencyContact.objects.create(
**emergency_contact_data)
registration.emergency_contact = emergency_contact
registration.save()
return registration
......@@ -38,7 +38,11 @@ class RegistrationViewSet(
"name": "France"
}
},
"emergency_contact": "urgences@oser-cs.fr",
"emergency_contact": {
"first_name": "John",
"last_name": "Doe",
"contact": "urgences@oser-cs.fr"
}
"submitted": "2018-04-07T23:07:05.943305+02:00"
}
]
......@@ -65,6 +69,18 @@ class RegistrationViewSet(
`country` : optional, default is `"FR"`. Must be given as a
[country code ](https://en.wikipedia.org/wiki/Country_code).
### Emergency contact
Emergency contact format is the following:
{
"first_name": "...",
"last_name": "...",
"contact": "..."
}
Contact is free-form: it can be a phone number, an email address...
"""
queryset = Registration.objects.all()
......
......@@ -3,7 +3,7 @@
from core.factory import DocumentFactory
from core.serializers import DocumentSerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
class DocumentEndpointsTest(
......
"""EmergencyContact model tests."""
from register.models import EmergencyContact
from register.factory import EmergencyContactFactory
from tests.utils import ModelTestCase
class EmergencyContactTest(ModelTestCase):
"""Test the EmergencyContact model."""
model = EmergencyContact
field_tests = {
'first_name': {
'max_length': 50,
'verbose_name': 'prénom',
'blank': True,
},
'last_name': {
'max_length': 50,
'verbose_name': 'nom',
'blank': True,
},
'contact': {
'max_length': 100,
},
}
model_tests = {
'ordering': ('last_name', 'first_name',),
'verbose_name': "contact d'urgence",
'verbose_name_plural': "contacts d'urgence",
}
@classmethod
def setUpTestData(cls):
cls.obj = EmergencyContactFactory.create()
def test_str(self):
expected = '{o.first_name} {o.last_name}'.format(o=self.obj)
self.assertEqual(expected, str(self.obj))
......@@ -4,7 +4,7 @@ from showcase_site.factory import ActionFactory
from showcase_site.serializers import ActionSerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
class ActionEndpointsTest(SimpleReadOnlyResourceTestMixin,
......
......@@ -7,7 +7,7 @@ from showcase_site.models import Article
from showcase_site.factory import ArticleFactory, CategoryFactory
from showcase_site.serializers import ArticleSerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
from tests.utils import SerializerTestCaseMixin
......
......@@ -4,7 +4,7 @@ from showcase_site.factory import CategoryFactory
from showcase_site.serializers import CategorySerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
class CategoryEndpointsTest(
......
......@@ -3,7 +3,7 @@
from showcase_site.factory import KeyFigureFactory
from showcase_site.serializers import KeyFigureSerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
class KeyFigureEndpointsTest(
......
......@@ -4,7 +4,7 @@ from showcase_site.models import Partner
from showcase_site.factory import PartnerFactory
from showcase_site.serializers import PartnerSerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
class PartnerEndpointsTest(
......
......@@ -3,7 +3,7 @@
from showcase_site.factory import TestimonyFactory
from showcase_site.serializers import TestimonySerializer
from tests.utils import SimpleAPITestCase
from .mixins import SimpleReadOnlyResourceTestMixin
from tests.utils import SimpleReadOnlyResourceTestMixin
class TestimonyEndpointsTest(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment