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 rethinkdb import RethinkDB
|
||||
from routes.conditions import create_conditions_routes
|
||||
|
||||
rethinkUrl = os.environ.get('RETHINKDB_URL')
|
||||
rethinkPort = os.environ.get('RETHINKDB_PORT')
|
||||
from db import get_connection, getRethinkDB
|
||||
|
||||
app = Flask(__name__)
|
||||
r = getRethinkDB()
|
||||
|
||||
# Connect to RethinkDB
|
||||
r = RethinkDB()
|
||||
conn = r.connect(rethinkUrl, rethinkPort, db='finfree')
|
||||
|
||||
create_conditions_routes(app, r, conn)
|
||||
|
||||
create_conditions_routes(app)
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
cursor = r.table("conditions").run(conn)
|
||||
cursor = r.table("conditions").run(get_connection())
|
||||
conditions = list(cursor)
|
||||
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
|
||||
import rethinkdb as r
|
||||
from db import get_connection, getRethinkDB
|
||||
|
||||
table_name = 'conditions'
|
||||
route = table_name
|
||||
r = getRethinkDB()
|
||||
|
||||
def create_conditions_routes(app, r, conn):
|
||||
def create_conditions_routes(app):
|
||||
# Create a table (if not exists)
|
||||
if table_name not in r.table_list().run(conn):
|
||||
r.table_create(table_name).run(conn)
|
||||
if table_name not in r.table_list().run(get_connection()):
|
||||
r.table_create(table_name).run(get_connection())
|
||||
|
||||
# Routes for CRUD operations
|
||||
|
||||
# Create operation
|
||||
@app.route('/' + route + '/create', methods=['GET'])
|
||||
def create_form():
|
||||
return render_template('conditions/create.html')
|
||||
return render_template(route + '/create.html')
|
||||
|
||||
@app.route('/' + route, methods=['POST'])
|
||||
def create_condition():
|
||||
|
|
@ -22,24 +23,25 @@ def create_conditions_routes(app, r, conn):
|
|||
data = request.json
|
||||
else: # Assuming form data is in 'application/x-www-form-urlencoded' format
|
||||
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'))
|
||||
|
||||
# Read operation
|
||||
@app.route('/' + route, methods=['GET'])
|
||||
def get_conditions():
|
||||
cursor = r.table(table_name).run(conn)
|
||||
cursor = r.table(table_name).run(get_connection())
|
||||
conditions = list(cursor)
|
||||
return jsonify(conditions)
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['GET'])
|
||||
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)
|
||||
if request.headers.get('Content-Type') == 'application/json':
|
||||
return json
|
||||
else:
|
||||
return render_template('conditions/update.html', data=cursor)
|
||||
return render_template(route + '/update.html', data=cursor)
|
||||
|
||||
# Update operation
|
||||
@app.route('/' + route + '/<id>', methods=['POST'])
|
||||
|
|
@ -48,16 +50,16 @@ def create_conditions_routes(app, r, conn):
|
|||
data = request.json
|
||||
else:
|
||||
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>"
|
||||
|
||||
# Delete operation
|
||||
@app.route('/' + route + '/delete/<id>', methods=['POST'])
|
||||
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'})
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['DELETE'])
|
||||
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'})
|
||||
|
|
|
|||
Loading…
Reference in New Issue