Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
leximpact-server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
leximpact
Simulateur dotations aux communes
leximpact-server
Commits
3beedccd
Commit
3beedccd
authored
Jun 27, 2020
by
Augustin Wenger
Committed by
sandcha
Jul 6, 2020
Browse files
Options
Downloads
Patches
Plain Diff
Adds a function to check fields within the request.
parent
e8acc292
No related branches found
No related tags found
1 merge request
!19
Ajoute un endpoint /dotations
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/handlers/dotations.py
+27
-2
27 additions, 2 deletions
server/handlers/dotations.py
tests/server/handlers/test_dotations.py
+6
-1
6 additions, 1 deletion
tests/server/handlers/test_dotations.py
with
33 additions
and
3 deletions
server/handlers/dotations.py
+
27
−
2
View file @
3beedccd
from
Simulation_engine.simulate_dotations
import
simulate
# Checks whether all dictionnaries in the model exist in the target dict.
# Returns True or False, and the problematic field
def
check_keys_dict
(
dict_to_check
,
model
):
for
k
,
v
in
model
.
items
():
if
k
not
in
dict_to_check
:
# key does not exist
return
False
,
k
if
isinstance
(
v
,
dict
):
if
not
isinstance
(
dict_to_check
[
k
],
dict
):
# should be a dict but is not
return
False
,
k
res_child
,
node_err
=
check_keys_dict
(
dict_to_check
[
k
],
v
)
if
not
res_child
:
return
False
,
"
.
"
.
join
([
k
,
node_err
])
return
True
,
"
All required keys are in the checked dictionary
"
def
check_request_body
(
request_body
):
required_dict
=
{
"
reforme
"
:
{
"
dotations
"
:
{
"
montants
"
:
{
"
dgf
"
:
None
},
"
communes
"
:
None
}}}
result
,
errorfield
=
check_keys_dict
(
request_body
,
required_dict
)
if
not
result
:
return
{
"
Error
"
:
"
Missing required
'
{}
'
field in request body.
"
.
format
(
errorfield
)},
400
class
Dotations
(
object
):
...
...
@@ -7,8 +31,9 @@ class Dotations(object):
request_body
=
params
[
"
body
"
]
# vérifier le format
if
"
dotations
"
not
in
request_body
:
return
{
"
Error
"
:
"
Missing
'
dotations
'
field in request body.
"
},
400
check_result
=
check_request_body
(
request_body
)
if
check_result
is
not
None
:
return
check_result
# calculer
simulation_result
=
simulate
(
request_body
)
...
...
This diff is collapsed.
Click to expand it.
tests/server/handlers/test_dotations.py
+
6
−
1
View file @
3beedccd
...
...
@@ -14,7 +14,12 @@ def test_dotations_request_body_error(client, headers):
def
test_dotations
(
client
,
headers
):
request
=
{
"
dotations
"
:
{}
"
reforme
"
:
{
"
dotations
"
:
{
"
montants
"
:
{
"
dgf
"
:
16
},
"
communes
"
:
{}
}
}
}
response_function
=
partial
(
client
.
post
,
"
dotations
"
,
headers
=
headers
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment