Skip to content

Commit 2b31b3d

Browse files
author
Ankur Srivastava
committed
some more cleanup
1 parent ae51cd0 commit 2b31b3d

File tree

8 files changed

+39
-52
lines changed

8 files changed

+39
-52
lines changed

backend/flask_app/app_utils/__init__.py

Whitespace-only changes.

backend/flask_app/app_utils/html_codes.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

backend/flask_app/app_utils/utilities.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

backend/flask_app/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# !/usr/bin/env python
2+
# -*- coding: utf-8 -*-
13
"""This module has configurations for flask app."""
24

35
import logging

backend/flask_app/factory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# !/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
"""Docstring for module."""
3+
"""Create app and create a default first user."""
44

55

66
import os
@@ -18,7 +18,6 @@ def create_app():
1818
app = Flask(__name__)
1919
config_name = os.getenv('FLASK_CONFIGURATION', 'default')
2020
app.config.from_object(CONFIG[config_name])
21-
# app.secret_key = app.config['SECRET_KEY']
2221
db.init_app(app)
2322
Security(app, user_datastore)
2423
CORS(app, headers=['Content-Type'])

backend/flask_app/http_codes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# !/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""Commonly used HTML status codes."""
4+
5+
6+
class Status(object):
7+
"""Bunch of commonly used http statuses.
8+
9+
More from:
10+
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
11+
"""
12+
13+
HTTP_OK_BASIC = 200
14+
HTTP_OK_CREATED = 201
15+
HTTP_OK_ACCEPTED = 202
16+
HTTP_OK_NORESPONSE = 204
17+
HTTP_BAD_REQUEST = 400
18+
HTTP_BAD_UNAUTHORIZED = 401
19+
HTTP_BAD_FORBIDDEN = 403
20+
HTTP_BAD_NOTFOUND = 404
21+
HTTP_BAD_CONFLICT = 409
22+
HTTP_SERVICE_UNAVAILABLE = 503
23+
HTTP_INTERNAL_TIMEOUT = 504

backend/flask_app/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# !/usr/bin/env python
2+
# -*- coding: utf-8 -*-
13
"""All the database related entities are in this module."""
24

35

backend/flask_app/server.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# !/usr/bin/env python
2+
# -*- coding: utf-8 -*-
13
"""Entry point for the server application."""
24

35
import json
@@ -10,7 +12,7 @@
1012
JWTManager, jwt_required, create_jwt, get_jwt_identity, get_jwt
1113
)
1214

13-
from .app_utils import html_codes
15+
from .http_codes import Status
1416
from .factory import create_app, create_user
1517

1618
logger = logging.getLogger(__name__)
@@ -49,9 +51,9 @@ def logout():
4951
# TODO: handle this logout properly, very weird implementation.
5052
identity = get_jwt_identity()
5153
if not identity:
52-
return jsonify({"msg": "Token invalid"}), 401
54+
return jsonify({"msg": "Token invalid"}), Status.HTTP_BAD_UNAUTHORIZED
5355
logger.info('Logged out user !!')
54-
return 'logged out successfully', 200
56+
return 'logged out successfully', Status.HTTP_OK_BASIC
5557

5658

5759
@app.route('/api/login', methods=['POST'])
@@ -64,13 +66,13 @@ def login():
6466
password = params.get('password', None)
6567

6668
if not username:
67-
return jsonify({"msg": "Missing username parameter"}), 400
69+
return jsonify({"msg": "Missing username parameter"}), Status.HTTP_BAD_REQUEST
6870
if not password:
69-
return jsonify({"msg": "Missing password parameter"}), 400
71+
return jsonify({"msg": "Missing password parameter"}), Status.HTTP_BAD_REQUEST
7072

7173
# TODO Check from DB here
7274
if username != 'admin' or password != 'admin':
73-
return jsonify({"msg": "Bad username or password"}), 401
75+
return jsonify({"msg": "Bad username or password"}), Status.HTTP_BAD_UNAUTHORIZED
7476

7577
# Identity can be any data that is json serializable
7678
ret = {'jwt': create_jwt(identity=username), 'exp': datetime.utcnow() + current_app.config['JWT_EXPIRES']}
@@ -83,16 +85,16 @@ def get_data():
8385
"""Get dummy data returned from the server."""
8486
jwt_data = get_jwt()
8587
if jwt_data['roles'] != 'admin':
86-
return jsonify(msg="Permission denied"), 403
88+
return jsonify(msg="Permission denied"), Status.HTTP_BAD_FORBIDDEN
8789

8890
identity = get_jwt_identity()
8991
if not identity:
90-
return jsonify({"msg": "Token invalid"}), 401
92+
return jsonify({"msg": "Token invalid"}), Status.HTTP_BAD_UNAUTHORIZED
9193

9294
data = {'Heroes': ['Hero1', 'Hero2', 'Hero3']}
9395
json_response = json.dumps(data)
9496
return Response(json_response,
95-
status=html_codes.HTTP_OK_BASIC,
97+
status=Status.HTTP_OK_BASIC,
9698
mimetype='application/json')
9799

98100

0 commit comments

Comments
 (0)