Esempio n. 1
0
 def success(data, http_code=200):
     return FlaskResponse(JSONEncoder().encode({
         "data": data,
         "error": None
     }),
                          status=http_code,
                          mimetype='application/json')
 def error(error):
     return FlaskResponse(JSONEncoder().encode({
         "data": None,
         "error": {
             "message": error.message,
             "code": error.internal_code
         }
     }), status=error.http_code, mimetype='application/json')
Esempio n. 3
0
async def insert(request):
    entity = await request.json()
    parent_id = entity.get('parent_id')
    if parent_id:
        parent_id = ObjectId(parent_id)
        entity['parent_id'] = parent_id

    db = request.app['db']
    await db.entity.insert_one(entity)

    return web.json_response(text=JSONEncoder().encode(entity))
Esempio n. 4
0
async def subtree(request):
    id = request.match_info.get('id')
    db = request.app['db']

    result = []
    async for doc in db.entity.aggregate(
            get_entity_parent_pipeline(ObjectId(id))):
        parents = doc.get('parents')
        for parent in parents:
            result.append(parent.get('_id'))

    return web.json_response(text=JSONEncoder().encode(result))
Esempio n. 5
0
async def find(request):
    data = await request.json()
    db = request.app['db']

    entities = db.entity.find(
        {"$text": {
            "$search": data.get('search_request')
        }})

    result = []
    async for entity in entities:
        result = []
        async for doc in db.entity.aggregate(
                get_entity_parent_pipeline(entity.get('_id'))):
            parents = doc.get('parents')
            for parent in parents:
                result.append(parent.get('_id'))

    return web.json_response(text=JSONEncoder().encode(result))
Esempio n. 6
0
def get_places():
    lat = float(request.args.get('lat'))
    lon = float(request.args.get('lng'))
    radius = request.args.get('radius') or 5000.0

    if not lat or not lon:
        return 'Bad Request', 400

    radius /= 1.40

    # Earth’s radius, sphere
    earth_radius = 6378137.0

    # Coordinate offsets in radians
    d_lat = radius / earth_radius
    d_lon = radius / (earth_radius * math.cos(math.pi * lat / 180))

    # OffsetPosition, decimal degrees
    lat0 = lat + d_lat * 180 / math.pi
    lon0 = lon + d_lon * 180 / math.pi

    lat1 = lat - d_lat * 180 / math.pi
    lon1 = lon - d_lon * 180 / math.pi

    places = list(
        mongo.db.places.find({
            "location.lat": {
                "$lt": lat0,
                "$gt": lon0
            },
            "location.lng": {
                "$lt": lat1,
                "$gt": lon1
            }
        }))
    return JSONEncoder().encode(places)
Esempio n. 7
0
from pymongo import ReturnDocument
from pymongo.write_concern import WriteConcern

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.web import RequestHandler, HTTPError, StaticFileHandler
from tornado.options import define, options
from tornado.log import app_log

from utils import IntEncoder, normalise_uri, JSONEncoder, anonymise_ip

# Add our own encoder to the JSON module, handles datetime objects for us
json._default_encoder = JSONEncoder()

UTC = timezone(timedelta(0))

define("domain", type=str, default="https://bndr.it",
       help="Domain for short URLs")
define("port", default="8000", help="Server port", type=int)


REDIRECT_TEMPLATE ="""
<html>
    <head>
        <meta http-equiv="refresh" content="3;url={preferred_binder}" />
        <link
          rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"