PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/api/routes/Story.py

https://gitlab.com/fdemian/Shelob
Python | 113 lines | 89 code | 23 blank | 1 comment | 6 complexity | a495081f929fb3c5fdcc49fa73e097b9 MD5 | raw file
  1. import json
  2. from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
  3. from api.model.models import Story
  4. from api.authentication.AuthenticatedHandler import AuthenticatedHandler
  5. from tornado.gen import coroutine
  6. class StoryHandler(AuthenticatedHandler):
  7. def data_received(self, chunk):
  8. pass
  9. # GET /story/id
  10. def get(self, story_id):
  11. session = self.settings['db']
  12. try:
  13. story = session.query(Story).filter(Story.id == story_id).one()
  14. content = json.loads(story.content)
  15. if story.category is None:
  16. category = {'id': -1, 'name': "uncatalogued"}
  17. else:
  18. category = {'id': story.category.id, 'name': story.category.name}
  19. comments = []
  20. for comment in story.comments:
  21. json_comment = {
  22. 'id': comment.id,
  23. 'author': comment.author,
  24. 'content': comment.content,
  25. 'avatar': comment.avatar,
  26. 'date': str(comment.date),
  27. 'url': comment.url,
  28. }
  29. comments.append(json_comment)
  30. response = {
  31. 'id': story.id,
  32. 'title': story.title,
  33. 'category': category,
  34. 'content': content,
  35. 'date': str(story.date),
  36. 'comments': comments,
  37. 'tags': story.tags,
  38. 'is_draft': story.is_draft
  39. }
  40. status = 200
  41. status_str = 'Ok'
  42. except NoResultFound:
  43. status = 500
  44. status_str = "Error"
  45. response = {'message': 'No stories found for the specified id.'}
  46. except MultipleResultsFound:
  47. status = 500
  48. status_str = "error"
  49. response = {'message': 'Multiple stories found for the specified id.'}
  50. self.set_header("Content-Type", "application/jsonp;charset=UTF-8")
  51. self.set_header("Access-Control-Allow-Origin", "*")
  52. self.set_status(status, status_str)
  53. self.write(response)
  54. @coroutine
  55. def trace(self):
  56. response = {"message": "This is not a valid method for this resource."}
  57. self.set_status(405, 'Error')
  58. self.set_header("Access-Control-Allow-Origin", "*")
  59. self.write(response)
  60. return
  61. @coroutine
  62. def connect(self):
  63. response = {"message": "This is not a valid method for this resource."}
  64. self.set_status(405, 'Error')
  65. self.set_header("Access-Control-Allow-Origin", "*")
  66. self.write(response)
  67. return
  68. @coroutine
  69. def options(self):
  70. response = {}
  71. self.set_header("Content-Type", "test/plain;charset=UTF-8")
  72. self.set_header("Access-Control-Allow-Origin", "*")
  73. self.set_header("Access-Control-Allow-Headers", "Authorization")
  74. self.set_status(200, "Ok")
  75. self.write(response)
  76. return
  77. @coroutine
  78. def patch(self):
  79. response = {"message": "This is not a valid method for this resource."}
  80. self.set_status(405, 'Error')
  81. self.set_header("Access-Control-Allow-Origin", "*")
  82. self.write(response)
  83. return
  84. @coroutine
  85. def head(self):
  86. response = {"message": "This is not a valid method for this resource."}
  87. self.set_status(405, 'Error')
  88. self.set_header("Access-Control-Allow-Origin", "*")
  89. self.write(response)
  90. return