<reponame>Macsnow14/Thrive-voice-server<filename>src/therver.py
# -*- coding: utf-8 -*-
# @Author: Macsnow
# @Date:   2017-05-02 23:48:34
# @Last Modified by:   Macsnow
# @Last Modified time: 2017-05-03 00:59:50
from flask import Flask, session, request
from flask_script import Manager
from flask.ext.restful import Api, abort, reqparse, Resource


app = Flask(__name__)
app.secret_key = b'\<KEY>'
manager = Manager(app)
api = Api(app)
userOnline = {}
parser = reqparse.RequestParser()
parser.add_argument('username', type=str)


def abortIfUserUnauthenticated(request):
    if 'username' not in session:
        abort(404, message="user not online.")


def abortIfUserNotOnline(username):
    if username not in userOnline:
        abort(403, message="Unauthenticated, you need to login first.")


def abortIfUsernameDuplicate(username):
    if username in userOnline:
        abort(400, message="Duplicate username, pick another one.")


class UserOnline(Resource):
    """to check who's one the line.

    treat online users as a resource and use restful design pattern.

    Extends:
        Resource

    Methods:
        get -- get user list.
        post -- user log in.
        delete -- user log out.
    """

    def get(self):
        abortIfUserUnauthenticated(request)
        return userOnline.keys(), 200

    def post(self):
        username = parser.parse_args()['username']
        abortIfUsernameDuplicate(username)
        user = {'host': request.remote_addr, 'port': request.environ.get('REMOTE_PORT')}
        userOnline[username] = user
        session['username'] = username
        return userOnline[username], 201

    def delete(self):
        abortIfUserUnauthenticated(request)
        username = session['username']
        session.pop('username')
        userOnline.pop(username)
        return '', 204


api.add_resource(UserOnline, '/', '/userOnline')


if __name__ == '__main__':
    manager.run()
