def logout(): key = request.headers.get('key') user = KeyStore.search(key) if user: KeyStore.delete(key) return jsonify(status="ok", message="See ya :D", data={}) return jsonify(status="fail", message="not authenticated", data={})
def auth(): if request.method == 'POST': # Login username = request.form["username"] password = GetHash( request.form["password"] ) user = auth_db.find_one({ "username":username, }) if not user or not validPasssword(password, user['password']): return jsonify(status="ok", message="Authenticate failed", data={}), 401 key = GenerateKey() KeyStore.save(key, user) return jsonify(status="ok", message="Welcome back, {0}.".format(username), data={"key":key, "user":user}) if request.method == 'PUT': # Register username = request.form["username"] password = GetHash( request.form["password"] ) fullname = request.form["fullname"] email = request.form["email"] user = auth_db.find_one({ "username":username, }) if user: return jsonify(status="fail", message="Username {0} already exist".format(username), data={}) result = auth_db.insert_one({"username":username, "password":password}) if not result: return jsonify(status="fail", data="DB Fail"), 500 return jsonify(status="ok", message="Welcome {0}".format(username), data={})
def storage(file_id=None): key = request.headers.get('x-key') user = KeyStore.search(key) if not user: abort(401) if request.method is 'POST': file = request.files['file'] room_id = request.form['room_id'] if file and allowed_file(file.filename): # secure filename filename = secure_filename(file.filename) # get id from document id ret = file_db.insert_one({"filename": filename, "room": ObjectID(room_id)}) file_id = str(ret['_id']) # save file file.save(os.path.join(Config['UPLOAD_FOLDER'], file_id+find_name)) return jsonify(status="ok", message="Yippee!", data={'file_id':fileid,}) return jsonify(status="fail", message="EXTENSION NOT AllOWED ") if request.method is 'GET': if file_id is not None: file = storage_db.find_one({'_id': ObjectID(file_id)}) if not file: return jsonify(status="fail", message="FILE NOT FOUND") return send_from_directory(Config['UPLOAD_FOLDER'], str(file['_id'])+file['filename']) abort(400)
def room(rid=None): key = request.headers.get('x-key') user = KeyStore.search(key) if not user: abort(401) # get room info if rid and request.method is 'GET': if ObjectId(oid) in user['rooms']: room = model.rooms.get_by_id(rid) users = model.users.get_by_room(room['users_id']) return jsonify( status='OK', message='', data={'room':room, 'users':users }) abort(400) # get all user's room elif not rid and request.method is 'GET': skip = request.form.get('skip',0) limit = request.form.get('limit',10) rooms = model.rooms.get(rid, skip=skip, limit=limit) return jsonify(status='OK', message='', data={'rooms':rooms}) # create new room elif request.method is 'POST': title = request.form['title'] user_id = user['_id'].to_s pin = request.form['pin'] ret = model.rooms.add(title=title, user_id=user_id, pin=pin) return jsonify(status='ok', message="created", data={'room_id':ret}) # add user to room elif rid and request.method is 'PUT': user_id = request.form['user_id'] ret = model.rooms.add_user(room_id=oid, user_id=user_id) if ret: return jsonify(status='ok', message='', data={}) else: abort(400) # user leave room elif rid and request.method is 'DELETE': user_id = request.form['user_id'] ret = model.rooms.remove_user(room_id=oid, user_id=user_id) if ret: return jsonify(status='ok', message='', data={}) else: abort(400) abort(405)
def user(uid=None): key = request.headers.get('x-key') user = KeyStore.search(key) if uid: if not user: abort(401) if uid=="me": uid = user["_id"] res = model.users.get(uid) return jsonify(status="ok", message="", data=res) if request.method == 'GET': if not user: abort(401) search = request.args.get("search", "") skip = request.args.get("skip", 0) limit = request.args.get("limit", 10) res = model.users.search(search, skip=skip, limit=limit) return jsonify(status="ok", message="", data=res) elif request.method == 'POST': token = request.form["token"] user = model.users.login(token) if user: key = GenerateKey() KeyStore.save(key, user) user["key"] = key return jsonify(status="ok", message="", data=user) else: model.users.add(token) return jsonify(status="ok", message="news", data={}) elif request.method == 'PUT': token = request.form["token"] name = request.form["name"] fullname = request.form["fullname"] image = request.form["image"] if model.users.update(token, name, fullname, image): return jsonify(status="ok", message="", data={}) else: abort(404) elif request.method == 'DELETE': key = request.headers.get('x-key') user = KeyStore.search(key) if not user: abort(401) KeyStore.delete(key) return jsonify(status="ok", message="", data={})
def message(id=None): key = request.headers.get('x-key') user = KeyStore.search(key) if not user: abort(401)