def main(): import getPop, jsonify fn = './data/N40W074.hgt' siz = os.path.getsize(fn) dim = int(math.sqrt(siz / 2)) assert dim * dim * 2 == siz, 'Invalid file size' data = numpy.fromfile(fn, numpy.dtype('>i2'), dim * dim).reshape( (dim, dim)) floodMap = hgtMap(data.tolist()) floodStartLocation = floodMap.latLongToPointApprox(40.366, -73.88) userSelectedIntensity = 5 # provided by user (frontend) floodLitres = (10**6) * (userSelectedIntensity**2) floodMap.minFloodHeight = 100 floodMap.setWater(floodStartLocation[0], floodStartLocation[1], floodLitres) mins = 5 tickInterations = mins for _ in range(tickInterations): floodMap = tick(floodMap) pmap = getPop.getPopulation(floodMap) totalPop = getPop.totalPop(pmap) jsonify.jsonify(floodMap, pmap, totalPop)
def test_basic(): for v in [ 1, 1.0, 'foo', u'bar', [1, 2], {'a': 1, 'b': 2}]: assert jsonify(v) == v assert jsonify((1, 'a', {'b': 2})) == [ 1, 'a', {'b': 2}]
def get(self): user = users.get_current_user() logging.info("Debug User: '******'") if user: user_data = models.UserData.get_for_user_id(user.user_id()) # TODO(chris): remove this hack! We added email to # UserData, and it's required, so this ensures that on the # next page load, UserData will be updated properly. if user_data.email is None: logging.info("Added %s for UserData %s" % (user.email(), user_data.key.urlsafe())) user_data.email = user.email() user_data.put() else: user_data = None if user_data: user_cards, global_cards = yield (( models.Card .query(models.Card.user_key == ndb.Key(urlsafe=user_data.key.urlsafe())) .order(models.Card.next_review) .fetch_async(500)), models.Card .query() .order(-models.Card.added) .fetch_async(500), ) following_keys = [k.urlsafe() for k in user_data.following] user_key = user_data.key.urlsafe() username = str(user) user_nickname = user_data.nickname user_email = user_data.email else: user_cards = [] global_cards = (models.Card .query().order(-models.Card.added).fetch(1000)) following_keys = [] user_key = None username = None user_nickname = None user_email = None template = JINJA_ENVIRONMENT.get_template('index.html') env = { 'user': user_data, 'user_key': user_key, 'user_nickname': user_nickname, 'user_email': user_email, 'username': json.dumps(username), 'following_keys': jsonify.jsonify(following_keys), 'users': users, 'user_cards': jsonify.jsonify(user_cards), 'global_cards': jsonify.jsonify(global_cards), } self.response.write(template.render(env))
def get(self): user = users.get_current_user() logging.info("Debug User: '******'") if user: user_data = models.UserData.get_for_user_id(user.user_id()) # TODO(chris): remove this hack! We added email to # UserData, and it's required, so this ensures that on the # next page load, UserData will be updated properly. if user_data.email is None: logging.info("Added %s for UserData %s" % (user.email(), user_data.key.urlsafe())) user_data.email = user.email() user_data.put() else: user_data = None if user_data: user_cards, global_cards = yield ( (models.Card.query(models.Card.user_key == ndb.Key( urlsafe=user_data.key.urlsafe())).order( models.Card.next_review).fetch_async(500)), models.Card.query().order(-models.Card.added).fetch_async(500), ) following_keys = [k.urlsafe() for k in user_data.following] user_key = user_data.key.urlsafe() username = str(user) user_nickname = user_data.nickname user_email = user_data.email else: user_cards = [] global_cards = ( models.Card.query().order(-models.Card.added).fetch(1000)) following_keys = [] user_key = None username = None user_nickname = None user_email = None template = JINJA_ENVIRONMENT.get_template('index.html') env = { 'user': user_data, 'user_key': user_key, 'user_nickname': user_nickname, 'user_email': user_email, 'username': json.dumps(username), 'following_keys': jsonify.jsonify(following_keys), 'users': users, 'user_cards': jsonify.jsonify(user_cards), 'global_cards': jsonify.jsonify(global_cards), } self.response.write(template.render(env))
def handle_coc_exception(error): # Log our own exceptions at error level (no need for stack trace, these are "expected" if isinstance(error, COCException): app.logger.error("COCException guid={0} msg={1} user_msg={2}".format( error.guid, error.message, error.user_message)) response = jsonify(error.to_dict()) else: app.logger.exception(error) response = jsonify({ "success": False, "message": "There was an error on the server." }) return response
def create_quote_resp_json(quote, body_valid, tags_valid): error = { 'error': 'validation', 'error_msg': '' } if not body_valid: error['error_msg'] += 'Body too large' if not tags_valid: error['error_msg'] += 'Tags too large' if not body_valid or not tags_valid: rs = jsonify(error, 'application/vnd.pyqdb-error+json') rs.status_code = 413 return rs rs = jsonify(quote, Quote.json_mimetype) add_loc_hdr(rs, '/quotes/%s' % (quote.id)) rs.status_code = 201 return rs
def create_quote_resp_json(quote, body_valid, tags_valid): error = {'error': 'validation', 'error_msg': ''} if not body_valid: error['error_msg'] += 'Body too large' if not tags_valid: error['error_msg'] += 'Tags too large' if not body_valid or not tags_valid: rs = jsonify(error, 'application/vnd.pyqdb-error+json') rs.status_code = 413 return rs rs = jsonify(quote, Quote.json_mimetype) add_loc_hdr(rs, '/quotes/%s' % (quote.id)) rs.status_code = 201 return rs
def latest(): incr, start, next, prev = parse_qs(request.args) quotes = db.latest(incr, start) admin = authDB.isAuthenticated(request) if request.wants_json(): next_link = '/quotes?start=%s' % (next) prev_link = '/quotes?start=%s' % (prev) json = { 'quotes': quotes, 'links': [ build_link(next_link, 'pyqdb/quotes/next', Quote.list_json_mimetype), build_link(prev_link, 'pyqdb/quotes/prev', Quote.list_json_mimetype) ] } rs = jsonify(json, Quote.list_json_mimetype) add_link_hdr(rs, next_link, 'pyqdb/quotes/next') if start > 0: add_link_hdr(rs, prev_link, 'pyqdb/quotes/prev') return rs return render_template('quotes.html', nav=navs, quotes=quotes, page='quotes', next=next, prev=prev, isAdmin=admin)
def to_json(self): archive = { "format": "JSONCardArchive", "version": self.VERSION, "cards": map(self._card_to_archive_card, self.cards), } return jsonify.jsonify(archive)
def user_follows(handler): """Get the users followed by and following a certain users. Called via a route like: /api/user/<user_key>/follows """ path = handler.request.path user_key = path[len('/api/user/'):-len('/follows')] user_data = ndb.Key(urlsafe=user_key).get() if not user_data: return "User not found" # TODO make async following_data = ndb.get_multi(user_data.following) followers_data = ndb.get_multi(user_data.followers) # Finally ready to do the update data = { 'user_data': user_data, 'following': following_data, 'followers': followers_data } return jsonify.jsonify(data)
def log_client_message(): log_message = request.get_json() message = log_message['message'] level = log_message['level'] client_logging_service.log(message, level) return jsonify({"success": True})
def welcome(): if request.wants_json(): links = navs links.append(build_link('/', 'self', 'application/json')) root = {'version': '0.1', 'title': 'VT Bash', 'links': links} return jsonify(root, 'application/json') news = News() return render_template('index.html', nav=navs, news=news.news)
def welcome(): if request.wants_json(): links = navs links.append(build_link('/', 'self', 'application/json')) root = { 'version': '0.1', 'title': 'VT Bash', 'links': links } return jsonify(root, 'application/json') news = News() return render_template('index.html', nav=navs, news=news.news)
def get(self): user = users.get_current_user() logging.info("Debug User: '******'") if user: user_data = models.UserData.get_for_user_id(user.user_id()) else: user_data = None if user_data: user_cards, global_cards = yield (( models.Card .query(models.Card.user_key == ndb.Key(urlsafe=user_data.key.urlsafe())) .order(models.Card.next_review) .fetch_async(500)), models.Card .query() .order(-models.Card.added) .fetch_async(500), ) following_keys = [k.urlsafe() for k in user_data.following] else: user_cards = [] global_cards = (models.Card .query().order(-models.Card.added).fetch(1000)) following_keys = [] if user_data: username = str(user) else: username = None template = JINJA_ENVIRONMENT.get_template('index.html') env = { 'user': user_data, 'username': json.dumps(username), 'following_keys': jsonify.jsonify(following_keys), 'users': users, 'user_cards': jsonify.jsonify(user_cards), 'global_cards': jsonify.jsonify(global_cards), } self.response.write(template.render(env))
def card_search(handler): """Search cards with query parameter "q". Returns a (possibly empty) list of JSONified models.Card entities. See search.py for query processing details. """ query = handler.request.get('q', '') search_results = search.query_cards(query, limit=20, ids_only=True) results = ndb.get_multi([ndb.Key(urlsafe=result.doc_id) for result in search_results]) return jsonify.jsonify(results)
def fetch_votes(quote_id): quote = db.get(quote_id) if quote is None: abort(404) json = { 'links': [ build_link('/quotes/%s' %(quote_id), 'pyqdb/quote', Quote.json_mimetype), build_link('/quotes/%s/votes' %(quote_id), 'pyqdb/quote/cast-vote', Vote.json_mimetype, method='put') ], 'type': '', # up or down 'id': quote_id } return jsonify(json, Vote.json_mimetype)
def get_content_list(): app.logger.info("get_content_list()") content_service = ContentService() content_list = content_service.get_content_list() ret = { "success": True, "content": content_list } return jsonify(ret)
def get_content(content_id): app.logger.info("get_content() - content_id={0}".format(content_id)) content_service = ContentService() content = content_service.get_content(content_id) ret = { "success": True, "html": content['html'] } return jsonify(ret)
def get_posts(): app.logger.info("get_posts()") blog_posts_service = BlogPostsService() posts = blog_posts_service.get_posts() ret = { "success": True, "posts": posts } return jsonify(ret)
def entity_view(handler, route_root): """Query for a single entity by Key.""" path = handler.request.path response = '{}' if not path.startswith(route_root) and len(path) > len(route_root): return response entity_key = path[len(route_root):] entity = ndb.Key(urlsafe=entity_key).get() if entity: response = jsonify.jsonify(entity, pretty_print=True) return response
def update_content(): content_data = request.get_json() app.logger.info("update_content() - data={0}".format(content_data)) content_id = content_data['content_id'] html = content_data['html'] content_service = ContentService() success = content_service.update_content(content_id, html) ret = { "success": success } return jsonify(ret)
def new_quote(): if request.wants_json(): rs = jsonify( { 'body': "Quote here", 'tags': [], 'link': build_link('/quotes', 'pyqdb/quote/new', Quote.json_mimetype, method='post', title='Create a new quote') }, Quote.json_mimetype) add_link_hdr(rs, '/quotes', 'pyqdb/quote/new') return rs return render_template('submit.html', nav=navs)
def cast_vote(quote_id): ip = request.remote_addr quote = db.get(quote_id) if quote is None: abort(404) if request.provided_json(): return json_nyi() else: type = request.form['type'] if type == "up": quote = db.up_vote(quote_id, ip) elif type == "down": quote = db.down_vote(quote_id, ip) else: abort(400) return jsonify(quote, Quote.json_mimetype)
def latest(): incr,start,next,prev = parse_qs(request.args) quotes = db.latest(incr, start) admin = authDB.isAuthenticated(request) if request.wants_json(): next_link = '/quotes?start=%s' % (next) prev_link = '/quotes?start=%s' % (prev) json = {'quotes': quotes, 'links': [ build_link(next_link, 'pyqdb/quotes/next', Quote.list_json_mimetype), build_link(prev_link, 'pyqdb/quotes/prev', Quote.list_json_mimetype) ] } rs = jsonify(json, Quote.list_json_mimetype) add_link_hdr(rs, next_link, 'pyqdb/quotes/next') if start > 0: add_link_hdr(rs, prev_link, 'pyqdb/quotes/prev') return rs return render_template('quotes.html', nav=navs, quotes=quotes, page='quotes', next=next, prev=prev, isAdmin=admin)
def authenticate(): post_data = request.get_json() app.logger.info("authenticate() - {0}".format(post_data)) username = post_data['username'] password = post_data['password'] session = authentication_service.authenticate(username, password) if session is None: raise COCException("Failed to authenticate.") else: ret = { "success": True, "token": session['token'], "expires": session['expires'] } return jsonify(ret)
def card_search(handler): """Search cards with query parameter "q". Returns a (possibly empty) list of JSONified models.Card entities. See search.py for query processing details. """ user = users.get_current_user() if user: user_data = models.UserData.get_for_user_id(user.user_id()) user_key = user_data.key.urlsafe() else: user_key = None query = handler.request.get('q', '') search_results = search.query_cards(query, limit=20, ids_only=True, user_key=user_key) results = ndb.get_multi([ndb.Key(urlsafe=result.doc_id) for result in search_results]) return jsonify.jsonify(results)
def fetch_votes(quote_id): quote = db.get(quote_id) if quote is None: abort(404) json = { 'links': [ build_link('/quotes/%s' % (quote_id), 'pyqdb/quote', Quote.json_mimetype), build_link('/quotes/%s/votes' % (quote_id), 'pyqdb/quote/cast-vote', Vote.json_mimetype, method='put') ], 'type': '', # up or down 'id': quote_id } return jsonify(json, Vote.json_mimetype)
def add_post(): post_data = request.get_json() app.logger.info("add_post() - {0}".format(post_data)) title = post_data['title'] summary = post_data['summary'] html = post_data['html'] tags = post_data['tags'] iso_date_string = post_data['date'] date_object = dateutil.parser.parse(iso_date_string) blog_posts_service = BlogPostsService() post_id = blog_posts_service.add_post(title, summary, html, tags, date_object) ret = { "success": True, "post_id": post_id } return jsonify(ret)
def card_search(handler): """Search cards with query parameter "q". Returns a (possibly empty) list of JSONified models.Card entities. See search.py for query processing details. """ user = users.get_current_user() if user: user_data = models.UserData.get_for_user_id(user.user_id()) user_key = user_data.key.urlsafe() else: user_key = None query = handler.request.get('q', '') search_results = search.query_cards(query, limit=20, ids_only=True, user_key=user_key) results = ndb.get_multi( [ndb.Key(urlsafe=result.doc_id) for result in search_results]) return jsonify.jsonify(results)
def test_explicit(): e = Expl('foo', 'a') result = {'name': 'foo', 'objs': ['a']} assert jsonify(e) == result assert jsonify([e]) == [result]
def encode(object): return SafeString(base64.standard_b64encode(jsonify(object)))
def card_query(handler): """Query for multiple cards. See main.py for usage examples. TODO(jace): return a query cursor, too? """ tag = handler.request.get("tag", None) tag_list = tag.split(',') if tag else None review = handler.request.get('review', None) reviewAll = handler.request.get('reviewAll', False) include_followers = handler.request.get('include_followers', None) user_key = handler.request.get('user', None) if review and user_key: handler.error(400) return "'review' and 'user_key' cannot be used together." if review and include_followers: handler.error(400) return "'review' and 'include_followers' cannot be used together." if include_followers and not user_key: handler.error(400) return "'review' and 'include_followers' cannot be used together." if review: # if asked for review cards, get them for the current user only current_user = get_current_user(handler) if not current_user: handler.error(400) return "must be logged in to quer for review cards." user_key = current_user.key.urlsafe() query = models.Card.query() if include_followers: user_data = ndb.Key(urlsafe=user_key).get() if not user_data: handler.error(500) return "UserData not found." user_list = user_data.following + [ndb.Key(urlsafe=user_key)] query = query.filter(models.Card.user_key.IN(user_list)) elif user_key: query = query.filter(models.Card.user_key == ndb.Key(urlsafe=user_key)) if tag_list: query = query.filter(models.Card.tags.IN(tag_list)) if review: # For review mode, we sort by next scheduled review query = query.order(models.Card.next_review) else: query = query.order(-models.Card.added) results = query.fetch(100) response = '[]' if results: if review and not reviewAll: # TODO(jace) if the current user is asking for review cards but # hasn't explicitly asked to review ALL cards, then we truncate # the results to include just cards scheduled on or before # today... the future can wait. now = datetime.datetime.now() results = [card for card in results if card.next_review <= now] response = jsonify.jsonify(results, pretty_print=True) return response
def user_view_current(handler): """Return information about the currently logged in user.""" return jsonify.jsonify(users.get_current_user())
def tags(): format = request.args.get('format', 'html') if request.wants_json(): return jsonify(db.tags(), Tag.list_json_mimetype) return render_template('tags.html', nav=navs)
def json_nyi(): json_error = {'error': 'nyi', 'error_msg': 'search not yet implemented'} rs = jsonify(json_error, 'application/vnd.pyqdb-error+json') rs.status_code = 501 return rs
def card_query(handler): """Query for multiple cards. See main.py for usage examples. TODO(jace): return a query cursor, too? """ tag = handler.request.get("tag", None) tag_list = tag.split(',') if tag else None review = handler.request.get('review', None) reviewAll = handler.request.get('reviewAll', False) include_followers = handler.request.get('include_followers', None) user_key = handler.request.get('user', None) if review and user_key: handler.error(400) return "'review' and 'user_key' cannot be used together." if review and include_followers: handler.error(400) return "'review' and 'include_followers' cannot be used together." if include_followers and not user_key: handler.error(400) return "'review' and 'include_followers' cannot be used together." if review: # if asked for review cards, get them for the current user only current_user = get_current_user(handler) if not current_user: handler.error(400) return "must be logged in to quer for review cards." user_key = current_user.key.urlsafe() query = models.Card.query() if include_followers: user_data = ndb.Key(urlsafe=user_key).get() if not user_data: handler.error(500) return "UserData not found." user_list = user_data.following + [ndb.Key(urlsafe=user_key)] query = query.filter(models.Card.user_key.IN(user_list)) elif user_key: query = query.filter(models.Card.user_key == ndb.Key(urlsafe=user_key)) if tag_list: query = query.filter(models.Card.tags.IN(tag_list)) if review: # For review mode, we sort by next scheduled review query = query.order(models.Card.next_review) else: query = query.order(-models.Card.added) results = query.fetch(100) response = '[]' if results: if review and not reviewAll: # TODO(jace) if the current user is asking for review cards but # hasn't explicitly asked to review ALL cards, then we truncate # the results to include just cards scheduled on or before # today... the future can wait. logging.error("Attempting to truncate review list") now = datetime.datetime.now() results = [card for card in results if card.next_review <= now] response = jsonify.jsonify(results, pretty_print=True) return response
def return_json(self, data): data_json = jsonify.jsonify(data, camel_cased=True) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(data_json)
def new_quote(): if request.wants_json(): rs = jsonify({'body': "Quote here", 'tags': [], 'link': build_link('/quotes', 'pyqdb/quote/new', Quote.json_mimetype, method='post', title='Create a new quote')}, Quote.json_mimetype) add_link_hdr(rs, '/quotes', 'pyqdb/quote/new') return rs return render_template('submit.html', nav=navs)
def images(): return jsonify(list(get_pictures(page=int(request.args.get('page', 1)))))
def test_explicit(): assert jsonify(State(name='Illinois', abbr='IL')) == 'Illinois'
def tags(): format = request.args.get('format', 'html') if request.wants_json(): return jsonify( db.tags(), Tag.list_json_mimetype ) return render_template('tags.html', nav=navs)
def __json__(self): return { 'name': self.name, 'objs': [jsonify(o) for o in self.objs]}
def test_sqlobject(): f = Foo(name='foo') b = Bar(name='bar', value=1, foo=f) assert jsonify(f) == {'name': 'foo', 'id': f.id} assert jsonify(b) == {'name': 'bar', 'value': 1, 'fooID': f.id, 'id': b.id}