Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. Traceback (most recent call last):
  2. File "app.py", line 36, in <module>
  3. @app.route('/bot')
  4. File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/flask/app.py", line 1250, in decorator
  5. self.add_url_rule(rule, endpoint, f, **options)
  6. File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/flask/app.py", line 66, in wrapper_func
  7. return f(self, *args, **kwargs)
  8. File "/home/mike/Programing/Rasa/myflaskapp/myFlaskAppenv/lib/python3.5/site-packages/flask/app.py", line 1221, in add_url_rule
  9. 'existing endpoint function: %s' % endpoint)
  10. AssertionError: View function mapping is overwriting an existing endpoint function: about
  11.  
  12. from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
  13. #from data import Articles
  14. from flask_mysqldb import MySQL
  15. from wtforms import Form, StringField, TextAreaField, PasswordField, validators
  16. from passlib.hash import sha256_crypt
  17. from functools import wraps
  18.  
  19. #for chatbot
  20. import random
  21.  
  22. app = Flask(__name__)
  23.  
  24. # Config MySQL
  25. app.config['MYSQL_HOST'] = 'localhost'
  26. app.config['MYSQL_USER'] = 'root'
  27. app.config['MYSQL_PASSWORD'] = 'root'
  28. app.config['MYSQL_DB'] = 'myflaskapp'
  29. app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
  30. # init MYSQL
  31. mysql = MySQL(app)
  32.  
  33. #Articles = Articles()
  34.  
  35. # Index
  36. @app.route('/')
  37. def index():
  38. return render_template('home.html')
  39.  
  40.  
  41. # About
  42. @app.route('/about')
  43. def about():
  44. return render_template('about.html')
  45.  
  46. # About
  47. @app.route('/bot')
  48. def about():
  49. return render_template('bot.html')
  50.  
  51.  
  52. # Articles
  53. @app.route('/articles')
  54. def articles():
  55. # Create cursor
  56. cur = mysql.connection.cursor()
  57.  
  58. # Get articles
  59. result = cur.execute("SELECT * FROM articles")
  60.  
  61. articles = cur.fetchall()
  62.  
  63. if result > 0:
  64. return render_template('articles.html', articles=articles)
  65. else:
  66. msg = 'No Articles Found'
  67. return render_template('articles.html', msg=msg)
  68. # Close connection
  69. cur.close()
  70.  
  71.  
  72. #Single Article
  73. @app.route('/article/<string:id>/')
  74. def article(id):
  75. # Create cursor
  76. cur = mysql.connection.cursor()
  77.  
  78. # Get article
  79. result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
  80.  
  81. article = cur.fetchone()
  82.  
  83. return render_template('article.html', article=article)
  84.  
  85.  
  86. # Register Form Class
  87. class RegisterForm(Form):
  88. name = StringField('Name', [validators.Length(min=1, max=50)])
  89. username = StringField('Username', [validators.Length(min=4, max=25)])
  90. email = StringField('Email', [validators.Length(min=6, max=50)])
  91. password = PasswordField('Password', [
  92. validators.DataRequired(),
  93. validators.EqualTo('confirm', message='Passwords do not match')
  94. ])
  95. confirm = PasswordField('Confirm Password')
  96.  
  97.  
  98. # User Register
  99. @app.route('/register', methods=['GET', 'POST'])
  100. def register():
  101. form = RegisterForm(request.form)
  102. if request.method == 'POST' and form.validate():
  103. name = form.name.data
  104. email = form.email.data
  105. username = form.username.data
  106. password = sha256_crypt.encrypt(str(form.password.data))
  107.  
  108. # Create cursor
  109. cur = mysql.connection.cursor()
  110.  
  111. # Execute query
  112. cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
  113.  
  114. # Commit to DB
  115. mysql.connection.commit()
  116.  
  117. # Close connection
  118. cur.close()
  119.  
  120. flash('You are now registered and can log in', 'success')
  121.  
  122. return redirect(url_for('login'))
  123. return render_template('register.html', form=form)
  124.  
  125.  
  126. # User login
  127. @app.route('/login', methods=['GET', 'POST'])
  128. def login():
  129. if request.method == 'POST':
  130. # Get Form Fields
  131. username = request.form['username']
  132. password_candidate = request.form['password']
  133.  
  134. # Create cursor
  135. cur = mysql.connection.cursor()
  136.  
  137. # Get user by username
  138. result = cur.execute("SELECT * FROM users WHERE username = %s", [username])
  139.  
  140. if result > 0:
  141. # Get stored hash
  142. data = cur.fetchone()
  143. password = data['password']
  144.  
  145. # Compare Passwords
  146. if sha256_crypt.verify(password_candidate, password):
  147. # Passed
  148. session['logged_in'] = True
  149. session['username'] = username
  150.  
  151. flash('You are now logged in', 'success')
  152. return redirect(url_for('dashboard'))
  153. else:
  154. error = 'Invalid login'
  155. return render_template('login.html', error=error)
  156. # Close connection
  157. cur.close()
  158. else:
  159. error = 'Username not found'
  160. return render_template('login.html', error=error)
  161.  
  162. return render_template('login.html')
  163.  
  164. # Check if user logged in
  165. def is_logged_in(f):
  166. @wraps(f)
  167. def wrap(*args, **kwargs):
  168. if 'logged_in' in session:
  169. return f(*args, **kwargs)
  170. else:
  171. flash('Unauthorized, Please login', 'danger')
  172. return redirect(url_for('login'))
  173. return wrap
  174.  
  175. # Logout
  176. @app.route('/logout')
  177. @is_logged_in
  178. def logout():
  179. session.clear()
  180. flash('You are now logged out', 'success')
  181. return redirect(url_for('login'))
  182.  
  183. # Dashboard
  184. @app.route('/dashboard')
  185. @is_logged_in
  186. def dashboard():
  187. # Create cursor
  188. cur = mysql.connection.cursor()
  189.  
  190. # Get articles
  191. result = cur.execute("SELECT * FROM articles")
  192.  
  193. articles = cur.fetchall()
  194.  
  195. if result > 0:
  196. return render_template('dashboard.html', articles=articles)
  197. else:
  198. msg = 'No Articles Found'
  199. return render_template('dashboard.html', msg=msg)
  200. # Close connection
  201. cur.close()
  202.  
  203. # Article Form Class
  204. class ArticleForm(Form):
  205. title = StringField('Title', [validators.Length(min=1, max=200)])
  206. body = TextAreaField('Body', [validators.Length(min=30)])
  207.  
  208. # Add Article
  209. @app.route('/add_article', methods=['GET', 'POST'])
  210. @is_logged_in
  211. def add_article():
  212. form = ArticleForm(request.form)
  213. if request.method == 'POST' and form.validate():
  214. title = form.title.data
  215. body = form.body.data
  216.  
  217. # Create Cursor
  218. cur = mysql.connection.cursor()
  219.  
  220. # Execute
  221. cur.execute("INSERT INTO articles(title, body, author) VALUES(%s, %s, %s)",(title, body, session['username']))
  222.  
  223. # Commit to DB
  224. mysql.connection.commit()
  225.  
  226. #Close connection
  227. cur.close()
  228.  
  229. flash('Article Created', 'success')
  230.  
  231. return redirect(url_for('dashboard'))
  232.  
  233. return render_template('add_article.html', form=form)
  234.  
  235.  
  236. # Edit Article
  237. @app.route('/edit_article/<string:id>', methods=['GET', 'POST'])
  238. @is_logged_in
  239. def edit_article(id):
  240. # Create cursor
  241. cur = mysql.connection.cursor()
  242.  
  243. # Get article by id
  244. result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
  245.  
  246. article = cur.fetchone()
  247. cur.close()
  248. # Get form
  249. form = ArticleForm(request.form)
  250.  
  251. # Populate article form fields
  252. form.title.data = article['title']
  253. form.body.data = article['body']
  254.  
  255. if request.method == 'POST' and form.validate():
  256. title = request.form['title']
  257. body = request.form['body']
  258.  
  259. # Create Cursor
  260. cur = mysql.connection.cursor()
  261. app.logger.info(title)
  262. # Execute
  263. cur.execute ("UPDATE articles SET title=%s, body=%s WHERE id=%s",(title, body, id))
  264. # Commit to DB
  265. mysql.connection.commit()
  266.  
  267. #Close connection
  268. cur.close()
  269.  
  270. flash('Article Updated', 'success')
  271.  
  272. return redirect(url_for('dashboard'))
  273.  
  274. return render_template('edit_article.html', form=form)
  275.  
  276. # Delete Article
  277. @app.route('/delete_article/<string:id>', methods=['POST'])
  278. @is_logged_in
  279. def delete_article(id):
  280. # Create cursor
  281. cur = mysql.connection.cursor()
  282.  
  283. # Execute
  284. cur.execute("DELETE FROM articles WHERE id = %s", [id])
  285.  
  286. # Commit to DB
  287. mysql.connection.commit()
  288.  
  289. #Close connection
  290. cur.close()
  291.  
  292. flash('Article Deleted', 'success')
  293.  
  294. return redirect(url_for('dashboard'))
  295.  
  296. @app.route('/chat',methods=["POST"])
  297. def chat():
  298. try:
  299. user_message = request.form["text"]
  300. response = requests.get("http://localhost:5000/parse",params={"q":user_message})
  301. response = response.json()
  302. entities = response.get("entities")
  303. topresponse = response["topScoringIntent"]
  304. intent = topresponse.get("intent")
  305. print("Intent {}, Entities {}".format(intent,entities))
  306. if intent == "gst-info":
  307. response_text = gst_info(entities)# "Sorry will get answer soon" #get_event(entities["day"],entities["time"],entities["place"])
  308. elif intent == "gst-query":
  309. response_text = gst_query(entities)
  310. else:
  311. response_text = get_random_response(intent)
  312. return jsonify({"status":"success","response":response_text})
  313. except Exception as e:
  314. print("HOUSTON ! WE GOT AN EXCETPITON !")
  315. print(e)
  316. return jsonify({"status":"success","response":"Sorry I am not trained to do that yet..."})
  317.  
  318. if __name__ == '__main__':
  319. app.secret_key='Es un secret'
  320. app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement