Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Rex Dri
Rex Dri
Commits
7e101126
Commit
7e101126
authored
Sep 16, 2018
by
Florent Chehab
Browse files
Updated scholarship model
parent
2a664161
Pipeline
#27359
passed with stages
in 2 minutes and 35 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/migrations/0002_auto_20180916_1154.py
0 → 100644
View file @
7e101126
# 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'
),
),
]
backend/models/abstract/scholarship/scholarship.py
View file @
7e101126
...
...
@@ -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
...
...
backend/tests/test_scholarhip_validate.py
View file @
7e101126
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment