public
Created

testing of Flask and Neo4j

  • Download Gist
gistfile1.pytb
Python traceback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
import os
import jpype
from neo4j import GraphDatabase
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
 
app = Flask(__name__)
app.config.from_object(__name__)
app.config['ADMIN'] ='Matthew'
 
app.config['DATABASE']='/tmp/graphtest'
 
def connectDB():
return GraphDatabase(app.config['DATABASE'])
 
 
def initDB():
db = connectDB()
 
with db.transaction:
users = db.node()
roles = db.node()
 
db.reference_node.USERS(users)
db.reference_node.ROLES(roles)
 
userIndex = db.node.indexes.create(users)
 
user = db.node(name=app.config['ADMIN'])
user.INSTANCE_OF(users)
userIndex['name'][app.config['ADMIN']] = user
 
role = db.node(type='superadmin')
role.INSTANCE_OF(roles)
 
role.ASSIGN_TO(user)
 
db.shutdown()
 
print "Database initialized."
 
 
def testDB():
db = connectDB()
 
with db.transaction:
userIndex = db.node.indexes.get('users')
user = userIndex['name'][app.config['ADMIN']].single
username = user['name']
 
db.shutdown()
 
print "Admin username is '%s'. Database exists." % username
 
 
@app.before_request
def before_request():
jpype.attachThreadToJVM()
g.db = connectDB()
 
 
@app.teardown_request
def teardown_request(exception):
g.db.shutdown()
 
 
@app.route('/')
def index():
 
with g.db.transaction:
userIndex = g.db.node.indexes.get('users')
user = userIndex['name'][app.config['ADMIN']].single
username = user['name']
 
fields = dict(username=username)
return str(fields)
# return render_template('index.html', fields=fields)
 
 
if os.path.exists(app.config['DATABASE']) == False:
initDB()
else:
testDB()
 
 
if __name__ == "__main__":
app.run()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.