Esempio n. 1
0
    def POST(self, action):
        if action == "upload_photo":
            # this is to prevent IE from downloading the JSON.
            web.header("Content-Type", "text/plain")
        else:
            web.header("Content-Type", "application/json")
        set_no_cache()

        # check if we have the action
        if action not in self.POST_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])

        # act
        if action == "register":
            return jsond(db.register(d.uid, d.email, d.password))
        elif action == "login":
            u = db.checkLogin(d.uid, d.password)
            if u:
                session.uuid = str(u["_id"])
                return jsond({"uid": u["uid"]})
            else:
                return error.wrong_login()

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

        if action == "follow":
            return jsond(db.follow(uuid, d.uid))

        elif action == "unfollow":
            return jsond(db.unfollow(uuid, d.uid))

        elif action == "publish":
            req = spec.extract(self.EXTRACT_SPECS["publish_request"], d)
            return jsond(db.publish(uuid, **req))

        elif action == "remove":
            req = spec.extract(self.EXTRACT_SPECS["remove_request"], d)
            return jsond(db.remove(uuid, **req))

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

        elif action == "upload_photo":
            try:
                d = web.input(photo={})
                if "photo" in d:
                    u = db.get_user(uuid)
                    photo.resize_save(u["uid"], d.photo.file)
                    if db.update_photo(uuid, True).has_key("success"):
                        return jsond({"success": 1})
                return error.photo_upload_failed()
            except Exception, e:
                traceback.print_exc()
                return error.photo_upload_failed()
Esempio n. 2
0
    def POST(self, action):
        if action == 'upload_photo':
            # this is to prevent IE from downloading the JSON.
            web.header('Content-Type', 'text/plain')
        else:
            web.header('Content-Type', 'application/json')
        set_no_cache()

        # check if we have the action
        if action not in self.POST_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])

        # act
        if action == 'register':
            return jsond(db.register(d.uid, d.email, d.password))
        elif action == 'login':
            u = db.checkLogin(d.uid, d.password)
            if u:
                session.uuid = str(u['_id'])
                return jsond({'uid': u['uid']})
            else:
                return error.wrong_login()

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

        if action == 'follow':
            return jsond(db.follow(uuid, d.uid))

        elif action == 'unfollow':
            return jsond(db.unfollow(uuid, d.uid))

        elif action == 'publish':
            req = spec.extract(self.EXTRACT_SPECS['publish_request'], d)
            return jsond(db.publish(uuid, **req))

        elif action == 'remove':
            req = spec.extract(self.EXTRACT_SPECS['remove_request'], d)
            return jsond(db.remove(uuid, **req))

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

        elif action == 'upload_photo':
            try:
                d = web.input(photo={})
                if 'photo' in d:
                    u = db.get_user(uuid)
                    photo.resize_save(u['uid'], d.photo.file)
                    if db.update_photo(uuid, True).has_key('success'):
                        return jsond({'success': 1})
                return error.photo_upload_failed()
            except Exception, e:
                traceback.print_exc()
                return error.photo_upload_failed()
Esempio n. 3
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()
Esempio n. 4
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()