PageRenderTime 31ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/api/routes/Alerts.py

https://gitlab.com/fdemian/Shelob
Python | 160 lines | 115 code | 39 blank | 6 comment | 5 complexity | 3b6dc391278ec774dae7992ba79179c7 MD5 | raw file
  1. import json
  2. from api.model.models import Notification
  3. from api.authentication.AuthenticatedHandler import AuthenticatedHandler
  4. from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
  5. from tornado.gen import coroutine
  6. from api.Utils import authenticated
  7. class AlertsHandler(AuthenticatedHandler):
  8. def data_received(self, chunk):
  9. pass
  10. # GET /alerts
  11. @authenticated
  12. def get(self):
  13. if not self.settings['notifications_enabled']:
  14. response = {'message': "Notifications disabled."}
  15. self.set_status(501, 'Error')
  16. self.set_header("Access-Control-Allow-Origin", "*")
  17. self.write(response)
  18. return
  19. session = self.settings['db']
  20. current_user = self.current_user
  21. alerts = session.query(Notification).filter(Notification.user_id == current_user, Notification.read == False)\
  22. .order_by(Notification.id.desc())\
  23. .all()
  24. data = []
  25. for notification in alerts:
  26. json_notification = {
  27. 'id': notification.id,
  28. 'type': notification.type,
  29. 'text': notification.text,
  30. 'link': notification.link,
  31. 'read': notification.read
  32. }
  33. data.append(json_notification)
  34. response = {"notifications": data}
  35. self.set_status(200, 'Ok ')
  36. self.set_header("Access-Control-Allow-Origin", "*")
  37. self.write(response)
  38. return
  39. # TODO: change to POST method?
  40. # -- REASON: Successful response returns a body (and shouldn't).
  41. # -- Otherwise the method is same as PUT.
  42. # PUT /alerts
  43. @authenticated
  44. def put(self):
  45. request = self.request.body.decode("utf-8")
  46. json_request = json.loads(json.loads(request))
  47. session = self.settings['db']
  48. try:
  49. notification_id = int(json_request["id"])
  50. notification = session.query(Notification).filter(Notification.id == notification_id).one()
  51. # Modify all the fields.
  52. notification.type = json_request["type"]
  53. notification.text = json_request["text"]
  54. notification.link = json_request["link"]
  55. notification.read = json_request["read"]
  56. session.commit()
  57. status = 200
  58. status_str = 'Ok'
  59. response = {'id': notification_id}
  60. except NoResultFound:
  61. status = 500
  62. status_str = "Error"
  63. response = {'message': 'No notifications with the id' + notification_id + 'found.'}
  64. except MultipleResultsFound:
  65. status = 500
  66. status_str = "Error"
  67. response = {'message': 'More than one notification with the id' + notification_id + ' was found.'}
  68. self.set_header("Content-Type", "application/jsonp;charset=UTF-8")
  69. self.set_header("Access-Control-Allow-Origin", "*")
  70. self.set_status(status, status_str)
  71. self.write(response)
  72. return
  73. @coroutine
  74. def post(self):
  75. response = {"message": "This is not a valid method for this resource."}
  76. self.set_status(405, 'Error')
  77. self.set_header("Access-Control-Allow-Origin", "*")
  78. self.write(response)
  79. return
  80. @coroutine
  81. def delete(self):
  82. response = {"message": "This is not a valid method for this resource."}
  83. self.set_status(405, 'Error')
  84. self.set_header("Access-Control-Allow-Origin", "*")
  85. self.write(response)
  86. return
  87. @coroutine
  88. def trace(self):
  89. response = {"message": "This is not a valid method for this resource."}
  90. self.set_status(405, 'Error')
  91. self.set_header("Access-Control-Allow-Origin", "*")
  92. self.write(response)
  93. return
  94. @coroutine
  95. def connect(self):
  96. response = {"message": "This is not a valid method for this resource."}
  97. self.set_status(405, 'Error')
  98. self.set_header("Access-Control-Allow-Origin", "*")
  99. self.write(response)
  100. return
  101. @coroutine
  102. def options(self):
  103. response = {"message": "This is not a valid method for this resource."}
  104. self.set_status(405, 'Error')
  105. self.set_header("Access-Control-Allow-Origin", "*")
  106. self.write(response)
  107. return
  108. @coroutine
  109. def patch(self):
  110. response = {"message": "This is not a valid method for this resource."}
  111. self.set_status(405, 'Error')
  112. self.set_header("Access-Control-Allow-Origin", "*")
  113. self.write(response)
  114. return
  115. @coroutine
  116. def head(self):
  117. response = {"message": "This is not a valid method for this resource."}
  118. self.set_status(405, 'Error')
  119. self.set_header("Access-Control-Allow-Origin", "*")
  120. self.write(response)
  121. return