aboutsummaryrefslogtreecommitdiff
path: root/src/lb_app/api.py
blob: 6dfe2e8dea2f76c37267c52866992fa30400b808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import (Flask, Blueprint, g, redirect, request, session, jsonify, url_for)
from flask_restplus import Resource, Api, fields

from bson.json_util import dumps
from json import loads

from .app_db.user import User

app = Flask(__name__, instance_relative_config=True)

bp = Blueprint('api', __name__, url_prefix='/api')

env = app.config['DEBUG']

if env is not False:
    env = '/doc'

api = Api(bp, doc=env)

user = User(app)

user_model = api.model('User', {
  'username': fields.String(required=True, description='Username of account', example='Jean')
})

@api.route('/users', endpoint='users')
class Users(Resource):
    def get(self):
        """ Get user list """
        return loads(dumps(user.all()))


@api.route('/user/register')
class Register(Resource):
    @api.expect(user_model)
    def post(self):
        """ Register new user """
        args = request.get_json(force=True)
        status = user.register(args.get('username'))

        if status is None:
            return loads('{"msg": "done"}'), 201
        return loads('{"msg": "'+status+'"}'), 200