Exemplo n.º 1
0
    def GET(self, action):
        web.header("Content-Type", "application/json")
        set_no_cache()

        # check if we have the action
        if action not in self.GET_ACTIONS:
            return error.wrong_action()

        # get the input data if we have the spec
        if action in self.VALIDATE_SPECS:
            d = get_input(self.VALIDATE_SPECS[action])

        uuid = session.get("uuid", None)
        if not uuid:
            return error.not_logged_in()

        if action == "stream":
            param = spec.extract(self.EXTRACT_SPECS["stream_request"], d)
            if param["type"] == "user":
                if not param["uid"]:
                    raise web.badrequest()
            elif param["type"] == "list":
                if not param["list_id"]:
                    raise web.badrequest()
            ret = db.stream(uuid, **param)
            if "error" in ret:
                return jsond(ret)
            else:
                return jsond(spec.extract(self.EXTRACT_SPECS["stream_response"], ret))

        elif action == "current_user":
            u = db.get_user(uuid)
            return jsond(spec.extract(self.EXTRACT_SPECS["current_user"], u))

        elif action == "userinfo":
            u = db.find_user(d.uid)
            if not u:
                return error.user_not_found()
            u["isfollowing"] = bson.objectid.ObjectId(uuid) in u["follower"]
            return jsond(spec.extract(self.EXTRACT_SPECS["userinfo"], u))

        elif action == "get_following":
            param = spec.extract(self.EXTRACT_SPECS["userlist_request"], d)
            ret = db.get_following(uuid, **param)
            new_items = [spec.extract(self.EXTRACT_SPECS["userinfo"], u) for u in ret["items"]]
            ret["items"] = new_items
            return jsond(ret)

        elif action == "get_follower":
            param = spec.extract(self.EXTRACT_SPECS["userlist_request"], d)
            ret = db.get_follower(uuid, **param)
            new_items = [spec.extract(self.EXTRACT_SPECS["userinfo"], u) for u in ret["items"]]
            ret["items"] = new_items
            return jsond(ret)

        elif action == "get_message":
            ret = db.get_message(uuid, d.msg_id)
            if "error" in ret:
                return jsond(ret)
            else:
                return jsond(spec.extract(self.EXTRACT_SPECS["stream_response"], ret))

        elif action == "validate":
            act = d.action
            if act in self.VALIDATE_SPECS:
                errors = spec.validate(self.VALIDATE_SPECS[act], web.input())
                if errors:
                    return jsond(errors)
                else:
                    return jsond({"success": 1})
            else:
                return error.wrong_action()

        elif action == "recommend_user":
            return jsond({"users": [spec.extract(self.EXTRACT_SPECS["userinfo"], u) for u in db.recommend_user(uuid)]})

        elif action == "get_lists":
            ret = db.get_lists(uuid, d.get("uid"))
            if "error" in ret:
                return jsond(ret)
            else:
                return jsond({"items": [spec.extract(self.EXTRACT_SPECS["listinfo"], l) for l in ret]})

        elif action == "get_list_info":
            ret = db.get_list_info(uuid, d["id"])
            if "error" in ret:
                return jsond(ret)
            else:
                return jsond(spec.extract(self.EXTRACT_SPECS["listinfo"], ret))

        elif action == "get_list_users":
            param = spec.extract(self.EXTRACT_SPECS["list_userlist_request"], d)
            ret = db.get_list_users(uuid, param["id"], param["skip"])
            new_items = [spec.extract(self.EXTRACT_SPECS["userinfo"], u) for u in ret["items"]]
            ret["items"] = new_items
            return jsond(ret)

        elif action == "search":
            req = spec.extract(self.EXTRACT_SPECS["search_request"], d)
            ret = db.search(uuid, **req)
            if "error" in ret:
                return jsond(ret)
            else:
                return jsond(spec.extract(self.EXTRACT_SPECS["stream_response"], ret))

        return error.not_implemented()
Exemplo n.º 2
0
    def GET(self, action):
        web.header('Content-Type', 'application/json')
        set_no_cache()

        # check if we have the action
        if action not in self.GET_ACTIONS:
            return error.wrong_action()

        # get the input data if we have the spec
        if action in self.VALIDATE_SPECS:
            d = get_input(self.VALIDATE_SPECS[action])

        uuid = session.get('uuid', None)
        if not uuid:
            return error.not_logged_in()

        if action == 'stream':
            param = spec.extract(self.EXTRACT_SPECS['stream_request'], d)
            if param['type'] == 'user':
                if not param['uid']:
                    raise web.badrequest()
            elif param['type'] == 'list':
                if not param['list_id']:
                    raise web.badrequest()
            ret = db.stream(uuid, **param)
            if 'error' in ret:
                return jsond(ret)
            else:
                return jsond(
                    spec.extract(self.EXTRACT_SPECS['stream_response'], ret))

        elif action == 'current_user':
            u = db.get_user(uuid)
            return jsond(spec.extract(self.EXTRACT_SPECS['current_user'], u))

        elif action == 'userinfo':
            u = db.find_user(d.uid)
            if not u:
                return error.user_not_found()
            u['isfollowing'] = bson.objectid.ObjectId(uuid) in u['follower']
            return jsond(spec.extract(self.EXTRACT_SPECS['userinfo'], u))

        elif action == 'get_following':
            param = spec.extract(self.EXTRACT_SPECS['userlist_request'], d)
            ret = db.get_following(uuid, **param)
            new_items = [
                spec.extract(self.EXTRACT_SPECS['userinfo'], u)
                for u in ret['items']
            ]
            ret['items'] = new_items
            return jsond(ret)

        elif action == 'get_follower':
            param = spec.extract(self.EXTRACT_SPECS['userlist_request'], d)
            ret = db.get_follower(uuid, **param)
            new_items = [
                spec.extract(self.EXTRACT_SPECS['userinfo'], u)
                for u in ret['items']
            ]
            ret['items'] = new_items
            return jsond(ret)

        elif action == 'get_message':
            ret = db.get_message(uuid, d.msg_id)
            if 'error' in ret:
                return jsond(ret)
            else:
                return jsond(
                    spec.extract(self.EXTRACT_SPECS['stream_response'], ret))

        elif action == 'validate':
            act = d.action
            if act in self.VALIDATE_SPECS:
                errors = spec.validate(self.VALIDATE_SPECS[act], web.input())
                if errors:
                    return jsond(errors)
                else:
                    return jsond({'success': 1})
            else:
                return error.wrong_action()

        elif action == 'recommend_user':
            return jsond({
                'users': [
                    spec.extract(self.EXTRACT_SPECS['userinfo'], u)
                    for u in db.recommend_user(uuid)
                ]
            })

        elif action == 'get_lists':
            ret = db.get_lists(uuid, d.get('uid'))
            if 'error' in ret:
                return jsond(ret)
            else:
                return jsond({
                    'items': [
                        spec.extract(self.EXTRACT_SPECS['listinfo'], l)
                        for l in ret
                    ]
                })

        elif action == 'get_list_info':
            ret = db.get_list_info(uuid, d['id'])
            if 'error' in ret:
                return jsond(ret)
            else:
                return jsond(spec.extract(self.EXTRACT_SPECS['listinfo'], ret))

        elif action == 'get_list_users':
            param = spec.extract(self.EXTRACT_SPECS['list_userlist_request'],
                                 d)
            ret = db.get_list_users(uuid, param['id'], param['skip'])
            new_items = [
                spec.extract(self.EXTRACT_SPECS['userinfo'], u)
                for u in ret['items']
            ]
            ret['items'] = new_items
            return jsond(ret)

        elif action == 'search':
            req = spec.extract(self.EXTRACT_SPECS['search_request'], d)
            ret = db.search(uuid, **req)
            if 'error' in ret:
                return jsond(ret)
            else:
                return jsond(
                    spec.extract(self.EXTRACT_SPECS['stream_response'], ret))

        return error.not_implemented()
Exemplo n.º 3
0
                return error.photo_upload_failed()
            except Exception, e:
                traceback.print_exc()
                return error.photo_upload_failed()

        elif action == "logout":
            session.kill()
            return jsond({"success": 1})

        elif action == "create_list":
            return jsond(db.create_list(uuid, d.name))

        elif action == "remove_list":
            return jsond(db.remove_list(uuid, d["id"]))

        elif action == "add_to_list":
            return jsond(db.add_to_list(uuid, d["id"], d.uid))

        elif action == "remove_from_list":
            return jsond(db.remove_from_list(uuid, d["id"], d.uid))

        return error.not_implemented()


# export wsgi application
application = app.wsgifunc()

# if not run as wsgi, start web.py built-in server
if __name__ == "__main__":
    app.run()
Exemplo n.º 4
0
                return error.photo_upload_failed()
            except Exception, e:
                traceback.print_exc()
                return error.photo_upload_failed()

        elif action == 'logout':
            session.kill()
            return jsond({'success': 1})

        elif action == 'create_list':
            return jsond(db.create_list(uuid, d.name))

        elif action == 'remove_list':
            return jsond(db.remove_list(uuid, d['id']))

        elif action == 'add_to_list':
            return jsond(db.add_to_list(uuid, d['id'], d.uid))

        elif action == 'remove_from_list':
            return jsond(db.remove_from_list(uuid, d['id'], d.uid))

        return error.not_implemented()


# export wsgi application
application = app.wsgifunc()

# if not run as wsgi, start web.py built-in server
if __name__ == '__main__':
    app.run()