Skip to content

Commit 7c1ce32

Browse files
committed
Pushed basic skeleton, will test it further and update it.
1 parent c643117 commit 7c1ce32

36 files changed

+749
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
# sqlite files
104+
*.sqlite

app/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from flask import Flask
2+
from flask_marshmallow import Marshmallow
3+
from flask_sqlalchemy import SQLAlchemy
4+
5+
from config import config
6+
7+
db = SQLAlchemy()
8+
ma = Marshmallow()
9+
10+
11+
def create_app(config_name='default'):
12+
app = Flask(__name__, static_folder='./static')
13+
app.config.from_object(config[config_name])
14+
config[config_name].init_app(app)
15+
16+
# Plugins initialization goes here
17+
db.init_app(app)
18+
ma.init_app(app)
19+
20+
from .views import views as views_blueprint
21+
app.register_blueprint(views_blueprint)
22+
23+
from .api.v1 import api_v1 as api_v1_blueprint
24+
app.register_blueprint(api_v1_blueprint, url_prefix='/api/v1')
25+
26+
return app

app/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import v1

app/api/v1/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from flask import Blueprint, g
2+
from flask_httpauth import HTTPTokenAuth
3+
4+
from app.models.user import User
5+
6+
api_v1 = Blueprint('v1', __name__)
7+
auth_v1 = HTTPTokenAuth(scheme='Token')
8+
9+
10+
@auth_v1.verify_token
11+
def verify_token(token):
12+
user = User.verify_auth_token(token)
13+
if user:
14+
g.user = user
15+
g.token = token
16+
return True
17+
return False
18+
19+
20+
from . import auth, father, son, errors

app/api/v1/auth/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import views

app/api/v1/auth/schemas.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from marshmallow import fields
2+
3+
from app import ma
4+
5+
6+
class LoginUserSchema(ma.Schema):
7+
email = fields.Email(required=True)
8+
password = fields.Str(required=True)

app/api/v1/auth/views.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import request, abort
2+
from app.api.v1.exceptions import ValidationError
3+
from app.api.v1.utils import json
4+
from app.models.user import User
5+
from .schemas import LoginUserSchema
6+
from .. import api_v1
7+
8+
9+
10+
11+
@api_v1.route('/auth/login', methods=['POST'])
12+
@json
13+
def auth_login():
14+
json_data = request.get_json()
15+
if not json_data:
16+
abort(400)
17+
schema = LoginUserSchema()
18+
result = schema.load(json_data)
19+
if result.errors:
20+
raise ValidationError(result.errors)
21+
user = User.query.filter_by(email=result.data['email']).first()
22+
if user is not None and user.verify_password(result.data['password']):
23+
return {'token': user.generate_auth_token().decode('ascii')}
24+
else:
25+
abort(401)

app/api/v1/errors.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from app.api.v1 import api_v1, auth_v1
2+
from app.api.v1.exceptions import ValidationError
3+
from app.api.v1.utils import json
4+
5+
6+
@api_v1.errorhandler(400)
7+
@json
8+
def bad_request(e):
9+
return {'error': 'Bad request, did you include all the data correctly?'}, 400
10+
11+
12+
@api_v1.errorhandler(401)
13+
@json
14+
def unauthorized(e):
15+
return {'error': 'Unauthorized, did not provide proper auth credentials'}, 401
16+
17+
18+
@api_v1.errorhandler(404)
19+
@json
20+
def not_found(e):
21+
return {'error': 'Not found'}, 404
22+
23+
24+
@api_v1.errorhandler(405)
25+
@json
26+
def not_allowed(e):
27+
return {'error': 'Method not allowed'}, 405
28+
29+
30+
@api_v1.errorhandler(500)
31+
@json
32+
def internal_server_error(e):
33+
return {'error': 'Internal server error'}, 500
34+
35+
36+
@api_v1.errorhandler(ValidationError)
37+
@json
38+
def validation_error(e):
39+
return {'error': 'Bad request, did you include all the data correctly?', 'info': e.info}, 400
40+
41+
42+
@auth_v1.error_handler
43+
@json
44+
def auth_error():
45+
return {'error': 'Unauthorized, did not provide proper auth credentials'}, 401

app/api/v1/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ValidationError(Exception):
2+
def __init__(self, info):
3+
self.info = info

app/api/v1/father/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import views

0 commit comments

Comments
 (0)