Commit 7e101126 authored by Florent Chehab's avatar Florent Chehab

Updated scholarship model

parent 2a664161
Pipeline #27359 passed with stages
in 2 minutes and 35 seconds
# Generated by Django 2.0.3 on 2018-09-16 09:54
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('backend', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='countryscholarship',
name='other_advantages',
field=models.TextField(blank=True, default=''),
),
migrations.AddField(
model_name='universityscholarship',
name='other_advantages',
field=models.TextField(blank=True, default=''),
),
migrations.AlterField(
model_name='countryscholarship',
name='amount_max',
field=models.DecimalField(decimal_places=2, max_digits=20, null=True, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AlterField(
model_name='countryscholarship',
name='amount_min',
field=models.DecimalField(decimal_places=2, max_digits=20, null=True, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AlterField(
model_name='countryscholarship',
name='currency',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='backend.Currency'),
),
migrations.AlterField(
model_name='universityscholarship',
name='amount_max',
field=models.DecimalField(decimal_places=2, max_digits=20, null=True, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AlterField(
model_name='universityscholarship',
name='amount_min',
field=models.DecimalField(decimal_places=2, max_digits=20, null=True, validators=[django.core.validators.MinValueValidator(0)]),
),
migrations.AlterField(
model_name='universityscholarship',
name='currency',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='backend.Currency'),
),
]
......@@ -16,7 +16,9 @@ SCHOLARSHIP_FREQUENCIES = (
class Scholarship(BasicModule):
type = models.CharField(max_length=200)
currency = models.ForeignKey(Currency, on_delete=models.PROTECT)
currency = models.ForeignKey(Currency, null=True, on_delete=models.PROTECT)
other_advantages = models.TextField(blank=True, default="")
frequency = models.CharField(
max_length=1,
choices=SCHOLARSHIP_FREQUENCIES,
......@@ -24,12 +26,14 @@ class Scholarship(BasicModule):
)
amount_min = models.DecimalField(
null=True,
max_digits=20,
decimal_places=2,
validators=[MinValueValidator(0)]
)
amount_max = models.DecimalField(
null=True,
max_digits=20,
decimal_places=2,
validators=[MinValueValidator(0)]
......@@ -44,9 +48,14 @@ class ScholarshipSerializer(BasicModuleSerializer):
def my_validate(self, attrs):
attrs = super(ScholarshipSerializer, self).my_validate(attrs)
if attrs['amount_max'] < attrs['amount_min']:
raise serializers.ValidationError(
"Amount_max should be greater or equal than amount_min")
if attrs['amount_min'] is not None:
if attrs['currency'] is None:
raise serializers.ValidationError(
"A currency must be specified when there is a value")
if attrs['amount_max'] is not None:
if attrs['amount_max'] < attrs['amount_min']:
raise serializers.ValidationError(
"amount_max should be greater or equal than amount_min")
return attrs
......
......@@ -2,16 +2,26 @@ from django.test import TestCase
from backend.models.abstract.scholarship import ScholarshipSerializer
import pytest
from rest_framework.validators import ValidationError
from backend.models.currency import Currency
class ScholarshipTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.EUR = Currency.objects.create(
code="EUR",
name="Euro",
one_EUR_in_this_currency=1
)
def test_scholarhip_validation(self):
ser = ScholarshipSerializer()
attrs = {
'useful_links': [],
'comment': ''
'comment': '',
'currency': self.EUR
}
with pytest.raises(ValidationError):
attrs['amount_min'] = 200
attrs['amount_max'] = 100
......
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