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
a47c2c0b
Commit
a47c2c0b
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Allow to retrieve QRCodes via API
Pass ?qrcode=true as query string
parent
75e1752a
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
+13
-6
13 additions, 6 deletions
app/api/main.py
app/models.py
+9
-4
9 additions, 4 deletions
app/models.py
tests/functional/test_api.py
+8
-0
8 additions, 0 deletions
tests/functional/test_api.py
with
30 additions
and
10 deletions
app/api/main.py
+
13
−
6
View file @
a47c2c0b
...
@@ -35,9 +35,16 @@ def get_item_by_id_or_ics_id(id_):
...
@@ -35,9 +35,16 @@ def get_item_by_id_or_ics_id(id_):
return
item
return
item
def
get_generic_model
(
model
):
def
get_generic_model
(
model
,
args
):
"""
Return data from model as json
:param model: model class
:param MultiDict args: args from the request
:returns: data from model as json
"""
items
=
model
.
query
.
order_by
(
model
.
name
)
items
=
model
.
query
.
order_by
(
model
.
name
)
data
=
[
item
.
to_dict
()
for
item
in
items
]
qrcode
=
args
.
get
(
'
qrcode
'
,
'
false
'
).
lower
()
==
'
true
'
data
=
[
item
.
to_dict
(
qrcode
=
qrcode
)
for
item
in
items
]
return
jsonify
(
data
)
return
jsonify
(
data
)
...
@@ -170,7 +177,7 @@ def patch_item(id_):
...
@@ -170,7 +177,7 @@ def patch_item(id_):
@bp.route
(
'
/manufacturers
'
)
@bp.route
(
'
/manufacturers
'
)
@jwt_required
@jwt_required
def
get_manufacturers
():
def
get_manufacturers
():
return
get_generic_model
(
Manufacturer
)
return
get_generic_model
(
Manufacturer
,
request
.
args
)
@bp.route
(
'
/manufacturers
'
,
methods
=
[
'
POST
'
])
@bp.route
(
'
/manufacturers
'
,
methods
=
[
'
POST
'
])
...
@@ -183,7 +190,7 @@ def create_manufacturer():
...
@@ -183,7 +190,7 @@ def create_manufacturer():
@bp.route
(
'
/models
'
)
@bp.route
(
'
/models
'
)
@jwt_required
@jwt_required
def
get_models
():
def
get_models
():
return
get_generic_model
(
Model
)
return
get_generic_model
(
Model
,
request
.
args
)
@bp.route
(
'
/models
'
,
methods
=
[
'
POST
'
])
@bp.route
(
'
/models
'
,
methods
=
[
'
POST
'
])
...
@@ -196,7 +203,7 @@ def create_model():
...
@@ -196,7 +203,7 @@ def create_model():
@bp.route
(
'
/locations
'
)
@bp.route
(
'
/locations
'
)
@jwt_required
@jwt_required
def
get_locations
():
def
get_locations
():
return
get_generic_model
(
Location
)
return
get_generic_model
(
Location
,
request
.
args
)
@bp.route
(
'
/locations
'
,
methods
=
[
'
POST
'
])
@bp.route
(
'
/locations
'
,
methods
=
[
'
POST
'
])
...
@@ -209,7 +216,7 @@ def create_locations():
...
@@ -209,7 +216,7 @@ def create_locations():
@bp.route
(
'
/status
'
)
@bp.route
(
'
/status
'
)
@jwt_required
@jwt_required
def
get_status
():
def
get_status
():
return
get_generic_model
(
Status
)
return
get_generic_model
(
Status
,
request
.
args
)
@bp.route
(
'
/status
'
,
methods
=
[
'
POST
'
])
@bp.route
(
'
/status
'
,
methods
=
[
'
POST
'
])
...
...
This diff is collapsed.
Click to expand it.
app/models.py
+
9
−
4
View file @
a47c2c0b
...
@@ -158,8 +158,11 @@ class QRCodeMixin:
...
@@ -158,8 +158,11 @@ class QRCodeMixin:
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
name
return
self
.
name
def
to_dict
(
self
):
def
to_dict
(
self
,
qrcode
=
False
):
return
{
'
id
'
:
self
.
id
,
'
name
'
:
self
.
name
}
d
=
{
'
id
'
:
self
.
id
,
'
name
'
:
self
.
name
}
if
qrcode
:
d
[
'
qrcode
'
]
=
utils
.
image_to_base64
(
self
.
image
())
return
d
class
Action
(
QRCodeMixin
,
db
.
Model
):
class
Action
(
QRCodeMixin
,
db
.
Model
):
...
@@ -174,8 +177,10 @@ class Model(QRCodeMixin, db.Model):
...
@@ -174,8 +177,10 @@ class Model(QRCodeMixin, db.Model):
description
=
db
.
Column
(
db
.
Text
)
description
=
db
.
Column
(
db
.
Text
)
items
=
db
.
relationship
(
'
Item
'
,
back_populates
=
'
model
'
)
items
=
db
.
relationship
(
'
Item
'
,
back_populates
=
'
model
'
)
def
to_dict
(
self
):
def
to_dict
(
self
,
qrcode
=
False
):
return
{
'
id
'
:
self
.
id
,
'
name
'
:
self
.
name
,
'
description
'
:
self
.
description
}
d
=
super
().
to_dict
(
qrcode
)
d
[
'
description
'
]
=
self
.
description
return
d
class
Location
(
QRCodeMixin
,
db
.
Model
):
class
Location
(
QRCodeMixin
,
db
.
Model
):
...
...
This diff is collapsed.
Click to expand it.
tests/functional/test_api.py
+
8
−
0
View file @
a47c2c0b
...
@@ -136,6 +136,14 @@ def test_get_generic_model(endpoint, session, client, readonly_token):
...
@@ -136,6 +136,14 @@ def test_get_generic_model(endpoint, session, client, readonly_token):
check_response_message
(
response
,
'
Not enough segments
'
,
422
)
check_response_message
(
response
,
'
Not enough segments
'
,
422
)
response
=
get
(
client
,
f
'
/api/
{
endpoint
}
'
,
readonly_token
)
response
=
get
(
client
,
f
'
/api/
{
endpoint
}
'
,
readonly_token
)
check_names
(
response
,
names
)
check_names
(
response
,
names
)
response
=
get
(
client
,
f
'
/api/
{
endpoint
}
?qrcode=true
'
,
readonly_token
)
check_names
(
response
,
names
)
for
item
in
response
.
json
:
assert
'
qrcode
'
in
item
response
=
get
(
client
,
f
'
/api/
{
endpoint
}
?qrcode=false
'
,
readonly_token
)
check_names
(
response
,
names
)
for
item
in
response
.
json
:
assert
'
qrcode
'
not
in
item
@pytest.mark.parametrize
(
'
endpoint
'
,
ENDPOINTS
)
@pytest.mark.parametrize
(
'
endpoint
'
,
ENDPOINTS
)
...
...
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