itetteh

app.py

Apr 19th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. import MySQLdb as mdb
  2. from view import *
  3. from flask import Flask,render_template, flash,url_for,redirect,request,jsonify
  4. from flask.ext.bootstrap import Bootstrap
  5.  
  6. app= Flask(__name__)
  7. app.config['SECRET_KEY'] = 'Good luck Trying to Guess my password'
  8. bootstrap = Bootstrap(app)
  9.  
  10. def connection():
  11. conn = mdb.connect(host='localhost',user='root',passwd='iloveGod123',db='GALAXZY')
  12. c=conn.cursor()
  13. return c,conn
  14.  
  15. @app.route('/', methods=['GET', 'POST'])
  16. def index():
  17. form = NameForm(prefix = "form")
  18. form1 = CountryForm(prefix= "form1")
  19. form2 = CountyForm(prefix = "form2")
  20. form3 = CityForm(prefix = "form3")
  21.  
  22. if form.validate_on_submit():
  23. Cont_Name = form.cont_Name.data
  24. Cont_Pop = form.cont_pop.data
  25. Cont_Area = form.cont_area.data
  26. Planet_Name = form.planet_Name.data
  27. try:
  28. c,conn = connection()
  29. c.execute("INSERT INTO Continent (Cont_Name,Cont_Pop, Cont_Area,Planet_Name) VALUES (%s,%s,%s,%s)",(Cont_Name,Cont_Pop,Cont_Area,Planet_Name))
  30. c.execute("INSERT INTO Planets (Planet_Name,Continents) VALUES (%s,%s)",(Planet_Name,Cont_Name))
  31. flash("Data has been inserted")
  32. except mdb.IntegrityError as e:
  33. flash('The Continent Name "%s" already exits in Database' % form.cont_Name.data)
  34. finally:
  35. conn.commit()
  36. c.close()
  37. return redirect(url_for('index'))
  38.  
  39. elif form1.validate_on_submit():
  40. Cont_Name = form1.cont_Name.data
  41. Country_Name = form1.country_Name.data
  42. Country_Pop = form1.country_pop.data
  43. Country_Area = form1.country_area.data
  44. try:
  45. c,conn = connection()
  46. c.execute("INSERT INTO Country (Country_Name,Country_Pop, Country_Area,Cont_Name) VALUES (%s,%s,%s,%s)",(Country_Name,Country_Pop,Country_Area,Cont_Name))
  47. flash("Data has been inserted")
  48. except mdb.IntegrityError as e:
  49. flash('The Country Name "%s" already exits in Database' % form1.cont_Name.data)
  50. finally:
  51. conn.commit()
  52. c.close()
  53. return redirect(url_for('index'))
  54.  
  55. elif form2.validate_on_submit():
  56. County_Name = form2.count_Name.data
  57. County_Pop = form2.count_pop.data
  58. County_Area = form2.count_area.data
  59. try:
  60. c,conn = connection()
  61. c.execute("INSERT INTO County (Cont_Name,Cont_Pop, Cont_Area) VALUES (%s,%s,%s)",(County_Name,County_Pop,County_Area))
  62. flash("Data has been inserted")
  63. except mdb.IntegrityError as e:
  64. flash('The County Name "%s" already exits in Database' % form2.count_Name.data)
  65. finally:
  66. conn.commit()
  67. c.close()
  68. return redirect(url_for('index'))
  69.  
  70. elif form3.validate_on_submit():
  71. Town_Name = form3.town_Name.data
  72. City_State = form3.city_state.data
  73. City_Name = form3.city_Name.data
  74. City_Pop = form3.city_pop.data
  75. City_Area = form3.city_area.data
  76. try:
  77. c,conn = connection()
  78. c.execute("INSERT INTO City (City_Name,City_Pop, City_Area,City_State,Town_Name) VALUES (%s,%s,%s,%s,%s)",(City_Name,City_Pop,City_Area,City_State,Town_Name))
  79. flash("Data has been inserted")
  80. except mdb.IntegrityError as e:
  81. # flash('The City Name %(1) and State Name %(2) are already exits in Database ' %{"1":form3.city_Name.data, "2":form3.city_state.data})
  82. flash('The City Name "%s" already exits in Database' % form3.city_Name.data)
  83. finally:
  84. conn.commit()
  85. c.close()
  86. return redirect(url_for('index'))
  87.  
  88. return render_template('main.html',form=form,form1=form1,form2=form2,form3=form3)
  89.  
  90.  
  91. @app.route("/delete", methods=['POST','GET'])
  92. def deletes():
  93. formA= DeleteContinent(prefix = "formA")
  94. form1A = DeleteCountry(prefix= "form1A")
  95. form2A = DeleteCounty(prefix = "form2A")
  96. form3A = DeleteCity(prefix = "form3A")
  97. if formA.validate_on_submit():#request.method=='POST':
  98. cont_key = formA.del_cont.data
  99. try:
  100. c,conn = connection()
  101. c.execute('delete FROM Continent where Cont_Name = %s',(cont_key,))
  102. flash("All rows deleted")
  103. except:
  104. flash('The row with value "%s" is not found in the table' %formA.del_cont.data)
  105. finally:
  106. conn.commit()
  107. c.close()
  108. return redirect(url_for('deletes'))
  109.  
  110. elif form1A.validate_on_submit():#request.method=='POST':
  111. count_key = form1A.del_count.data
  112. try:
  113. c,conn = connection()
  114. c.execute('delete FROM Country where Country_Name = %s',(count_key,))
  115. except:
  116. flash('The row with value "%s" is not found in the table' %form1A.count_key.data)
  117. finally:
  118. conn.commit()
  119. c.close()
  120. return redirect(url_for('deletes'))
  121.  
  122. elif form2A.validate_on_submit():#request.method=='POST':
  123. conty_key = form2A.del_conty.data
  124. try:
  125. c,conn = connection()
  126. c.execute('delete FROM County where Cont_Name = %s',(conty_key,))
  127. flash("All rows deleted")
  128. except:
  129. flash('The row with value "%s" is not found in the table' %form2A.conty_key.data)
  130. finally:
  131. conn.commit()
  132. c.close()
  133. return redirect(url_for('deletes'))
  134.  
  135. elif form3A.validate_on_submit():#request.method=='POST':
  136. city_key = form3A.del_city.data
  137. try:
  138. c,conn = connection()
  139. c.execute('delete FROM City where City_Name = %s',(city_key,))
  140. flash("All rows deleted")
  141. except:
  142. flash('The row with value "%s" is not found in the table' %formA.city_key.data)
  143. finally:
  144. conn.commit()
  145. c.close()
  146. return redirect(url_for('deletes'))
  147.  
  148. return render_template("delete.html",formA=formA,form1A=form1A,form2A=form2A,form3A=form3A)
  149.  
  150. @app.route("/update")
  151. def updates():
  152. return render_template("update.html")
  153.  
  154. @app.route("/viewSingle", methods=['POST','GET'])
  155. def SingleView():
  156. if request.method=="POST":
  157. if request.form['submit']=="View CONTINENT":
  158. c,conn = connection()
  159. c.execute('''select * from Country''')
  160. data = c.fetchall ()
  161. c.close ()
  162. conn.close ()
  163. return render_template("view.html",data=data)
  164.  
  165. @app.route("/viewAll")
  166. def viewsAll():
  167. c,conn = connection()
  168. c.execute('''select * from Country''')
  169. #data=c.fetchall()
  170. #data1=json.dumps(data)
  171.  
  172. return str([columns[0] for columns in c.fetchall()])
  173. #return str(a)
  174. c.close()
  175. conn.close()
  176. #return a,b,c,d
  177. #render_template("view.html")
  178.  
  179. if __name__ == "__main__":
  180. app.run(debug = True,host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment