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
2133c82f
Commit
2133c82f
authored
Aug 30, 2018
by
Florent Chehab
Browse files
Moved some custom validation from django to rest framework
parent
1ff60054
Pipeline
#26776
failed with stages
in 2 minutes and 4 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/models/module/scholarship.py
View file @
2133c82f
...
@@ -21,23 +21,20 @@ class Scholarship(BasicModule):
...
@@ -21,23 +21,20 @@ class Scholarship(BasicModule):
validators
=
[
MinValueValidator
(
0
)]
validators
=
[
MinValueValidator
(
0
)]
)
)
# Need custom validation
def
clean
(
self
,
*
args
,
**
kwargs
):
if
self
.
amount_max
<
self
.
amount_min
:
raise
serializers
.
ValidationError
(
"Amount_max should be greater or equal than amount_min"
)
super
(
Scholarship
,
self
).
clean
(
*
args
,
**
kwargs
)
def
save
(
self
,
*
args
,
**
kwargs
):
self
.
full_clean
()
super
(
Scholarship
,
self
).
save
(
*
args
,
**
kwargs
)
class
Meta
:
class
Meta
:
abstract
=
True
abstract
=
True
class
ScholarshipSerializer
(
BasicModuleSerializer
):
class
ScholarshipSerializer
(
BasicModuleSerializer
):
def
validate
(
self
,
attrs
):
print
(
attrs
)
if
attrs
[
'amount_max'
]
<
attrs
[
'amount_min'
]:
raise
serializers
.
ValidationError
(
"Amount_max should be greater or equal than amount_min"
)
return
attrs
class
Meta
:
class
Meta
:
model
=
Scholarship
model
=
Scholarship
fields
=
'__all__'
fields
=
'__all__'
...
...
backend/models/university/universitySemestersDates.py
View file @
2133c82f
...
@@ -27,31 +27,25 @@ class UniversitySemestersDates(BasicModule):
...
@@ -27,31 +27,25 @@ class UniversitySemestersDates(BasicModule):
@
classmethod
@
classmethod
def
get_serializer
(
cls
):
def
get_serializer
(
cls
):
return
UniversitySemestersDatesSerializer
return
UniversitySemestersDatesSerializer
# Need custom validation
# TODO add tests
def
clean
(
self
,
*
args
,
**
kwargs
):
class
UniversitySemestersDatesSerializer
(
BasicModuleSerializer
):
spring_ok
=
check_nones
(
self
.
spring_begin
,
self
.
spring_end
)
autumn_ok
=
check_nones
(
self
.
autumn_begin
,
self
.
autumn_end
)
def
validate
(
self
,
attrs
):
spring_ok
=
check_nones
(
attrs
[
'spring_begin'
],
attrs
[
'spring_end'
])
autumn_ok
=
check_nones
(
attrs
[
'autumn_begin'
],
attrs
[
'autumn_end'
])
if
not
spring_ok
:
if
not
spring_ok
:
spring_ok
=
self
.
spring_begin
<
self
.
spring_end
spring_ok
=
attrs
[
'
spring_begin
'
]
<
attrs
[
'
spring_end
'
]
if
not
autumn_ok
:
if
not
autumn_ok
:
autumn_ok
=
self
.
autumn_begin
<
self
.
autumn_end
autumn_ok
=
attrs
[
'
autumn_begin
'
]
<
attrs
[
'
autumn_end
'
]
# no check if spring_end < autumn_begin in case info is not available
# no check if spring_end < autumn_begin in case info is not available
if
not
(
spring_ok
and
autumn_ok
):
if
not
(
spring_ok
and
autumn_ok
):
print
(
"ici"
)
raise
serializers
.
ValidationError
(
"Check your dates !"
)
# TODO better error
raise
serializers
.
ValidationError
(
"Check your dates !"
)
super
(
UniversitySemestersDates
,
self
).
clean
(
*
args
,
**
kwargs
)
def
save
(
self
,
*
args
,
**
kwargs
):
return
attrs
self
.
full_clean
()
super
(
UniversitySemestersDates
,
self
).
save
(
*
args
,
**
kwargs
)
class
UniversitySemestersDatesSerializer
(
BasicModuleSerializer
):
class
Meta
:
class
Meta
:
model
=
UniversitySemestersDates
model
=
UniversitySemestersDates
...
...
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