Advertisement
mamamaria

Flask

Oct 5th, 2021
1,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. @app.route("/question", methods=['POST', 'GET'])
  2. def question():
  3.     if 'username' not in session:
  4.         return redirect(url_for('login'))
  5.        
  6.     form_text = '''
  7.        <p>Question form:</p>
  8.        <form method="post">
  9.            <label for="theme">Theme:</label><br>
  10.            <input type="text" id="theme" name="theme"><br>
  11.            <label for="question">Question:</label><br>
  12.            <textarea id="question" name="question"></textarea>
  13.            <br>
  14.            <input type="submit" value="Submit">
  15.        </form>
  16.        '''
  17.     if request.method == 'POST':
  18.         title = request.form['title']
  19.         question = request.form['question']
  20.         username = session['username']
  21.  
  22.         with get_db().session() as db:
  23.            
  24.             while (True):
  25.                 id = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
  26.                 result = db.run(
  27.                 r'MATCH (Q:Question {id: $id}) RETURN Q',
  28.                 id = id
  29.                 ).single()
  30.                 if result is None:
  31.                     break
  32.             db.run(
  33.                 r'MATCH (U:User {username:$username}) CREATE (Q:Question {title:$title, question:$question, id: $id}) <-[r:ASKED]-(U)',
  34.                 title=title, question=question, username = username, id=id
  35.             )
  36.    
  37.     return form_text
  38.  
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement