簡體   English   中英

py2neo和flask查詢

[英]py2neo and flask query

我的應用程序出現問題,我正在嘗試使用py2neo在flask中運行代碼。 我有最新版本的NEO4j和python2.7

這是我在USER類中的功能代碼

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)

我正在從視圖文件中調用,並且在視圖文件中導入了用戶類,但是當我運行此頁面時,它將顯示錯誤

這是代碼f查看文件

@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)

當我在頁面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 -

在python 2.7和py2neo版本3中,我們不能使用這樣的查詢,我們需要這樣的查詢

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)

感謝企業社會責任

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM