Neonian

app.py

May 10th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. from bottle import route, run, template
  2. import jsonpickle
  3. import mysql.connector
  4.  
  5. import requests
  6.  
  7.  
  8. class Object:
  9. def to_JSON(self):
  10. return json.dumps(self, default=lambda o: o.__dict__,
  11. sort_keys=True, indent=4)
  12. def resultJSON(message,result):
  13.  
  14. json=Object()
  15. json.message=message
  16. json.data=[]
  17.  
  18. for row in result:
  19. rowd=Object()
  20. rowd.student_id=row[0]
  21. rowd.lat=row[1]
  22. rowd.long=row[2]
  23. json.data.append(rowd)
  24.  
  25.  
  26. return jsonpickle.encode(json,unpicklable=False)
  27.  
  28. @route('/hello/:name')
  29. def index(name='World'):
  30. cnx=False
  31. result=[]
  32. try:
  33. cnx = mysql.connector.connect(user='Ralph2', password='CMT3313Ral!',
  34. host='Ralph2.db.11698469.hostedresource.com',
  35. database='Ralph2')
  36. cursor = cnx.cursor()
  37. cursor.execute("""
  38. SELECT * FROM StudentPosition
  39. """)
  40. result = cursor.fetchall()
  41.  
  42.  
  43.  
  44.  
  45. finally:
  46. if (cnx):
  47. return resultJSON("Successfully retrieved "+name,result)
  48. cnx.close()
  49. else:
  50. return resultJSON("Failed to retrieve data",result);
  51.  
  52.  
  53. ################################################################################
  54. @route('/login')
  55. def login():
  56. r = requests.get("http://buildapp.co.uk/StudentCloud/v/2/api/v1/?url=drive/files/-1")
  57. return r.content
  58.  
  59. run(host='localhost', port=8082)
Add Comment
Please, Sign In to add comment