reconnect after disconnect
This commit is contained in:
parent
a56cf12c2a
commit
02ab93fc53
16
app.py
16
app.py
|
|
@ -1,23 +1,15 @@
|
||||||
import os
|
|
||||||
from flask import Flask, render_template, jsonify
|
from flask import Flask, render_template, jsonify
|
||||||
from rethinkdb import RethinkDB
|
|
||||||
from routes.conditions import create_conditions_routes
|
from routes.conditions import create_conditions_routes
|
||||||
|
from db import get_connection, getRethinkDB
|
||||||
rethinkUrl = os.environ.get('RETHINKDB_URL')
|
|
||||||
rethinkPort = os.environ.get('RETHINKDB_PORT')
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
r = getRethinkDB()
|
||||||
|
|
||||||
# Connect to RethinkDB
|
create_conditions_routes(app)
|
||||||
r = RethinkDB()
|
|
||||||
conn = r.connect(rethinkUrl, rethinkPort, db='finfree')
|
|
||||||
|
|
||||||
create_conditions_routes(app, r, conn)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
cursor = r.table("conditions").run(conn)
|
cursor = r.table("conditions").run(get_connection())
|
||||||
conditions = list(cursor)
|
conditions = list(cursor)
|
||||||
return render_template('index.html', data={"conditions": conditions })
|
return render_template('index.html', data={"conditions": conditions })
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
import os
|
||||||
|
from rethinkdb import RethinkDB
|
||||||
|
|
||||||
|
rethinkUrl = os.environ.get('RETHINKDB_URL')
|
||||||
|
rethinkPort = os.environ.get('RETHINKDB_PORT')
|
||||||
|
rethinkdb = "finfree"
|
||||||
|
|
||||||
|
r = RethinkDB()
|
||||||
|
|
||||||
|
connection = None
|
||||||
|
|
||||||
|
def get_connection():
|
||||||
|
global connection
|
||||||
|
if connection is None or not connection.is_open():
|
||||||
|
connection = r.connect(rethinkUrl, rethinkPort, db=rethinkdb)
|
||||||
|
|
||||||
|
return connection
|
||||||
|
|
||||||
|
def getRethinkDB():
|
||||||
|
global r
|
||||||
|
return r
|
||||||
|
|
@ -1,20 +1,21 @@
|
||||||
from flask import jsonify, request, redirect, render_template
|
from flask import jsonify, request, redirect, render_template
|
||||||
import rethinkdb as r
|
from db import get_connection, getRethinkDB
|
||||||
|
|
||||||
table_name = 'conditions'
|
table_name = 'conditions'
|
||||||
route = table_name
|
route = table_name
|
||||||
|
r = getRethinkDB()
|
||||||
|
|
||||||
def create_conditions_routes(app, r, conn):
|
def create_conditions_routes(app):
|
||||||
# Create a table (if not exists)
|
# Create a table (if not exists)
|
||||||
if table_name not in r.table_list().run(conn):
|
if table_name not in r.table_list().run(get_connection()):
|
||||||
r.table_create(table_name).run(conn)
|
r.table_create(table_name).run(get_connection())
|
||||||
|
|
||||||
# Routes for CRUD operations
|
# Routes for CRUD operations
|
||||||
|
|
||||||
# Create operation
|
# Create operation
|
||||||
@app.route('/' + route + '/create', methods=['GET'])
|
@app.route('/' + route + '/create', methods=['GET'])
|
||||||
def create_form():
|
def create_form():
|
||||||
return render_template('conditions/create.html')
|
return render_template(route + '/create.html')
|
||||||
|
|
||||||
@app.route('/' + route, methods=['POST'])
|
@app.route('/' + route, methods=['POST'])
|
||||||
def create_condition():
|
def create_condition():
|
||||||
|
|
@ -22,24 +23,25 @@ def create_conditions_routes(app, r, conn):
|
||||||
data = request.json
|
data = request.json
|
||||||
else: # Assuming form data is in 'application/x-www-form-urlencoded' format
|
else: # Assuming form data is in 'application/x-www-form-urlencoded' format
|
||||||
data = request.form.to_dict()
|
data = request.form.to_dict()
|
||||||
result = r.table(table_name).insert(data).run(conn)
|
|
||||||
|
result = r.table(table_name).insert(data).run(get_connection())
|
||||||
return redirect(request.referrer or url_for('index'))
|
return redirect(request.referrer or url_for('index'))
|
||||||
|
|
||||||
# Read operation
|
# Read operation
|
||||||
@app.route('/' + route, methods=['GET'])
|
@app.route('/' + route, methods=['GET'])
|
||||||
def get_conditions():
|
def get_conditions():
|
||||||
cursor = r.table(table_name).run(conn)
|
cursor = r.table(table_name).run(get_connection())
|
||||||
conditions = list(cursor)
|
conditions = list(cursor)
|
||||||
return jsonify(conditions)
|
return jsonify(conditions)
|
||||||
|
|
||||||
@app.route('/' + route + '/<id>', methods=['GET'])
|
@app.route('/' + route + '/<id>', methods=['GET'])
|
||||||
def get_condition(id):
|
def get_condition(id):
|
||||||
cursor = r.table(table_name).get(id).run(conn)
|
cursor = r.table(table_name).get(id).run(get_connection())
|
||||||
json = jsonify(cursor)
|
json = jsonify(cursor)
|
||||||
if request.headers.get('Content-Type') == 'application/json':
|
if request.headers.get('Content-Type') == 'application/json':
|
||||||
return json
|
return json
|
||||||
else:
|
else:
|
||||||
return render_template('conditions/update.html', data=cursor)
|
return render_template(route + '/update.html', data=cursor)
|
||||||
|
|
||||||
# Update operation
|
# Update operation
|
||||||
@app.route('/' + route + '/<id>', methods=['POST'])
|
@app.route('/' + route + '/<id>', methods=['POST'])
|
||||||
|
|
@ -48,16 +50,16 @@ def create_conditions_routes(app, r, conn):
|
||||||
data = request.json
|
data = request.json
|
||||||
else:
|
else:
|
||||||
data = request.form.to_dict()
|
data = request.form.to_dict()
|
||||||
r.table(table_name).get(id).update(data).run(conn)
|
r.table(table_name).get(id).update(data).run(get_connection())
|
||||||
return "<script>window.close();</script>"
|
return "<script>window.close();</script>"
|
||||||
|
|
||||||
# Delete operation
|
# Delete operation
|
||||||
@app.route('/' + route + '/delete/<id>', methods=['POST'])
|
@app.route('/' + route + '/delete/<id>', methods=['POST'])
|
||||||
def delete_post(id):
|
def delete_post(id):
|
||||||
r.table(table_name).get(id).delete().run(conn)
|
r.table(table_name).get(id).delete().run(get_connection())
|
||||||
return jsonify({'message': 'condition deleted successfully'})
|
return jsonify({'message': 'condition deleted successfully'})
|
||||||
|
|
||||||
@app.route('/' + route + '/<id>', methods=['DELETE'])
|
@app.route('/' + route + '/<id>', methods=['DELETE'])
|
||||||
def delete_condition(id):
|
def delete_condition(id):
|
||||||
r.table(table_name).get(id).delete().run(conn)
|
r.table(table_name).get(id).delete().run(get_connection())
|
||||||
return jsonify({'message': 'condition deleted successfully'})
|
return jsonify({'message': 'condition deleted successfully'})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue