py2neo и запрос фляги

python flask neo4j py2neo

224 просмотра

1 ответ

У меня есть проблема в моем приложении, я пытаюсь запустить код в колбу с py2neo. У меня последняя версия 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)

Я звоню из моего файла представления, и я импортировал пользовательский класс в файл представления, но когда я запускаю эту страницу, то это показывает ошибку

это код для просмотра файла

@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 -
Автор: csr Источник Размещён: 08.11.2019 11:01

Ответы (1)


0 плюса

Решение

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

Спасибо КСО

Автор: csr Размещён: 23.08.2016 06:27
Вопросы из категории :
32x32