def serve(environ, start_response): """ Handle a WSGI request and response. """ make_db_connection() code, data = call_handler(environ) close_db_connection() response_headers = [('Content-Type', 'application/json; charset=utf-8')] if isinstance(data, dict): response_headers += set_cookie_headers(data.pop('cookies', {})) status = str(code) + ' ' + status_codes.get(code, 'Unknown') start_response(status, response_headers) if isinstance(data, str): body = data.encode() elif isinstance(data, dict): body = json.dumps(data, default=json_serial, ensure_ascii=False) body = body.encode() return [body]
def serve(environ, start_response): """ Handle a WSGI request and response. """ db_conn = make_db_connection() request = construct_request(db_conn, environ) code, data = call_handler(request) is_json = isinstance(data, dict) close_db_connection(db_conn) content_type = (('Content-Type', 'application/json; charset=utf-8') if is_json else ('Content-Type', 'text/plain; charset=utf-8')) cookie_headers = (set_cookie_headers(data.pop('cookies', {})) if is_json else []) response_headers = [content_type] + cookie_headers status = str(code) + ' ' + status_codes.get(code, 'Unknown') start_response(status, response_headers) if is_json: data = json.dumps(data, default=json_serial, ensure_ascii=False) return [data.encode()]
'modified': r.now(), 'user_id': 'doris', 'card_id': 'minus-choice-a', 'unit_id': 'minus', 'response': 1, 'score': 1, 'learned': 0.5, }, { 'id': 'response3', 'created': r.now(), 'modified': r.now(), 'user_id': 'doris', 'card_id': 'times-choice-a', 'unit_id': 'times', 'response': 1, 'score': 1, 'learned': 1, }, { 'id': 'response4', 'created': r.now(), 'modified': r.now(), 'user_id': 'doris', 'card_id': 'slash-choice-a', 'unit_id': 'slash', 'response': 1, 'score': 1, 'learned': 0.5 }]).run(database.db_conn)) close_db_connection()
'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'user_id': 'doris', 'kind': 'create_topic', 'data': { 'user_name': 'Eileen', 'topic_name': '%s is fun' % unit_data['name'], 'entity_kind': 'unit', 'entity_name': unit_data['name'], }, 'read': False, 'tags': [], }).run(db_conn) r.table('users_sets').insert({ 'id': 'doris-sets', 'created': r.time(2014, 1, 1, 'Z'), 'modified': r.time(2014, 1, 1, 'Z'), 'user_id': 'doris', 'set_ids': [sample_id for sample_id, set_data in sample_data['sets'].items()], }).run(db_conn) close_db_connection(db_conn) es_populate()
def db_conn(request): db_conn = make_db_connection() request.addfinalizer(lambda: close_db_connection(db_conn)) return db_conn
'user_id': 'doris', 'card_id': 'minus-choice-a', 'unit_id': 'minus', 'response': 1, 'score': 1, 'learned': 0.5, }, { 'id': 'response3', 'created': r.now(), 'modified': r.now(), 'user_id': 'doris', 'card_id': 'times-choice-a', 'unit_id': 'times', 'response': 1, 'score': 1, 'learned': 1, }, { 'id': 'response4', 'created': r.now(), 'modified': r.now(), 'user_id': 'doris', 'card_id': 'slash-choice-a', 'unit_id': 'slash', 'response': 1, 'score': 1, 'learned': 0.5 }]) .run(db_conn)) close_db_connection(db_conn)
'user_id': 'doris', 'card_id': 'minus-choice-a', 'unit_id': 'minus', 'response': 1, 'score': 1, 'learned': 0.5, }, { 'id': 'response3', 'created': r.now(), 'modified': r.now(), 'user_id': 'doris', 'card_id': 'times-choice-a', 'unit_id': 'times', 'response': 1, 'score': 1, 'learned': 1, }, { 'id': 'response4', 'created': r.now(), 'modified': r.now(), 'user_id': 'doris', 'card_id': 'slash-choice-a', 'unit_id': 'slash', 'response': 1, 'score': 1, 'learned': 0.5 }]) .run(database.db_conn)) close_db_connection()
def es_populate(): setup_db() db_conn = make_db_connection() # Empty the database es.indices.delete(index='entity', ignore=[400, 404]) # Add users users = r.table('users').run(db_conn) for user in users: data = pick(json_prep(user), ('id', 'name')) data['avatar'] = get_avatar(user['email']) es.index( index='entity', doc_type='user', body=data, id=user['id'], ) # Add units units = (r.table('units') .filter(r.row['status'].eq('accepted')) .group('entity_id') .max('created') .default(None) .ungroup() .map(r.row['reduction']) .run(db_conn)) for unit in units: es.index( index='entity', doc_type='unit', body=json_prep(unit), id=unit['entity_id'], ) # Add cards cards = (r.table('cards') .filter(r.row['status'].eq('accepted')) .group('entity_id') .max('created') .default(None) .ungroup() .map(r.row['reduction']) .run(db_conn)) for card in cards: es.index( index='entity', doc_type='card', body=json_prep(card), id=card['entity_id'], ) # Add subjects subjects = (r.table('subjects') .filter(r.row['status'].eq('accepted')) .group('entity_id') .max('created') .default(None) .ungroup() .map(r.row['reduction']) .run(db_conn)) for subject in subjects: es.index( index='entity', doc_type='subject', body=json_prep(subject), id=subject['entity_id'], ) # Add topics topics = r.table('topics').run(db_conn) for topic in topics: es.index( index='entity', doc_type='topic', body=json_prep(topic), id=topic['id'], ) # Add posts posts = r.table('posts').run(db_conn) for post in posts: data = json_prep(post) topic = (r.table('topics') .get(data['topic_id']) .run(db_conn)) user = (r.table('users') .get(data['user_id']) .run(db_conn)) data['topic'] = json_prep(topic) data['user'] = pick(json_prep(user), ('id', 'name')) es.index( index='entity', doc_type='post', body=data, id=post['id'], ) close_db_connection(db_conn)
def es_populate(): db_conn = make_db_connection() # Empty the database es.indices.delete(index='entity', ignore=[400, 404]) # Add users cur = db_conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) with cur: cur.execute("SELECT * FROM users;") data = cur.fetchall() users = [row for row in data] db_conn.commit() for user in users: data = pick(json_prep(user), ('id', 'name')) data['avatar'] = get_avatar(user['email']) es.index( index='entity', doc_type='user', body=data, id=convert_uuid_to_slug(user['id']), ) # Add units cur = db_conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) with cur: cur.execute(""" SELECT DISTINCT ON (entity_id) * FROM units WHERE status = 'accepted' ORDER BY entity_id, created DESC; """) data = cur.fetchall() units = [row for row in data] db_conn.commit() for unit in units: es.index( index='entity', doc_type='unit', body=json_prep(unit), id=convert_uuid_to_slug(unit['entity_id']), ) # Add cards cur = db_conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) with cur: cur.execute(""" SELECT DISTINCT ON (entity_id) * FROM cards WHERE status = 'accepted' ORDER BY entity_id, created DESC; """) data = cur.fetchall() cards = [row for row in data] db_conn.commit() for card in cards: es.index( index='entity', doc_type='card', body=json_prep(card), id=convert_uuid_to_slug(card['entity_id']), ) # Add subjects cur = db_conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) with cur: cur.execute(""" SELECT DISTINCT ON (entity_id) * FROM subjects WHERE status = 'accepted' ORDER BY entity_id, created DESC; """) data = cur.fetchall() subjects = [row for row in data] db_conn.commit() for subject in subjects: es.index( index='entity', doc_type='subject', body=json_prep(subject), id=convert_uuid_to_slug(subject['entity_id']), ) """ TODO-1 fix these # Add topics topics = r.table('topics').run(db_conn) for topic in topics: es.index( index='entity', doc_type='topic', body=json_prep(topic), id=topic['id'], ) # Add posts posts = r.table('posts').run(db_conn) for post in posts: data = json_prep(post) topic = (r.table('topics') .get(data['topic_id']) .run(db_conn)) user = (r.table('users') .get(data['user_id']) .run(db_conn)) data['topic'] = json_prep(topic) data['user'] = pick(json_prep(user), ('id', 'name')) es.index( index='entity', doc_type='post', body=data, id=post['id'], ) """ close_db_connection(db_conn)
def test_make_and_close_db_connection(): db_conn = make_db_connection() assert not isinstance(db_conn, str) close_db_connection(db_conn)