コード例 #1
0
ファイル: server.py プロジェクト: thinxer/tau
def get_input(s):
    """
    Helper method to validate input data accroding to the spec.
    """
    errors = spec.validate(s, web.input())
    if errors:
        raise web.badrequest()
    return web.input()
コード例 #2
0
ファイル: server.py プロジェクト: thinxer/tau
def get_input(s):
    '''
    Helper method to validate input data accroding to the spec.
    '''
    errors = spec.validate(s, web.input())
    if errors:
        raise web.badrequest()
    return web.input()
コード例 #3
0
ファイル: server.py プロジェクト: thinxer/tau
    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()
コード例 #4
0
ファイル: models.py プロジェクト: yourapi/biz.data.processing
 def validate(self):
     """Validate this object based on its spec document."""
     return validate(self, getattr(self, 'spec', None))
コード例 #5
0
ファイル: server.py プロジェクト: thinxer/tau
    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()