def _api_response_content(self, response): if isinstance(response, dict): return response try: return util.loads(response.content[response.content.index("(") + 1 : -2]) except ValueError: return util.loads(response.content)
def _api_response_content(self, response): if isinstance(response, dict): return response try: return util.loads(response.content[response.content.index('(') + 1:-2]) except ValueError: return util.loads(response.content)
def api_upload(request): content_type = request.META['CONTENT_TYPE'] url = '' filedata = None if 'file' in request.FILES: filedata = "".join(request.FILES['file'].chunks()) elif content_type.startswith('application/json'): json = util.loads(request.raw_post_data) url = json.get('url', '').strip() filedata = _fetch_url(url) elif content_type.startswith('application/base64-png'): token = 'data:image/png;base64,' header, data = request.raw_post_data.split(',', 2) if not header.startswith('data:') or not 'image/png' in header or not 'base64' in header: return {'success': False, 'code': 'unknown', 'reason': 'Missing data url header.'} else: filedata = b64decode(data) if filedata: return _got_imagedata(filedata, request, url=url) else: raise ServiceError("No file or url.")
def test_properties_dont_get_serialized(self): # CommentDetails should only serialize its dict contents, not any of its member properties. cmt = create_comment().details() cmt.test_foo_property = 1 d = util.loads(util.dumps(cmt)) self.assertFalse('test_foo_property' in d)
def update(self, fs=None, web=None): """ Renders the footer image and saves it to the filesystem (i.e. S3 in production). `web` is an optional perf optimization. """ from canvas.thumbnailer import generate_thumbnails data = self.render_image().getvalue() footer_meta = generate_thumbnails(data, fs=fs, image_type='footer', filename=self.get_path()) # Update the Content details. content_key = self.comment.reply_content.details_key old_raw_meta = redis.get(content_key) if old_raw_meta: meta = util.loads(old_raw_meta) else: meta = {} meta.update(footer_meta) redis.set(content_key, util.dumps(meta)) self.comment.reply_content.details.force() self.comment.details.force() return meta
def render_POST(self, request): key, pdic = parse_header(request.getHeader('content-type')) if not key == 'application/json': return return_json(request, { 'success': False, 'message': 'Invalid Content-type.' }) jsargs = util.loads(request.content.read()) source = jsargs.get('source', "").encode('ascii') name = sha1(source).hexdigest() @inlineCallbacks def upload_js(): try: op = self.fs.operation() op.save(name, source) yield op.wait() respond_json( request, { 'success': True, 'name': name, 'url': Config['script_base_url'] + name }) except Exception, e: log.err() respond_json(request, { 'success': False, 'code': 'unknown', 'reason': repr(e) })
def api_upload(request): content_type = request.META.get('CONTENT_TYPE', '') url = '' filedata = None if 'file' in request.FILES: filedata = "".join(request.FILES['file'].chunks()) elif content_type.startswith('application/json'): json = util.loads(request.body) url = json.get('url', '').strip() filedata = _fetch_url(url) elif content_type.startswith('application/base64-png'): token = 'data:image/png;base64,' header, data = request.body.split(',', 2) if not header.startswith('data:') or not 'image/png' in header or not 'base64' in header: return {'success': False, 'code': 'unknown', 'reason': 'Missing data url header.'} else: filedata = b64decode(data) if filedata: ret = _got_imagedata(filedata, request, url=url) from canvas.cache_patterns import cache from canvas.models import Content util.papertrail.debug('UPLOADS: _got_imagedata, actual cache is {} for content ID {}'.format(cache.get('content:%s:full_details_v26' % ret['content']['id']), ret['content']['id'])) util.papertrail.debug('UPLOADS: _got_imagedata, actual content object for ID {} exists: {}'.format(ret['content']['id'], Content.all_objects.filter(id=ret['content']['id']).exists())) util.papertrail.debug('UPLOADS: _got_imagedata: {} {}'.format(ret.get('success'), ret['content']['id'])) return ret else: raise ServiceError("No file or url.")
def create_gif_content(**kwargs): content = create_content(**kwargs) key = "content:%s:details" % content.id details = util.loads(redis.get(key)) details["original"]["animated"] = True redis.set(key, util.dumps(details)) return content
def create_gif_content(**kwargs): content = create_content(animated=True, **kwargs) key = 'content:%s:details' % content.id details = util.loads(redis.get(key)) details['original']['animated'] = True redis.set(key, util.dumps(details)) return content
def api_upload(request): content_type = request.META['CONTENT_TYPE'] url = '' filedata = None if 'file' in request.FILES: filedata = "".join(request.FILES['file'].chunks()) elif content_type.startswith('application/json'): json = util.loads(request.raw_post_data) url = json.get('url', '').strip() filedata = _fetch_url(url) elif content_type.startswith('application/base64-png'): token = 'data:image/png;base64,' header, data = request.raw_post_data.split(',', 2) if not header.startswith( 'data:' ) or not 'image/png' in header or not 'base64' in header: return { 'success': False, 'code': 'unknown', 'reason': 'Missing data url header.' } else: filedata = b64decode(data) if filedata: return _got_imagedata(filedata, request, url=url) else: raise ServiceError("No file or url.")
def test_post_documentation(self): result = self.post('/public_api/posts/') self.assertAPISuccess(result) result = self.get('/public_api/posts/') self.assertAPISuccess(result) json = util.loads(result.content) self.assertIn('documentation', json.keys()) self.assertEqual(posts.__doc__, json['documentation'])
def test_get_post_by_short_id(self): comment = create_comment() result = self.get('/public_api/posts/{0}'.format(short_id(comment.id))) self.assertAPISuccess(result) result = self.post('/public_api/posts/{0}'.format(short_id(comment.id))) self.assertAPISuccess(result) json = util.loads(result.content) self.check_comment(comment.details(), json)
def backlog(self, start=-1): try: raw_json = yield self.svc.redis.zrangebyscore(self.rc.msg_backlog.key, start + 1, count=100, withscores=True) except RuntimeError: raise Channel.DisconnectedError messages = [{'id': int(id), 'payload': util.loads(payload)} for (payload, id) in raw_json] if not messages: returnValue({}) else: returnValue({self.rc.channel: messages})
def _details(self): base = util.loads(self.data) base.update({"id": self.id, "ts": self.timestamp, "activity_type": self.activity_type}) # TODO have a UserDetails for example.com too to get rid of this branch. if self.actor_id: base["actor"] = UserDetails.from_id(self.actor_id).to_client() return base
def view_wrapper(request, *args, **kwargs): try: request.JSON = loads(request.raw_post_data) if request.raw_post_data else {} except JSONDecodeError: temp_view = _bad_json_view else: temp_view = view return _handle_json_response(temp_view, json_response_processor, request, *args, **kwargs)
def test_get_post_by_short_id(self): comment = create_comment() result = self.get('/public_api/posts/{0}'.format(short_id(comment.id))) self.assertAPISuccess(result) result = self.post('/public_api/posts/{0}'.format(short_id( comment.id))) self.assertAPISuccess(result) json = util.loads(result.content) self.check_comment(comment.details(), json)
def test_post_respects_anonymity(self): comment = create_comment(anonymous=True) id_list = [short_id(comment.id)] req = {'ids': id_list} result = self.post('/public_api/posts/', data=req) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['posts'])) self.assertEqual("Anonymous", json['posts'][0]['author_name'])
def test_post_respects_anonymity(self): comment = create_comment(anonymous=True) id_list = [ short_id(comment.id) ] req = {'ids': id_list} result = self.post('/public_api/posts/', data=req) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['posts'])) self.assertEqual("Anonymous", json['posts'][0]['author_name'])
def test_respect_anonymous_posts_via_post(self): user = create_user() comments = [create_comment(author=user, anonymous=True) for x in range(3)] username = user.username result = self.post('/public_api/users/', data={'ids':[username]}) self.assertAPISuccess(result) json = util.loads(result.content) returned_posts = [x['id'] for x in json['users'][0]['posts']] self.assertEqual(0, len(returned_posts))
def test_get_single_post_by_posted_id(self): comment = create_comment() id_list = [short_id(comment.id)] req = {'ids': id_list} result = self.post('/public_api/posts/', data=req) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['posts'])) self.check_comment(comment.details(), json['posts'][0])
def test_get_single_distinct_post_by_posted_ids(self): comment = create_comment() id_list = [ short_id(comment.id), short_id(comment.id), short_id(comment.id) ] req = {'ids': id_list} result = self.post('/public_api/posts/', data=req) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['posts'])) self.check_comment(comment.details(), json['posts'][0])
def _get(request, key): key = make_cookie_key(key) val = request.COOKIES.get(key) if val is not None: val = util.loads(urllib.unquote(val)) return ( key, val, )
def _promotion_stickers(following_ids): feed_promotion_keys = [ UserFeedPromotionBuffer.get_key(user_id) for user_id in following_ids ] return [( util.loads(promotion), score, ) for promotion, score in redis.zunion( feed_promotion_keys, withscores=True, transaction=False)]
def test_get_multiple_posts_by_post(self): comment = create_comment() comment1 = create_comment() id_list = [short_id(x.id) for x in [comment, comment1]] req = {'ids': id_list} result = self.post('/public_api/posts/', data=req) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(2, len(json['posts'])) self.check_comment(comment.details(), json['posts'][0])
def view_wrapper(request, *args, **kwargs): try: body = request.body request.JSON = loads(body) if body else {} except JSONDecodeError as e: temp_view = _bad_json_view else: temp_view = view return _handle_json_response(temp_view, json_response_processor, request, *args, **kwargs)
def test_respect_anonymous_posts_via_post(self): user = create_user() comments = [ create_comment(author=user, anonymous=True) for x in range(3) ] username = user.username result = self.post('/public_api/users/', data={'ids': [username]}) self.assertAPISuccess(result) json = util.loads(result.content) returned_posts = [x['id'] for x in json['users'][0]['posts']] self.assertEqual(0, len(returned_posts))
def ServiceResponse(request, response, **kwargs): """ Serializes the response to primarily JSON, but you can force it to produce YAML by sending a format=yaml request variable. """ formatted_response = client_dumps(response) if request.POST.get("format") == "yaml": formatted_response = yaml.safe_dump(loads(formatted_response), default_flow_style=False) # Allows us to force a mimetype in a request arg. mimetype = request.POST.get("force_mimetype", 'application/json') return HttpResponse(formatted_response, mimetype, **kwargs)
def get_redis(request, key): """ Grabs an entity from Redis given a key. """ for getter in [redis.hgetall, redis.get]: try: value = getter(key) assert value return dict(value_in_redis=util.loads(value)) except Exception, e: logging.debug(e) continue
def test_get_users_by_post(self): user = create_user() user1 = create_user() user2 = create_user() users = [user, user1, user2] data = {'ids': [user.username, user1.username, user2.username, "trololo"]} result = self.post('/public_api/users/', data=data) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(len(users), len(json['users'])) returned_users = [x['user'] for x in json['users']] for u in users: self.assertIn(u.username, returned_users)
def _details(self): base = util.loads(self.data) base.update({ 'id': self.id, 'ts': self.timestamp, 'activity_type': self.activity_type, }) #TODO have a UserDetails for example.com too to get rid of this branch. if self.actor_id: base['actor'] = UserDetails.from_id(self.actor_id).to_client() return base
def _api_call(cls, path, data=None, client=None, user=None, password=None, method="post"): data = data or {} response = getattr(cls, method)(path, data=util.dumps(data), client=client, user=user, password=password, content_type="application/json") try: content = util.loads(response.content) except ValueError: # Probably not a JSON response, so just return a string. content = response.content return content
def test_get_paged_user_posts(self): user = create_user() knobs.PUBLIC_API_PAGINATION_SIZE = 5 first_five = [ create_comment(author=user) for x in range(knobs.PUBLIC_API_PAGINATION_SIZE) ] second_five = [ create_comment(author=user) for x in range(knobs.PUBLIC_API_PAGINATION_SIZE) ] data = {'ids': [user.username]} result = self.post('/public_api/users/', data=data) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['users'])) self.assertEqual(knobs.PUBLIC_API_PAGINATION_SIZE, len(json['users'][0]['posts'])) returned_ids = [x['id'] for x in json['users'][0]['posts']] for x in second_five: self.assertIn(short_id(x.id), returned_ids) data = { 'ids': [{ 'user': user.username, 'skip': knobs.PUBLIC_API_PAGINATION_SIZE }] } result = self.post('/public_api/users/', data=data) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['users'])) self.assertEqual(knobs.PUBLIC_API_PAGINATION_SIZE, len(json['users'][0]['posts'])) returned_ids = [x['id'] for x in json['users'][0]['posts']] for x in first_five: self.assertIn(short_id(x.id), returned_ids)
def test_get_users_by_post(self): user = create_user() user1 = create_user() user2 = create_user() users = [user, user1, user2] data = { 'ids': [user.username, user1.username, user2.username, "trololo"] } result = self.post('/public_api/users/', data=data) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(len(users), len(json['users'])) returned_users = [x['user'] for x in json['users']] for u in users: self.assertIn(u.username, returned_users)
def test_get_post_and_replies(self): comment = create_comment(anonymous=True) rep1 = create_comment(parent_comment=comment, replied_comment=comment) rep2 = create_comment(parent_comment=comment, replied_comment=rep1) rep3 = create_comment(parent_comment=comment, replied_comment=rep1) rep4 = create_comment(parent_comment=comment) reply_ids = [short_id(x.id) for x in [rep1, rep2, rep3, rep4]] req = {'ids': [short_id(comment.id)]} result = self.post('/public_api/posts/', data=req) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['posts'])) post = json['posts'][0] for id in reply_ids: self.assertIn(id, [x['id'] for x in post['replies']])
def test_get_paged_user_posts(self): user = create_user() knobs.PUBLIC_API_PAGINATION_SIZE = 5 first_five = [create_comment(author=user) for x in range(knobs.PUBLIC_API_PAGINATION_SIZE)] second_five = [create_comment(author=user) for x in range(knobs.PUBLIC_API_PAGINATION_SIZE)] data = {'ids': [user.username]} result = self.post('/public_api/users/', data=data) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['users'])) self.assertEqual(knobs.PUBLIC_API_PAGINATION_SIZE, len(json['users'][0]['posts'])) returned_ids = [x['id'] for x in json['users'][0]['posts']] for x in second_five: self.assertIn(short_id(x.id), returned_ids) data = {'ids': [{'user':user.username, 'skip':knobs.PUBLIC_API_PAGINATION_SIZE}]} result = self.post('/public_api/users/', data=data) self.assertAPISuccess(result) json = util.loads(result.content) self.assertEqual(1, len(json['users'])) self.assertEqual(knobs.PUBLIC_API_PAGINATION_SIZE, len(json['users'][0]['posts'])) returned_ids = [x['id'] for x in json['users'][0]['posts']] for x in first_five: self.assertIn(short_id(x.id), returned_ids)
def update(fs, content, image_type, save_to_db=True): filename = content.details()['original']['name'] # Prevent issues with unicode filenames. filename = filename.encode('ascii') data = fs.read(filename) thumbnailer = Thumbnailer(fs) meta = util.loads(redis.get(content.details_key)) meta.update(thumbnailer.store(data, image_type)) update_metadata(content, meta) if save_to_db: content.save() redis.set(content.details_key, util.dumps(meta)) content.details.force()
def test_get_user_by_username(self): user = create_user() comments = [create_comment(author=user) for x in range(3)] anon_comments = [create_comment(author=user, anonymous=True) for x in range(3)] username = user.username result = self.get('/public_api/users/{0}'.format(username)) self.assertAPISuccess(result) result = self.post('/public_api/users/{0}'.format(username)) self.assertAPISuccess(result) json = util.loads(result.content) self.check_user(user, json) returned_posts = [x['id'] for x in json['posts']] for c in comments: self.assertIn(short_id(c.id), returned_posts) for c in anon_comments: self.assertNotIn(short_id(c.id), returned_posts)
def test_respect_moderated_posts(self): user = create_user() comments = [create_comment(author=user) for x in range(4)] comments[0].moderate_and_save(Visibility.HIDDEN, user) comments[1].moderate_and_save(Visibility.DISABLED, user) comments[2].moderate_and_save(Visibility.UNPUBLISHED, user) comments[3].moderate_and_save(Visibility.CURATED, user) username = user.username result = self.get('/public_api/users/{0}'.format(username)) self.assertAPISuccess(result) json = util.loads(result.content) returned_posts = [x['id'] for x in json['posts']] self.assertIn(short_id(comments[0].id), returned_posts) self.assertNotIn(short_id(comments[1].id), returned_posts) self.assertNotIn(short_id(comments[2].id), returned_posts) self.assertIn(short_id(comments[3].id), returned_posts)
def test_get_user_by_username(self): user = create_user() comments = [create_comment(author=user) for x in range(3)] anon_comments = [ create_comment(author=user, anonymous=True) for x in range(3) ] username = user.username result = self.get('/public_api/users/{0}'.format(username)) self.assertAPISuccess(result) result = self.post('/public_api/users/{0}'.format(username)) self.assertAPISuccess(result) json = util.loads(result.content) self.check_user(user, json) returned_posts = [x['id'] for x in json['posts']] for c in comments: self.assertIn(short_id(c.id), returned_posts) for c in anon_comments: self.assertNotIn(short_id(c.id), returned_posts)
def _details(self): base = util.loads(self.data) base.update({ 'id': self.id, 'ts': self.timestamp, 'activity_type': self.activity_type, }) #TODO have a UserDetails for example.com too to get rid of this branch. if self.actor: if settings.PROJECT == 'canvas': base['actor'] = { 'id': self.actor.id, 'username': self.actor.username, } elif settings.PROJECT == 'drawquest': base['actor'] = UserDetails.from_id(self.actor.id).to_client() return base
def update(fs, content, image_type, save_to_db=True): from drawquest.apps.content_metadata.models import save_content_metadata_from_legacy_dict filename = content.details()['original']['name'] # Prevent issues with unicode filenames. filename = filename.encode('ascii') data = fs.read(filename) thumbnailer = Thumbnailer(fs) meta = util.loads(redis.get(content.details_key)) meta.update(thumbnailer.store(data, image_type)) if save_to_db: content.save() save_content_metadata_from_legacy_dict(content, meta) content.details.force()
def render_POST(self, request): key, pdic = parse_header(request.getHeader('content-type')) if not key == 'application/json': return return_json(request, {'success': False, 'message': 'Invalid Content-type.'}) jsargs = util.loads(request.content.read()) source = jsargs.get('source', "").encode('ascii') name = sha1(source).hexdigest() @inlineCallbacks def upload_js(): try: op = self.fs.operation() op.save(name, source) yield op.wait() respond_json(request, {'success': True, 'name': name, 'url': Config['script_base_url'] + name}) except Exception, e: log.err() respond_json(request, {'success': False, 'code': 'unknown', 'reason': repr(e)})
def _details(self): base = util.loads(self.data) base.update( { "id": self.id, "ts": self.timestamp, "activity_type": dict((v, k) for k, v in ACTIVITY_TYPE_IDS.items())[self.activity_type], } ) if self.quest_id: base["quest_id"] = self.quest_id if self.comment_id: base["comment_id"] = self.comment_id # TODO have a UserDetails for example.com too to get rid of this branch. if self.actor_id: base["actor"] = UserDetails.from_id(self.actor_id).to_client() return base