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 """ users = loads(dumps(user.all())) if not users: return loads('{"msg": "No users"}'), 404 print(users) return loads(dumps(user.all())), 200 @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')) return loads('{"msg": "'+status['msg']+'"}'), status['code'] @api.route('/users/') @api.doc(params={'username': 'Jean'}) class Remove(Resource): def delete(self, username): """ Remove a user """ status = user.remove(username) return loads('{"msg": "'+status['msg']+'"}'), status['code']