Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. from bottle import get, post, run, route, request, response
  2. import psycopg2, json
  3. from db import *
  4.  
  5. def checkcredentials(username, password):
  6. cur = conn.cursor()
  7. cur.execute("SELECT password = crypt(%s, password) FROM users WHERE name = %s", (password, username))
  8. result = cur.fetchone()
  9. if result is not None:
  10. return str(result[0])
  11. else:
  12. return "false"
  13.  
  14. @get('/login')
  15. def login():
  16. checkcredentials(request.params.username, request.params.password)
  17.  
  18. @get('/patients')
  19. def getPatients():
  20. if checkcredentials(request.params.username, request.params.password):
  21. cur = conn.cursor()
  22. query = "SELECT * FROM patients ORDER BY lname DESC;"
  23. cur.execute(query)
  24. returnme = json.dumps(safefetchall(cur, 0))
  25. cur.close()
  26. return returnme
  27. response.status=403
  28.  
  29. @post('/addpatient')
  30. def addPatient():
  31. if checkcredentials(request.params.username, request.params.password):
  32. cur = conn.cursor()
  33. query = "INSERT INTO patients(fname,lname,dob,address,city,state,country,zip,zip_plus,prim_phone,sec_phone,emerg_phone,gender) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
  34. params = (request.params.fname, request.params.lname, request.params.dob, request.params.address,
  35. request.params.city, request.params.state, request.params.country, request.params.zip,
  36. request.params.zip_plus, request.params.prim_phone, request.params.sec_phone,
  37. request.params.emerg_phone, request.params.gender)
  38. cur.execute(query, params)
  39. cur.close()
  40. response.status=201
  41. return
  42. response.status=403
  43.  
  44. @get('/<pid:int>/basicinfo')
  45. def info(pid=''):
  46. if checkcredentials(request.params.username, request.params.password):
  47. cur = conn.cursor()
  48. query = "SELECT * FROM patient_basic_info WHERE patient_id = %s ORDER BY recorddate DESC"
  49. if request.params.all == 0:
  50. query += " LIMIT 1"
  51. cur.execute(query + ";", str(pid))
  52. returnme = json.dumps(safefetchall(cur, 1))
  53. cur.close()
  54. return returnme
  55. response.status=403
  56.  
  57. @post('/<pid:int>/addbasicinfo')
  58. def addBasicInfo():
  59. if checkcredentials(request.params.username, request.params.password):
  60. cur = conn.cursor()
  61. query = "INSERT INTO patient_basic_info(patient_id, height, weight, heart_rate, systolic_pressure, diastolic_pressure, notes) VALUES(%s, %s, %s, %s, %s, %s, %s);"
  62. params = (request.params.patient_id, request.params.height, request.params.weight, request.params.heart_rate,
  63. request.params.systolic_pressure, request.params.diastolic_pressure, request.params.notes)
  64. cur.execute(query, params)
  65. cur.close()
  66. response.status=201
  67. return
  68. response.status=403
  69.  
  70. @get('/<pid:int>/bloodpanel')
  71. def blood(pid=''):
  72. if checkcredentials(request.params.username, request.params.password):
  73. cur = conn.cursor()
  74. query = "SELECT * FROM blood_panel WHERE patient_id = %s ORDER BY recorddate DESC"
  75. if request.params.all == 0:
  76. query += " LIMIT 1"
  77. cur.execute(query + ";", str(pid))
  78. returnme = json.dumps(safefetchall(cur, 2))
  79. cur.close()
  80. return returnme
  81. response.status=403
  82.  
  83. @post('/<pid:int>/addbloodpanel')
  84. def addBloodPanel():
  85. if checkcredentials(request.params.username, request.params.password):
  86. cur = conn.cursor()
  87. query = "INSERT INTO blood_panel(patient_id, albumin, alkaline_phosphatase, alt, ast, bun, calcium, chloride, carbon_dioxide, creatinine, glucose, potassium, sodium, bilirubin, protein)"
  88. + " VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
  89. params = (request.params.patient_id, request.params.albumin, request.params.alkaline_phosphatase, request.params.alt,
  90. request.params.ast, request.params.bun, request.params.calcium, request.params.chloride, request.params.carbon_dioxide, request.params.creatinine,
  91. request.params.glucose, request.params.potassium, request.params.sodium, request.params.bilirubin, request.params.protein)
  92. cur.execute(query, params)
  93. cur.close()
  94. response.status=201
  95. return
  96. response.status=403
  97.  
  98. conn = psycopg2.connect(database="SHARP", user="test", password="hello", host="localhost", port="5433")
  99.  
  100. run(host='localhost', port=8080, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement