I have issue in my application, I am trying to run a code in flask with py2neo. I have latest version of NEO4j and python2.7
Here is my code for fucntion in USER class
class User:
def __init__(self, username):
self.username = username
def find(self):
user = graph.find_one("User", "username", self.username)
def add_challenge(self,challenge_title,total_question_per_user,challengecat,percentage_question,prize,ranks,challenge_status):
query = '''
MATCH (u:User),(p:Prize),(ca:Category)
WHERE u.username = {username} and p.pid = {prize} and ca.catname = {challengecat}
CREATE (ch:Challenge {chid: str(uuid.uuid4()),challenge_title: {challenge_title}, total_question_per_user: {total_question_per_user},challenge_status: {challenge_status},timestamp:timestamp(),date:date()}),
(p)-[:BELONG {rank: {ranks} }]->(ch),(ca)-[:BELONG {percentage_question: {percentage_question} }]->(ch)
'''
return graph.run(query,username=self.username,prize=prize,challengecat=challengecat,challenge_title=challenge_title,total_question_per_user=total_question_per_user,challenge_status=challenge_status,ranks=ranks,percentage_question=percentage_question)
I am calling from my view file and i imported user class in view file but when i run this page then it show error
this is code f view file
@app.route('/admin/add/challenge', methods = ['GET', 'POST'])
def admin_add_challenge():
if not session.get('username'):
return redirect(url_for('admin_login'))
if request.method == 'POST':
challenge_title = request.form['challenge_title']
total_question_per_user = request.form['total_question_per_user']
challengecat = request.form['challengecat']
percentage_question = request.form['percentage_question']
prize = request.form['prize']
ranks = request.form['ranks']
challenge_status = request.form['challenge_status']
if not challenge_title or not total_question_per_user or not ranks:
if not challenge_title:
flash('Please Enter Challenge')
if not total_question_per_user:
flash('Please Enter Number of question Per Player')
if not ranks:
flash('Please Enter Ranks for win this Challenge')
else:
User(session['username']).add_challenge(challenge_title,total_question_per_user,challengecat,percentage_question,prize,ranks,challenge_status)
flash('Challenge Added successfully')
return redirect(url_for('admin_add_challenge'))
categories = get_categories()
prizes = get_prizes()
return render_template('admin/admin_add_challenge.html',categories=categories,prizes=prizes)
Here is error when i submit form of challenge on page http://sitename/admin/add/challenge
ERROR in app: Exception on /admin/add/challenge [POST]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/gamepro/ddqcore/views.py", line 430, in admin_add_challenge
User(session['username']).add_challenge(challenge_title,total_question_per_user,challengecat,percentage_question,prize,ranks,challenge_status)
File "/root/gamepro/ddqcore/models.py", line 285, in add_challenge
return graph.run(query,username=self.username,prize=prize,challengecat=challengecat,challenge_title=challenge_title,total_question_per_user=total_question_per_user,challenge_status=challenge_status,ranks=ranks,percentage_question=percentage_question)
File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 731, in run
return self.begin(autocommit=True).run(statement, parameters, **kwparameters)
File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 1277, in run
self.finish()
File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 1296, in finish
self._sync()
File "/usr/local/lib/python2.7/site-packages/py2neo/database/__init__.py", line 1286, in _sync
connection.fetch()
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 337, in fetch
self.acknowledge_failure()
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 284, in acknowledge_failure
fetch()
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 337, in fetch
self.acknowledge_failure()
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 284, in acknowledge_failure
fetch()
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 322, in fetch
raw.writelines(self.channel.chunk_reader())
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 173, in chunk_reader
chunk_header = self._recv(2)
File "/usr/local/lib/python2.7/site-packages/py2neo/packages/neo4j/v1/bolt.py", line 156, in _recv
raise ProtocolError("Server closed connection")
ProtocolError: Server closed connection
49.32.44.55 - - [20/Aug/2016 06:49:05] "POST /admin/add/challenge HTTP/1.1" 500 -
In python 2.7 and py2neo version 3, we can not use query like this, we need to query like this one
selector = NodeSelector(graph)
selected_user = selector.select("User", username=user)
selected_prize = selector.select("Prize", pid=prize)
selected_cat = selector.select("Category",catname = challengecat)
challenge = Node("Challenge",chid=str(uuid.uuid4()),challenge_title=challenge_title,total_question_per_user=total_question_per_user,challenge_status=challenge_status,timestamp=timestamp(),date=date())
rel = Relationship(selected_user,"ADDED",challenge)
rel1 = Relationship(selected_prize,"BELONG",challenge)
rel2 = Relationship(selected_cat,"BELONG",challenge)
graph.create(rel)
graph.create(rel1)
graph.create(rel2)
Thanks CSR