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
5f41066b
Commit
5f41066b
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Add API blueprint
parent
5c62905e
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
app/api/__init__.py
+0
-0
0 additions, 0 deletions
app/api/__init__.py
app/api/items.py
+54
-0
54 additions, 0 deletions
app/api/items.py
app/extensions.py
+2
-0
2 additions, 0 deletions
app/extensions.py
app/factory.py
+4
-1
4 additions, 1 deletion
app/factory.py
environment.yml
+2
-0
2 additions, 0 deletions
environment.yml
with
62 additions
and
1 deletion
app/api/__init__.py
0 → 100644
+
0
−
0
View file @
5f41066b
This diff is collapsed.
Click to expand it.
app/api/items.py
0 → 100644
+
54
−
0
View file @
5f41066b
# -*- coding: utf-8 -*-
"""
app.api.items
~~~~~~~~~~~~~
This module implements the items API.
:copyright: (c) 2017 European Spallation Source ERIC
:license: BSD 2-Clause, see LICENSE for more details.
"""
from
flask
import
(
current_app
,
Blueprint
,
jsonify
,
request
)
from
flask_jwt_extended
import
create_access_token
,
jwt_required
from
flask_ldap3_login
import
AuthenticationResponseStatus
from
..extensions
import
ldap_manager
,
db
from
..models
import
User
bp
=
Blueprint
(
'
api
'
,
__name__
)
@bp.route
(
'
/login
'
,
methods
=
[
'
POST
'
])
def
login
():
try
:
username
=
request
.
get_json
()[
'
username
'
]
password
=
request
.
get_json
()[
'
password
'
]
except
(
TypeError
,
KeyError
):
return
jsonify
({
'
msg
'
:
'
Bad Request
'
}),
400
response
=
ldap_manager
.
authenticate
(
username
,
password
)
if
response
.
status
==
AuthenticationResponseStatus
.
success
:
current_app
.
logger
.
debug
(
f
'
{
username
}
successfully logged in
'
)
user
=
User
.
query
.
filter_by
(
username
=
username
).
first
()
if
user
is
None
:
# LDAP user not in database yet - create it
user
=
User
(
username
,
name
=
response
.
user_info
[
'
cn
'
],
email
=
response
.
user_info
[
'
mail
'
])
db
.
session
.
add
(
user
)
db
.
session
.
commit
()
payload
=
{
'
access_token
'
:
create_access_token
(
identity
=
user
.
id
)}
return
jsonify
(
payload
),
200
return
jsonify
({
'
msg
'
:
'
Invalid credentials
'
}),
401
@bp.route
(
'
/items
'
)
@jwt_required
def
get_items
():
sql
=
"""
SELECT item.id, item.name, vendor.name, model.name, location.name, status.name
FROM item
INNER JOIN vendor ON vendor.id = item.vendor_id
INNER JOIN model ON model.id = item.model_id
INNER JOIN location ON location.id = item.location_id
INNER JOIN status ON status.id = item.status_id
"""
result
=
db
.
engine
.
execute
(
sql
)
data
=
[[
item
for
item
in
row
]
for
row
in
result
]
return
jsonify
(
data
=
data
)
This diff is collapsed.
Click to expand it.
app/extensions.py
+
2
−
0
View file @
5f41066b
...
...
@@ -17,6 +17,7 @@ from flask_migrate import Migrate
from
flask_bootstrap
import
Bootstrap
from
flask_admin
import
Admin
from
flask_mail
import
Mail
from
flask_jwt_extended
import
JWTManager
convention
=
{
...
...
@@ -36,3 +37,4 @@ ldap_manager = LDAP3LoginManager()
bootstrap
=
Bootstrap
()
admin
=
Admin
(
template_mode
=
'
bootstrap3
'
)
mail
=
Mail
()
jwt
=
JWTManager
()
This diff is collapsed.
Click to expand it.
app/factory.py
+
4
−
1
View file @
5f41066b
...
...
@@ -13,10 +13,11 @@ import sqlalchemy as sa
from
flask
import
Flask
from
flask_admin.contrib.sqla
import
ModelView
from
.
import
settings
from
.extensions
import
db
,
migrate
,
login_manager
,
ldap_manager
,
bootstrap
,
admin
,
mail
from
.extensions
import
db
,
migrate
,
login_manager
,
ldap_manager
,
bootstrap
,
admin
,
mail
,
jwt
from
.models
import
User
,
Role
,
Action
,
Vendor
,
Model
,
Location
,
Status
,
Item
from
.main.views
import
bp
as
main
from
.users.views
import
bp
as
users
from
.api.items
import
bp
as
api
from
.defaults
import
defaults
...
...
@@ -84,6 +85,7 @@ def create_app():
login_manager
.
login_view
=
'
users.login
'
ldap_manager
.
init_app
(
app
)
mail
.
init_app
(
app
)
jwt
.
init_app
(
app
)
admin
.
init_app
(
app
)
admin
.
add_view
(
ModelView
(
Role
,
db
.
session
))
...
...
@@ -97,6 +99,7 @@ def create_app():
app
.
register_blueprint
(
main
)
app
.
register_blueprint
(
users
)
app
.
register_blueprint
(
api
,
url_prefix
=
'
/api
'
)
register_cli
(
app
)
...
...
This diff is collapsed.
Click to expand it.
environment.yml
+
2
−
0
View file @
5f41066b
...
...
@@ -48,10 +48,12 @@ dependencies:
-
dominate==2.3.1
-
flask-admin==1.5.0
-
flask-bootstrap==3.3.7.1
-
flask-jwt-extended==3.1.1
-
flask-ldap3-login==0.9.13
-
flask-mail==0.9.1
-
flask-migrate==2.0.4
-
flask-script==2.0.5
-
ldap3==2.2.4
-
pyasn1==0.2.3
-
pyjwt==1.4.2
-
visitor==0.1.3
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