class UserResource(Resource): @parse_params( Param('username', required=True), Param('pwd', required=True), Param('nickname', required=True), ) def post(self): user = create_user(username=request.params['username'], pwd_md5=request.params['pwd'], nickname=request.params['nickname']) if not user: return render_response(StatusDef.USER_EXISTS) token = login_user(user) return render_response(data=user.to_json(), cookies={'token': token})
class FriendsResource(Resource): @logined @parse_params(Param('other_uid', type=int, required=True)) def post(self): ret = follow_user(request.user_id, request.params['other_uid']) if not ret: return render_response(StatusDef.USER_LIKED) else: return render_response() @logined @parse_params(Param('other_uid', type=int, required=True)) def delete(self): ret = unfollow_user(request.user_id, request.params['other_uid']) if not ret: return render_response(StatusDef.USER_UNLIKED) else: return render_response()
class StatusesFilterResource(Resource): @parse_params( Param( 'type', default='sample', choices=('sample', 'filter', 'track', 'location') ), Param('sample', type=int, discard=True), Param('filter', discard=True), Param('track', discard=True), Param('location', discard=True) ) def get(self): quit = [False] filter_type = request.params['type'] arg = request.params.get(filter_type) data = [] filter_gen = filter_content(request.user_id, filter_type, arg, quit) for item in filter_gen: data.append(item) return render_response(data=data)
class SingleAdResource(Resource): def get(self, ad_id): ad = get_ad(ad_id) data = ad.to_json() return render_response(data=data) @parse_params(Param('action', choices=('click', 'action'), default='click')) def post(self, ad_id): data = record_click(**request.params) return render_response(data=data) def delete(self, ad_id): remove_ad(ad_id) return render_response()
class SessionResource(Resource): @logined def get(self): user = get_user(request.user_id) return render_response(data=user.to_json()) @parse_params(Param('username', required=True), Param('pwd', required=True)) def post(self): user = validate_user(username=request.params['username'], pwd_md5=request.params['pwd']) if not user: return render_response(StatusDef.USER_NOT_FOUND) token = login_user(user) return render_response(data=user.to_json(), cookies={'token': token}) @logined def delete(self): token = request.get_token() logout_user(token) return render_response()
class StatusesResource(Resource): @logined @parse_params( Param('timeline', choices=('home', 'profile'), default='home'), Param('page', type=int, default=1), Param('count', type=int, default=10) ) def get(self): statuses = get_status_contents( request.user_id, **request.params ) return render_response(data=statuses) @logined @parse_params( Param('content', required=True) ) def post(self): ret = post_status(request.user_id, request.params['content']) if not ret: return render_response(StatusDef.STATUS_CREATE_FAIL) else: return render_response()
class AdResource(Resource): @parse_params(Param('content', required=True)) def get(): locations = [] loc = ip_to_location('59.42.120.121' or request.ip) if loc: locations.append(loc) target_id, ad_id = target_ads(locations, request.params['content']) ad = get_ad(ad_id) data = ad.to_json() return render_response(data={'target_id': target_id, 'data': data}) @parse_params( Param('name', required=True), Param('type', choices=('cpa', 'cpc', 'cpm'), default='cpm'), Param('value', type=float, required=True), Param('content', required=True), Param('locations', type=list, required=True), ) def post(self): data = add_ad(**request.params) return render_response(data=data)