Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csentry
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Harrisson
csentry
Commits
a15143e6
Commit
a15143e6
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Add basic filtering to retrieve items via the API
Can use: - /api/items?serial_number=123456 - /api/items?location_id=1
parent
84175421
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app/api/main.py
+2
-1
2 additions, 1 deletion
app/api/main.py
app/utils.py
+17
-0
17 additions, 0 deletions
app/utils.py
tests/functional/test_api.py
+37
-0
37 additions, 0 deletions
tests/functional/test_api.py
with
56 additions
and
1 deletion
app/api/main.py
+
2
−
1
View file @
a15143e6
...
...
@@ -89,7 +89,8 @@ def login():
@jwt_required
def
get_items
():
# TODO: add pagination
items
=
Item
.
query
.
order_by
(
Item
.
_created
)
query
=
utils
.
get_query
(
Item
.
query
,
request
.
args
)
items
=
query
.
order_by
(
Item
.
_created
)
data
=
[
item
.
to_dict
()
for
item
in
items
]
return
jsonify
(
data
)
...
...
This diff is collapsed.
Click to expand it.
app/utils.py
+
17
−
0
View file @
a15143e6
...
...
@@ -12,6 +12,7 @@ This module implements utility functions.
import
base64
import
datetime
import
io
import
sqlalchemy
as
sa
class
InventoryError
(
Exception
):
...
...
@@ -103,3 +104,19 @@ def get_choices(iterable, allow_blank=False, allow_null=False):
choices
.
append
((
'
null
'
,
'
not set
'
))
choices
.
extend
([(
val
,
val
)
for
val
in
iterable
])
return
choices
def
get_query
(
query
,
args
):
"""
Retrieve the query from the arguments
:param query: sqlalchemy base query
:param MultiDict args: args from a request
:returns: query filtered by the arguments
"""
if
args
:
kwargs
=
args
.
to_dict
()
try
:
query
=
query
.
filter_by
(
**
kwargs
)
except
(
sa
.
exc
.
InvalidRequestError
,
AttributeError
)
as
e
:
raise
InventoryError
(
'
Invalid query arguments
'
,
status_code
=
422
)
return
query
This diff is collapsed.
Click to expand it.
tests/functional/test_api.py
+
37
−
0
View file @
a15143e6
...
...
@@ -357,3 +357,40 @@ def test_patch_item_parent(client, session, user_token):
assert
response
.
json
[
'
manufacturer
'
]
is
None
response
=
get
(
client
,
f
'
/api/items/
{
item3
.
ics_id
}
'
,
token
=
user_token
)
assert
response
.
json
[
'
manufacturer
'
]
==
'
Dell
'
def
test_get_items
(
client
,
session
,
readonly_token
):
# Create some items
session
.
add
(
models
.
Manufacturer
(
name
=
'
Dell
'
))
session
.
add
(
models
.
Status
(
name
=
'
Stock
'
))
session
.
add
(
models
.
Status
(
name
=
'
In service
'
))
session
.
add
(
models
.
Location
(
name
=
'
ESS
'
))
session
.
add
(
models
.
Location
(
name
=
'
ICS lab
'
))
item1
=
models
.
Item
(
serial_number
=
'
123456
'
,
ics_id
=
'
AAA001
'
,
location
=
'
ESS
'
,
status
=
'
In service
'
,
manufacturer
=
'
Dell
'
)
item2
=
models
.
Item
(
serial_number
=
'
234567
'
,
ics_id
=
'
AAA002
'
,
status
=
'
Stock
'
)
item3
=
models
.
Item
(
serial_number
=
'
345678
'
,
ics_id
=
'
AAA003
'
,
status
=
'
Stock
'
,
manufacturer
=
'
Dell
'
)
for
item
in
(
item1
,
item2
,
item3
):
session
.
add
(
item
)
session
.
commit
()
response
=
get
(
client
,
'
/api/items
'
,
token
=
readonly_token
)
assert
response
.
status_code
==
200
assert
len
(
response
.
json
)
==
3
check_items
(
response
,
(
item1
.
to_dict
(),
item2
.
to_dict
(),
item3
.
to_dict
()))
# test filtering
response
=
get
(
client
,
'
/api/items?serial_number=234567
'
,
token
=
readonly_token
)
assert
response
.
status_code
==
200
assert
len
(
response
.
json
)
==
1
check_items
(
response
,
(
item2
.
to_dict
(),))
# filtering on location_id works but not location (might want to change that)
response
=
get
(
client
,
f
'
/api/items?location_id=
{
item1
.
location_id
}
'
,
token
=
readonly_token
)
assert
response
.
status_code
==
200
assert
len
(
response
.
json
)
==
1
check_items
(
response
,
(
item1
.
to_dict
(),))
response
=
get
(
client
,
'
/api/items?location=ESS
'
,
token
=
readonly_token
)
check_response_message
(
response
,
'
Invalid query arguments
'
,
422
)
# using an unknown key raises a 422
response
=
get
(
client
,
'
/api/items?foo=bar
'
,
token
=
readonly_token
)
check_response_message
(
response
,
'
Invalid query arguments
'
,
422
)
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